Running parallel Claude Code agents without them conflicting
The first time I tried running two Claude Code agents on the same codebase simultaneously, they both tried to edit the same file at the same time. Neither knew about the other. The result was a broken file that neither agent recognized as broken.
After a week of multi-agent runs, here's how I handle parallel execution without conflicts.
The core problem
Agents that share a working directory will conflict unless you explicitly partition their work. Both agents can read and write any file. Neither knows what the other is doing. Conflicts are silent until something breaks.
1. Partition by file, not by function
Bad partitioning:
- Agent 1: "Handle all backend work"
- Agent 2: "Handle all frontend work"
This leads to agents needing the same config files, touching the same package.json, or both updating the same imports.
Good partitioning:
- Agent 1: "Only touch files in src/api/"
- Agent 2: "Only touch files in src/components/"
Give each agent a specific directory they own. Never overlap.
2. Add "your territory" to each agent's CLAUDE.md
## Scope
Your territory: src/api/**
Do not modify files outside src/api/.
If you need something from another directory, read it but do not write it.
Make it explicit and hard to misinterpret. "Backend work" is ambiguous. "src/api/" is not.
3. Shared read, exclusive write
Files that both agents need to read (like a shared config or types file) should be read-only for both. If you need a shared types file updated, do it in a separate sequential pass after both agents complete.
## File access
Read-only: types/index.ts, config/constants.ts
Writable: src/api/**
4. Coordination via a shared task file
If agents need to signal each other (Agent 1 finishes an API route, Agent 2 can now build the UI for it), use a shared task file:
# tasks/coordination.md
## Agent 1 (API)
- [x] POST /api/users complete
- [ ] POST /api/sessions in progress
## Agent 2 (UI)
- [x] Login form complete (using mock)
- [ ] Waiting for POST /api/sessions
Each agent reads this file and knows what the other has shipped.
5. Merge at the end, not during
Don't have agents integrate each other's work mid-flight. Let each agent complete its scope, then do a single integration pass with a third agent or manual review.
The pattern:
- Agent 1 completes API work → commits to branch
feature/api - Agent 2 completes UI work → commits to branch
feature/ui - Integration agent merges both into
feature/combinedand resolves conflicts
This is slower but the conflicts are localized and intentional.
The simple version for most cases
If you're just running 2-3 agents on independent tasks (not the same codebase), you don't need any of this. Just give each agent a completely separate directory and they'll never conflict.
The conflict problem only appears when agents share a working directory. If you can avoid that, do.
I manage multi-agent systems as part of my day-to-day work. The full set of workflow templates — including parallel fan-out, coordinator-worker, and review loops — is at builtbyzac.com/multi-agent-templates.html.
Top comments (0)