← Blog

Claude API Tool Use JSON Schema Example

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

Tool use (also called function calling) lets Claude decide when to call your code instead of just generating text. The core of this feature is a JSON Schema that describes each tool's name, purpose, and expected input. If you've searched for a working example, here's the short answer: you define tools as an array of objects with name, description, and an input_schema field that follows standard JSON Schema syntax, pass that array in your API request, and Claude responds with a tool_use content block containing the arguments it wants to call the tool with.

Below is a complete, working example — the full request shape, a realistic schema, and how to handle the response — followed by common mistakes that break tool calling and how to avoid them.

The Basic Tool Definition Shape

Every tool you give Claude needs three things:

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

input_schema is standard JSON Schema — type, properties, required, enum, nested objects, and arrays all work. Claude uses the description fields (on both the tool and each property) to decide when and how to call it, so write them like you're explaining the tool to a new teammate, not like internal variable names.

Full Request Example

Here's a complete curl request with one tool defined:

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-sonnet-4-5",
    "max_tokens": 1024,
    "tools": [
      {
        "name": "get_weather",
        "description": "Get the current weather for a given city",
        "input_schema": {
          "type": "object",
          "properties": {
            "location": {
              "type": "string",
              "description": "City and country, e.g. Lisbon, Portugal"
            },
            "unit": {
              "type": "string",
              "enum": ["celsius", "fahrenheit"]
            }
          },
          "required": ["location"]
        }
      }
    ],
    "messages": [
      { "role": "user", "content": "What is the weather in Lisbon right now?" }
    ]
  }'

If SubToAPI is fronting your Claude access, the request body is identical — only the host and auth header change:

curl https://api.subtoapi.app/v1/messages \
  -H "Authorization: Bearer $SUBTOAPI_KEY" \
  -H "content-type: application/json" \
  -d '{
    "model": "claude-sonnet-4-5",
    "max_tokens": 1024,
    "tools": [ /* same tool array as above */ ],
    "messages": [
      { "role": "user", "content": "What is the weather in Lisbon right now?" }
    ]
  }'

See /docs/tools for the full reference and /docs/quickstart if you're setting up your first key.

Reading the tool_use Response

When Claude decides to call a tool, the response contains a tool_use content block instead of (or alongside) plain text:

{
  "content": [
    {
      "type": "tool_use",
      "id": "toolu_01A2B3C4",
      "name": "get_weather",
      "input": {
        "location": "Lisbon, Portugal",
        "unit": "celsius"
      }
    }
  ],
  "stop_reason": "tool_use"
}

Check stop_reason first — if it's "tool_use", loop over content and look for blocks with type: "tool_use". The input field is already parsed JSON matching your schema; you don't need to parse a string.

Sending the Tool Result Back

Once your code runs the actual function, send the result back as a tool_result block in a new user message, referencing the id from the tool call:

const followUp = await fetch("https://api.subtoapi.app/v1/messages", {
  method: "POST",
  headers: {
    "Authorization": `Bearer ${process.env.SUBTOAPI_KEY}`,
    "content-type": "application/json"
  },
  body: JSON.stringify({
    model: "claude-sonnet-4-5",
    max_tokens: 1024,
    tools: [ /* same tools array */ ],
    messages: [
      { role: "user", content: "What is the weather in Lisbon right now?" },
      { role: "assistant", content: [toolUseBlock] },
      {
        role: "user",
        content: [
          {
            type: "tool_result",
            tool_use_id: "toolu_01A2B3C4",
            content: "18°C, partly cloudy"
          }
        ]
      }
    ]
  })
});

Claude then uses the result to produce a final natural-language answer. This request/response round trip is the entire tool use loop — there's no separate "function call" endpoint.

Schema Design Tips That Actually Matter

Multiple Tools in One Request

You can pass several tools in the same tools array; Claude picks which one (if any) to call based on the user's message:

"tools": [
  { "name": "get_weather", "description": "...", "input_schema": { ... } },
  { "name": "search_flights", "description": "...", "input_schema": { ... } },
  { "name": "convert_currency", "description": "...", "input_schema": { ... } }
]

If you're building this against SubToAPI, the same request format, streaming support, and usage metadata apply across every model you have access to — check /docs/messages for message formatting and /docs/streaming if you want tool use combined with streamed responses. Plans start at the Solo tier (€9) through Team and Scale for multi-seat setups — see /pricing for details, or /signup to get an API key and test the example above directly.

questions

Does Claude's tool use support full JSON Schema, including nested objects and arrays? Yes. input_schema supports standard JSON Schema keywords — type, properties, required, enum, items for arrays, and nested objects. Keep nesting shallow for reliability, but the schema itself isn't artificially limited.

What happens if Claude generates an input that doesn't match my schema? This is rare but possible, especially with very permissive schemas. Always validate the input object in your own code before executing the tool, and return a tool_result with an error message if validation fails — Claude will retry with corrected input.

Can I force Claude to always use a specific tool? Yes, using tool_choice with a specific tool name instead of letting Claude decide automatically. This is useful when you want guaranteed structured output rather than conversational text. See /docs/tools for the exact parameter syntax.

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 →