Skip to main content
Tools allow agents to extend their capabilities beyond text generation by calling functions, accessing databases, making API requests, or performing computations. The framework automatically handles function calling, parameter validation, and result formatting.

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
The framework uses JSON Schema to describe function parameters to the model, validates arguments, executes functions, and returns results.

Defining a Tool

Use the @tool decorator to convert any function into a tool:
Key Points:
  • 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

Pass tools to the agent when creating it:

Tool Schemas

The framework automatically generates JSON Schema from function signatures:
You can also provide an explicit schema:

Async Tools

Both sync and async functions are supported:

Tool Approval

Require user approval before executing sensitive tools:

Advanced Tool Features

Tool Invocation Limits

Control how many times a tool can be called:

Custom Result Parsing

Override how tool results are serialized:

Declaration-Only Tools

Create tools that agents can reason about without executing:

Tool Execution Flow

Best Practices

Tool Design Tips
  1. Clear Descriptions: Write detailed docstrings and parameter descriptions
  2. Input Validation: Use Pydantic Field constraints for validation
  3. Error Handling: Return helpful error messages, don’t raise exceptions
  4. Idempotency: Tools should be safe to retry
  5. Performance: Keep tools fast; use async for I/O operations
  6. Security: Always use approval_mode="always_require" for sensitive operations
Production Considerations
  • Rate Limiting: Protect external APIs from abuse
  • Timeout Handling: Set timeouts for external calls
  • Secret Management: Never hardcode API keys in tool code
  • Logging: Log tool executions for debugging and auditing
  • Cost Control: Monitor and limit expensive API calls
  • Testing: Test tools independently before agent integration

Example: Complete Weather Agent

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