Overview
In this guide, you’ll build an agent that can use custom tools to perform actions and retrieve information. You’ll learn how to:- Define custom function tools with the
@tooldecorator - Add tools to your agent
- Handle tool calls and responses
- Use type annotations and Pydantic for parameter validation
This guide builds on the Quickstart. Make sure you’ve completed it first.
Understanding Tools
Tools are Python functions that your agent can call to perform specific tasks. The Agent Framework automatically:- Generates JSON schemas from your function signatures
- Sends tool definitions to the LLM
- Executes tool calls when the model requests them
- Returns tool results back to the model
Create a Weather Agent
Let’s build an agent that can check the weather for different locations.Define a Function Tool
Create a function and decorate it with Key components:
@tool to make it available to your agent:@tooldecorator: Registers the function as a tool- Type annotations: Define parameter types for validation
AnnotatedwithField: Provides descriptions for the LLM- Docstring: Explains what the tool does (sent to the LLM)
- Return type: Specifies what the function returns
Create the Agent with Tools
Initialize the agent and provide the tool:
You can pass a single tool, a list of tools, or any iterable of tools to the
tools parameter.Complete Example
weather_agent.py
Run Your Agent
Execute the script:What Just Happened?
Here’s the flow of execution:LLM Analysis
The model analyzes your question and realizes it needs to call the
get_weather tool with location="Seattle".Add Multiple Tools
You can provide multiple tools to create more capable agents:Advanced Tool Patterns
Complex Parameter Types
Use Pydantic models for structured parameters:Async Tools
Tools can be async functions for I/O operations:Tool Approval
For production applications, require user approval before executing tools:approval_mode="always_require", the framework will pause execution and request user confirmation before calling the tool.
Best Practices
Write Clear Descriptions
Write Clear Descriptions
Provide detailed descriptions for both the function and its parameters. The LLM uses these to decide when and how to call your tools:
Use Type Hints
Use Type Hints
Always use type annotations. They help with:
- Automatic schema generation
- Runtime validation
- IDE autocomplete
- Better error messages
Handle Errors Gracefully
Handle Errors Gracefully
Return error messages as strings rather than raising exceptions:This allows the LLM to see the error and potentially retry or inform the user.
Keep Tools Focused
Keep Tools Focused
Each tool should do one thing well. Instead of a
manage_database tool that can read, write, and delete, create separate tools:Next Steps
Agent Concepts
Deep dive into agents, middleware, and sessions
Tool Patterns
Learn advanced tool patterns and best practices
Multi-Agent Systems
Build systems with multiple collaborating agents
API Reference
Explore the complete API documentation