AI Agents · How-to
How to Define and Call a Tool in an AI Agent
Give your agent the ability to act: define a tool with a schema, and dispatch it when the model calls it.
You'll need: A working agent loop and an Anthropic API key.
Steps
- Write the tool schema: name, a clear description, and a strict input JSON schema.
- Register the tool in the tools list you pass to the API.
- Write a dispatcher function that maps the tool name to real code and runs it.
- In the loop, when stop_reason is tool_use, extract each tool_use block's name and input.
- Run the dispatcher for each and collect the results.
- Append the results as a tool_result message keyed to each tool_use_id, then loop.
- Keep the description and schema precise — that's what the model actually reads.
Watch outThe model never sees your code, only the schema. A vague description or loose input types cause wrong or hallucinated calls.
Go deeper: Deploy Your First AI Agent
The AI guide has complete tool schemas, a full research agent, and the dispatcher pattern.
Get the guide