Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 7.6 — Several Tool Calls at Once
LLM Engineering · Example 1 of 2

Three tool calls in one turn

Tool times are simulated with numbers, so the output is the same everywhere. The Java example is single-threaded, as all runnable Java in this course is.

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.

01

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).

02

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

01

How does a model signal several tool calls in one turn, and how must the application answer?

02

Why is pairing results with calls by position a bug when tools run concurrently?

03

When should you not run tool calls in parallel?

Three tool calls in one turn
Sign in to run this example in your browser.

Expected output

the model asked for 3 tools in one turn
call_1 get_weather {"city":"Pune"} -> {"city":"Pune","temp_c":31} (120 ms)
call_2 get_weather {"city":"Delhi"} -> {"city":"Delhi","temp_c":36} (150 ms)
call_3 get_stock {"symbol":"ACME"} -> {"symbol":"ACME","price":42} (300 ms)
one after another: 570 ms
all at once: 300 ms (the slowest call)
tool messages for: call_1, call_2, call_3