Parallel agents all want port 3000: one dev-server port each
Parallel agents in git worktrees all fight over port 3000 or 5173. Give each one its own dev-server port, free leaked servers, and handle Xcode DerivedData.
Updated 6 min read
On this page
You have three or four Claude Code agents running in separate worktrees, each one starts Next.js, Express or Vite, and the second one onward fails with a "port in use" error or quietly lands on a different port than you expected.
Why it happens
- Worktrees isolate files, not the machine. Each worktree is its own checkout, but all agents share one network stack. Only one process can listen on port 3000 at a time.
- Frameworks default to a fixed port. Next.js usually starts on 3000 and Vite on 5173. Some tools silently try the next free port, which means you no longer know which agent owns which URL.
- Crashed agents leak servers. If an agent crashes or is killed, its dev server can keep running. The next session then hits "port in use" on a port nobody is using on purpose.
- Build caches live outside the worktree. Xcode keeps DerivedData under
~/Library/Developer/Xcode/DerivedDataby default. A fresh worktree means a long first build, and deleting the worktree does not delete its caches.
Two approaches that work in any terminal
Both work with or without our app. Both require your dev server to take its port from a flag or environment variable (check your framework's docs for the exact option).
- Hand out ports in the task prompt. Give each agent a range (agent A gets 3100-3109, agent B gets 3200-3209) and tell it to start its server with an explicit flag, such as
npm run dev -- --port 3100. People on a Claude Code community forum suggest exactly this. It depends on the agent following instructions and on you keeping the list straight. - Put a local proxy in front. Map one hostname per worktree to that worktree's port. You stop remembering port numbers, but you still need a unique port behind each hostname.
| Approach | Setup effort | Weak point |
|---|---|---|
| Ports in the prompt | Low | Relies on the agent obeying; you track the list |
| Local proxy with hostnames | Medium | Still needs a unique port per worktree |
SLED_PORT from Sledbot | Low, one script per repo | Dev server must read the variable |
Let Sledbot assign the port
Terminals in a Sledbot worktree get these environment variables:
| Variable | Value |
|---|---|
SLED_PORT | A port for this worktree. SLED_PORT_1 to SLED_PORT_9 are also set if you need more. |
SLED_WORKTREE | The worktree |
SLED_ROOT | The repo root |
SLED_BRANCH | The worktree's branch |
SLED_CONTEXT | <worktree>/.sledbot/context (git-excluded) |
Set up the repo scripts
- In the repo root, create
.sledbot/config.json(orsled.json). - Add the scripts you need:
{"setup": ["npm ci"], "copy": [".env"], "run": "npm run dev -- --port $SLED_PORT", "archive": "docker compose down"}- Start the agent with New task and leave Work in a new worktree on. It is on by default for git repos.
What each key does:
setup: commands that prepare a worktree, such as installing dependencies.copy: files brought into the worktree, such as.env.run: the dev-server command, started on the worktree's own port.archive: cleanup commands. Sledbot runs it when you archive a thread, for example to stop what the agent started.
From a terminal, sled worktree info|create|setup|run|archive drives the same workflow. If your app needs a front end plus an API, use SLED_PORT for one and SLED_PORT_1 for the other.
Clear "port in use" left by a crashed agent
- Open Resources in the sidebar, or press ⌥⌘R (Option-Command-R).
- Look under Listening ports and Left running for the stale process.
- Choose Free on the port and confirm Free port N?. You can also use Quit or Force Quit on the process.
Quick clear lists stale processes, ports, worktrees and caches on the machine, and Clear N items removes them. From the command line, sled top shows what is using CPU, memory and ports.
Xcode DerivedData and other caches outside the worktree
To keep builds isolated per worktree, point each one at its own DerivedData folder. With xcodebuild, use -derivedDataPath:
xcodebuild -scheme MyApp -derivedDataPath ./.derived buildThis builds into ./.derived instead of the shared location. Add that folder to .gitignore, and delete it when you remove the worktree. You can put the delete command in your archive script so it happens when you archive the thread.
Each new worktree costs a full first build, so avoid creating worktrees you will not use.
Clean up worktrees
- Open Insights then Cleanup, or choose Clean up worktrees… on a space.
- Choose Scan. Scanning only measures.
- Select items and use Move to Trash. Danger items are never selected in bulk.
Automatic cleanup runs once a day and only touches Safe items. All its rules are off by default except purging ~/.sled-trash after 7 days. The merged-worktrees rule (7 days) is available if you turn it on.
One known gap: worktrees for teammate threads live in <parent of repo>/.sled-worktrees/, and the "Sledbot worktrees" category may not list them. Check that folder by hand if disk space does not come back.
Checklist before you start several agents
- Each dev server reads its port from a flag or environment variable, not a hardcoded number.
- The
runscript in.sledbot/config.jsonuses$SLED_PORT. .envand other untracked files the app needs are incopy.- Anything an agent starts in the background is stopped in
archive. - Xcode projects build into a per-worktree
-derivedDataPath.
Troubleshooting
| Symptom | Cause | Fix |
|---|---|---|
| Dev server still starts on 3000 | The command is not reading $SLED_PORT | Pass the flag or variable your framework expects in run |
$SLED_PORT is empty | The agent is in your main folder, not a worktree terminal | Turn Work in a new worktree on |
| "Port in use" right after a crash | Leaked dev server | Free the port in Resources |
.env missing in the new worktree | It is untracked, so git did not bring it | Add it to copy |
If nothing works
If ports still collide after the steps above, contact support at macsuite.app/support and include the contents of your .sledbot/config.json, the output of sled top, the exact error text and what you already tried.
Subject: Sledbot worktree agents still collide on a dev-server port
Sledbot 1.0.0 on macOS (your version). I have 3 agents in worktrees.
My .sledbot/config.json "run" is: (paste it)
Agent 2 fails with: (paste the exact error)
echo $SLED_PORT in that terminal prints: (paste the value)
Resources > Listening ports shows: (paste what is listed)
I already tried: freeing the port, restarting the agent.Common questions
Can I just pick ports in the prompt instead?
Yes, and it works in any tool. The trade-off is that you track the list and the agent has to follow it. SLED_PORT removes both chores.
Does Sledbot run a proxy with per-worktree hostnames?
No. The fact sheet lists SLED_PORT variables, not a proxy. If you want hostnames, run a proxy yourself and point each hostname at that worktree's SLED_PORT.
Will SLED_PORT change between runs?
We do not document how the port is chosen, so do not hardcode it. Always read it from the variable in your run script.
Does Sledbot clear Xcode DerivedData?
Not specifically. Use -derivedDataPath per worktree and delete that folder in your archive script.