Claude API Tool Use JSON Schema Example
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
- Keep required fields minimal. Only mark a property
requiredif the tool genuinely can't run without it. Over-requiring fields causes Claude to guess or hallucinate values. - Use
enumfor constrained choices. It dramatically reduces malformed inputs compared to free-text strings you then have to validate yourself. - Avoid deeply nested schemas when possible. One level of nesting (objects inside objects) works fine; three or four levels deep increases the chance of malformed input, especially on smaller models.
- Write descriptions for humans, not machines. "The ISO 4217 currency code, e.g. USD" works better than "currency code string."
- Test with ambiguous prompts. If your schema allows two tools to plausibly handle the same request, Claude may pick the wrong one — add disambiguating detail to each
description.
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.