Tool Calling

Let agents invoke real functions in your environment—write files, call APIs, or modify state instead of just generating text.

Overview

Tool calling transforms your AI agents from text generators into capable assistants that can interact with the real world. Define tools with schemas, and the AI will decide when and how to use them.

Defining Tools

A tool consists of a name, description, JSON schema for parameters, and an invoke function:

tool-definition.ts

Using Tools with Agents

Pass your tools to the agent via the command:

tool-usage.ts

Multiple Tools

Provide multiple tools and the AI will choose which ones to use:

multiple-tools.ts

Best Practices

Tip

Write clear, descriptive tool descriptions. The AI uses these to decide which tool to use.
  • Validate inputs: Always validate tool inputs before executing operations.
  • Return useful data: Include relevant information in return values for the AI to use.
  • Handle errors gracefully: Return error information instead of throwing to let the AI recover.
  • Keep tools focused: Each tool should do one thing well.

Next Steps