Tools and Tool Calling
Tools allow AI models to perform actions and retrieve information beyond their training data. The AI SDK Core provides a flexible, type-safe system for defining and executing tools.What are Tools?
Tools are functions that models can call to:- Fetch real-time data (weather, stock prices, news)
- Query databases or APIs
- Perform calculations
- Execute commands
- Interact with external systems
Basic Tool Definition
Use thetool helper to define tools with type-safe inputs and outputs:
Tool Structure
Every tool contains these properties:string
A description of what the tool does. Helps the model decide when to use it.
Schema
Zod or JSON schema defining the tool’s input parameters. Used for validation and LLM guidance.
async function
Optional function that executes the tool with validated inputs. Returns the tool result.
boolean
Enable strict schema validation (when supported by provider)
Multi-Step Tool Execution
UsestopWhen to enable multi-step execution where the model can call tools and then use their results:
How It Works
- Step 1: Model receives the prompt and decides to call the
weathertool - Tool Execution: The
executefunction runs and returns weather data - Step 2: Model receives the tool result and generates a text response
Tool Choice
Control when and which tools the model uses:Tool Choice Options
default
Model decides whether and which tools to call
string
Model must call a tool (can choose which one)
string
Model must not call any tools
object
Model must call the specified tool
Tool Execution Options
Tools receive additional context in the second parameter:Tool Call ID
Messages
Access the conversation history:Abort Signals
Forward abort signals to long-running operations:Callbacks
onStepFinish
Called after each step completes:Tool Execution Lifecycle
Monitor tool execution:Tool Approval
Require user approval before executing sensitive tools:Handling Approval Requests
Dynamic Approval
Make approval decisions based on input:Strict Mode
Enable strict schema validation for more reliable tool calls:Not all providers support strict mode. For those that don’t, the option is ignored.
Input Examples
Provide example inputs to guide the model:Only Anthropic providers support input examples natively. Other providers ignore this setting.
Type-Safe Tool Results
Access typed tool results:Error Handling
Handle tool-related errors:Examples
Weather Assistant
Database Query Tool
Next Steps
MCP Tools
Use Model Context Protocol tools
Prompt Engineering
Tips for effective tool usage
Structured Data
Combine tools with structured output