Tools
Create type-safe tools with Zod schemas and automatic execution. Supports regular tools, generator tools with progress, manual tools, and automatic multi-turn execution.
The tool() Helper
The tool() function creates type-safe tools with Zod schema validation:
Tool Types
The SDK supports three types of tools, automatically detected from your configuration:
Regular Tools
Standard tools with an execute function:
Generator Tools
Tools that yield progress updates during execution. Add eventSchema to enable generator mode:
Progress events are streamed to consumers via getToolStream() and getFullResponsesStream().
Manual Tools
Tools without automatic execution - you handle the tool calls yourself:
Use getToolCalls() to retrieve manual tool calls for processing.
Schema Definition
Input Schema
Define what parameters the tool accepts:
Output Schema
Define the structure of results returned to the model:
Event Schema (Generator Tools)
Define progress/status events for generator tools:
Type Inference
The SDK provides utilities to extract types from tools:
Using Tools with callModel
Single Tool
Multiple Tools
Type-Safe Tool Calls with as const
Use as const for full type inference on tool calls:
TurnContext
Tool execute functions receive a TurnContext with conversation state:
TurnContext Properties
Tool Execution
callModel automatically executes tools and handles multi-turn conversations. When the model calls a tool, the SDK executes it, sends the result back, and continues until the model provides a final response.
Automatic Execution Flow
When you provide tools with execute functions:
Execution Sequence
- Model receives prompt and generates tool call
- SDK extracts tool call and validates arguments
- Tool’s execute function runs
- Result is formatted and sent back to model
- Model generates final response (or more tool calls)
- Process repeats until model is done
Controlling Execution Rounds
maxToolRounds (Number)
Limit the maximum number of tool execution rounds:
Setting maxToolRounds: 0 disables automatic execution - you get raw tool calls.
maxToolRounds (Function)
Use a function for dynamic control:
The function receives TurnContext and returns true to continue or false to stop.
Accessing Tool Calls
getToolCalls()
Get all tool calls from the initial response (before auto-execution):
getToolCallsStream()
Stream tool calls as they complete:
Tool Stream Events
getToolStream()
Stream both argument deltas and preliminary results:
Event Types
Tool Result Events
When using getFullResponsesStream(), you can also receive tool.result events that fire when a tool execution completes:
ToolResultEvent Type
The tool.result event provides the final output from tool execution along with all intermediate preliminaryResults that were yielded during execution (for generator tools). This is useful when you need both real-time progress updates and a summary of all progress at completion.
Parallel Tool Execution
When the model calls multiple tools, they execute in parallel:
Manual Tool Handling
For tools without execute functions:
Execution Results
Access execution metadata through getResponse():
Error Handling
Tool Execution Errors
Errors in execute functions are caught and sent back to the model:
Validation Errors
Invalid tool arguments are caught before execution:
Graceful Error Handling
Handle errors gracefully in execute functions:
Best Practices
Descriptive Names and Descriptions
Schema Descriptions
Add .describe() to help the model understand parameters:
Idempotent Tools
Design tools to be safely re-executable:
Timeout Handling
Wrap long-running operations:
Next Steps
- nextTurnParams - Tool-driven context injection
- Stop Conditions - Advanced execution control
- Examples - Complete tool implementations