tools
top-level parameter of the API request. Each tool definition includes:
Parameter | Description |
---|---|
name | The name of the tool. Must match the regex ^[a-zA-Z0-9_-]{1,64}$ . |
description | A detailed plaintext description of what the tool does, when it should be used, and how it behaves. |
input_schema | A JSON Schema object defining the expected parameters for the tool. |
Example simple tool definition
get_weather
, expects an input object with a required location
string and an optional unit
string that must be either “celsius” or “fahrenheit”.tools
parameter, we construct a special system prompt from the tool definitions, tool configuration, and any user-specified system prompt. The constructed prompt is designed to instruct the model to use the specified tool(s) and provide the necessary context for the tool to operate properly:
Example of a good tool description
Example poor tool description
ticker
parameter means. The poor description is too brief and leaves Claude with many open questions about the tool’s behavior and usage.
tool_choice
field like so:
auto
allows Claude to decide whether to call any provided tools or not. This is the default value when tools
are provided.any
tells Claude that it must use one of the provided tools, but doesn’t force a particular tool.tool
allows us to force Claude to always use a particular tool.none
prevents Claude from using any tools. This is the default value when no tools
are provided.tool_choice
parameter will invalidate cached message blocks. Tool definitions and system prompts remain cached, but message content must be reprocessed.tool_choice
as any
or tool
, we will prefill the assistant message to force a tool to be used. This means that the models will not emit a chain-of-thought text
content block before tool_use
content blocks, even if explicitly asked to do so.
tool_choice: {"type": "any"}
and tool_choice: {"type": "tool", "name": "..."}
are not supported and will result in an error. Only tool_choice: {"type": "auto"}
(the default) and tool_choice: {"type": "none"}
are compatible with extended thinking.{"type": "auto"}
for tool_choice
(the default) and add explicit instructions in a user
message. For example: What's the weather like in London? Use the get_weather tool in your response.
record_summary
tool with a particular schema. See Tool use with Claude for a full working example.
tool_choice
is set to auto
(this is the default value, see Forcing tool use), and Sonnet and Haiku can be prompted into doing it.
For example, given the prompt “What’s the weather like in San Francisco right now, and what time is it there?”, Claude might respond with:
"Before answering, explain your reasoning step-by-step in tags."
to the user message or system prompt.
It’s important to note that while the <thinking>
tags are a common convention Claude uses to denote its chain of thought, the exact format (such as what this XML tag is named) may change over time. Your code should treat the chain of thought like any other assistant-generated text, and not rely on the presence or specific formatting of the <thinking>
tags.
disable_parallel_tool_use=true
when tool_choice type is auto
, which ensures that Claude uses at most one tooldisable_parallel_tool_use=true
when tool_choice type is any
or tool
, which ensures that Claude uses exactly one toolComplete parallel tool use example
Complete test script for parallel tools
System prompts for parallel tool use
User message prompting
disable_parallel_tool_use
. To work around this, we recommend enabling token-efficient tool use, which helps encourage Claude to use parallel tools. This beta feature also reduces latency and saves an average of 14% in output tokens.If you prefer not to opt into the token-efficient tool use beta, you can also introduce a “batch tool” that can act as a meta-tool to wrap invocations to other tools simultaneously. We find that if this tool is present, the model will use it to simultaneously call multiple tools in parallel for you.See this example in our cookbook for how to use this workaround.stop_reason
of tool_use
and one or more tool_use
content blocks that include:
id
: A unique identifier for this particular tool use block. This will be used to match up the tool results later.name
: The name of the tool being used.input
: An object containing the input being passed to the tool, conforming to the tool’s input_schema
.Example API response with a `tool_use` content block
name
, id
, and input
from the tool_use
block.input
.role
of user
, and a content
block containing the tool_result
type and the following information:
tool_use_id
: The id
of the tool use request this is a result for.content
: The result of the tool, as a string (e.g. "content": "15 degrees"
) or list of nested content blocks (e.g. "content": [{"type": "text", "text": "15 degrees"}]
). These content blocks can use the text
or image
types.is_error
(optional): Set to true
if the tool execution resulted in an error.Example of successful tool result
Example of tool result with images
Example of empty tool result
tool
or function
, Anthropic’s API integrates tools directly into the user
and assistant
message structure.Messages contain arrays of text
, image
, tool_use
, and tool_result
blocks. user
messages include client content and tool_result
, while assistant
messages contain AI-generated content and tool_use
.max_tokens
stop reasonmax_tokens
limit, and the truncated response contains an incomplete tool use block, you’ll need to retry the request with a higher max_tokens
value to get the full tool use.
pause_turn
stop reasonpause_turn
stop reason, indicating that the API has paused a long-running turn.
Here’s how to handle the pause_turn
stop reason:
pause_turn
:
Tool execution error
content
along with "is_error": true
:Invalid tool name
description
values in your tool definitions.However, you can also continue the conversation forward with a tool_result
that indicates the error, and Claude will try to use the tool again with the missing information filled in:<search_quality_reflection> tags
Server tool errors
is_error
results for server tools.For web search specifically, possible error codes include:too_many_requests
: Rate limit exceededinvalid_input
: Invalid search query parametermax_uses_exceeded
: Maximum web search tool uses exceededquery_too_long
: Query exceeds maximum lengthunavailable
: An internal error occurredParallel tool calls not working