Advanced Claude

Tool Use (Function Calling)

Give Claude the ability to call your functions and APIs to take real actions in your application.

What is Tool Use?

Tool use (also called function calling) allows Claude to request that your application execute a function and return the result. This enables Claude to:

  • Search the web
  • Query your database
  • Call external APIs
  • Execute code
  • Create, read, update files
  • Take actions in your application

How It Works

  1. You define available tools (name, description, parameters)
  2. Claude decides whether to use a tool
  3. Claude returns a tool_use content block
  4. Your app executes the tool
  5. You return the result to Claude
  6. Claude continues with the result

Tool Definition Schema

Tools use JSON Schema to define their parameters. Be descriptive — Claude uses the description to decide when and how to use the tool.

Example

typescript
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic();

// Define tools
const tools: Anthropic.Tool[] = [
  {
    name: "get_weather",
    description: "Get the current weather for a city",
    input_schema: {
      type: "object",
      properties: {
        city: {
          type: "string",
          description: "The city name, e.g., 'San Francisco'",
        },
        unit: {
          type: "string",
          enum: ["celsius", "fahrenheit"],
          description: "Temperature unit",
        },
      },
      required: ["city"],
    },
  },
  {
    name: "search_docs",
    description: "Search the DevForge Academy documentation",
    input_schema: {
      type: "object",
      properties: {
        query: { type: "string", description: "Search query" },
        limit: { type: "number", description: "Max results (1-10)" },
      },
      required: ["query"],
    },
  },
];

// Tool execution
async function executeToolCall(
  toolName: string,
  toolInput: Record<string, unknown>
) {
  if (toolName === "get_weather") {
    return { temperature: 72, condition: "sunny", city: toolInput.city };
  }
  if (toolName === "search_docs") {
    return [{ title: "Getting Started", url: "/docs/start" }];
  }
}

// Agentic loop
async function runAgent(userMessage: string) {
  const messages: Anthropic.MessageParam[] = [
    { role: "user", content: userMessage }
  ];

  while (true) {
    const response = await client.messages.create({
      model: "claude-opus-4-5",
      max_tokens: 4096,
      tools,
      messages,
    });

    if (response.stop_reason === "end_turn") {
      return response.content[0].type === "text"
        ? response.content[0].text : "";
    }

    if (response.stop_reason === "tool_use") {
      messages.push({ role: "assistant", content: response.content });
      const toolResults = [];
      for (const block of response.content) {
        if (block.type === "tool_use") {
          const result = await executeToolCall(block.name, block.input);
          toolResults.push({
            type: "tool_result" as const,
            tool_use_id: block.id,
            content: JSON.stringify(result),
          });
        }
      }
      messages.push({ role: "user", content: toolResults });
    }
  }
}
Try it yourself — TYPESCRIPT