Skip to main content

Tools

Tools are async functions that the LLM can call. They’re the bridge between model reasoning and real-world actions.

Basic @tool

Key Rules

  1. Must be async@tool enforces async def. No sync functions.
  2. Docstring is the spec — The model sees your docstring as the tool description.
  3. Best Practices matter — The Best Practices section is injected into the system prompt as <tool_best_practices>. Use it to guide the model on when/how to use the tool.
  4. Type annotations define the schema — Parameter types become the tool’s JSON schema.

Docstring Structure

Composing Toolsets

Pass multiple tools to an agent:

Built-in Toolsets

FileToolset

Workspace-scoped file operations with stale-write protection:
FileToolset provides 5 tools: read_file, read_image, grep, sed, echo_into.

PyRepl

Persistent Python REPL with runtime primitives:
PyRepl provides 2 tools: execute_code, reset_repl.

Multimodal Returns

Tools can return images and mixed content:
Multimodal returns are restructured by the runtime through an internal transcript patch — the image is placed in a user message (as required by most providers).

Tuple Returns (mixed content)

Return both text and images:
For multiple images, return a non-empty list or pair text with a list:

Long Output Handling

For tools that produce very long output:
With too_long_to_file=True, if the result exceeds ~20,000 tokens, the framework:
  1. Writes the full output to a temporary file
  2. Sends a truncated version + file path to the model
  3. The model can then read specific sections if needed

Dynamic Tool Creation

Create tools programmatically:

System Prompt Injection

Each tool’s Best Practices section is collected and injected as a <tool_best_practices> block at the top of the system prompt. This is automatic — you don’t need to reference tools in your docstring. Additionally, tools can provide dynamic prompt injection via prompt_injection_builder:
API Reference: Decorators | API Reference: Builtins