Pair results by id, not by position
The finish order is simulated by sorting on the call times.
A model can return several tool calls in one reply. Each has its own id, they are independent of one another, so run them together to save time, and send back one tool message per call matched by id.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write pair_by_id(calls, results) where results is a list of (tool_call_id, content) in any order. Return the list of tool message dicts in the order of calls (each call is a dict with an id).
Write batch_time(times, parallel) that returns the total simulated milliseconds: the sum if parallel is false, the maximum if true, and 0 for an empty list.
Explain it without notes
How does a model signal several tool calls in one turn, and how must the application answer?
Why is pairing results with calls by position a bug when tools run concurrently?
When should you not run tool calls in parallel?
Expected output
finished in this order: call_2, call_3, call_1
pairing by arrival position (wrong):
call_1 asked about Pune, got {"city":"Delhi","temp_c":36}
call_2 asked about Delhi, got {"city":"Mumbai","temp_c":33}
call_3 asked about Mumbai, got {"city":"Pune","temp_c":31}
pairing by id (right):
call_1 asked about Pune, got {"city":"Pune","temp_c":31}
call_2 asked about Delhi, got {"city":"Delhi","temp_c":36}
call_3 asked about Mumbai, got {"city":"Mumbai","temp_c":33}