Skip to main content
While large language models (LLMs) have incredible generation capabilities, they struggle with discrete tasks (e.g. mathematics) and interacting with the outside world (e.g. getting the weather). Tools are actions that an LLM can invoke. The results of these actions can be reported back to the LLM to be considered in the next response. For example, when you ask an LLM for the “weather in London”, and there is a weather tool available, it could call a tool with London as the argument. The tool would then fetch the weather data and return it to the LLM. The LLM can then use this information in its response.

What is a tool?

A tool is an object that can be called by the model to perform a specific task. You can use tools with generateText and streamText by passing them to the tools parameter.

Tool structure

A tool consists of three properties:

Description

An optional description that influences when the tool is selected:

Input schema

A schema that defines and validates the tool’s input:

Execute function

An async function that is called with the validated input:

Multi-step tool calling

By default, generateText stops after a single step. Use stopWhen to enable multi-step execution:
For streaming, use streamText with the same configuration:

Tool choice

Control when tools are called using the toolChoice parameter:

Auto (default)

Let the model decide whether to call tools:

Required

Force the model to call at least one tool:

None

Prevent the model from calling any tools:

Specific tool

Force the model to call a specific tool:

Schema types

The AI SDK supports multiple schema libraries:

Zod

JSON Schema

Accessing tool results

After generation, you can access tool calls and results:

Streaming tool calls

With streamText, you can stream tool calls and results:

Error handling

Handle errors in tool execution:
Errors are captured in toolResults:

Tool packages

Tools can be packaged and distributed through npm. Many ready-to-use tool packages are available:
See the tools documentation for a comprehensive list of available tool packages.