What is Tool Use?

Tool use lets Claude call functions you define — to fetch data or take actions in your app.

On its own, Claude only produces text. Tool use (also called function calling) lets Claude ask your code to run a function and hand back the result — so it can look up live data, do a calculation, or trigger an action.

The idea

  • You describe the tools Claude may use (name, description, inputs).
  • When Claude needs one, it responds asking to call it, with the arguments.
  • Your code runs the tool and sends the result back.
  • Claude uses that result to finish its answer.

Why it matters for web apps

Tools connect Claude to your database, your APIs, and your business logic — turning a text generator into an assistant that can actually do things in your product.

Example

Example · json
{
  "name": "get_weather",
  "description": "Get the current weather for a city.",
  "input_schema": {
    "type": "object",
    "properties": {
      "location": { "type": "string", "description": "City name, e.g. Paris" }
    },
    "required": ["location"]
  }
}

When to use it

  • Let Claude call a getWeather(city) function to fetch real-time data before answering a user's weather question.
  • Allow Claude to call a searchProducts(query) tool so a shopping assistant returns results from your live catalogue.
  • Give Claude a createCalendarEvent tool so an AI assistant can schedule meetings directly in Google Calendar.

More examples

Request without tools — no live data

Shows the limitation without tools: Claude cannot access live data and says so.

Example · javascript
// Without tools, Claude can only use its training data
const msg = await client.messages.create({
  model: "claude-haiku-4-5",
  max_tokens: 64,
  messages: [{ role: "user", content: "What is today's EUR/USD rate?" }],
});
// Claude will say it cannot access real-time data

Declare a tool and let Claude use it

Adds a tool definition to the request so Claude can decide to call get_exchange_rate instead of guessing.

Example · javascript
const msg = await client.messages.create({
  model: "claude-haiku-4-5",
  max_tokens: 256,
  tools: [{
    name: "get_exchange_rate",
    description: "Returns the current exchange rate between two currencies.",
    input_schema: {
      type: "object",
      properties: {
        from: { type: "string", description: "Source currency code, e.g. EUR" },
        to:   { type: "string", description: "Target currency code, e.g. USD" }
      },
      required: ["from", "to"]
    }
  }],
  messages: [{ role: "user", content: "What is today's EUR/USD rate?" }],
});

Detect when Claude wants to use a tool

Checks stop_reason to detect a tool-use request, then reads the tool name and input from the content block.

Example · javascript
if (msg.stop_reason === "tool_use") {
  const toolUse = msg.content.find(b => b.type === "tool_use");
  console.log("Claude wants to call:", toolUse.name);
  console.log("With inputs:", toolUse.input);
}

Discussion

  • Be the first to comment on this lesson.