
Parallel Tool Calls and Tool Errors: Getting the tool_result Shape Right
- كاتب موثّق
- Claude API
محتويات المقال
tool_use blocks in one response. The loop runs the first check, sends its result and calls the API again, which answers with a 400: tool_use ids were found without tool_result blocks immediately after. After that is fixed, a second bug appears. On days when the payments check times out, the exception escapes the loop and the user gets "Something went wrong", even though two of the three answers were ready.
The common misconception
Some other APIs carry results in special toolorfunctionmessages, so one message per result feels natural.Early prototypes trigger one tool at a time, and one result per message works fine there. In backend code, an unexpected exception should fail fast instead of being swallowed. Showing the model an error message feels risky, as if it might invent an answer around it.
What actually happens
One assistant turn, one user turn
stop_reason: "tool_use" "can contain several tool_use blocks in a single assistant turn." The docs set three rules for the reply. Return one tool_result for each tool_use block, "all together in the next user message." Match each result with tool_use_id. And put every tool_result block before any text, because nothing may come between the assistant's tool use message and your results. Claude's docs point out the contrast with other APIs: there's no tool role here, and results are content blocks inside an ordinary user message.
{ "role": "user", "content": [ {"type": "tool_result", "tool_use_id": "toolu_01", "content": "healthy"}, {"type": "tool_result", "tool_use_id": "toolu_02", "content": "healthy"}, { "type": "tool_result", "tool_use_id": "toolu_03", "is_error": true, "content": "Timed out after 5 s. Report payments as unknown." }, {"type": "text", "text": "Keep it to one line per service."} ]}tool_use ID or a result that isn't the first block in the message. There's a quieter failure too. Sending a separate user message for each result is, according to the parallel tool use page, the most common reason Claude stops making parallel calls: the history "teaches" it to avoid them.
How you run the calls is your choice
asyncio.gather or a thread pool. Run tools with side effects, shared state or ordering requirements one after another. If you stop early, say because an earlier write failed, every skipped call still gets a tool_result with is_error: true and a short explanation, such as "Not executed: the preceding write_file call failed."
A failing tool is a result, not a crash
content with "is_error": true, and Claude "will then incorporate this error into its response to the user." Make it instructive: what went wrong and what to do next, not just "failed." If Claude's call was invalid, for example missing a required parameter, an error result lets it try again, and the docs say Claude "will retry 2–3 times with corrections before apologizing to the user."
is_error: true", carrying the exception's message rather than the stack trace. And MCP draws the identical line between protocol errors and tool execution errors, which go back in the result with isError: true so the model can self-correct.
The health check loop, fixed
def handle_tools(reply): for call in [b for b in reply.content if b.type == "tool_use"]: out = check_health(call.input["service"]) # TimeoutError escapes messages.append({"role": "user", "content": [ # one message each {"type": "tool_result", "tool_use_id": call.id, "content": out}, ]}) reply = client.messages.create( # 400: other tool_use ids unanswered model="claude-opus-5-5", max_tokens=4096, tools=TOOLS, messages=messages, ) return replyfrom concurrent.futures import ThreadPoolExecutordef run_one(call): result = {"type": "tool_result", "tool_use_id": call.id} try: result["content"] = check_health(call.input["service"]) except TimeoutError: # a result for Claude, not a crash result["is_error"] = True result["content"] = "Timed out after 5 s. Report it as unknown." return resultdef handle_tools(reply): calls = [b for b in reply.content if b.type == "tool_use"] with ThreadPoolExecutor() as pool: # independent, read-only checks results = list(pool.map(run_one, calls)) # keeps the call order messages.append({"role": "user", "content": results}) # one user turn return client.messages.create( model="claude-opus-5-5", max_tokens=4096, tools=TOOLS, messages=messages, )print(handle_tools(reply).content[0].text)try sits around the tool, not around the loop, so a bug in your own request-building code still raises.
tool_result blocks come first, right after the tool_use turn | |
is_error: true with a message Claude can act on | |
tool_result, marked as an error |
How to apply it in your project
Group results by assistant turn Collect every tool_useblock from the response, build all the results, and send them in one user message, results first and text last.Pick an execution strategy per tool Run read-only calls in parallel. Run calls with side effects in order, stop at the first failure, and mark the skipped ones with is_error: true.Catch failures at the tool boundary Convert timeouts, failed API calls and validation errors into error results that say what happened and what to do next. Make invalid calls rarer Write precise tool descriptions and add strict: true, so inputs always match your schema.Check the transcripts Count tool_useblocks per response to confirm Claude calls in parallel, and treat the "without tool_result blocks" 400 as a formatting bug in your loop.
Key takeaways
One assistant turn with several tool_useblocks gets one user message holding everytool_result.Match results with tool_use_idand put them before any text in that message.Execution order is yours: parallel for independent reads, sequential for side effects. A failed or skipped tool still returns a tool_result, withis_error: trueand an instructive message.One message per result can cause 400s and teaches Claude to stop calling tools in parallel.
التعليقات (0)