E3 · Lesson 04
Parallel tool calls: reads commute, writes do not
Ask the local model to "read notes/a.txt and notes/b.txt" and it replies with two tool calls in one turn (measured on qwen3:4b, 25 October 2026). Module 1's loop runs them one after the other. If each read takes 200 ms, that is 400 ms where 200 would do, and an agent that reads ten documents waits two seconds for nothing. Running them at once is easy. Running them at once correctly takes two rules.
Rule 1: results go back in the order asked
Each result carries its call's id, and a model matches results to calls by id. Return them in the order the model asked anyway. Some servers check that every call in the turn is answered before the next user content. Many models also lean on position, and a log that interleaves calls and results is miserable to read in an incident (Module 18).
Rule 2: only calls that commute run together
Two calls commute if running them in either order leaves the same state and the same results. Two reads commute. A read and a write of the same file do not: read-then-write returns the old text, write-then-read the new. Two e-mails do not commute either. Their effects are independent, but the recipient's inbox now has an order, and so does your audit trail.
The loop cannot work out commutativity from a name, so you declare it: parallel_safe is the set of tools that change nothing. Any other call is a barrier. Everything asked before it finishes before it starts, and nothing after it starts until it ends.
sequenceDiagram
participant L as loop
participant T as tools
Note over L: turn: read a, read b, write a, read a
par reads together
L->>T: read_file a
L->>T: read_file b
end
T-->>L: alpha, beta
L->>T: write_file a (alone: a barrier)
T-->>L: ok
L->>T: read_file a
T-->>L: new
Note over L: results returned in call order: alpha, beta, ok, new
loop/parallel.py scans the turn's calls in order. A run of consecutive safe calls goes to a thread pool, each unsafe call runs alone, and results are written into slots by position, so their order is the calls' order whatever order they finished in. The check measures it with three reads that each sleep 200 ms: the turn takes about 200 ms, with three tools active at once, and the results come back in the order asked.
Safe to reorder is not safe
READ_ONLY includes fetch_url. It changes nothing in the lab and commutes with every read, so it may run in parallel. It is also the lab's way out: a URL can carry data to whoever owns the host (Module 1 lesson 6; Module 11). Parallelism is a property of the effects. Security is a property of the information flow. The two classifications are made for different reasons, and confusing them in either direction is a bug. Treating fetch_url as unsafe only slows the agent down. Treating it as harmless because it is read-only is how data leaves.
Threads, not processes, and a small pool
The tools are HTTP calls to the lab, so the work is waiting, and threads are the right tool (Python's GIL is released while a socket waits). Keep the pool small. Four workers is the default. A model that asks for twenty fetches in one turn should not open twenty connections to one server, and E10 shows what a burst does to a server's tail latency. The pool also bounds what a misbehaving model can do in one turn, which the budget (lesson 6) does not: the budget counts model calls, not tool calls.
Check yourself
- A turn asks for
read_file a,send_email,read_file b,read_file c,write_file d. Which calls run concurrently, and in how many groups? - Why must a refused call (lesson 2) still occupy its slot in the results?
- Give a pair of calls on two different tools that do not commute, although their arguments name nothing in common.
Answers
- Four groups:
read_file aalone (a run of one safe call), thensend_emailalone, thenread_file bandread_file ctogether, thenwrite_file dalone. Only b and c run concurrently. - Every call in the turn must be answered, with its own id and in its place. The slot holds the refusal text, so the model learns which call failed and why, and the results stay aligned with the calls.
list_filesandwrite_fileof a new file.list_filestakes no arguments, yet a listing before the write lacks the file and one after includes it. Both touch the directory. Commutativity is about shared state, not about what the arguments name.