Skip to main content

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 the tool 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

Use stopWhen to enable multi-step execution where the model can call tools and then use their results:

How It Works

  1. Step 1: Model receives the prompt and decides to call the weather tool
  2. Tool Execution: The execute function runs and returns weather data
  3. 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:
Tool execution errors appear in the result:

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