v0.10.5

Claude Code Tool Calls

Last updated


How claude-code@ turns toolDefs into a text protocol and parses tool calls back out of the CLI's reply, since the CLI runs with its native tools disabled.

What the model sees


When toolDefs is non-empty, the process starts with a tool system prompt and the first turn carries a <tools> block:
  1. <tools>
  2. names: read_file, search_web, find_tools
  3. find_tools: {"name":"find_tools","description":"...","parameters":{...}}
  4. </tools>

Only tool names are listed. The one exception is a tool named find_tools: its full definition is included, and the prompt tells the model to call find_tools with {"query": "select:NAME[,NAME]"} (or keywords) to fetch a schema before calling any other tool. Declare a find_tools tool that returns the requested schemas when you pass more than a handful of tools. When the tool list changes between turns, a new <tools> block is sent and replaces the old one.
Later turns arrive as <user>, <assistant>, <system>, and <tool_result name="..."> blocks; earlier assistant tool calls are replayed as <tool_call> lines.

Call formats accepted


The model is told to answer with:
  1. <tool_call name="read_file">{"path": "README.md"}</tool_call>

The parser also accepts the shapes models produce on their own:
ShapeArguments
<tool_call name="X">{json}</tool_call>the JSON body as-is; a body that contains closing tags is still read whole
<invoke name="X"> with <parameter name="k">v</parameter> childrenrebuilt as a JSON object; each value is JSON-decoded when possible, otherwise kept as a string
<invoke_tool name="X"> with parameterssame as invoke
<invoke_tool> with name and arguments parametersname and arguments read from those parameters

Namespace-prefixed tags (<ns:invoke>) and a </parameter_name> closing tag are accepted too. Any of these tags may carry either a JSON body or <parameter> children.

What is not a call


CaseResult
A tag inside a fenced code block or inline codekept as text
<tool_call name="x">{}</tool_call>dropped (a placeholder, not a call)
A call with an empty namedropped
An empty bodyarguments become {}
A tag with no closing tagit and all text after it kept as text

Resulting Output


Each parsed call becomes a ToolCall with Type: "function" and an ID call_ + 24 hex characters, and FinishReason is tool_calls. Text outside the call blocks stays in Content; when at least one call was parsed and that text is only empty tag pairs, Content is set to Processing. A reply with neither text nor calls returns the error claude returned neither an answer nor tool calls.

中文