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
Examples:
end_call
Ends the current call and disconnects. The actual hangup occurs after the agent’s final speech completes, so the user hears the full goodbye message before disconnection.
end_call uses a conservative policy that only ends the call when:
- The user’s objective is fully complete
- The user explicitly says goodbye
- The agent has said a natural goodbye
Custom Description
We recommend providing a custom description tailored to your use case. The description fully replaces the default—it is not appended—so include complete instructions with explicit do/don’t guidance.agent_as_handoff
Creates a handoff tool from another Agent—the easiest way to implement multi-agent workflows.
When called,
agent_as_handoff automatically sends the handoff message, updates the call settings if specified, triggers the new agent’s introduction, and routes all future events to it.
HTTP Server Tools
Usehttp_server_tool to call an HTTP API from JSON schemas, without writing a tool function.
Parameters
Schema format
Bothrequest_body_schema and query_params_schema take "type": "object" and a "properties" dict. Each property supports:
List the properties the LLM must fill in a top-level
required array. Properties not listed are optional. Path parameters are always required.
Set constant_value at any nesting depth in request_body_schema:
Path and query parameters
Path parameters come from{param} in the URL as required strings. Use path_params_schema for custom descriptions or types:
GET requests, set the query string with query_params_schema:
Response format
The tool returns structured JSON to the LLM. A2xx status sets ok to true with the response under body; any other status, connection error, or timeout sets ok to false with the detail under error. Responses over 4096 characters are truncated.
Inputs are validated when you construct the tool. Malformed schemas, missing
${ENV_VAR} values, type mismatches, and parameter name collisions raise ValueError.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.
knowledge_base tool accepts is_background too, so the agent can keep talking while a retrieval is in flight:
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