본문으로 건너뛰기
L12.7

Parallel Tool Calls

About 40 min

Goal

By the end of this lesson, you can identify which tool operations may safely run in parallel and join their results without breaking dependencies, budgets, or side-effect rules.

Start with dependencies​

Suppose you are preparing breakfast. Toasting bread and pouring juice can happen at the same time because neither result is needed to begin the other. Spreading butter on the toast cannot happen before the toast exists. "Can these jobs run at once?" is therefore really a dependency question.

Parallel tool calls work the same way. Independent operations can overlap and reduce total waiting time, but dependent or conflicting operations must stay ordered. The safe mental model is a small dependency graph: arrows mean "this result or effect must happen before that work can start." Parallelism is an optimization only after those arrows and resource limits are clear.

weather_lookup ─┐
├─→ combine results
calendar_read ──┘
refund_order ─────→ waits for approval first

Parallel reads still have limits​

Read-only operations are often easier to parallelize, but even reads can have limits. Two expensive searches may exceed a rate limit or shared budget. A harness should bound concurrency and include all parallel work in the task's cost and timeout accounting.

Side effects require more caution. Two writes to the same record may race. A refund and a cancellation might conflict. Even if each action is individually authorized, running them concurrently can create an illegal combined state. The controller should serialize conflicting effects or use application-level concurrency controls.

Join, cancel, and trace branches​

Joining parallel results means waiting for the required set of outcomes and deciding what to do with partial failure. If one of three independent reads fails, the task may continue with two results, retry the failed call, or stop because all three were required. That rule should be explicit before execution.

Cancellation also matters. If one parallel branch proves the task should stop, the harness may need to cancel or ignore other branches. Cancellation should not pretend a side effect was undone. An already-completed external action still needs reconciliation and recording.

Tracing parallel work requires parent-child relationships and timing. Two child spans can overlap under the same controller step. This makes it possible to see that total latency was closer to the slowest branch than the sum of all branch durations.

Parallelism does not add authority​

Parallelism does not increase authority. Each call still passes tool selection, permission, approval, and sandbox rules. A common mistake is to validate the batch as a whole and then assume every child action is legal. Each child should remain independently checkable.

Start with conservative parallelism. Parallelize only operations with clear independence and bounded resource use. Optimization is valuable when it reduces a measured bottleneck, not when it makes state transitions harder to reason about for no demonstrated benefit.

Predict

Two tool calls both write to the same account balance. What is the safest default?

Run the local Lab​

Run:

python labs/notebooks/level-12/l12-07-parallel-tools.py

The Lab models four operations and a dependency graph.

  1. Run it unchanged. It prints waves: [['inventory_lookup', 'user_profile'], ['recommendation'], ['write_report']] and wave count: 3. The two reads share the first wave because neither depends on the other.
  2. Before editing, predict the waves if inventory_lookup must wait for user_profile.
  3. In deps, change only "inventory_lookup":set(), to "inventory_lookup":{"user_profile"}, and rerun.
  4. Now it prints waves: [['user_profile'], ['inventory_lookup'], ['recommendation'], ['write_report']] and wave count: 4: the two reads are in separate waves. The script still ends with PASS: every task runs after its dependencies: the scheduler added a wave instead of violating the new dependency.

Loading lab…

Write the core logic yourself​

Open:

labs/notebooks/level-12/l12-07-parallel-tools-exercise.py

Implement dependency-wave scheduling so independent work can run together, dependent work waits, and a cycle cannot silently stall the scheduler.

Run:

python labs/notebooks/level-12/l12-07-parallel-tools-exercise.py

A correct implementation reaches the PASS: marker. Try hints 1 and 2 before comparing with the completed exercise (labs/answers/advanced/level-12/l12-07-parallel-tools-answer.py).

Stuck? Show hint 1

Schedule every ready task together using only work completed before the current wave; a cycle must fail.

Python syntax stuck? See P3 — loops and batching. For the function's input/output contract and how its checks fail, see P9 — function contracts.

Stuck? Show hint 2

Track done as a set; select sorted tasks whose dependencies are subsets of done; raise RuntimeError on an empty wave.

Hint 3 — completed TODO code

Try the earlier hints first. Compare only after attempting the exercise.

def dependency_waves(deps):
done = set()
waves = []
while len(done) < len(deps):
ready = sorted(name for name, needs in deps.items() if name not in done and needs <= done)
if not ready:
raise RuntimeError("dependency cycle or unsatisfied dependency")
waves.append(ready)
done.update(ready)
return waves

Quick Check

1. When are two tool calls good candidates for parallel execution?
2. What should a join policy define?
3. Why trace parallel calls as child spans?

0 of 3 questions answered.

Explain it back​

Give an example with three tool calls: two that can run together and one that must wait. Explain the dependency and what the harness should do if one parallel read fails.

Check your explanation

A strong answer mentions:

  • For example, read account status and order details concurrently when neither depends on the other's result.
  • An action that uses both readings, such as proposing a refund decision, must wait for their join and then pass its own authorization check.
  • If one read fails, record that child failure, apply bounded retry/cancellation policy, and avoid executing dependent side effects on incomplete evidence.

Key Takeaways

  • Parallelize only work with clear independence.
  • Bound concurrency, cost, and time across the whole batch.
  • Conflicting side effects need serialization or coordination.
  • Join and cancellation rules should be explicit.
  • Every child action keeps its own permission and tracing checks.

Next Lesson

Next, L12.8 — Delegation and Subagents turns one large goal into bounded subtasks without giving away uncontrolled authority.

References

Lesson actions

Completion is stored locally on this device.

View progress