← Blog

Claude API Tool Use: Writing JSON Schemas That Work

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

If you're integrating tool use (function calling) with the Claude API, the JSON schema you write for each tool's input_schema is the single most important factor in whether Claude calls your tools correctly. A vague or malformed schema leads to missing arguments, wrong types, or Claude not calling the tool at all. This article covers how to structure JSON schemas for Claude's tool use feature, the constraints Claude actually respects, and common mistakes to avoid.

The short answer: Claude's tool use follows a subset of JSON Schema (draft 2020-12 style), declared per tool as an input_schema object with type: "object", a properties map, and a required array. Claude uses the schema both to decide whether to call a tool and to generate arguments that validate against it. Get the types, descriptions, and required fields right, and Claude's tool calls become predictable enough to parse and execute without extra validation layers.

The Basic Shape of a Tool Definition

Every tool you pass to the Claude API needs three things: a name, a description, and an input_schema. The schema is where JSON Schema rules apply.

{
  "name": "get_weather",
  "description": "Get the current weather for a specific city and country.",
  "input_schema": {
    "type": "object",
    "properties": {
      "city": {
        "type": "string",
        "description": "The city name, e.g. Lisbon"
      },
      "country_code": {
        "type": "string",
        "description": "ISO 3166-1 alpha-2 country code, e.g. PT"
      },
      "unit": {
        "type": "string",
        "enum": ["celsius", "fahrenheit"],
        "description": "Temperature unit for the response"
      }
    },
    "required": ["city", "country_code"]
  }
}

A few things matter here more than people expect:

Supported JSON Schema Features

Claude's tool use supports the common JSON Schema primitives: type, properties, required, enum, items (for arrays), description, and nested object types. It does not reliably support every advanced JSON Schema keyword (like oneOf, allOf, or complex $ref chains), so for production use it's safer to keep schemas flat or shallow and avoid relying on conditional schema logic.

A nested object and array example:

{
  "name": "create_invoice",
  "description": "Create an invoice with one or more line items.",
  "input_schema": {
    "type": "object",
    "properties": {
      "customer_id": { "type": "string" },
      "currency": { "type": "string", "enum": ["EUR", "USD", "GBP"] },
      "line_items": {
        "type": "array",
        "items": {
          "type": "object",
          "properties": {
            "description": { "type": "string" },
            "quantity": { "type": "integer", "minimum": 1 },
            "unit_price_cents": { "type": "integer", "minimum": 0 }
          },
          "required": ["description", "quantity", "unit_price_cents"]
        }
      }
    },
    "required": ["customer_id", "currency", "line_items"]
  }
}

minimum and maximum are respected as hints but aren't hard guarantees — treat them as guidance for Claude, not a replacement for server-side validation of the final arguments.

Handling the Tool Call Response

When Claude decides to use a tool, the response contains a tool_use content block with name and input matching your schema. Your code is responsible for executing the tool and returning a tool_result block in the next message:

const response = 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",
    max_tokens: 1024,
    tools: [weatherTool],
    messages: [
      { role: "user", content: "What's the weather in Lisbon, Portugal?" }
    ]
  })
});

const data = await response.json();
const toolUse = data.content.find(b => b.type === "tool_use");
// toolUse.input is parsed JSON matching your input_schema

If you're routing Claude access through SubToAPI to get an application API key, consistent streaming, and usage metadata in one dashboard instead of juggling raw Anthropic credentials, tool use works the same way against /v1/messages — see the tool use docs and the messages API reference for the full request/response shape.

Common Schema Mistakes

Testing Your Schema

Before shipping a tool, run a handful of adversarial prompts against it: ambiguous requests, requests missing obvious required info, and requests that could match multiple tools. Check that Claude asks a clarifying question instead of guessing when required fields can't be inferred, and that it picks the correct tool when two schemas look similar. This catches schema ambiguity issues much earlier than production traffic will.

If you're prototyping this quickly, a free trial at signup and the quickstart guide get you making real tool-use calls in a few minutes, so you can iterate on schema wording without waiting on separate API provisioning.

questions

Does Claude support the full JSON Schema spec for tool use? No. Claude supports core keywords — type, properties, required, enum, items, description — but advanced composition keywords like oneOf, allOf, and deep $ref chains aren't reliably supported. Keep schemas flat and explicit.

Why does Claude sometimes omit a field I expect it to fill in? Usually because the field isn't in the required array, or its description doesn't make clear when a value is needed. Mark genuinely necessary fields as required and write descriptions that remove ambiguity.

Should I validate tool_use.input even if my schema is strict? Yes. The schema guides generation but isn't a hard runtime contract — always validate types, ranges, and business logic server-side before executing any tool call with real side effects.

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 →