tool message.
Functions as tools
Wrap any function withTool, or register it on an agent with @agent.tool. The name comes from the function, the description from the first paragraph of its docstring, the parameters from its signature and type annotations:
- Types.
str,int,float,bool,listanddictmap to their JSON types; anything else is sent as a string. - Required parameters. Parameters without a default are required.
- Descriptions. Each parameter’s description comes from the docstring, in Google style (
Args:) or reStructuredText (:param city:), or from a PydanticField(description=...)used as the default.
Tool accepts overrides: Tool(fn, name="weather", description="...", parameters={...}).
Return values
A tool returns anything: a string is sent as is, a Pydantic model as its JSON, other values throughjson.dumps (or str as a last resort). An exception is reported to the model as the tool’s result, so it can correct its call; the run goes on.
Tool classes
For a tool with configuration or state, subclassBaseTool and implement _run. The schema is built from _run’s signature:
Retries
A tool that raises is retried up tomax_retries times (3 by default), with an exponential backoff of 1 to 10 seconds, before its error goes back to the model. Set max_retries=1 for a tool that must not run twice:
requires_approval=True always runs once: a retry would repeat a change the user approved a single time. See Tool approvals.
Parallel calls
When the model asks for several tools in one turn, they run one after the other, in order, and their results keep the order of the calls. Calls to delegated agents (team members and sub-agents) run in parallel threads. Witharun, every tool call of a turn runs concurrently.
Large outputs
One file read or a broad search can fill the context window on its own.max_tool_output_tokens caps a single result: a longer one is set aside, the model gets its beginning and a reference, and a read_tool_output tool to page through the rest.