Skip to main content
Tools enable agents to perform actions beyond text generation, such as retrieving data, calling APIs, or executing code. The Agent Framework provides a flexible tool system with automatic schema generation and safety controls.

Creating Tools

Basic Tool with @tool Decorator

The @tool decorator converts any Python function into a tool:
Production Safety: Always use approval_mode="always_require" for tools in production environments. Only use "never_require" for samples and testing.

Tool with Multiple Parameters

Async Tools

Tools can be async for I/O operations:

Tool Schema

Automatic Schema Inference

The framework automatically generates JSON schemas from type hints:

Explicit Schema with Pydantic

Provide an explicit schema using a Pydantic model:

Explicit Schema with JSON

Use a raw JSON schema dictionary:

Tool Approval

Approval Modes

Control when tools require human approval:

Approval with Sessions

Handle approvals using sessions:

Advanced Tool Patterns

Tools in Classes

Organize related tools in a class:

Agent as Tool

Use one agent as a tool for another:

Tool with Session Injection

Access the session from within a tool:

Tool Error Handling

Control Tool Execution Count

Function Invocation Configuration

Customize how tools are called:

Tool Declaration Without Implementation

Declare tools without providing implementations (for external execution):

Built-in Tool Types

MCP Tools

Model Context Protocol tools for external integrations:

Shell Tools (Provider-Specific)

Some providers support built-in shell execution:

Tool Middleware

Intercept tool executions with function middleware:
See the Middleware guide for more details.

Best Practices

The function name and docstring are sent to the model to help it understand when to use the tool. Be clear and specific:
Use Pydantic Field to add descriptions that help the model understand parameter expectations:
Return data in a format that’s easy for the agent to parse and use:
Catch exceptions and return error messages that help the agent understand what went wrong:
Always require approval for tools that:
  • Modify data or state
  • Access sensitive information
  • Perform irreversible actions
  • Incur costs (API calls, resource usage)

API Reference

@tool Decorator

FunctionTool Class

Examples

Multi-Tool Agent