Home › Guides › AI Agents
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

  1. Write the tool schema: name, a clear description, and a strict input JSON schema.
  2. Register the tool in the tools list you pass to the API.
  3. Write a dispatcher function that maps the tool name to real code and runs it.
  4. In the loop, when stop_reason is tool_use, extract each tool_use block's name and input.
  5. Run the dispatcher for each and collect the results.
  6. Append the results as a tool_result message keyed to each tool_use_id, then loop.
  7. 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