Tools¶
Tools give agents the ability to take actions — call APIs, query databases, run code, or interact with external systems. The SDK provides the tool types below and a schema validation system.
Tool Types¶
| Type | Use Case | Deep Dive |
|---|---|---|
| FunctionTool | Wrap any Python function | Auto-generated JSON Schema from type hints |
| RESTTool | Call an HTTP API endpoint | No Python function needed -- just configure URL and method |
| MCPTool | Use an MCP server's tools | Official mcp client; remote (HTTP) or local (stdio); from_server() loads every tool |
decision_tool |
Let an agent classify text with the Decisions API | A FunctionTool that returns probabilities (1.84.0) |
web_search(), code_execution(), file_search() |
Search the web, run code or search file stores, run by the model's provider | Sent in OpenAI's, Anthropic's or Gemini's own shape; each call recorded and guarded (1.91.0) |
Quick Example¶
from fastaiagent import Agent, RESTTool, LLMClient
from fastaiagent.tool import tool
@tool(name="percent_of")
def percent_of(percent: float, value: float) -> str:
"""Return `percent` percent of `value`."""
return str(value * percent / 100)
agent = Agent(
name="assistant",
system_prompt="Use tools to answer questions.",
llm=LLMClient(provider="openai", model="gpt-4.1"),
tools=[
percent_of, # @tool returns a FunctionTool
RESTTool(name="weather", url="https://api.weather.com/v1", method="GET"),
],
)
result = agent.run("What is 15% of 230, and what's the weather in Tokyo?")
ToolResult¶
Every tool execution returns a ToolResult:
| Field | Type | Description |
|---|---|---|
output |
Any |
The tool's return value |
error |
str \| None |
Error message if execution failed |
success |
bool |
True if no error |
metadata |
dict |
Extra info (e.g., HTTP status code for REST tools) |
result = tool.execute({"query": "test"})
if result.success:
print(result.output)
else:
print(f"Error: {result.error}")
OpenAI Format¶
Tools are internally converted to OpenAI function-calling format for LLM communication:
fmt = tool.to_openai_format()
# {
# "type": "function",
# "function": {
# "name": "get_weather",
# "description": "Get current weather for a city.",
# "parameters": { ... }
# }
# }
This format is automatically converted to Anthropic's input_schema format when using the Anthropic provider.
Replay safety (replay_class)¶
Every tool type and the @tool decorator accept an optional replay_class. It
is a label the SDK records: it is stamped on each tool span as
fastaiagent.tool.replay_class and serialised in to_dict(). An external
(central) Replay engine can read it to decide whether a recorded tool call may be
re-executed or must have its recorded output injected instead. The SDK's
own rerun never reads it (see the note below).
The SDK's own rerun does not act on replay_class
Since 1.89.0 ForkedReplay.rerun() blocks every tool you did not supply or
allow, whatever its class. Run one for real with
with_tool_policy("live", allow=[name]), or replace it with
with_tool_override / with_tool_stub. The class is shown beside each
tool in the Local UI's fork dialog, to help you choose. See
Replay guarantees.
replay_class |
Meaning | What a central Replay engine does with it (the SDK's own rerun ignores it) |
|---|---|---|
"read_only" |
No side effects; safe to call again (e.g. a GET, a pure lookup). | May be re-executed during replay. |
"idempotent" |
Repeating the call with the same args is safe / converges. | Recorded output is injected (not re-executed). |
"side_effecting" |
Has observable side effects (writes, payments, emails). | Recorded output is injected; never re-executed. |
from fastaiagent import RESTTool
from fastaiagent.tool import tool
# Marked explicitly by the developer — a GET is NOT auto-classified read_only.
weather = RESTTool(
name="weather", url="https://api.weather.com/v1", method="GET",
replay_class="read_only",
)
@tool(name="upsert_user", replay_class="idempotent")
def upsert_user(user_id: str, name: str) -> str:
...
@tool(name="charge_card") # unmarked → resolves to "side_effecting"
def charge_card(amount: int) -> str:
...
- Default is
"side_effecting"— the safe choice. The central Replay engine never re-executes an unmarked tool. - Never auto-inferred. A
GETRESTToolis not automaticallyread_only; only an explicit mark makes a tool re-executable. Auto-inferring would violate the replay-safety invariant. - Validated strictly. A value outside
read_only/idempotent/side_effectingraisesValueErrorat construction, andTool.from_dictraises it too (it builds through the same constructor). Only the SDK's replay-rebuild path, which reads tools back out of a trace, coerces an unknown value toside_effectinginstead of raising. - The resolved value is emitted on every tool-call span as
fastaiagent.tool.replay_class(see Tracing).
Serialization¶
All tool types support roundtrip serialization:
# Serialize
data = tool.to_dict()
# {
# "name": "weather",
# "description": "Get weather",
# "tool_type": "rest_api",
# "parameters": { ... },
# "config": {"url": "...", "method": "GET", ...}
# }
# Restore (auto-dispatches to correct class)
restored = Tool.from_dict(data)
# Returns RESTTool if tool_type is "rest_api", FunctionTool if "function", etc.
FunctionTool callables and the ToolRegistry. Python function objects can't be serialized to JSON, so
FunctionTool.to_dict()only stores the schema. To makefrom_dict()usable for Agent Replay, everyFunctionToolthat is constructed with a livefn=...is automatically registered in the process-wideToolRegistry. WhenTool.from_dict()sees a tool name that's been registered, it returns the live tool (with the callable) instead of a schema-only skeleton. A replay rerun uses a registered tool only when you allow it (with_tool_policy("live", allow=[...]), 1.89.0).Stand-ins that must not register. A stub or test double built with the name of a real tool would replace the real one in the registry. Pass
register=False:FunctionTool(name="charge_card", fn=fake, register=False)or@tool(name="charge_card", register=False).with_tool_overrideandwith_tool_stubdo this for you.
ToolRegistry¶
A process-wide, name-keyed registry that holds live tool callables so replay can rebind them after reconstruction from a trace.
from fastaiagent import FunctionTool, ToolRegistry, tool
# Creating a FunctionTool with a callable auto-registers it
def lookup_order(order_id: str) -> str:
"""Look up an order by ID."""
return f"Order {order_id}: shipped"
t = FunctionTool(name="lookup_order", fn=lookup_order)
assert ToolRegistry.get("lookup_order") is t
# The @tool decorator also auto-registers
@tool(name="echo")
def echo(msg: str) -> str:
return msg
assert ToolRegistry.get("echo") is not None
API¶
| Method | Behavior |
|---|---|
ToolRegistry.register(tool) |
Store a tool by tool.name. Last-write-wins — re-registering the same name replaces. Returns the tool. |
ToolRegistry.get(name) |
Return the registered tool, or None. |
ToolRegistry.all() |
Return a copy of the full registry (name → tool). |
ToolRegistry.unregister(name) |
Remove by name, returning the removed tool or None. |
ToolRegistry.clear() |
Drop all entries. Intended for tests. |
When you need it¶
You generally do not need to touch ToolRegistry directly — auto-registration at FunctionTool.__init__ covers the common case. You need it explicitly when:
- Replaying a trace in a different process than the one that created the tools, with those tools allowed to run. Import your tool module in the replay process so the tools get registered at import time, or re-register manually, and allow them with
with_tool_policy("live", allow=[...]). - Unit tests that want to start from a clean slate — call
ToolRegistry.clear()in setup. - Distinct tools with the same name — registration is last-write-wins, so give tools unique names if you care about isolation.
What happens when a tool isn't registered?¶
When Tool.from_dict() reconstructs a FunctionTool whose name isn't in the registry, it logs a warning and returns a schema-only skeleton. Calling tool.aexecute() on that skeleton returns a ToolResult(error="No function attached..."). Replay reruns that invoke the tool will surface the error to the agent (as the tool message content), not crash — the agent can then react to the "tool missing" signal.
Execution Policy¶
Every tool type accepts optional keyword args that govern how a call runs — all off by default, so unset means no change in behavior:
| Option | Effect |
|---|---|
timeout |
Per-call wall-clock timeout in seconds; the call is cancelled and reported as an error when exceeded. A sync (def) tool runs in a worker thread when it has a timeout, and keeps running in the background after one (details). |
max_retries / retry_delay |
Retry transient failures (exceptions or timeouts) with exponential backoff (retry_delay * 2**attempt). |
output_type |
Validate/coerce the return value against a Pydantic-compatible type (e.g. parse a dict into a BaseModel); a mismatch is returned to the model as an error. Only needed when the raw return isn't already the type you want. |
validate_args |
(FunctionTool only, default True) Validate/coerce the model's arguments against the function's type hints. |
deferred |
(1.91.0) Not sent to the model until it asks for it through the built-in load_tools tool, so a large toolset costs no context on turns that don't use it (details). |
See FunctionTool → Timeout, Retry & Output Validation for details.
Sync vs Async¶
# Sync — applies the execution policy above
result = tool.execute({"query": "test"})
# Async — applies the execution policy above
result = await tool.ainvoke({"query": "test"})
# Async, raw — bypasses the policy (no timeout / retry / output validation)
result = await tool.aexecute({"query": "test"})
execute() and ainvoke() are the policy-aware entry points: the agent loop
uses ainvoke, and so does a Chain tool node
since 1.78.0 (before, a chain called aexecute and the policy silently did not
apply there). Call aexecute() directly only to deliberately bypass the
policy. All work from sync or async contexts (including Jupyter notebooks).
Error Handling¶
from fastaiagent._internal.errors import (
ToolError, # Base tool error
ToolExecutionError, # Tool failed during execution
ToolSchemaError, # Invalid tool schema
SchemaDriftError, # Response schema has drifted
)
try:
result = tool.execute({"bad": "args"})
except ToolExecutionError as e:
print(f"Tool failed: {e}")
Next Steps¶
- FunctionTool — Wrap Python functions as tools
- Context & Dependency Injection — Pass runtime dependencies to tools
- RESTTool — Call HTTP APIs as tools
- MCPTool — Connect to MCP servers
- Schema Drift Detection — Detect when tool responses change
- Using Tools with Agents — How to attach tools to agents