Skip to main content
Tool calling enables language models to interact with external functions and APIs, extending their capabilities beyond text generation.

Define a Tool

Create tools using defineTool() with Zod schemas:

Basic Tool Usage

Implement a complete tool calling flow:

Tool Choice Strategies

Control when and how tools are used:

Multiple Tools

Provide multiple tools for the model to choose from:

Handling Tool Calls

Best practices for executing tool calls:

Tool Calling with Streaming

Handle tool calls during streaming:

Advanced Tool Examples

Tool Result Messages

Structure tool results properly:

Helper: Convert Result to Message

Use resultToMessage() to simplify conversation building:

Best Practices

Always validate tool arguments before execution:
Help the model understand when to use tools:
Return errors as tool results instead of throwing:
Prevent tools from hanging:

Next Steps

Chat Completion

Learn basic message generation

Structured Outputs

Combine tools with structured responses

Streaming

Stream tool calls in real-time

Multi-Modal

Use tools with image inputs