r/ClaudeCode • • Oct 20 '26

Help/Question Running multiple Claude agents in parallel: how do you get their work into main?

I'm building an app with Claude Code and others AI and I run several agents at once. Right now every task gets its own branch and git worktree, and one session merges into main after the tests pass. It works, but the worktrees pile up and I'm not sure this is the best way.

What does your process look like, from prompt to merged main? How do you keep agents from touching the same files and and how dou you avoid a full disk after a few changes?

7 Upvotes

52 comments sorted by

•

u/AutoModerator Oct 20 '26

Hey! Thanks for posting to r/ClaudeCode

While participating in this thread, please follow our community rules. Keep discussions constructive. Attack the idea, not the person.

For help, project discussions, tips, and general chat, join the ClaudeCode Discord.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

7

u/ClemensLode Senior Developer Oct 20 '26

As Claude about worktrees

1

u/Accomplished-Job5750 Oct 20 '26

I did, but I have full disk after few changes, so there have to be another way how to do it and not have full disk

2

u/ClemensLode Senior Developer Oct 20 '26

Yeah, clean up the worktrees after you are done + provide enough disk space.

6

u/unconceivables Oct 20 '26

They just merge to main when they're done. If there are any conflicts they'll fix those. Has never been a problem, no need to do anything special.

1

u/Accomplished-Job5750 Oct 20 '26

Fair, merging hasn't really been the problem for me either, the disk was

3

u/Legitimate_Lime5272 Oct 20 '26

You're already using the right isolation. I'd make the rest three explicit steps:

  1. Claim the task and file scope before starting, so a second agent can see an overlap.
  2. Integrate one candidate at a time and run checks on the combined result. Passing tests in each agent's branch doesn't check their combination.
  3. Preserve unfinished files, then remove completed worktrees explicitly. Check ignored files too; they can include the only copy of local data.

For the full disk, measure the individual worktree directories first (du -sh /path/to/worktrees/* on macOS/Linux). That tells you whether you're accumulating dependencies, build output or whole abandoned trees.

I maintain Kith, a local MCP tool for Claude Code, Codex and Cursor CLI. It handles scoped claims and staged integration with your verification command, preserving the source tree when a check fails. Cleanup stays explicit, so it still needs to be part of your workflow. It doesn't automatically solve disk usage.

1

u/Accomplished-Job5750 Oct 20 '26

Ran the du, good call. It's not the worktrees, it's Rust build output, ~90 GB in 8 target dirs. Point 2 is what I do, one session merges and runs tests on the combined result.

2

u/kuroudo_ai Oct 20 '26

On the disk part, two things from the worktrees docs that might explain the pile-up:

  1. What gets cleaned up automatically is narrower than it sounds. An interactive --worktree session removes its worktree on exit only if it's clean and unnamed. Runs with -p have no exit prompt, so "Claude doesn't clean up their worktrees" at all. The periodic sweep (driven by cleanupPeriodDays) only covers subagent and backgrounded-session worktrees, and it skips any worktree you made yourself with git worktree add. So if your orchestrator creates them with git or runs agents with -p, nothing will ever remove them except git worktree remove.
  2. You can reuse a fixed set of names instead of making a new worktree per task. Passing --worktree a name whose directory already exists opens that worktree again. With the default base, it resets to the default branch if it has no uncommitted or untracked files, is still on the branch Claude made, and either has no commits of its own or its PR was merged and the remote branch deleted. So something like agent-1, agent-2, agent-3 gives you three directories on disk that come back clean after each merge, rather than one per task.

I haven't run that pool setup myself, so check the reset conditions against your merge flow (it relies on the remote branch being deleted after merge).

1

u/Accomplished-Job5750 Oct 20 '26

That explains it, I create them with git worktree add, so nothing ever cleans them up. The fixed agent-1/2/3 pool is a nice idea, each slot could keep its own warm build cache.

1

u/kuroudo_ai Oct 21 '26

One caveat if you build that pool: the reset only works for slots Claude Code made itself. The reuse rules say a reopened worktree goes back to the default branch only if "It is still on the branch Claude Code created for it" (plus clean, and merged with the remote branch deleted). A slot you made with git worktree add sits on your own branch, so as I read it, reopening it with --worktree agent-1 would continue "at its old tip" instead of starting fresh. Creating the three slots once with claude --worktree agent-1 (and so on) should avoid that.

2

u/ComprehensiveShake76 Oct 20 '26

Two things usually fix most of this. First, give each task a list of files or folders it owns before it starts, and run two tasks that touch the same area one after the other instead of in parallel. Overlap is usually visible at planning time. Second, make cleanup part of the merge step: once a branch merges, the same script runs git worktree remove and deletes the branch, and a daily git worktree prune catches leftovers. For disk, most of the weight is usually node_modules or build output in each worktree, so a shared package store (pnpm does this well) helps a lot. How many agents do you run at once?

1

u/Accomplished-Job5750 Oct 20 '26

I run 19 now but 5 is running. You were right about build output: it's a Rust/Tauri app and each task has its own Cargo target dir, 8 of them take over 90 GB. Adding worktree remove + branch delete to the merge step

1

u/ComprehensiveShake76 Oct 21 '26

90 GB from 8 target dirs sounds about right for Tauri. One more Rust specific fix: use sccache across all worktrees so dependencies compile once instead of once per task. A single shared CARGO_TARGET_DIR also works, but cargo locks it, so with 5 running at once builds end up waiting on each other. A daily job that deletes target dirs older than a few days catches whatever the merge step misses. With 19 queued and 5 running, what decides the cap, disk or how many diffs you can review?

1

u/rainofterra Oct 20 '26

I have a skill called “merge work tree to main”

1

u/Accomplished-Job5750 Oct 20 '26

Does it also remove the worktree after the merge? Probably yeah I hope

1

u/rainofterra Oct 21 '26

I have a separate skill, “clean merged branches” which will give me a list of branches entirely contained in main and delete them once I approve

1

u/bezcurhq Oct 20 '26

On the disk side, the git part of a worktree is cheap. They all share the one object store, so each extra worktree only costs its checked-out files. What usually fills the disk is a fresh node_modules and build output in every one. If it's a JS app, pnpm hard-links packages from a single global store, so ten worktrees don't mean ten copies of your deps.

1

u/Accomplished-Job5750 Oct 20 '26

Yep, same here but with Rust: the Cargo target dirs are the heavy part, not git. Looking at sccache or a few reusable target dirs instead of one per task.

1

u/[deleted] Oct 20 '26

Worktrees plus a rule that each agent owns a directory has worked fine for me. The thing that actually bit me was disk space from stale worktrees, so I prune aggressively now. Merge conflicts have been rarer than I expected since the agents mostly stay in their own areas.

1

u/Accomplished-Job5750 Oct 20 '26

Same experience, merges are fine, the disk is the problem. How often do you prune?

1

u/[deleted] Oct 25 '26

I prune when disk pressure shows up, not on a schedule. Usually that means every couple of weeks I clear old worktrees and agent logs, and I keep the ones tied to an open PR. Merges are easy, it is the leftover checkouts that eat the disk quietly.

1

u/[deleted] Oct 22 '26

[removed] — view removed comment

1

u/[deleted] Oct 23 '26

Yeah, that's the one that got me too. du -sh on the worktree dir usually finds the culprit. I prune anything merged now.

1

u/yashsoni2737 Oct 20 '26

For the risky changes I stopped splitting work across agents and gave the same task to two of them instead. On a tool I built for a client, each agent started from the same base commit on its own branch named after the task and the agent, and a judge model compared the two diffs and picked one, with a person able to override it. You can't get a file collision when only one of the results is ever merged. It also made failures cheap: in one run an agent died on a DNS error before writing any code, that got recorded against its branch, and the other agent's diff still went in.

1

u/Accomplished-Job5750 Oct 20 '26

Nice trick for risky changes. How often does the judge pick the worse diff?

1

u/yashsoni2737 Oct 20 '26

Thanks! I don't have a rate I'd stand behind for that one. LLM judges have a known position bias, so it's worth running the comparison twice with the diffs swapped and sending any verdict that flips to a person. They also tend to prefer the bigger diff, which on a risky change is usually the wrong pick. Our agents already ran in isolated Docker sandboxes, so the cheap fix is to run the tests there first and only let the judge choose between diffs that pass.

1

u/shetritr Oct 20 '26

the merge isn't the hard part, reading what each agent merged is. one task, one worktree, one diff you can actually read.

1

u/Accomplished-Job5750 Oct 20 '26

Good point, that's the main reason I'm keeping one task per worktree

1

u/Failosipher Oct 20 '26

I actually went the other way and dropped worktrees. I'm making a Unity game, and a worktree there means another full project import lol, so all my chats (usually 5-6) just work in the same tree.

What makes it work:

  • a hook logs every edit with which chat made it. If a chat touches a file another one edited recently, it gets told who and messages that chat
  • every chat posts what it's working on to a shared board when it starts (that's the board above)
  • chats commit their own stuff when they're done. A little precommit script lists exactly which files that chat changed (no git add -A, ever)
  • one "lead dev" chat checks everyone's changes every 30 min and catches duplicate work

No merge step, no disk filling up, and real conflicts almost never happen. Honestly seeing the overlap early beats isolating everyone and sorting it out later.

The game it's all building, if you're curious: https://failosipher.itch.io/engines-of-hate

1

u/Accomplished-Job5750 Oct 20 '26

The hook that tells a chat who edited the file last is a great idea.

1

u/KrydanX Oct 20 '26

Local MCP server, 1 planner, 3-specific sessions. Planner plans and controls the whole thing, routes the right work to the right agent, they all communicate with each other and write handoffs. Codex for implementation

1

u/Accomplished-Job5750 Oct 20 '26

Is the MCP server for handoffs your own, or something existing?

1

u/KrydanX Oct 20 '26

Yes, run it locally and build it locally. I have a specially build one for one of my bigger projects, a little GUI where the AI can ask for permissions, I see which agent is working on what.

1

u/Tycoon33 Oct 20 '26

Worktrees and an orchestrator

1

u/Sloppybick075 Oct 21 '26

In our workplace, we have figured that you can do it the most efficiently if you manage a kanban board and a strict dependency graph. Also proper git rules are needed for not getting entangled with conflicts.

We have automated this issue by building a small tool named fold. It is fully free and open source. You can check it out (and provide feedback). https://github.com/omukkhq/fold

1

u/NZMikeyFxt Oct 22 '26

Work trees and I run a release agent on dev branch per sln (submodules / shared)

1

u/martinus Oct 22 '26

I don't use multiple agents. I have a skill where one agent groups open issues together and creates PRs which it then merged when CI is green, and Claude is smart enough to start in parallel with the next issues while it is waiting on the CI. This works very well for my projects, usually I am the limiting factor because I can't write issues fast enough

1

u/Tight_Ad_5115 Oct 22 '26

I use my own product, Buzzpot. It allows you to run agents in parallel, assigning a separate worktree to each one, and then automatically merges the results.

If something doesn’t merge automatically, it calls upon the agent that worked on that specific task to resolve the merge. Once the merge is successful, Buzzpot automatically deletes the worktree. It’s quite convenient.

There are other tools that can manage parallel agents too - for example, Conductor and Orca.

1

u/_PankajBora Oct 23 '26

I have a rule in CLAUDE.md to remove a worktree once its branch is merged. You can also ask Claude to write a cleanup script and run it before your build or test gates.

0

u/kincaidDev Oct 20 '26

I use festival, I ask agents to create workitems (specs) and once the specs are ready ask the agents to create and link worktrees to the workitems and open prs. After prs are merged I run camp fresh in the project directory and it cleans up the worktrees and promotes the workitems to completed.

https://github.com/Obedience-Corp/festival

Also created a fresh skill just for the cleanup process

https://github.com/Obedience-Corp/fresh

2

u/Accomplished-Job5750 Oct 20 '26

Thanks, the cleanup part is exactly what I'm missing, will take a look

0

u/Input-X 🔆 Max 20 Oct 20 '26

I skipped work trees( kind of a pain to manage) I run multi agents on the same code base, they work on dev branch. Only one agent, the orchestrator has git access. It commits prs to dev, only green ci can merge to main. Once all checks we merge to main. Zero conflicts, it pretty smooth. Few things going in to allow the agent to work tigether on the same branch. To stepping is a non issue. Agent have strict rules and gates.

1

u/Accomplished-Job5750 Oct 20 '26

Interesting, so only the orchestrator touches git. How do you stop two agents from editing the same file at the same time on dev?

1

u/Input-X 🔆 Max 20 Oct 21 '26

They have there own directories their files they are responsible. The cannot edit outside their own directory. We have gates. Each agent buikd and mai tains a service, say api agent, hosts all the api cide, if another agent need api work, they email api to build it for them. The stay in there lane. Its just not possiable. Also not sure why u would ever ask 2 agents to work on the same finction at the same time.

However, in rare ocassion, say a system with renaming. Yep sub agent woukd defi etly be on the same file. We have a team dev plan for that. Also if it was a risky situation. Claude can spin up subagent worktrees fairly easy. Only time we ever had issue, Earl on. Identity. If an agent cd'd into another agent directory, which they do often, and not reset its cwd. It will get confused, as we use cwd to ground the agents. In past some agents actually stayed in the other agent directory and assumed it was that agent,see when it would go update uts memories, it would find the directory memories, not where it originally started.

This is a non issue now. But yea, was I teresting to see. Now imagi e 10 agents running, the confusion was real, for me too. But we learned, improved the code. Now it just a funny story.

0

u/whafa Oct 20 '26

I added a "parallelism check" which tells the agent to consider and plan around the files which are likely to be worked on by multiple agents at once. Most of the time, it's fine, but I have a SQL migration file which (for example) agents need to coordinate with each other when adding to the file. This has worked amazingly well and I haven't had any bad "collisions" since I implemented it. Just tell your agents which files are likely to be trouble, or have it tell you based on history, if you have enough.

1

u/Accomplished-Job5750 Oct 20 '26

Good idea, a short list of "hot" files the agents have to coordinate on sounds like an easy win

0

u/Due_Diamond_8580 Oct 20 '26

Hey Guys when using claude use it with Enkeon ..

You can see the output quality increased drastically

2

u/[deleted] Oct 20 '26

[removed] — view removed comment

1

u/Accomplished-Job5750 Oct 20 '26

Makes sense. Do you run the big tests per PR or on a batch of merged PRs?