Syllabus Lesson 249 of 250 · Working with Model Replies
Working with Model Replies

Parallel Tool Calls: One Round Trip

Why it matters Ask "what's the weather in Paris, Tokyo and Lima?" and a current model asks for all three tools in one reply. Answer them wrongly and the model either loses track or stops calling tools in parallel.

The rules for the reply you send back:

  • One user message whose content is a list of tool_result blocks, one for every tool_use block, in the same order. Splitting them across several messages teaches the model to stop asking in parallel.
  • Each result echoes its call's id as tool_use_id.
  • A tool that fails still gets a result: is_error: true and a short message, so the model can explain or try another way. Never drop it, and never let one failing tool crash the whole turn.
  • A tool the model invented (not in your registry) is an error result too, never something you try to run.

(Before this message goes back, the model's own reply, with all its blocks including thinking, is appended to the conversation unchanged.)

About these replies: they are simulated. Each one was written for this course in exactly the shape the Claude Messages API returns (content blocks, stop_reason, usage), and each copies something real models do. They are not recordings of a live model, so the exact wording is ours; the shapes and the problems are the real ones your code will meet.

Your turn

Write tool_results(reply, tools), where tools maps a name to a function. Return {"role": "user", "content": [...]} with one block per tool_use block, in order: {"type": "tool_result", "tool_use_id": ..., "content": str(result), "is_error": False} when the tool ran, and is_error: True with content "unknown tool: NAME" or "error: MESSAGE" (the exception's message) when it did not. Keep the starter's REPLIES and TOOLS as they are: the check reads them.

Spotted a problem in this lesson? Report it · Ask about this lesson

This exercise opens soon

Finish the lesson before it and this one unlocks. The whole course is free, no account needed, and you can read this lesson right now.