Skip to main content

Tool Calling

Tools (also called function calling) enable LLMs to interact with external systems. The library uses TypeBox schemas for type-safe tool definitions with automatic validation.
This library only includes models that support tool calling, as it’s essential for agentic workflows.

Defining Tools

Tools are defined with TypeBox schemas:
For Google API compatibility, use StringEnum helper instead of Type.Enum. Type.Enum generates anyOf/const patterns that Google doesn’t support.

TypeBox Schemas

TypeBox provides rich schema types:

Basic Tool Usage

Tool Results with Images

Tool results support both text and images:

Validating Tool Arguments

Use validateToolCall to validate arguments against schemas:

Streaming Tool Calls

Tool arguments are streamed and progressively parsed:
Important notes about partial arguments:
  • During toolcall_delta, arguments contains best-effort parse of partial JSON
  • Fields may be missing, incomplete, or truncated mid-word
  • String values may be cut off mid-sentence
  • Arrays and objects may be partially populated
  • At minimum, arguments will be {}, never undefined
  • Google provider does not support streaming tool calls - you get one toolcall_delta with full arguments

Multiple Tool Calls

Models can call multiple tools in one response:

Tool Choice

Control when tools are called:

Error Handling

Handle tool execution errors:

Real-World Example

Complete tool calling workflow:

Provider-Specific Notes

Google

  • Does not support streaming tool arguments
  • Receives single toolcall_delta event with complete arguments
  • Use StringEnum instead of Type.Enum for enums

Mistral

  • Requires tool call IDs to be exactly 9 alphanumeric characters
  • Library handles this automatically via normalization

OpenRouter

  • Tool support depends on underlying model
  • Some models may not support all tool features

Custom Models

  • Check model.api to determine tool format
  • OpenAI-compatible APIs use OpenAI tool format
  • Anthropic API uses Anthropic tool format
  • Google APIs use Google function calling format

Next Steps

Streaming

Learn about streaming tool calls

Thinking

Combine tools with reasoning