What are Tools?
Tools (also called “functions” or “function tools”) are Python/C# functions that agents can invoke to:- Retrieve real-time data (weather, stock prices, news)
- Perform calculations or data processing
- Execute actions (send emails, create tickets)
- Access databases or search engines
- Interact with external APIs
Defining a Tool
- Python
- .NET
Use the Key Points:
@tool decorator to convert any function into a tool:- The docstring becomes the tool description shown to the model
- Use
Annotated[type, Field(description="...")]for parameter descriptions - Set
approval_mode="always_require"in production to require user confirmation - Return type should be JSON-serializable or a string
Using Tools with Agents
- Python
- .NET
Pass tools to the agent when creating it:
Tool Schemas
- Python
- .NET
The framework automatically generates JSON Schema from function signatures:You can also provide an explicit schema:
Async Tools
- Python
- .NET
Both sync and async functions are supported:
Tool Approval
- Python
- .NET
Require user approval before executing sensitive tools:
Advanced Tool Features
Tool Invocation Limits
- Python
- .NET
Control how many times a tool can be called:
Custom Result Parsing
- Python
- .NET
Override how tool results are serialized:
Declaration-Only Tools
- Python
- .NET
Create tools that agents can reason about without executing:
Tool Execution Flow
Best Practices
Tool Design Tips
- Clear Descriptions: Write detailed docstrings and parameter descriptions
- Input Validation: Use Pydantic Field constraints for validation
- Error Handling: Return helpful error messages, don’t raise exceptions
- Idempotency: Tools should be safe to retry
- Performance: Keep tools fast; use async for I/O operations
- Security: Always use
approval_mode="always_require"for sensitive operations
Example: Complete Weather Agent
- Python
- .NET
Next Steps
Middleware
Intercept and modify tool invocations with middleware
Sessions
Manage conversation state across tool calls
Observability
Monitor tool performance and usage
Agents
Learn more about agent capabilities