Main session and subagents: brief, parallelism, isolation
A subagent is a separate executor with its own context that returns only a summary to the main session, so that context stays clean. The brief is written for a colleague who never saw the conversation: goal, decisions, files, done when, out of scope. Only independent parts run in parallel, and files are separated by owner or worktree, because git does not protect against simultaneous writes.
Agentic development · Orchestration
Two agents edited one file. Only one change survived. Who lost and why was git silent?
The main session launched two subagents: one updates authorisation tests, the other adds a new field to the same fixture. Both edited fixtures/users.ts in a shared working copy. The second wrote the file based on the version it had read earlier and erased the first one's edit. There was no error, git showed nothing, and five new test users vanished. This article covers how the main session and subagents are organised, what to put into a brief, when to run in parallel and how to separate files.
How it works and why. The main session plans, hands out tasks, collects results and checks them. A subagent is a separate executor with its own context, its own set of tools and, if needed, its own model (in Claude Code it is described by a file in .claude/agents/ with a name, a description, tools and a model). It returns only a summary to the main session, so reading a hundred files does not clutter its context. That is the main reason subagents exist: they protect the main session's context and let you limit the rights of a separate executor.
Main session and subagents
Figure 1. The subagent's context does not reach the main session: only the summary does.
Model by the price of the task. Mechanical work with a simple check (searching usages, summarising logs, repetitive edits) does not need the strongest model: a cheaper and faster one usually copes and the result is easy to verify. Complex design, a non-obvious bug and review of risky changes are better given to a stronger one. Specific models and prices change fast, so the rule is worded through complexity and the cost of a mistake. More in the article on cost.
The brief. A normal subagent knows nothing about your conversation, so a brief is written for a colleague who saw nothing. A structure that works:
Goal: one or two sentences;
Decided: conventions, names, formats, what is already known;
Files: entry points and the closest example to follow;
Done when: a verifiable condition;
Checks to run;
Out of scope: what not to touch.
Example: a bad and a good brief. Bad: "Update the authorisation tests". The subagent does not know which tests, what counts as done and which files belong to others. Better:
Goal: tests in tests/auth/ must use the new login helper
instead of filling in the form by hand.
Decided: the helper tests/helpers/login.ts exists, signature
login(page, user). Do not change it.
Files: tests/auth/*.spec.ts. Pattern: tests/auth/logout.spec.ts.
Done when: npm test -- auth passes and there are no manual
form-filling steps in tests/auth/.
Out of scope: fixtures/ (another agent edits them), configs.
Report: changed files and the run result, up to 10 lines.
The "Out of scope" line solves the problem from the start of the article: file ownership is named. The "Done when" line gives the main session something to check the work against instead of just believing the report.
Parallel or sequential. Only independent parts run in parallel. If B depends on A's result (for example on a field contract), either fix the contract first or run A and then B. Parallelism is not free: each subagent needs a brief and the results must be merged and checked.
File conflicts. Two agents in one working copy can overwrite each other's edits and git does not help here: it merges commits, not simultaneous writes to a file. The solution: one owner per file, or a worktree for each agent.
# a separate working copy and branch for each agent
git worktree add ../wt-auth -b agent/auth-tests
git worktree add ../wt-fixtures -b agent/fixtures
# after the work and the merge
git worktree remove ../wt-auth
A worktree is a separate working copy of the same repository with shared history. It does not contain files that git ignores (for example .env) and has no installed dependencies: they must be prepared. That is the price of isolation.
One copy or a worktree
Risk
One directory for two
Agents write to the same files. The last write wins, with no error.
Isolated
A worktree for each
Each works in its own branch. Merging happens deliberately, with ordinary git conflicts.
Figure 2. A worktree turns silent overwriting into a visible branch conflict.
Limits and alternatives. A single agent is enough when the change is small, the edits are tightly coupled or the brief is longer than the work. Orchestration is justified when the parts are truly independent or side work would clutter the main session. A subagent's report is also a claim: the main session checks the result instead of retelling it. A subagent without a brief that sets limits acts on its own interpretation of the task.
Common mistakes.
A brief in the style "you saw the conversation": the subagent did not and guesses.
Running dependent parts in parallel: B's result is built on an old assumption about A.
A shared working copy with no file owners: silent overwriting.
Treating a subagent's summary as verified: the main session verifies.
Check yourself
Two subagents change one file in a shared copy. Why does git show no conflict and what do you change?
Answer
A git conflict arises when merging commits, but here both write to the same file on disk and the last write overwrites the first. Give each file one owner or run each agent in its own worktree and merge the branches deliberately.
A brief to a subagent: "Update the authorisation tests". What is wrong with it and which two lines raise the reliability of the result the most?
Answer
The subagent did not see the conversation, so it does not know which tests, what counts as done and which files belong to others. The most value comes from "Done when" (a verifiable condition, such as a run command) and "Out of scope" (files of other executors).
Related lessons: “Orchestration patterns: executor and reviewer, fan-out”, “Merging the results of parallel agents and resolving conflicts”, “Choosing a model and the cost of subagents”.
In an interview.Junior: A subagent is a separate executor with its own context that returns only a summary. I write the brief as if the executor saw nothing: goal, decided items, files, a done criterion, limits. Middle/Senior: I use parallelism only for independent parts, because each agent costs a brief, a merge and a check. I separate files by owners or worktrees: git does not protect against simultaneous writes in one copy. I verify a subagent's summary by diff and run, and choose the model by complexity and the cost of a mistake.
Orchestration patterns: executor and reviewer, fan-out
In the executor and reviewer pattern the benefit is a separate context: the reviewer sees the task and the diff but not the author's reasoning. So it does not repeat the author's assumptions. It returns findings with a location and a reason, in two or three rounds, and a human settles disputes. Fan-out suits uniform independent parts with one result format; a sequential pipeline is safer for dependent steps.
Agentic development · Orchestration
An agent wrote the code and reviewed it itself: "all good". Why is that not a review?
An agent implemented a request rate limit, then in the same conversation was asked to "check yourself". It answered that everything was correct and offered arguments for its own decisions. It did not see the missing handling of an empty key: in its context that key was "obviously non-empty". The brief, parallelism and isolation are covered in the previous article of the section. Here are the two most common patterns and when each is justified.
How it works and why. An agent that wrote the code tends to explain its own decisions and sees in the code what it intended, not what came out. A fresh context looks at the result the way a human would: without the history of reasoning. So the "executor and reviewer" pattern gives a benefit not from model strength but from the separate context. The same with parallelism: it is justified when the parts are independent and harmful when they are not.
Executor and reviewer
Figure 1. The reviewer does not see the executor's report and reasoning: otherwise it inherits its wrong assumptions.
Executor and reviewer: how to set it up.
the reviewer gets the task, the done criteria and the diff, but not the executor's report or reasoning;
the reviewer's instruction lists what to look for (logic errors, missed boundary cases, leaving the task limits, weakened tests) and requires findings with a place in the code and a reason, not a general rating;
the number of rounds is limited to two or three: without a limit the reviewer finds trifles, the executor fixes them and the loop never ends;
when agents disagree, a human decides. A reviewer agent reduces the load but does not remove responsibility.
The reviewer may also be a stronger model, but the main benefit is the separate context, not the model's strength.
Example: what the reviewer saw that the author did not. An instruction to the reviewer (shortened):
Task (unchanged): limit of 100 requests per minute per key.
Data: the diff in review.diff.
Look for: logic, boundary values (0, 100, 101, an empty
key), leaving the task limits, weak tests.
Format: a list "file:line, problem, why". No general
rating. Fix nothing.
The reviewer returned: "limiter.ts:14, an empty key is counted as one shared counter for everybody, why: the key is taken from a header that may be absent". The executor did not see it because it knew the header is always present. The disputed question "should a request without a key be rejected" is decided by a human, because it is a requirement, not a bug.
Fan-out / fan-in. The main session splits the work into several independent parts, launches a subagent for each (fan-out) and collects the results (fan-in). It suits uniform tasks: porting twenty test files to a new API, checking several modules for the same problem, testing several hypotheses about a failure at once.
the parts are truly independent, otherwise agents get in each other's way;
each gets its own complete brief and the same result format so that collecting answers is simple;
it is decided beforehand who collects and verifies the result: that is the main session's job, not a separate agent "on trust";
the cost grows in proportion to the number of agents, so the number of parts is not made larger than the benefit.
A sequential pipeline. When a step depends on the previous one (plan → implementation → check), agents are launched one after another and pass on only what is needed. It is often more reliable than parallelism.
In parallel or in turn
Fan-out
Independent uniform parts
20 files to a new API, several modules for the same problem. Faster by the clock, costlier in tokens.
Pipeline
Dependent steps
Plan, implementation, check: each step needs the result of the previous. Simpler and more reliable.
Figure 2. Both are legitimate: the choice depends on whether the parts depend on each other.
Limits and alternatives. A small change, tightly coupled edits, a task where the brief is longer than the work: several agents here add brief and merge costs with no gain. Start with one and complicate only when it gives a tangible benefit. A reviewer agent does not find what is not visible from the diff and the task (requirements, agreements, decision history), so it complements human review, not replaces it.
Common mistakes.
Giving the reviewer the executor's report and reasoning: it repeats the same assumptions and the benefit disappears.
Not limiting the number of rounds: endless minor fixes.
Fan-out for dependent parts: agents conflict and the result has to be untangled.
Handing the collection of the result to another agent "on trust": nobody verifies the outcome.
Check yourself
Why should the reviewer not have the executor's report and how does that differ from just "one more agent"?
Answer
The report holds the executor's reasoning and assumptions: after reading them the reviewer looks at the code with the same eyes and repeats the omissions. The benefit of a separate context exists only when the reviewer sees the task and the result, not the path to it.
You need to port 20 independent test files to a new API. When is fan-out justified and what must be decided beforehand?
Answer
The files are independent and the task is uniform, so yes. Decide beforehand the result format and who collects and verifies the outcome (the main session), plus how many agents to launch, because cost grows with their number.
Related lessons: “Main session and subagents: brief, parallelism, isolation”, “Merging the results of parallel agents and resolving conflicts”, “Choosing a model and the cost of subagents”, “Reviewing agent code: where to start and what usually goes wrong”.
In an interview.Junior: The executor writes the code and a reviewer with a clean context looks for problems. I use fan-out for uniform independent parts and do dependent steps in turn. Middle/Senior: The reviewer's value is the separate context: it sees the task and the diff, without the author's reasoning, and returns findings with a place and a reason. A limit of two or three rounds, and disputes are decided by a human. Fan-out always with one result format and collection in the main session, because cost grows with the number of agents. For dependent steps a pipeline is more reliable.
Merging the results of parallel agents and resolving conflicts
Merge parallel agents' results one at a time and run the tests after each merge. Git sees only textual conflicts, while a semantic one (a renamed function and a new call to the old name) shows up only in tests. Reduce conflicts in advance: one owner per file, shared places with one executor, the contract in both briefs. Constant conflicts mean dependent parts, so re-split the work.
Agentic development · Orchestration
Two agent branches are green separately. After the merge CI is red. Where do you look?
Parallel agents returned two branches. The first renamed the function getUser to fetchUser, the second added a new getUser call in another file. Git merged them with no conflicts because the lines do not overlap. Each branch is green on its own. Together the code does not build. Parallel agents return separate edits and they must be combined into one working change. How to separate files between agents is in the article on the brief and isolation. Here: what to do with the results.
How it works and why. Git checks whether the same lines were changed, not whether behaviour is consistent. So conflicts come in two kinds. A textual one is visible at once: both branches changed the same place. A semantic one git does not see: each branch is green alone and together they fail. Examples: two agents added a migration with the same number, renamed a function that another's code calls, used different formats of one field, added the same test identifiers. So checking the branches alone is not enough.
Two kinds of conflict
Textual
Git shows it at once
Both branches changed the same place. Visible during the merge, resolved by hand or by an agent.
Semantic
Git is silent
Branches merged cleanly but break together: a rename, a migration number, a field format. Visible only through tests.
Figure 1. The more dangerous one is the one invisible during the merge.
Prevention beats cure.
split the work so that each file has one owner;
give shared places (a route registry, a dependency lock file, a catalogue of test identifiers, generated files) to one executor or leave them to the main session at the end;
if one part depends on another, agree on the contract first (field names, formats, signatures) and write it into both briefs.
Merge order.
Merge one branch at a time, starting from the smallest or the one the others depend on.
Before each merge bring the branch up to the current base.
After each merge run the tests, not only at the end: then you see which exact change broke the integration.
Regenerate generated files with the command instead of merging them by hand.
At the end a full run in a clean environment.
Example: finding who broke it. After merging two branches the build fails. If both were merged together with a single run, you would have to guess. Step by step:
git switch main
git merge agent/rename-user # green run
npm run build && npm test # ok
git merge agent/new-endpoint
npm run build # error: getUser does not exist
Now it is clear what broke at the second merge: the new endpoint branch calls the old name. The fix is small (update the call) and the cause became visible. If results contradict in behaviour (different interpretations of a requirement), that is a question for a human: what is right is being decided here, not how to merge text.
Who resolves conflicts. A simple textual conflict can be handled by the main session or by one agent given both briefs and a final criterion. A semantic one is found by tests after each merge. Behavioural disputes go to a human.
Checking reports. Each agent's report is compared with its diff: did it touch files outside its limits, did it remove tests. Reports of "all passed" are verified by what the main session runs, not repeated.
Merge order
Figure 2. A test after each merge answers "which branch broke it".
Limits and alternatives. If conflicts occur every time, the parts were dependent. Then re-split the work or run it sequentially instead of perfecting the merge. For branches that touch only different directories the ordinary order is enough. For dependent changes a stack of branches is more convenient, where each builds on the previous one and merging goes from the bottom up.
Common mistakes.
Checking only separate branches: a semantic conflict is invisible until they are joined.
Merging everything at once and running tests once: the failure cannot be tied to a branch.
Merging generated files by hand: the result differs from what the command would give.
Letting agents resolve contradictory requirements: that is a human decision.
Check yourself
Two agent branches merged without conflicts but the build failed. Why did git not warn and how do you find the cause?
Answer
Git compares changed lines, not code consistency, so a semantic conflict (a renamed function and a new call of the old name) stays unnoticed. The cause is found by merging one branch at a time and running the build and tests after each.
Conflicts in parallel agent branches repeat every time. What does it mean and what do you change?
Answer
The parts of the work were dependent. Change the split (each file has an owner, shared places go to one executor) or run the work sequentially instead of improving the merge process.
Related lessons: “Main session and subagents: brief, parallelism, isolation”, “Orchestration patterns: executor and reviewer, fan-out”, “Choosing a model and the cost of subagents”.
In an interview.Junior: I merge parallel branches one at a time and run tests after each merge. I compare an agent's report with its diff. Middle/Senior: I distinguish a textual conflict, which git sees, from a semantic one, visible only through tests. I reduce them in advance: file owners, shared files with one executor, a contract in the briefs. If conflicts are constant, that signals a wrong split and I re-split or run sequentially. A contradiction of requirements is decided by a human, not by agents.
Choosing a model and the cost of subagents
Subagent cost is made of input tokens (paid again at every step), output tokens, the number of steps and the number of agents. Parallelism cuts time, not cost: each agent reads its own context and brief. Pick the model by the price of a mistake and ease of checking: cheaper for mechanical work, stronger for design and risky changes. Compare the cost of an accepted result, including review time.
Agentic development · Orchestration
Four agents finished sooner and the bill grew. Is that normal?
A team parallelised a test migration: four subagents instead of one. Time dropped threefold while token use grew almost twofold, because each agent read the same shared files on its own. The general rule for choosing a model by complexity is in the first article of the section. Here: what the cost is made of and how to manage it without tying to specific models and prices, which change fast.
How it works and why. An agent works in steps and at every step the model receives the whole accumulated context again. So the cost depends not only on the length of the answer but also on how much was read earlier and how many steps were taken. That explains why "read a big log at the start" is paid for many times.
What the cost is made of.
Input tokens: at every step the agent "sees" the context again, so big files and long logs are paid for many times. Some providers lower the price of a repeated prompt prefix through caching, but that is a reduction, not free, and it works only while the cache is warm;
Output tokens: code and long reports the agent generates. Their price per token is usually higher than input;
Number of steps: every failed attempt and retry adds cost;
Number of agents: each subagent reads its own context and spends tokens on the brief, and a reviewer doubles part of the work.
Where the cost comes from
Figure 1. Parallelism shortens clock time, not total tokens.
Parallelism cuts time, not cost. Four agents will finish sooner but together spend no less than one, and usually more because of repeated reading of shared context and the briefs.
How to choose a model. Look at two things: how costly a mistake is and how easy the result is to check.
searching usages, summarising logs, repetitive edits with tests: a cheaper and faster model;
ordinary implementation from a clear plan with tests: a mid-level model;
design, a non-obvious bug, security and risky-change review: the strongest model, because the cost of a mistake is high.
The escalation ladder. Start with a cheaper model and move to a stronger one if the result fails the check. But a cheap model needing three rounds of fixes may cost more than a strong one on the first try, and the most expensive thing remains the time of the human who checks. So look at the cost of an accepted result, not of a single request.
Example: a comparison on your own tasks. Twenty uniform test-porting tasks were run on two models. Illustrative numbers (not real prices):
Model
Accepted as is
Tokens
Review minutes
Cheaper
12 / 20
1.0 (base)
95
Stronger
19 / 20
1.6
40
The stronger one used more tokens but gave seven more accepted results (19 against 12) and 55 fewer minutes of human review. The cost of an accepted result is lower with the stronger one. For another kind of task (searching usages) the picture would be the opposite. That is why you measure on your own tasks rather than by general rankings.
A cheap request or a cheap result
Price of a request
Fewer tokens per attempt
Good for mechanical work with a simple check where a mistake is seen at once.
Price of a result
Tokens plus review and rework time
Counted for complex or risky work where the human spends the most.
Figure 2. Both metrics are legitimate: they answer the question for different kinds of task.
How to keep the cost in check.
clear the context between unrelated tasks;
name files instead of letting the agent read the whole repository;
ask for concise reports;
limit the number of review rounds and retries and, where the tool allows, set a step or budget limit;
do not launch several agents where one is enough.
Limits and alternatives. Prices and model names change, so a comparison from two years ago proves nothing. Do not optimise pennies where a human spends hours on checking. Sometimes it is smarter to buy reliability: a stronger model for a security review costs less than one missed defect. General model rankings are a weak basis for choice because your tasks and code are different.
Common mistakes.
Choosing a model by the price of a request without counting rework and review time.
Parallelising to save money: time is saved, not tokens.
Giving the strongest model everything: mechanical work does not need it.
Choosing by a general ranking without measuring on your own tasks.
Check yourself
Four subagents instead of one finished three times faster but the bill grew. Why does parallelism not lower the cost?
Answer
Parallelism shortens clock time while tokens add up: each agent reads its own context and brief, often the same shared files. The total spend is usually higher than for one agent.
A cheap model needed three rounds of fixes, a strong one got it right first time. How do you compare cost correctly?
Answer
By the cost of an accepted result: tokens of all rounds plus the human's time for review and rework. A model that is cheaper per request can cost more than a strong one, and human time is usually the costliest component.
Related lessons: “Main session and subagents: brief, parallelism, isolation”, “Orchestration patterns: executor and reviewer, fan-out”, “Instruction files, skills and the context budget”.
In an interview.Junior: I choose the model by the task's complexity and the cost of a mistake: a cheaper one for mechanical work with a simple check, a stronger one for complex and risky work. Parallelism saves time, not money. Middle/Senior: I count the cost of an accepted result: tokens, retries, human review time. I measure on my own tasks the share of results without rework instead of trusting general rankings. I keep costs down by clearing context, naming files instead of reading the whole repository, concise reports and round limits.