Skip to the lesson
CivOps AI Academy · F15AI Fleet: Several Agents at Once, Without Collisions
0%

Chapter 1 · Session 16

Split the work so agents cannot collide

Several agents make you faster only when the work is cut into pieces that do not touch the same files, in an order that respects what depends on what. Claude Code drafts that cut as a wave plan; you check it and write each piece a brief before any agent starts.

35 min2 stepsNo agents running yet

By the end of this chapter you can

  • Say why more agents do not mean proportionally faster, and what the real limit is.
  • Have Claude Code write docs/waves.md: tasks, waves, owners and the open pull requests to avoid.
  • Turn wave 2 into briefs with acceptance checks written first, merged before any agent starts.

What a fleet is, and what it is not

A fleet is several Claude Code sessions working at the same time, each on one task, each in its own copy of the repository on its own branch, each ending in a pull request that the checks and a person judge. Nothing about the rules changes: every change is still a branch, a pull request, green checks, owner two's approval and a merge. What changes is that you have three pull requests to read instead of one [1].

That is also the limit. In 1967 Gene Amdahl showed that running work in parallel is capped by the part that stays serial [2]. In a fleet the serial part is you: reading each pull request, merging them one at a time and sorting out anything that conflicts. If a fifth of the effort is yours, eight agents give at best about 3.3 times the speed of one.

Amdahl's law for an agent fleetSpeed-up at eight agents is 5.9 times with 5% serial work, 3.3 times with 20%, and 2.1 times with 40%. Speed-up with n agents when a share of the work is serial (review, merge, fixing conflicts)1×2×4×6×8×12345678agents working in parallel5% serial: 5.9×20% serial: 3.3×40% serial: 2.1×dashed: perfect 8× (never happens)
Amdahl's law for a fleet, drawn to scale. Your review and merging decide the speed-up far more than the number of agents. Size the fleet to what you can read carefully in a morning.

A good task for an agent

PropertyTestStamping plant example
SmallOne pull request you can read in under 30 minutesThe hold screen's reason picker
IndependentNeeds nothing from another task in the same waveThe queue filter does not wait for the picker
OwnedWrites only the folders its brief names, and nobody else in its wave writes themapp/operator/holds/ and its tests
ProvableAcceptance checks written before the work startsGiven a hold with no reason, when saved, then it is refused with a message
BoundedCan be done in about 80 agent turns; stops after two identical failuresStop and report instead of trying a third time

Some tasks need others first: nothing can show hold reasons before the table that holds them exists. Tasks with nothing before them form the first wave; when they merge, the next wave can start. Changes to the database, to the package list and to the checks are never handed to a parallel agent: they belong to you, the lead, and usually make up wave 1.

Tasks in wavesSix tasks in three waves: the hold reasons table first and alone, then three tasks in parallel, then two, each arrow a dependency. Wave 1 · you, in the dayT1hold reasons tableWave 2 · three agents at onceT2hold reason pickerT3queue filter by pressT4lot history exportWave 3 · two agents at onceT5holds-per-press widgetT6phone layout fixesA task starts when the tasks it depends on have merged; inside a wave, no two tasks touch the same files
The stamping plant's next piece of work, in waves. The new table goes first and alone, built in the day with you watching. Three agents then work at once, then two.
File ownership across agentsA grid of three agents by six folders: each screen folder written by exactly one agent; migrations, package.json and .github by you only. File ownership for wave 2: each folder has one owner; shared files belong to you, the leadagent 1agent 2agent 3app/operator/holds/writesapp/supervisor/queue/writesapp/lots/export/writessupabase/migrations/package.json.github/you only: schema, packages and the checksTwo agents writing one file is the most common cause of a broken wave
Who writes what in wave 2. Each screen folder has exactly one agent; the schema, the package list and the checks stay with you.
Step 1

Have Claude Code write the wave plan

15 minutes

Why

Agents that are handed overlapping work collide: one push is refused because another pull request touches the same file, or the same fix is built twice. Cutting the work by folder before any agent starts is the cheapest way to prevent it, and a written plan is something you and owner two can check in five minutes.

Do this

Terminal or PowerShell
cd ~/code/acme-platform && git switch main && git pull && claude
Paste into Claude Code
Read docs/INTENT.md, docs/SCHEMA-PLAN.md, docs/measures.md and every file in docs/briefs/. Do not change any code.

I want to build the next piece of the platform with several Claude Code agents working at the same time. On a new branch fleet/wave-plan, write docs/waves.md with these sections:

1. "Still to build": every change the platform still needs for the decision in docs/INTENT.md, one line each, in plain English. If you are unsure whether something is wanted, ask me; do not invent features.
2. "Tasks": each change rewritten as a task that is small (one pull request I can read in under 30 minutes), independent (needs nothing from another task in its wave), owned (names the folders it may write, which no other task in its wave writes), provable (has acceptance checks I can read, written before the work starts) and bounded (about 80 agent turns).
3. "Waves": group the tasks so a task depends only on tasks in earlier waves. Any migration, any change to package.json, .github/ or the checks goes in wave 1 or to me as the lead, never to a parallel agent.
4. "Owners": a table per wave of folder and the one task that writes it. package.json, package-lock.json, supabase/, .github/, CLAUDE.md and .claude/ are owned by me, the lead.
5. "Open pull requests": the open pull requests and the files each touches (gh pr list, then gh pr diff with --name-only). If a task needs one of those files, put it in a later wave and say which pull request it waits for.
6. "Fleet size": the widest wave, and how many agents I should run: no more than the widest wave, and no more than 3 the first time.

Open a pull request titled "Wave plan", wait for the checks, and tell me the waves in plain words.

You will see recreated screen

claude — acme-platform
⏺ Read(docs/INTENT.md)
⎿ Read 38 lines
⏺ Bash(gh pr list --json number,title,headRefName)
⎿ #41 Lot status colours on the supervisor queue
1
⏺ Write(docs/waves.md)
+## Waves
+Wave 1 (lead, in the day): T1 hold_reasons table and its tests
+Wave 2 (3 agents): T2 hold reason picker · T3 queue filter by press · T4 lot history export
+Wave 3 (2 agents): T5 holds-per-press widget · T6 phone layout fixes
+## Owners, wave 2
+app/operator/holds/ T2
+app/supervisor/queue/ T3 (waits for #41, which touches it)
+app/lots/export/ T4
2
⏺ Pull request #44 adds docs/waves.md: 6 tasks in 3 waves. Wave 1 is a migration, so it is yours. Wave 2 has three tasks with no shared folders; T3 should start after #41 merges. Suggested fleet size: 3. Checks are green.
Claude Code reads the documents and the open pull requests before it plans. Your tasks will come from your own intent.
  1. It looks at open pull requests first, so no task is handed a file someone else is changing.
  2. Each folder in a wave has exactly one task. The overlap with #41 is named, not ignored.

Check it worked

Pull request Wave plan has one new file, docs/waves.md, with all six sections. No folder appears twice in one wave, and migrations, package.json and .github/ are marked as yours.

Fastest path

Asking for the open pull requests in the same prompt saves the most common collision before it happens. Merge the plan as soon as owner two approves: every agent will read it from main.

If it goes wrong

Two tasks in one wave both need package.json (a new library each)

Say: Move every package change to the lead. List the packages needed and I will add them in one pull request before wave 2 starts. Two agents changing one file is the most common cause of a collision.

The plan has eight agents and twelve tasks in one wave

Say: Cut wave 2 to the three most valuable tasks and move the rest to later waves. You read every pull request; more than you can read carefully in a morning only adds conflicts.

It lists features nobody asked for

Say: Remove anything that docs/INTENT.md does not need, or list it under Not now. Unasked-for work is unreviewed scope.

Step 2

Do wave 1 yourself, then write wave 2's briefs

20 minutes

Why

Wave 1 is the change everything else depends on, usually a migration, so it is done the way Session 5 taught: one agent, you watching, applied by you after merge. Then each wave 2 task gets a brief with its acceptance checks written first. The checks, not the agent's opinion, decide when a task is done, and writing them before the work is the only way an agent cannot quietly fit them to what it built.

Do this

If your wave plan has a wave 1, build it now, as in Session 5: a new migration with its tests, reviewed, merged and applied (from Session 11 the deploy applies it). Then paste:

Paste into Claude Code
Read docs/waves.md (merged), docs/briefs/TEMPLATE.md and the newest brief in docs/briefs/ as an example.

On a new branch fleet/wave-2-briefs, write one brief per wave 2 task, numbered after the last brief in docs/briefs/. Each follows the template and also has:
- "Folders you own": the only folders this agent may create or change, from the Owners table.
- "Do not touch": every other folder, and always package.json, package-lock.json, supabase/, .github/, CLAUDE.md and .claude/. If the task seems to need one of them, stop and say so.
- "Acceptance checks": written now, in plain English, as Given / When / Then, including at least one refused or wrong input and one check at phone width. The agent turns each into a test and shows it failing before the change and passing after.
- "Limits": stop after two identical failures and report; stop if the work needs a folder you do not own.
- "Finish": open a pull request titled with the brief's number and name, wait for every check on the latest commit, and report the test command, its exit code and the last lines of its output.

Do not write any code for the tasks. Open a pull request titled "Wave 2 briefs", wait for the checks and show me the link.

Read each brief's acceptance checks with the person who will use the screen, as you did with the schema plan. Owner two approves; merge.

You will see recreated screen

claude — acme-platform
⏺ Write(docs/briefs/07-hold-reason-picker.md)
+## Folders you own
+app/operator/holds/ and tests/operator/holds/
+## Do not touch
+Everything else, and always package.json, supabase/, .github/, CLAUDE.md, .claude/
+## Acceptance checks
+1. Given a hold, when the operator saves it, then a reason from the list is required.
+2. Given no reason, when saved, then it is refused with: Choose a reason.
+3. Given a 390 px phone, then the picker's targets are at least 44 px.
+## Limits
+Stop after two identical failures and report.
1
⏺ Pull request #45 adds briefs 07, 08 and 09 for wave 2. No code. Each names its own folders, which do not overlap, and has 3 to 5 acceptance checks including a refused input and a phone check. Checks are green.2
One brief per task. The acceptance checks are the part you read most carefully.
  1. Written before any agent starts, in words the operator can check.
  2. No code yet: the briefs merge first, because each agent starts from main and must be able to read its brief there.

Check it worked

docs/briefs/ on main has one new brief per wave 2 task. Each names its folders, has a Do not touch list, acceptance checks with a refused input and a phone check, and the two-failure limit.

If it goes wrong

An acceptance check says only "the page works" or "the tests pass"

Say: Rewrite check N as Given / When / Then with the exact input and the exact result the operator sees. A check nobody can fail is not a check.

A brief needs a new column or table

That is a wave 1 task. Move it to the lead: write the migration in the day, merge and apply it, then write the brief.

Knowledge check

Two wave 2 tasks both need a new package added to package.json. What is the fix?

Knowledge check

Your review and merging are 20% of the total effort. Roughly what speed-up can eight agents give?

References

  1. Anthropic: Building effective agents (the orchestrator-workers pattern). https://www.anthropic.com/engineering/building-effective-agents
  2. Amdahl, G. M. (1967). Validity of the single processor approach to achieving large scale computing capabilities. https://doi.org/10.1145/1465482.1465560
  3. Claude Code docs: Run agents in parallel. https://code.claude.com/docs/en/agents

Chapter 2 · Session 16

One agent first, then three, each in its own worktree

Each agent gets its own worktree: a separate folder on its own branch, sharing the repository's history. You add the fleet's rules to CLAUDE.md, cap the spending, prove one task with one agent, and only then start the other two.

40 min4 stepsThree terminals at the end

By the end of this chapter you can

  • Add the rules for working in parallel to CLAUDE.md and start docs/fleet-ledger.md.
  • Set the spending ceiling before more than one agent runs.
  • Start Claude Code in a worktree with claude --worktree, prove one task, then add two more agents.
  • Keep each agent's context small: one brief, its own folders, a fresh session per task.

A worktree per agent

A git worktree is a second working folder for the same repository, on its own branch [1]. Claude Code makes one for you: claude --worktree <name> creates the folder .claude/worktrees/<name>/ on a new branch worktree-<name>, starting from main as it is on GitHub, and starts the session inside it [2]. While a session is in its worktree, Claude Code refuses edits and commands aimed at your main folder. Three terminals, three worktrees: three agents that cannot edit each other's files.

Shared by every agent on your computerWhat goes wrongThe rule
The git stashOne agent's set-aside work is taken or lost by anotherNo git stash: commit to your own branch instead
The temporary folder (/tmp)A test log is half empty or belongs to another jobWrite scratch files inside your own worktree
"Stop every process called node"Another agent's test run is killedStop only the processes you started, by their ID
Your Claude plan's usage windowThree agents use it up three times as fastCap spending; start agents a minute or two apart
GitHub checks and Vercel previewsPull requests queue for the same buildersFewer, smaller pull requests; never re-run old checks

The first three rows are real: in our build, agents on one machine overwrote each other's stashes, logs and builds until these rules went into the project's standing instructions.

What each agent knows: keep it small

An agent knows only what is in its context: Claude Code's own instructions, your CLAUDE.md, the brief, and everything it has read and run so far. Every request sends all of it again. As it grows, the model recalls what is in it less reliably, and every request costs more [3]. So: one short CLAUDE.md every agent shares (Claude Code's guidance is under about 200 lines [4]), one brief per agent that points to files instead of pasting them, and a fresh session for each task.

What fills the context windowStacked from top: system prompt and tools, project memory, the brief, accumulated files and turns, and new tool results. One request from an agent 40 turns into a task (about 90,000 tokens), drawn to scaleSystem prompt and tool definitionsstable: cachedProject memory (CLAUDE.md or AGENTS.md)stable: cachedThe briefstable: cachedFiles read, command output, earlier turnsgrows: cached once sentThis turn's new tool resultsnew: written to the cacheKeep the top stable (same instructions, same order) and most of every request is billed at the cache-read price
What fills one request. The top layers are the same on every request, so the prompt cache serves them at a fraction of the price. The middle grows with everything the agent has read: keep tasks small and start a new session per task.
Step 3

Add the fleet's rules to CLAUDE.md and start the ledger

10 minutes

Why

Every agent reads CLAUDE.md before anything else, so a rule written there once applies to all of them, tonight and every night after. These rules are the ones we had to add after agents in our own build overwrote each other's work, reported "all tests pass" when they had not, went round in circles, and weakened a check to get to green. The ledger starts now so the first run is counted.

Do this

Paste into Claude Code
On a new branch fleet/rules, add a section "Working in parallel" to CLAUDE.md, keeping the whole file under 200 lines (move detail into docs/ and point to it if you need room):

- Each agent works only in its own worktree under .claude/worktrees/, on its own branch, and only in the folders its brief owns. Before starting, list the open pull requests and the files each touches; if your task needs one of those files, say which pull request it overlaps and stop. If a push is refused for an overlap, do not rebuild the commit: say which pull request we are waiting for.
- Never use git stash (commit to your branch instead), never write to /tmp (use a folder inside your worktree that git ignores), and never stop processes by name (pkill, killall, taskkill /IM): stop only processes you started, by their ID.
- Never edit, skip or loosen an existing test, check, limit or CI step to make something pass, and never use a different command to do something you were refused. If you think a check is wrong, stop and explain why.
- Before saying something works, show the exact command, its exit code and the last 20 lines of its output. Never pipe a test command into another command. If you could not run it, say "could not measure", not "passed".
- After two identical failures, stop and report what failed, what you tried, and whether the cause is in our code or outside it.
- A red check counts only if it ran on the branch's latest commit. If the branch is behind main, merge origin/main into it (no rebase, no force push), push, and wait for the new checks before diagnosing anything.

Also add .claude/worktrees/ to .gitignore, and create docs/fleet-ledger.md: a table with the columns Date, Task (brief), Run (worktree), Cost estimate, Check runs until green, Merged, Review minutes, Notes, and one paragraph explaining cost per merged change.

Open a pull request titled "Fleet rules", wait for the checks and report.

You will see recreated screen

claude — acme-platform
⏺ Write(CLAUDE.md)
+## Working in parallel
+- Own worktree, own branch, only the folders your brief owns.
+- No git stash, no /tmp, never stop processes by name.
+- Never weaken a test or check to get to green. Stop and explain.
+- Proof = command, exit code, last 20 lines. Never pipe tests.
+- Two identical failures: stop and report.
+- Red check on an old commit: merge origin/main, push, wait.
1
⏺ Write(.gitignore)
+.claude/worktrees/
2
⏺ Pull request #46: CLAUDE.md is now 118 lines with the parallel-work rules; .gitignore ignores agents' worktrees; docs/fleet-ledger.md is ready for the first run. Checks are green.
The rules go into the file every agent reads first. The ledger starts empty.
  1. Short rules, each from a real failure. The whole file stays under 200 lines.
  2. Without this, every agent's folder shows up as untracked files in your main folder.

Check it worked

On main: CLAUDE.md has the section Working in parallel and is under 200 lines; .gitignore has .claude/worktrees/; docs/fleet-ledger.md exists with its columns.

If it goes wrong

CLAUDE.md is now 300 lines

Say: Move anything that is not a rule for every task into docs/ and leave a one-line pointer. Keep CLAUDE.md under 200 lines. A long file costs context on every request and is followed less reliably.

It wants to put the rules in .claude/settings.json instead

Say: Leave .claude/settings.json as it is: it holds the deny rules from Session 1. These are instructions, so they go in CLAUDE.md.

Step 4

Set the spending ceiling before you add agents

5 minutes

Why

Three agents spend about three times as fast as one, and from tonight they will run while nobody watches. A ceiling set on the account is the one limit no agent can talk its way past. It is a person's job, on the provider's own page, like every other account setting in this course.

Do this

On a company Team plan (Session 1), the organisation's usage page holds the spend limits for usage beyond each seat's allowance:

Open

https://claude.ai/admin-settings/usage

Organization settings, Usage: check that usage credits have a monthly spend limit you are comfortable with, for the organisation and for your own seat.

If the fleet runs on a Claude Console account (paid per token) instead, set the spend limit on its workspace in the Console's Settings, under Workspaces and Limits [5]. Then write the limit, and where it is set, in docs/fleet-ledger.md above the table.

You will see recreated screen

claude.ai/admin-settings/usage
MembersBillingUsageClaude Code
Usage credits
Usage credits1
Organization monthly spend limit$3002
Spent this month$41.20
The usage page. The spend limit caps what agents can draw beyond the plan's allowance.
  1. On, so a busy day does not stop the fleet halfway; off, if you would rather it stopped.
  2. The ceiling. Pick a number you would not mind losing on a bad night, and write it in the ledger.

Check it worked

A spend limit is set where your fleet's usage is billed, and docs/fleet-ledger.md states it and where it is set.

If it goes wrong

I cannot see the Usage page

Only owners or members with billing access can. Ask owner one, or the person who bought the seats in Session 1, to set the limit.

Agents stop with "You've hit your session limit"

Your seat's usage window is used up; it resets at the time shown. Run fewer agents at once, keep tasks small, and start them a few minutes apart.

Step 5

Prove the task with one agent first

15 minutes

Why

Starting several agents before one has worked multiplies every mistake in the brief, the rules or the setup by the number of agents. In our own build, five agents left running overnight finished almost nothing because they all queued for one shared resource nobody had measured. One agent, watched, shows how long a task takes, where it gets stuck and what the agents will share.

Do this

Terminal or PowerShell
cd ~/code/acme-platform && git switch main && git pull && claude --worktree wave2-task1
Paste into Claude Code
You are agent 1 of wave 2, in your own worktree. Read CLAUDE.md, then docs/waves.md, and carry out the FIRST task of wave 2, following its brief in docs/briefs/.

1. Before you start: run npm ci, list the open pull requests and the files each touches, and stop if any overlap your brief's folders.
2. Turn each acceptance check into a test and show me it failing before you change anything.
3. Write only in the folders your brief owns.
4. Push, open the pull request, wait for every check on the latest commit, and fix anything red before reporting.
5. Then tell me how long it took, how many check runs it needed, what you got stuck on, anything in the brief that was unclear, and what agents working at the same time would share (Claude usage, GitHub checks, Vercel previews, the development database) and how many could use each at once.

You will see recreated screen

claude — acme-platform
Created worktree .claude/worktrees/wave2-task1 on branch worktree-wave2-task11
⏺ Bash(gh pr list --json number,files)
⎿ No open pull request touches app/operator/holds/
⏺ Bash(npm test -- tests/operator/holds)
⎿ 3 failed (expected: the picker does not exist yet)
2
⏺ Bash(gh pr checks 47 --watch)
⎿ ci pass · schema pass · clamps pass · Vercel preview ready
⏺ Pull request #47 is green on its latest commit. 41 minutes, 2 check runs (the first failed a lint rule). Stuck once: the brief did not say whether the reason list is per site. Shared: your Claude usage window, GitHub checks (each pull request runs 4) and Vercel previews. No agent uses the development database: worktrees have no .env.local.3
The session runs inside its own worktree. The first line under the prompt shows where.
  1. A new folder and branch just for this agent. Your main folder is untouched.
  2. Red first: proof that the tests test something.
  3. The numbers you need before adding agents, and an unclear brief to fix before the next one.

Check it worked

Pull request for task 1 is open and green on its latest commit, and you have written its time, check runs and what it got stuck on in the ledger's Notes.

Fastest path

Fix the brief's unclear point now, in the briefs for tasks 2 and 3 as well, before starting them. One sentence now saves two agents asking the same question.

If it goes wrong

Error: run claude once in this folder first to accept the trust dialog

Run claude in ~/code/acme-platform once, answer Yes, type /exit, and run the worktree command again.

npm test fails in the worktree with a missing key

Correct: a worktree has no .env.local, so no keys, and agents never need one. Say: Tests must not need a real key. Use the test setup's made-up values, or leave this test to CI, and tell me which. Do not copy .env.local into worktrees.

It reports "all tests pass" but the pull request is red

Say: The checks on the pull request are red. Show me gh pr checks for the latest commit, read the failed log, fix the cause and wait for green before reporting. The check on GitHub is the judge, never the summary.

Step 6

Add the second and third agents, a few minutes apart

10 minutes

Why

Once one task has gone through cleanly, the others can run at the same time because the wave plan gave them separate folders. Starting them a few minutes apart spreads their large first requests, which all count against the same usage window, and gives you time to see each one start properly.

Do this

Open a second terminal window:

Second terminal
cd ~/code/acme-platform && claude --worktree wave2-task2
Paste into Claude Code
You are agent 2 of wave 2, in your own worktree. Read CLAUDE.md, then docs/waves.md, and carry out the SECOND task of wave 2, following its brief in docs/briefs/. Run npm ci; list the open pull requests and their files and stop on any overlap; show each new test failing first; write only in your folders; push, open the pull request, wait for every check on the latest commit and fix anything red before you report. Report time taken, check runs and anything unclear in the brief.

A few minutes later, a third terminal: the same command with wave2-task3, and the same prompt with agent 3 and the THIRD task. Then, in any terminal in ~/code/acme-platform:

Terminal or PowerShell
git worktree list

You will see recreated screen

Terminal
$ git worktree list
~/code/acme-platform a91c2e0 [main]
~/code/acme-platform/.claude/worktrees/wave2-task1 5d03b7a [worktree-wave2-task1]1
~/code/acme-platform/.claude/worktrees/wave2-task2 e41f9c2 [worktree-wave2-task2]
~/code/acme-platform/.claude/worktrees/wave2-task3 0b7d611 [worktree-wave2-task3]2
Three worktrees beside your main folder, each on its own branch.
  1. Task 1 is finished and waiting for review; its folder stays until you merge.
  2. One folder and one branch per agent. If two lines show the same branch, stop both agents.

Check it worked

Three pull requests are open, one per wave 2 task, each changing only its brief's folders (the Files changed tab).

If it goes wrong

A push is refused, or two pull requests change the same file

Two agents were given overlapping work. Stop the later one (Esc, then /exit, and choose Keep). Say to it later: Your push overlaps pull request #N. Do not rebuild the commit; wait until #N merges. Then fix the Owners table in docs/waves.md.

An agent's test log is empty, or another agent's build was killed

An agent used a shared tool: the stash, /tmp or stopping processes by name. Check that the Fleet rules pull request is merged and that the agent started after it. Restart that agent's task in a fresh session.

An agent is silent for twenty minutes

Silence is not death: long thinking prints nothing. Check whether its files are changing (git -C .claude/worktrees/wave2-task2 status) before you stop it.

Knowledge check

Why does each agent run in its own worktree?

Knowledge check

Five agents were left running and almost nothing was finished. What should have come first?

References

  1. Git documentation: git-worktree. https://git-scm.com/docs/git-worktree
  2. Claude Code docs: Run parallel sessions with worktrees. https://code.claude.com/docs/en/worktrees
  3. Anthropic: Effective context engineering for AI agents. https://www.anthropic.com/engineering/effective-context-engineering-for-ai-agents
  4. Claude Code docs: How Claude remembers your project (CLAUDE.md). https://code.claude.com/docs/en/memory
  5. Claude Code docs: Manage costs effectively (spend limits). https://code.claude.com/docs/en/costs

Chapter 3 · Session 16

Count every run, merge one at a time

Judge a fleet the way a plant judges a press: by cost per good part. Here the good part is a merged change that passed its checks. You read each agent's cost, merge the pull requests in wave order with the checks re-run each time, and work out what one merged change really cost.

30 min3 stepsEnds with a gate

By the end of this chapter you can

  • Read an agent's cost and cache share with /usage, and clear the session between tasks.
  • Merge pull requests one at a time, updating each from main and waiting for checks on its latest commit.
  • Record each run in docs/fleet-ledger.md and work out your own cost per merged change.

Cost per merged change, not per token

A night's token bill means little on its own. What matters is how many changes passed their checks, survived your review and merged, and what each one cost in total. Element F01, chapter 11, works through what an eight-agent night costs (about $69 to $199 in tokens, depending on the model tier, at prices checked in October 2026) and has a calculator for your own numbers [1]. Take its middle case, eight workers on a mid-tier model with good caching, about $115.78 in tokens, and suppose six of the eight pull requests merge:

Cost of one merged changeTokens, workers (mid-tier model, cached): $19.3; Tokens, your planning session: $1.86; Retries and CI allowance (25%): $5.29; Your review, 20 min at $60 an hour: $20 One merged change costs about $46, and the biggest single line is your own review timeTokens, workers (mid-tier model, cached)$19.30Tokens, your planning session$1.86Retries and CI allowance (25%)$5.29Your review, 20 min at $60 an hour$20.00
One merged change, worked through and drawn to scale. Tokens divided by 6 merged changes, plus 25% for retries and checks, plus 20 minutes of your review at a $60-an-hour loaded labour rate.
LineWorkingPer merged change
Worker tokens$115.78 ÷ 6$19.30
Your planning session$11.14 ÷ 6$1.86
Retries and checks allowance25% of the token lines$5.29
Your review20 minutes at $60 an hour$20.00
Total$46.45

Two lessons. The merge rate moves the cost more than the token price: if only three of eight merge, every change costs twice as much in tokens. Better briefs and acceptance checks written first raise it. And your review is the biggest single line, so make it fast: small pull requests and checks that decide for you.

The cheapest model that passes

Model tiers differ in price by about four times [2]. Most fleet tasks, built from a clear brief with checks written first, do not need the largest. Choose the model in Claude Code with /model; start a task on the default, and move up a tier only if it fails its checks twice. Models and prices change every few months, so this course names tiers, not models. Whatever you choose, the checks decide.

Matching the model to the taskThree tiers of model, small, mid and largest, each with the tasks it suits and its list price. Pick the cheapest model that passes the task's acceptance checksSmall, fast model$1 in / $5 outrename, format, lint fixessummarise logs and test outputfirst pass over a long fileMid-tier model$2 in / $10 outmost build tasks from a clear brieftests, views, widgets, formsthe default for workersLargest model$4 in / $20 outplanning and splitting the workhard bugs and design choicesreviewing other agents' workList prices per million tokens, checked October 2026; prices and models change
Matching the model to the task. List prices per million tokens of input and output, checked October 2026. Check the pricing page on the day you choose.
Step 7

Read each agent's cost, then clear it

5 minutes

Why

Each session knows what it spent and how much of its input came from the prompt cache, which is about a tenth of the price of fresh input. Writing it down per task is how you learn your own cost per merged change instead of guessing. Clearing afterwards stops the next task paying, on every request, for the last task's files.

Do this

In each agent's terminal, once its pull request is green:

Type into that agent's Claude Code
/usage

Write the Total cost and the cache share in docs/fleet-ledger.md's Notes for that task (you will commit them in Step 9). If that agent will do another task, clear it first:

Type into that agent's Claude Code
/clear

You will see recreated screen

claude — acme-platform
> /usage
Total cost: $6.841
Total duration (wall): 41m 12s
Prompt cache (main): 212 requests · 93% of input tokens from cache · 3 misses · warm2
The Session block at the top of /usage. The figure is an estimate at list price; your bill is on the provider's usage page.
  1. This task's estimated cost. On a subscription it counts against the plan's usage, not a bill, but it is still the number to compare.
  2. Above 90% is healthy. A low share means something at the top of the context keeps changing: a new tool or connector mid-session, or a different CLAUDE.md.

Check it worked

Every wave 2 task has its cost estimate and cache share written down.

If it goes wrong

The cache share is low from the first requests

Each session started with something different at the top. Keep one CLAUDE.md for every agent and put task detail in the brief, not in CLAUDE.md. Set up tools and connectors before you start, not during.

One task cost five times the others

Read its conversation: usually it read far more files than it needed or went round in circles. Next time, point the brief at exact files, and keep the two-failure stop.

Step 8

Merge in wave order, one at a time

15 minutes

Why

Every merge changes main, so a pull request that was green against this morning's main may not be green against main with the first one merged. Updating each from main and waiting for its checks on the new commit catches the clash before it reaches the plant's platform. A red check from before the update is about an old commit: re-running it proves nothing.

Do this

Open

https://github.com/acme-ops/acme-platform/pulls

Open pull requests. Take them in the order of docs/waves.md.

For each, in turn: read Files changed (only its brief's folders; no existing test changed, only new ones added), open its preview on your phone, and have owner two approve. Merge the first. For each next one, GitHub shows This branch is out-of-date with the base branch: choose Update branch with a merge commit (not rebase), wait until every check is green on that new commit, then merge. Before merging, paste into that agent's terminal:

Paste into Claude Code
GitHub has merged main into your branch (Update branch). Pull it, then list every file and test this branch added and confirm each is still there after the merge. Show me only what this branch changed since it left main (git diff origin/main...HEAD, three dots), one line per file on why. If a check is red, tell me which commit it ran on before diagnosing anything. Do not rebase or force push.

You will see recreated screen

github.com/acme-ops/acme-platform/pull/48
ConversationCommitsChecksFiles changed
Queue filter by press (brief 08)
Update branch2
Merge pull request
The second pull request after the first has merged. Update it, then wait for the checks on the new commit.
  1. The first merge changed main. This pull request has not been tested against it.
  2. Update with a merge commit. Never rebase or force push: the deny rule from Session 1 stops Claude Code doing it anyway.
  3. Green on the new commit, named here. Only then merge.

Check it worked

All wave 2 pull requests are merged, each with every check green on its latest commit, and main's own checks are green after the last merge.

If it goes wrong

A pull request is red for a reason that has nothing to do with it, or its logs have expired

The verdict is about an old commit. Do not re-run old runs (in our build, re-running old checks sent 113 failure emails in one day). Update the branch from main and judge only the checks on the new commit. A Vercel deploy marked Canceled because a newer one replaced it is not a failure.

Update branch reports conflicts

Two tasks touched the same file after all. In that agent's terminal: Merge origin/main into this branch, resolve the conflicts keeping both changes, run the tests, push, and explain each conflict in plain words. No rebase, no force push. Then fix docs/waves.md's Owners table.

After the update, a test or file this branch added has gone

Someone undid a batch of changes on main and the undo took yours with it; no conflict is reported. Say: Find the commit that removed it and tell me before changing anything.

Files changed shows an existing test edited or a check loosened

Do not merge. A green check against a test the agent changed proves nothing. Say: Restore the original test; if you think it is wrong, explain why and I will decide.

Step 9

Fill in the ledger and work out your cost per merged change

10 minutes

Why

After one wave you have real numbers: what each task cost, how many check runs it needed, whether it merged and how long you spent reading it. Those beat any estimate in this course and tell you, before tonight, whether a night of agents is worth it for your plant.

Do this

Paste into Claude Code in your repository folder, after replacing the <...> parts
On a new branch fleet/ledger-wave-2, add one row to docs/fleet-ledger.md for each wave 2 task. Use these numbers from /usage: <paste each task's cost and cache share>. My review minutes: <paste per task>. Read the rest from GitHub (gh pr view and gh pr checks): merged or not, and how many check runs each needed until green on its latest commit.

Then work out the cost per merged change: total cost of every run, merged or not, plus 25% for retries and checks, divided by the number merged, plus my review minutes at $60 an hour (change it to: <my loaded hourly rate>). Show the working in a short paragraph under the table. Open a pull request titled "Fleet ledger: wave 2" and wait for the checks.

Then close each agent: type /exit in its terminal and choose Remove for the worktree, since its work is merged.

You will see recreated screen

claude — acme-platform
⏺ Write(docs/fleet-ledger.md)
+| 2026-10-20 | 07 hold reason picker | wave2-task1 | $6.84 | 2 | yes | 18 | brief unclear on per-site reasons |
+| 2026-10-20 | 08 queue filter | wave2-task2 | $5.12 | 1 | yes | 12 | |
+| 2026-10-20 | 09 lot export | wave2-task3 | $9.30 | 3 | yes | 25 | CSV header names fixed on review |
+Cost per merged change: ($21.26 × 1.25) ÷ 3 = $8.86 in tokens, plus 18.3 review minutes ($18.33) = $27.19.
1
⏺ Pull request #50 records wave 2: three runs, three merged, $27.19 per merged change, of which two thirds is your review time.2
The ledger after the first wave, in your main folder.
  1. One row per run, merged or not. Failed runs count too: they are part of the cost of the ones that merged.
  2. Review time is usually the biggest line, as in the worked example.

Check it worked

docs/fleet-ledger.md on main has a row per wave 2 run and your cost per merged change, and git worktree list shows only your main folder.

If it goes wrong

The worktree will not remove: it is locked or has changes

Check its pull request is merged. Then in a terminal in ~/code/acme-platform: git worktree remove .claude/worktrees/wave2-task1. If git says it is locked, run git worktree unlock on it first. Never force-remove a worktree whose work is not on GitHub.

My cost per merged change is far above the example

Look at the merge rate first: a run that did not merge still costs. Usually a brief was unclear or a task too big. Split it, sharpen its acceptance checks, and run it again tomorrow.

Before you go on · Session 16 is done whenTick each one

Knowledge check

Eight pull requests cost $120 in tokens and three merge. Which change lowers the cost per merged change most?

Knowledge check

The second pull request was green last night, but the first has just merged. What do you do before merging the second?

References

  1. Anthropic: Claude pricing. https://platform.claude.com/docs/en/about-claude/pricing
  2. Claude plans and pricing. https://claude.com/pricing
  3. Claude Code docs: Manage costs effectively (track your costs with /usage). https://code.claude.com/docs/en/costs
  4. Claude Code docs: How Claude Code uses prompt caching. https://code.claude.com/docs/en/prompt-caching
  5. GitHub Docs: Keeping your pull request in sync with the base branch. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/keeping-your-pull-request-in-sync-with-the-base-branch

Chapter 4 · 12 questions · 80% passes

Final assessment

Twelve questions across the element. Score 80% (10 of 12) to pass. Your LMS records your score and each answer; you can review the chapters and try again.

15 min12 questions≈ 15 minutesRetake allowed

Choose one answer for each question, then submit. You will see the right answer and why for every question.

1. What mostly limits how much faster a fleet of agents makes you?
2. Which task is ready to hand to an agent in a wave?
3. Who changes package.json, supabase/migrations/ and .github/ during a wave?
4. Why are the briefs merged to main before the agents start?
5. What does claude --worktree wave2-task2 do?
6. An agent's test log is half empty and another agent's build was killed. What is the likely cause?
7. Why prove a task with one agent before starting three?
8. Claude Code's guidance for the length of CLAUDE.md is:
9. A pull request is red with expired logs, from a run before yesterday's fix reached main. What do you do?
10. Where is the fleet's spending ceiling set?
11. Three runs cost $21.26 in tokens and all three merge. With a 25% allowance, what is the token cost per merged change?
12. A green pull request's Files changed shows an existing test was edited. What do you do?