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
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
One directory for two
Agents write to the same files. The last write wins, with no error.
A worktree for each
Each works in its own branch. Merging happens deliberately, with ordinary git conflicts.
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).