Skip to main content
Tools extend agent capabilities beyond LLM generation. Agents can call tools to interact with external systems, execute code, search the web, and more.

Function tools

The simplest way to add tools is using Python functions:
The function docstring and type hints are used to generate the tool schema for the LLM.

Multiple tools

Async tools

Tools can be async functions:

MCP servers

Model Context Protocol (MCP) servers provide reusable tool integrations:
See MCP Servers guide for more details.

Code execution

Use CodeExecutorAgent to execute code safely:
See Code Execution example for a complete example.

AgentTool

Wrap an agent as a tool for another agent:

Custom tool classes

For more control, create a custom tool class:

Tool execution control

Max iterations

Control how many tool-calling rounds the agent can make:

Reflect on tool use

Have the agent reflect on tool results before responding:

Tool security

Always validate and sanitize tool inputs, especially for tools that:
  • Execute code
  • Access file systems
  • Make network requests
  • Interact with databases
Always use DockerCommandLineCodeExecutor for untrusted code execution to provide isolation.
Use Pydantic models to validate and sanitize tool inputs.
Give agents only the tools they need. Don’t provide file system access unless required.
Use timeouts for tools that make network requests or long-running operations.

Best practices

  1. Clear descriptions - Write detailed docstrings. The LLM uses them to understand when to call the tool.
  2. Type hints - Always use type hints. They’re used to generate the tool schema.
  3. Error handling - Handle errors gracefully in your tools:
  1. Structured outputs - For complex data, return structured formats (JSON, Pydantic models).

Next steps

MCP Servers

Deep dive into MCP server integration

Tool Integration Guide

Learn advanced tool patterns

Code Execution

See code execution in action

Web Browsing

Web browsing with MCP servers