Defining Tools
Any properly annotated function can be a tool. The SDK uses the function’s docstring as the description and type annotations for parameters:The first parameter of every tool must be
ctx (the tool context). This provides access to conversation state and is required for forward compatibility. Your tool parameters follow after ctx.Tool Types
Plain functions passed to
tools are automatically wrapped as loopback tools. Use decorators (@loopback_tool, @passthrough_tool, @handoff_tool) for explicit control.Loopback Tools (@loopback_tool)
The default behavior. The tool’s result is sent back to the LLM, which can then continue generating a response.
Passthrough Tools (@passthrough_tool)
Output events go directly to the user, bypassing the LLM. Use for deterministic actions.
EndCall, TransferCall, SendDtmf), deterministic responses.
Handoff Tools (@handoff_tool)
Transfers control to another handler. All future events are routed to the handoff target instead of the original agent.
event parameter receives different values depending on timing:
- First call:
eventisAgentHandedOff— use this to send a transition message - Subsequent calls:
eventis the actualInputEvent(UserTurnEnded, etc.)
Built-in Tools
| Tool | Description | When to Use |
|---|---|---|
end_call | Ends the call (with a goodbye message) | User says “goodbye”, issue resolved, or conversation complete |
transfer_call | Transfers to another number (E.164 format) | Escalating to human agents, routing to departments |
web_search | Searches the web for real-time info | Current events, live prices, recent news the LLM doesn’t know |
agent_as_handoff
Creates a handoff tool from another Agent—the easiest way to implement multi-agent workflows.
| Parameter | Type | Description |
|---|---|---|
agent | Agent | The agent to hand off to |
handoff_message | Optional[str] | Message spoken before the handoff |
name | Optional[str] | Tool name for the LLM |
description | Optional[str] | When the LLM should use this tool |
agent_as_handoff automatically sends the handoff message, triggers the new agent’s introduction, and routes all future events to it.
Long-Running Tools
By default, tool calls are terminated when the agent is interrupted (though any reasoning and tool call response values already produced are preserved for use in the next generation). For tools that are expected to take a long time to complete, setis_background=True. The tool will continue running in the background until completion regardless of interruptions, then loop back to the LLM to produce a response.
Background tools are useful when:
- The operation may take longer than typical user patience (e.g., complex searches, report generation)
- You want the user to be able to speak while the operation completes
- The result should be delivered even if the user interrupts with another question