Tool Implementation
Define, describe and run tools for Claude: write schemas and descriptions, drive the agentic loop, handle errors and parallel calls, choose client or server tools, add approval gates, and keep tool sets small and context-efficient.
Key points
- 1
A tool definition has a
name, a detaileddescriptionand aninput_schema(JSON Schema). The description is the most important factor in tool performance: say what the tool does, when to use it and when not to, what it returns, caveats, and the format of each parameter. - 2
Use schema features to constrain inputs, such as
enumfor fixed value sets and clear formats like ISO 8601 dates. Setstrict: truewhen tool inputs must always conform to the schema (correct types, required fields present). - 3
Agentic loop for client tools: while the response has
stop_reason: "tool_use", run each requested tool, send the results back astool_resultblocks in a new user message, and call the API again. Any other stop reason ends the loop. - 4
Every
tool_useblock needs a matchingtool_resultwith the sametool_use_id. For parallel calls, return all results in one user message, with thetool_resultblocks before any text. - 5
Report tool failures in the
tool_resultwithis_error: trueand an actionable message (what failed, and whether a retry or a different input might work). Do not drop the result or return an empty success. - 6
tool_choice:auto(the default; Claude decides),any(must use some tool),tool(must use the named tool),none(no tool calls). Forced options are not supported on every model or setting.disable_parallel_tool_uselimits Claude to one call per turn. - 7
Where tools run: user-defined and Anthropic-schema client tools (such as bash and text editor) execute in your app. Server tools (web search, web fetch, code execution, tool search) run on Anthropic's infrastructure with no handler code.
- 8
A server tool's loop can stop with
stop_reason: "pause_turn"when it hits its iteration limit. Resend the conversation, including the paused response, so Claude can continue. - 9
The harness, not the model, decides whether a requested call runs. Auto-run read-only tools, hold side-effecting or irreversible tools for human approval, log the decisions, and return a denial as the
tool_resultso Claude can adapt. - 10
In the Agent SDK,
allowed_toolsanddisallowed_toolsplus permission modes handle clear cases, and thecanUseToolcallback handles the rest at runtime, which is the place for an approval UI. - 11
Keep the tool set small and non-overlapping, and consolidate near-duplicates into one tool with parameters. Selection accuracy drops as the number of tools grows, and every definition costs context.
- 12
Context tools: tool search loads definitions on demand for large catalogs, programmatic tool calling lets Claude call tools from code so intermediate results stay out of context, prompt caching cuts the cost of a stable tool set, and context editing trims old tool results.
- 13
Return concise, high-signal results with human-readable names alongside IDs, rather than raw API dumps.
Read the source
Test yourself on Tool Implementation
Ten questions, with the answer and explanation after each one.