Lesson 5.4: Parallel agents, shared state and communication
Learn how to run agents in parallel safely: rate limits, shared state, conflicting writes, and communication patterns that scale without chaos.
By the end of this lesson you will be able to decide which work can run in parallel, work out how many agents a rate limit can usefully feed, explain how two agents can overwrite each other's work and choose a design that prevents it, pick between passing messages and sharing a store, and count the cost of letting agents talk to each other freely. You will work out that 120 tasks of 6 model calls each take 12 minutes with 6 or more workers against a limit of 60 calls a minute, that the clashes among agents sharing a pool of records grow with the square of their number, rising from 12 a day with 4 agents to 240 with 16, and that six agents talking in a full mesh need 15 channels where a hub needs 6. You should have read Lessons 5.1 to 5.3. No code is needed. The mathematics is division, minimum and counting.
The earlier lessons treated agents as taking turns: the lead hands work down, workers return results. But the main reason to have several agents is often to have them work at the same time. That is where a new family of problems begins, ones that anyone who has written concurrent software will recognise. Two agents may try to change the same thing. One may read a fact that another is about to change. A burst of requests may hit a rate limit and fail. Messages may arrive in a different order from the one you expected. None of this is specific to language models, and the standard remedies are well known. The difference is that agents are less predictable than ordinary code, so the remedies matter more. This lesson covers what can run in parallel, what limits it, how agents share information safely, and how they should talk.
What can run in parallel
Work can run in parallel when its pieces do not need each other's results. Three questions sort it out.
Does piece B need piece A's output? If yes, they must run in order. If no, they can run together.
Do the pieces write to the same place? Two pieces that only read can always run together. Two that write to the same record, file or document need care, which the second half of this lesson covers.
Is the order of results important? If the answer is assembled in a particular order, the lead can still run the pieces together and put the results in order afterwards.
A useful way to see this is as a graph. Draw each piece of work as a box and each dependency as an arrow. Boxes with no arrow between them can run together. The longest path through the graph, from the first box to the last, sets the minimum time, however many agents you have. This longest path is sometimes called the critical path. Adding agents shortens the job only until the critical path is the limit. Adding more beyond that costs money and saves nothing.
For example, a due-diligence task has six pieces: read the accounts, read the news, check the regulator's records, check the owner's background, combine all four into a risk note and write the cover email. The first four are independent and can run together. The fifth needs all four. The sixth needs the fifth. The critical path is one of the first four, then the fifth, then the sixth: three stages. Six agents would not finish faster than four, because only four pieces can run at the first stage and the later stages are sequential.
The limits on parallel work
Even independent pieces cannot always run together. Three limits matter in practice.
Rate limits
Model providers limit how many requests you may make per minute and how many tokens you may send. Tools and services have limits too: a search API may allow 10 requests a second, and a company system may allow only a few connections. Suppose your provider allows 60 model calls a minute. Each worker, working flat out, makes about 10 calls a minute. Six workers use the whole allowance. A seventh, tenth or twentieth worker does not make the job faster, because the extra calls are rejected or delayed. In the worst case they cause errors and retries that make things slower.
Here is the arithmetic. A job has 120 tasks, each needing 6 model calls, so 720 calls. With 3 workers making 10 calls a minute each, throughput is 30 calls a minute, and the job takes 720 ÷ 30 = 24 minutes. With 6 workers, throughput is 60, the limit, and it takes 12 minutes. With 10 workers the demand is 100 calls a minute, but only 60 are allowed, so the job still takes 12 minutes. Without the limit it would take 7.2. With 20 workers it is still 12. Beyond the sixth worker, extra agents bring only extra cost.
Predict first: a job needs 720 model calls. Each worker makes 10 calls a minute and your limit is 60 calls a minute. How long does it take with 3 workers, with 6, and with 20? Decide before reading on.
The answers are 24, 12 and 12 minutes. The limit caps useful parallelism at 60 ÷ 10 = 6 workers. The practical rules are to set a cap on workers equal to the limit divided by each worker's call rate, to queue the rest and to keep some headroom, since other jobs may share the same allowance.
Stragglers
As Lesson 5.1 noted, the job waits for its slowest piece. In parallel work, one agent that is stuck in a loop or waiting on a slow tool can hold up the rest. Give every worker a time limit, and have the lead decide what to do when it passes. Splitting work into many small pieces, handed out as workers become free, shortens the wait caused by one hard piece.
Cost
Parallel workers cost the same tokens as sequential ones, plus the extra fixed blocks. They also cost money faster. Ten workers at once can spend in an hour what a single agent would spend in ten. If a mistake in the brief sends all ten down a wrong path, you pay for ten wrong paths before you notice. A sensible habit is to run one worker first, check its result, and only then start the rest.
Sharing state
Agents that work on one job often need to share information. There are three common ways.
Way · How it works · Good for · Weak pointMessages only · Agents pass results to each other or to the lead. Nothing is shared · Independent pieces, clear hand-offs · Agents cannot see what others have learned until told
A shared store · All agents read and write a common place: a database, a document, a set of files · Work where pieces build on a growing body of facts · Conflicting writes and stale reads
A shared board · A common area, sometimes called a blackboard, where agents post findings and read others' posts · Open-ended problems where agents contribute different parts · Can get cluttered. Needs rules for who writes what
Messages only is the safest and should be the default. A shared store is more powerful and brings concurrency problems. Choose it only when pieces truly need each other's progress.
The problems of shared state
Four things go wrong when agents write to a shared place.
The lost update. Agent A reads a record: stock is 100. Agent B reads the same record: 100. A sells 10 and writes 90. B sells 5 and writes 95. The final value is 95, but the right answer is 85. A's update was lost. This is the classic problem, and agents make it easier to hit because each one reads, thinks for several seconds and then writes, which leaves a wide gap for another to slip in.
The stale read. An agent reads a fact, spends a minute working with it, and acts, while another agent has changed it in the meantime. The first acts on a world that no longer exists.
Conflicting decisions. Two agents each decide something that is fine alone and wrong together. Both book the same meeting room. Both claim the same support ticket and reply to the customer. Both fix the same bug in different ways.
An inconsistent picture. Agent A sees the supplier list before an update and agent B sees it after, so their results do not fit together.
How often will agents clash? Suppose w agents each update one randomly chosen record from a shared set of R records in a round, and the updates overlap in time. The number of pairs of agents is w(w − 1) ÷ 2, and each pair picks the same record with chance 1 ÷ R. So the expected number of clashes per round is about w(w − 1) ÷ (2R). With 8 agents and 50 records, that is 56 ÷ 100 = 0.56 clashes per round. Over 100 rounds in a day, about 56 clashes. With 4 agents it is 12 a day. With 16 agents it is 240. The count grows with the square of the number of agents, so doubling the agents roughly quadruples the clashes. This is why a design that worked with two agents fails when it is scaled to twenty.
Preventing clashes
There are four standard remedies, and a good design often uses more than one.
Ownership. Give each record, file or section exactly one agent that may write it. Others may read it but must ask the owner to change it. Two agents can never clash on something only one may write. This is the simplest and strongest remedy. In the invoice example, each worker owns its own batch. In a coding task, each agent owns its own files, or its own copy of the code that is merged later by a person or a merge step.
Append only. Instead of changing a record, agents add new entries to a log. Two agents adding entries cannot overwrite each other, because each addition is separate. A reader works out the current state by reading the log. This works well for findings, notes and decisions. It needs a rule for what to do when two entries disagree.
A version check. Each record carries a version number. An agent reads the record and its version, does its work, and writes back saying "update version 7 to version 8". If the record is no longer at version 7, the write is refused, and the agent must read again and redo its work. This is sometimes called optimistic concurrency. It lets agents work without waiting, at the price of repeating work when they clash. It suits cases where clashes are rare.
A lock. An agent takes a lock on a record before working on it, and others wait. This prevents clashes but can stall everything if an agent holds a lock and then hangs. Always give a lock a timeout so it releases itself, and avoid holding locks during long model calls.
Making retries safe
When agents retry after a failure, an action may happen twice. A worker sends an email, the confirmation is lost, it retries and the customer gets two emails. The remedy is an idempotency key: a unique ID attached to each action, such as "refund for order 5521". The system checks whether it has already done an action with that ID and, if so, does nothing. With idempotency keys, a retry is safe. Without them, every retry is a risk.
Where the agent should not decide
For anything where a clash would be costly, do not rely on an agent to notice. Put the rule in the tool or the database. A booking tool that refuses a double booking is more reliable than an instruction saying "check that the room is free". The agent can be wrong, but the tool's check is exact. This repeats a theme from the last lesson: enforce by design, not by request.
How agents should communicate
When agents need to talk, there are three patterns.
Hub and spoke. Every agent talks only to the lead, and the lead passes information on as needed. This is the pattern of Lesson 5.2. It is easy to follow and to log, and the lead is a single point where decisions are made. Its weakness is that the lead can become a bottleneck, and a worker cannot ask another worker a quick question.
A shared board. Agents post to and read from a common area. Communication is indirect: an agent does not address another, it leaves a note. This suits open-ended work where agents contribute different pieces, and it leaves a readable record. It needs rules, such as who may post what and how entries are labelled, or it fills with noise.
Free messaging. Any agent may message any other. This is the most flexible and the hardest to control, and its cost grows quickly. With n agents there are n(n − 1) ÷ 2 possible pairs. Six agents have 15. Ten have 45. Twenty have 190. With a hub there are only n channels: 6, 10 and 20. Every extra channel is a place for confusion, loops and cost. The conversation between agents also uses tokens, and agents that chat freely tend to chat more than the task needs.
For most systems the right choice is a hub, with a shared board added only where agents need to see each other's findings. Where agents must speak directly, limit it: allow only named pairs, set a cap on the number of messages and require a fixed format.
The shape of a message
A message between agents should be as structured as a hand-off record. It has a sender, a receiver, a task ID, a type (for example question, result or alert), a body in a known format and, where relevant, a reply deadline. A type field lets code route and filter messages without calling a model. A task ID ties every message to its job for later tracing.
When agents should stop talking
Agents that can message each other can keep going. Two agents asked to agree on a plan may exchange ten messages that add nothing. Cap the number of rounds, require that each message either asks a specific question or returns a specific result, and give the lead the right to decide when agents disagree. Do not let a debate between agents run unattended, since a longer discussion is not a better one.
Testing and debugging parallel systems
Concurrent systems fail in ways that are hard to repeat. A clash that happens once in fifty runs will not show up in three tests. Four habits help.
Test with more agents than you plan to use. Stress shows clashes that a small test hides.
Make runs replayable. Log every message, every read and every write with a time and a task ID, so you can reconstruct what happened in the order it happened.
Check invariants. After a run, verify things that must be true: totals add up, no record has two owners, each task was done exactly once. Code can do this cheaply.
Introduce delays on purpose. Slowing one agent randomly exposes problems that depend on timing.
Three worked examples
Example 1: parallel research with a shared board
A team of four researchers each investigate one question about a company. They post findings to a shared board, each as an entry with a source and a confidence label. Each researcher may only post to its own section, so there are no write clashes. A fifth agent, the synthesiser, reads the whole board when all four have finished and writes the report. Researchers may read others' entries as they go, which stops two of them chasing the same source. The board is append only, and entries are never edited, so a conflicting entry shows up as a second line and not a silent overwrite.
Example 2: inventory reconciliation
Eight agents reconcile stock across 50 warehouses. At first any agent could update any warehouse's record, and the team saw about 50 clashes a day, with some wrong stock figures. The fix was to give each agent six or seven warehouses it alone could write to. A shared record of changes was append only and every update used a version check. Clashes fell to zero, because no two agents could write the same warehouse, and the rare cross-warehouse transfer went through a tool that updated both records in one step or neither.
Example 3: code changes by several agents
Three agents each work on a different part of a software change. If they all edited the same working copy of the code, one agent's edit could overwrite another's. Instead, each gets its own copy of the code, a branch, and works only in its own files. A merge step combines the branches and runs the tests. If two agents changed the same file, the merge step reports the conflict and asks a person or a fourth agent to settle it. The rule is ownership in the working stage and a controlled merge after.
A design worksheet for parallel agents
1. Draw the work as a graph. Mark independent pieces and the critical path.
2. Find the limits: model rate limit, tool limits, cost. Compute the number of workers they can feed.
3. For each place agents share, decide: messages only, a shared store or a board. Start with messages only.
4. For every shared record, name its owner. Where there can be no single owner, choose append only, a version check or a lock with a timeout.
5. Give every action that changes the world an idempotency key.
6. Put critical rules in tools and databases, not in instructions.
7. Choose the communication pattern. Default to a hub. If agents must talk, name the pairs and cap the messages.
8. Set time limits and a cap on running agents. Run one worker first when the brief is new.
9. Log everything with a task ID, and write the invariants to check after a run.
10. Test with more agents than you plan to use.
Common mistakes
Adding agents without checking limits. Beyond the rate limit, more agents cost more and add nothing.
Assuming parallel always means faster. The critical path and the slowest piece set the time.
Shared writes with no owner. Two agents will clash, and the clashes grow with the square of their number.
Relying on the agent to notice a conflict. Enforce it in the tool or the database.
Retries without idempotency. An email or payment can happen twice.
Locks with no timeout. One hung agent can stall everyone.
Free messaging by default. Channels and tokens grow quickly and loops follow.
Unstructured messages. Use a sender, receiver, task ID, type and format.
Starting ten workers on an untested brief. Try one first.
Testing with a single agent. Concurrency problems appear only under load.
Key takeaways
Work can run in parallel when pieces do not depend on each other's output and do not write to the same place. The critical path sets the minimum time however many agents you add.
Rate limits cap useful parallelism. With 10 calls a minute per worker and a limit of 60, six workers are enough. 720 calls take 12 minutes with 6 workers or 20.
Prefer messages only. Use a shared store or board only when pieces need each other's progress.
Shared writes cause lost updates, stale reads, conflicting decisions and inconsistent views. Clashes grow with the square of the agents: 12 a day with 4, 56 with 8 and 240 with 16 in the lesson's example.
Prevent clashes with ownership, append-only logs, version checks and locks with timeouts. Use idempotency keys so retries are safe, and enforce critical rules in tools.
Communicate through a hub by default. Six agents in free messaging need 15 channels against 6 for a hub. Use structured messages and cap the rounds.
Test with more agents than you plan to use, log everything and check invariants after each run.
Check yourself
1. What is the critical path, and why does it limit how much parallel agents can help?
It is the longest chain of dependent pieces. The job cannot finish faster than that chain, however many agents work on the independent parts.
2. Each worker makes 15 calls a minute and the limit is 90 a minute. How many workers can the limit feed?
90 ÷ 15 = 6 workers.
3. Name the four ways shared state can go wrong.
The lost update, the stale read, conflicting decisions and an inconsistent picture.
4. Four agents update random records from a shared set of 20, once per round. About how many clashes occur per round?
4 × 3 ÷ (2 × 20) = 12 ÷ 40 = 0.3 clashes per round.
5. Why is ownership the simplest way to prevent write clashes?
If only one agent may write a record, no two agents can ever write it at once, so there is nothing to clash.
6. How many channels does free messaging need for eight agents, and how many does a hub need?
8 × 7 ÷ 2 = 28 channels for free messaging and 8 for a hub.
Try this
Set aside seventy-five minutes.
1. Take a job with six or more pieces. Draw its dependency graph and mark the critical path.
2. Estimate your rate limit and each worker's call rate. Use the first simulator to find how many workers help.
3. List every place agents would share information. For each, choose messages, a store or a board, and justify it.
4. For each shared record, write its owner. If none can own it, choose append only, a version check or a lock.
5. List every action that changes the outside world, such as sending, paying or booking, and give each an idempotency key.
6. Use the collision simulator with your own counts to see how many clashes to expect without these protections.
7. Choose the communication pattern, write the message format and set a cap on message rounds.
A good result is a plan for running agents in parallel: the graph, the worker cap, the owners of shared records, the protections against clashes and the communication rules. That completes Module 5. You can now decide when to split work, build an orchestrator and its workers, pass work between agents and run them side by side without chaos. The next module moves from building agents to trusting them: connecting them to real systems, bringing people into the loop and choosing the simplest design that works.
Tags: Agentic AI Parallel agents Shared state Concurrency Multi-agent systems