← Blog

Claude Function Calling Example Tutorial

2026-11-23 · 5 min read · SubToAPI Team

Claude's version of function calling is called tool use. It lets the model decide when to call a function you've defined, extract the right arguments from a conversation, and hand control back to your code so you can execute the real logic — a database query, a weather API call, a calculator, whatever you need. This tutorial walks through a complete, working example: defining a tool, sending it to Claude, parsing the tool call, and returning the result so the model can finish its answer.

If you're searching for "claude function calling example," the core thing to understand is that Claude doesn't execute your function itself. It outputs a structured request telling you which function to call and with what arguments. Your application runs the function and sends the result back in a follow-up message. This round-trip is the entire pattern — once you see it end to end, it's straightforward to extend to any number of tools.

How Claude tool use works

The flow has four steps:

  1. You send a request with a tools array describing each function (name, description, JSON schema for inputs).
  2. Claude replies with stop_reason: "tool_use" and a content block containing the tool name and input arguments.
  3. Your code runs the actual function and gets a result.
  4. You send that result back as a tool_result message, and Claude produces its final answer.

This is identical in spirit to OpenAI-style function calling, but the message shapes and stop reasons differ slightly, so copy-pasting code between the two APIs usually breaks something.

Step 1: Define a tool

Here's a simple weather-lookup tool definition:

{
  "name": "get_weather",
  "description": "Get the current weather for a given city",
  "input_schema": {
    "type": "object",
    "properties": {
      "city": {
        "type": "string",
        "description": "City name, e.g. 'Lisbon' or 'San Francisco'"
      },
      "unit": {
        "type": "string",
        "enum": ["celsius", "fahrenheit"]
      }
    },
    "required": ["city"]
  }
}

The description field matters more than people expect. Claude uses it to decide whether to call the tool at all, not just how to fill in arguments. Vague descriptions lead to missed or unnecessary calls.

Step 2: Send the request

Using curl against the Anthropic API shape (the same request/response structure works through a proxy like SubToAPI if you're already routing Claude traffic through one):

curl https://api.anthropic.com/v1/messages \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{
    "model": "claude-3-5-sonnet-latest",
    "max_tokens": 1024,
    "tools": [{
      "name": "get_weather",
      "description": "Get the current weather for a given city",
      "input_schema": {
        "type": "object",
        "properties": {
          "city": {"type": "string"},
          "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
        },
        "required": ["city"]
      }
    }],
    "messages": [
      {"role": "user", "content": "What is the weather like in Lisbon right now?"}
    ]
  }'

Step 3: Parse the tool_use response

Claude's reply will look something like this:

{
  "stop_reason": "tool_use",
  "content": [
    {
      "type": "text",
      "text": "I'll check the weather for you."
    },
    {
      "type": "tool_use",
      "id": "toolu_01A1B2C3",
      "name": "get_weather",
      "input": {"city": "Lisbon", "unit": "celsius"}
    }
  ]
}

In JavaScript, you check stop_reason and pull out the tool_use block:

const toolUseBlock = response.content.find(
  (block) => block.type === "tool_use"
);

if (toolUseBlock) {
  const { name, input, id } = toolUseBlock;
  const result = await runLocalFunction(name, input);

  // send the result back
  const followUp = await client.messages.create({
    model: "claude-3-5-sonnet-latest",
    max_tokens: 1024,
    tools: [weatherTool],
    messages: [
      { role: "user", content: "What is the weather like in Lisbon right now?" },
      { role: "assistant", content: response.content },
      {
        role: "user",
        content: [
          {
            type: "tool_result",
            tool_use_id: id,
            content: JSON.stringify(result)
          }
        ]
      }
    ]
  });
}

runLocalFunction is your own code — it could hit a weather API, query a database, or run any deterministic logic. Claude never sees your implementation, only the input it asked for and the result you return.

Step 4: Handle the final answer

Claude will now reply with plain text, incorporating the tool result naturally:

It's currently 18°C and partly cloudy in Lisbon.

If you have multiple tools, Claude can call more than one in the same turn, or chain calls across several turns (look something up, then use that result to call a second tool). Your loop needs to keep checking stop_reason until it comes back as end_turn instead of tool_use.

Common mistakes to avoid

Where SubToAPI fits in

If you're building this inside a product and want your team sharing one Claude subscription instead of juggling individual API keys, SubToAPI sits in front of the same message and tool-use format described here. You get sub_live_... application keys, per-key usage metadata, and team seats, while the request/response shapes for tool calling stay the same as what's documented above. See the tool use docs and messages API reference for the exact schema, or start with the quickstart if you're setting this up for the first time. Plans start at Solo for solo builders and scale up to Team and Scale tiers for larger groups — check pricing or just sign up for a free trial.

questions

Does Claude execute the function automatically? No. Claude only decides which function to call and generates the arguments. Your application code is responsible for actually running the function and returning the result.

Can Claude call more than one tool in a single turn? Yes. A response can contain multiple tool_use blocks. Your code should loop through all of them, execute each one, and return all results together in the follow-up message.

What's the difference between Claude tool use and OpenAI function calling? They solve the same problem but use different message structures: Claude uses tool_use/tool_result content blocks and a stop_reason of tool_use, while OpenAI uses a separate function_call field. The concepts map closely, but the JSON shapes aren't interchangeable.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →