Parallel work
Choose when to delegate independent work and define handoffs, shared-state rules and evaluation.
THE IDEA
Parallel work
Parallel execution helps when tasks have independent resources and outputs. It fails quietly when they do not: three workers rewriting the same line produce a conflict, and an external rate limit (2 concurrent requests) turns five "parallel" 4-minute tasks into 3 rounds, 12 minutes. Check which resources each task reads and writes before fanning out, and decide in advance how overlapping edits are merged.
Parallel work helps when tasks have independent resources and outputs. Shared files and external rate limits make apparently independent work collide or queue.
Predict first. Does three workers editing one file automatically produce a useful merge?
Chapter context
Delegation can divide a task into focused work units, but coordination creates costs and new failure paths. Add helpers when their independent contributions can be integrated and checked more clearly than one agent can do the work alone.
TRY IT YOURSELF
Part 1. Do it hands-on
Find conflicts and queueing delays by hand, then write the conflict detector and a merge.
Parallel work helps when tasks have independent resources and outputs. Shared files and external rate limits make apparently independent work collide or queue.
- To do: Three workers each rewrite line 10 of the same file differently. How many lines have conflicting edits?
- To do: Three workers edit lines 1, 10 and 20. Conflicting lines?
- To do: Five independent tasks, but the external API allows only 2 requests at once. How many rounds are needed?
- To do: Each task takes 4 minutes. Total minutes under that limit?
- To do: Does three workers editing one file automatically produce a useful merge? Type yes or no
Part 2. Do it in code
- conflicts(writes): writes maps a worker to the list of resources it writes (files, rows, lines). Return the sorted resources written by two or more workers.
- can_run_together(a, b): a and b are dicts {"reads": [...], "writes": [...]}. True only if neither writes something the other reads or writes.
- merge_edits(base, edits): base is a list of lines; edits maps a worker to {line_number (0-based): new text}. Apply every edit. If two workers change the same line to DIFFERENT text, keep the base text for that line and report the line. Identical changes are not conflicts. Return (merged_lines, sorted conflicting line numbers).
- rounds(n_tasks, max_concurrent): ceil(n_tasks / max_concurrent).
- total_minutes(n_tasks, minutes_each, max_concurrent): rounds × minutes_each.
Stuck, or something not working? You can come back to it any time.
CHECK YOUR UNDERSTANDING
Does three workers editing one file automatically produce a useful merge?
Chapter reference: math, worked example & code
This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.
Math and notation
With independent parallel task durations and merge time m, an ideal lower-bound schedule is , excluding orchestration overhead. If independent tasks each succeed with probability p, all n succeed with probability ; correlated failures invalidate that simple model.
Worked example
Three tasks take 2,5 and 3 minutes; integration takes 1. Ideal sequential duration is 11 minutes, while fully independent parallel duration is 6. Separately, three independent 90%-reliable components all succeed with probability 0.9^3=0.729. The latency gain does not itself establish a reliability gain.
Reference code
The following example uses the Python standard library and does not call a remote service.
from math import isclose
times,merge=[2,5,3],1
assert sum(times)+merge == 11
assert max(times)+merge == 6
assert isclose(.9**3,.729)
Python examples are provided for study and local execution; this page does not execute Python.
Common mistakes
Parallel workers need explicit ownership of shared artifacts. Agreement can reflect correlated failure. More agents can increase token usage and integration work even when wall-clock time falls.