Creating Tools
Basic Tool with @tool Decorator
The @tool decorator converts any Python function into a tool:
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:Best Practices
Use descriptive function names and docstrings
Use descriptive function names and docstrings
The function name and docstring are sent to the model to help it understand when to use the tool. Be clear and specific:
Provide detailed parameter descriptions
Provide detailed parameter descriptions
Use Pydantic
Field to add descriptions that help the model understand parameter expectations:Return structured, parseable data
Return structured, parseable data
Return data in a format that’s easy for the agent to parse and use:
Handle errors gracefully
Handle errors gracefully
Catch exceptions and return error messages that help the agent understand what went wrong:
Use approval for sensitive operations
Use approval for sensitive operations
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
Related
- Agents - Using tools with agents
- Middleware - Intercept tool executions
- Workflows - Tools in workflow contexts