Skip to main content

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 @tool decorator
  • 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
This enables your agent to interact with external systems, APIs, databases, and more.

Create a Weather Agent

Let’s build an agent that can check the weather for different locations.
1

Import Required Modules

Create a new file called weather_agent.py and import the necessary modules:
2

Define a Function Tool

Create a function and decorate it with @tool to make it available to your agent:
The approval_mode="never_require" setting is used for simplicity in this example. In production, use approval_mode="always_require" to require user confirmation before executing tools.
Key components:
  • @tool decorator: Registers the function as a tool
  • Type annotations: Define parameter types for validation
  • Annotated with Field: Provides descriptions for the LLM
  • Docstring: Explains what the tool does (sent to the LLM)
  • Return type: Specifies what the function returns
3

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.
4

Run the Agent

Ask the agent a question that requires using the tool:

Complete Example

weather_agent.py

Run Your Agent

Execute the script:
You should see output like:

What Just Happened?

Here’s the flow of execution:
1

User Query

You ask: “What’s the weather like in Seattle?”
2

LLM Analysis

The model analyzes your question and realizes it needs to call the get_weather tool with location="Seattle".
3

Tool Execution

The framework automatically calls your get_weather("Seattle") function.
4

Tool Response

Your function returns: “The weather in Seattle is cloudy with a high of 18°C.”
5

Final Response

The model receives the tool result and formulates a natural language response.

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:
When approval_mode="always_require", the framework will pause execution and request user confirmation before calling the tool.

Best Practices

Provide detailed descriptions for both the function and its parameters. The LLM uses these to decide when and how to call your tools:
Always use type annotations. They help with:
  • Automatic schema generation
  • Runtime validation
  • IDE autocomplete
  • Better error messages
Return error messages as strings rather than raising exceptions:
This allows the LLM to see the error and potentially retry or inform the user.
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