Worktrees, Parallelism & Cost Control

Scale quality and keep spend in check

Advanced 13 minBuilderDecision-maker
What you'll be able to do
  • Create isolated parallel sessions with `claude -w` (including branching from a PR with `#`) and understand why directory-scoped sessions plus worktrees beat branch-switching
  • Apply the core parallel quality patterns: Writer/Reviewer across sessions, fan-out across worktrees, and background agents
  • Pick the cheapest model and effort that does the job, and cap spend with `--max-budget-usd`, `--max-turns`, and `--bare`
  • Track spend and context with `/usage`, `/context`, and the cost-reduction habits (compact at 60%, `/clear`, subagents, MCP tool search)
  • Set guardrails at the plan and Console level, and account for the June 15 2026 separate Agent SDK credit pool
At a glance

Git worktrees give every parallel Claude session its own directory and branch so edits never collide, while a layered cost toolkit -- model choice, effort, hard budget caps, and aggressive context discipline -- keeps spend predictable as you scale. This lesson connects the two: parallelism is what makes quality patterns possible, and cost control is what keeps that parallelism affordable.

  1. 1The mental model: isolate, then economize
  2. 2Git worktrees: one command, full isolation
  3. 3Parallel quality patterns
  4. 4Cost control: model, effort, and hard caps
  5. 5Token discipline: keep every session lean
  6. 6Plan, Console, and the 2026 SDK credit pool

The mental model: isolate, then economize

Scaling Claude Code well rests on two ideas that pull in opposite directions, and the skill is holding both at once.

The first is isolation. Running several Claude sessions at once is only safe if they can't step on each other's files. A git worktree -- a separate working directory on its own branch that shares the same repository history and remote -- is the clean primitive for this. One Claude builds a feature in worktree A; another fixes a bug in worktree B; their edits never collide because they're in different directories on different branches.

The second is economy. Every one of those sessions burns tokens, and parallelism multiplies the bill. So the same lesson that teaches you to fan out also teaches you to keep each session cheap: the right model, the right effort, a hard budget ceiling, and ruthless context hygiene.

The through-line connecting them: sessions tie to directories, not branches. A session is a conversation keyed to a working-directory path; it does not snapshot your tree. Switch git branches under an open session and the files Claude sees change underneath it. Worktrees dissolve that hazard -- each branch lives in its own directory, so each gets its own session scope with no branch-switching surprises.

text
ONE REPO  (shared history + remote)
  ├─ main checkout        → session 1 (feature work)
  ├─ .claude/worktrees/   
  │    ├─ feature-auth/    → session 2 (isolated branch)
  │    └─ bugfix-123/      → session 3 (isolated branch)
  └─ each worktree = its own directory = its own session, no edit collisions

Key insight

Worktrees fix the directory-not-branch trap

Because a session is keyed to a directory path, switching branches in place changes the code a single conversation sees. A worktree gives each branch its own directory, so parallel branch work gets parallel, non-colliding sessions -- the isolation is structural, not a matter of remembering to be careful.

Git worktrees: one command, full isolation

Pass --worktree (or -w) to create an isolated worktree and start Claude inside it. By default the worktree is created under .claude/worktrees/<value>/ at your repository root, on a new branch named worktree-<value>:

bash
claude --worktree feature-auth

Run the same command with a different name in a second terminal and you have a second, fully isolated session:

bash
claude --worktree bugfix-123

Omit the name and Claude generates one such as bright-running-fox. You can also just tell Claude "work in a worktree" mid-session and it creates one via the EnterWorktree tool.

Key facts to internalize:

TopicBehavior
Storage path.claude/worktrees/<value>/ at the repo root (add it to .gitignore)
Branch nameA new branch worktree-<value>
Base branchBranches from origin/HEAD (clean tree matching the remote); falls back to local HEAD if no remote. Set worktree.baseRef: "head" in settings to always carry your unpushed work
Shared stateSame repository history and remote -- commits made in one worktree are visible to others via git
First-time trustRun plain claude once in the directory to accept the trust dialog before -w works (even with -p)
tmux panesclaude -w feat --tmux runs the worktree in a tmux session for visual management; iTerm2 uses native panes when available; --tmux=classic forces classic tmux
Non-git VCSConfigure WorktreeCreate / WorktreeRemove hooks (SVN, Perforce, Mercurial); .worktreeinclude is bypassed when a hook replaces git

Branch from a PR. Pass the PR number prefixed with # (or a full GitHub PR URL). Claude fetches pull/<number>/head from origin and creates the worktree at .claude/worktrees/pr-<number>:

bash
claude --worktree "#1234"

Copy gitignored files in. A worktree is a fresh checkout, so untracked files like .env aren't present. Add a .worktreeinclude file (uses .gitignore syntax) at the project root; matching files that are also gitignored get copied into every new worktree:

text
.env
.env.local
config/secrets.json

Tip

Cleanup is automatic when the worktree is clean

Exit a worktree session with no uncommitted changes, no untracked files, and no new commits, and Claude removes the worktree and its branch automatically (it prompts instead if the session was named). If changes exist, Claude asks whether to keep or remove it. Note: worktrees created with -w alongside -p (non-interactive) are NOT auto-cleaned -- remove them with git worktree remove.

Watch out

Trust the directory first

If you haven't accepted the workspace trust dialog, --worktree exits with an error and tells you to run claude in the directory first. This applies even when combined with -p, so do a one-time plain claude in any repo before scripting worktrees there.

Parallel quality patterns

Isolation is the enabler; these are the patterns that turn it into higher-quality output. There are three to keep in your toolkit.

1. Writer/Reviewer across sessions. Session A writes the code. Session B -- a fresh context with no memory of A's reasoning -- reviews it. The reviewer has no stake in the original approach, so it catches blind spots an author rationalizes away. Pair this with /code-review or /security-review in the reviewing session for structured passes. Run A and B in separate worktrees if they touch the same files.

2. Fan-out across worktrees. When a task decomposes into many independent units (migrate 20 files, fix the same lint across modules), spread them across isolated checkouts. The headless idiom from the docs:

bash
for f in $(cat files.txt); do
  claude -p "add input validation to $f" \
    --allowedTools "Edit" "Bash(git commit *)"
done

The built-in /batch <instruction> formalizes this: it decomposes the work into 5-30 units and spawns one background subagent per unit, each in its own isolated worktree.

3. Background agents. Detach long-running work so you keep your terminal. Start with claude --bg "<task>" (prints an ID and returns) or press Ctrl+B to background the current session. Monitor everything from one screen with claude agents, tail output with claude logs <id>, and reattach with claude attach <id>.

Subagents complement all three: they run in an isolated context and report only a summary back, so heavy exploration never bloats your main thread. Set isolation: worktree in a custom subagent's frontmatter and each subagent even gets its own temporary worktree -- removed automatically when it finishes without changes.

Example

Writer/Reviewer in practice

Terminal 1: claude -w add-rate-limit -- Claude implements rate limiting and commits. Terminal 2: claude -w add-rate-limit won't help (same name = same worktree); instead open a fresh session on that branch and prompt "review the rate-limiting changes on this branch for correctness and security; assume nothing about the author's intent." The reviewer reads the diff cold and flags the off-by-one you'd have missed.

Watch out

Agent teams are the expensive option

Full agent teams spawn multiple Claude instances, each with its own context window. The docs note they use roughly 7x more tokens than a standard session when teammates run in plan mode. Use Sonnet for teammates, keep teams small and focused, and clean them up when done -- idle teammates keep consuming tokens. They're disabled by default (CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS=1).

Cost control: model, effort, and hard caps

Claude Code charges by API token consumption, so cost control starts with the two biggest dials -- which model thinks, and how hard it thinks -- then adds hard ceilings for automation.

Model choice. Sonnet handles most coding tasks well and costs less than Opus; reserve Opus for genuinely hard architecture or multi-step reasoning. Switch mid-session with /model, set a default in /config, or pass --model at launch. For cheap subagent grunt work, specify model: haiku in the subagent config.

bash
claude --model sonnet          # default; cost-efficient for most work
claude --model opus            # reserve for hard reasoning

Effort. Extended thinking is on by default and bills as output tokens -- the default budget can be tens of thousands of tokens per request. For simple tasks, dial it down with /effort (low/medium/high/xhigh/max), set it in /model, disable thinking in /config, or cap it with MAX_THINKING_TOKENS=8000. The --effort flag sets it at launch.

Hard caps for headless/CI runs. Three flags turn an unbounded agent into a bounded job:

FlagSyntaxEffect
--max-budget-usdclaude -p --max-budget-usd 5.00 "q"Stops the run when estimated spend hits the USD amount (print mode)
--max-turnsclaude -p --max-turns 3 "q"Limits agentic turns; errors when the limit is reached
--bareclaude --bare -p "q"Minimal mode: skips hooks, skills, plugins, MCP, auto-memory, and CLAUDE.md; only Bash/Read/Edit. Smallest possible context per call

Combine them. A safe CI fan-out unit looks like claude -p --max-budget-usd 2 --max-turns 5 --allowedTools "Edit" "<task>" -- scoped tools, a dollar ceiling, and a turn ceiling so a runaway loop can't drain your account.

Tip

`--bare` is the leanest call there is

When you only need Claude to read, edit, and run a shell command -- no project context, no MCP, no skills -- --bare strips everything that would otherwise load into context (it even sets CLAUDE_CODE_SIMPLE). Perfect for tight, repetitive batch operations where CLAUDE.md and plugins are pure overhead. Note: claude setup-token does not work in --bare mode.

Token discipline: keep every session lean

Token cost scales with context size, so the cheapest session is the one that never accumulates junk. These habits compound -- across many parallel sessions they're the difference between a $13 day and a $40 one.

  • Compact at ~60%, not 95%. Run /compact [focus] manually before the window fills. Compacting early is cheaper and produces a sharper summary than waiting for auto-compaction at the limit. /compact Focus on code samples and API usage steers what survives.
  • /clear between unrelated tasks. Stale context from a finished task wastes tokens on every later message. /rename first so you can /resume later, then /clear. Mixing unrelated work in one session (the "kitchen-sink" anti-pattern) is the most common silent cost leak.
  • Subagents to isolate exploration. Delegate verbose operations -- reading a huge log, fetching docs, scanning a large module -- to a subagent. The verbose output stays in its context; only a summary returns to your main thread.
  • MCP tool search defers definitions. MCP tool schemas are deferred by default: only tool names enter context until Claude actually uses a tool. Run /context to see what's consuming space, prefer CLI tools (gh, aws, gcloud) which add no per-tool listing, and /mcp to disable servers you aren't using.
  • Trim CLAUDE.md. It loads at session start and stays even during unrelated work. Keep it under ~200 lines; move specialized workflow instructions into on-demand skills.

Track as you go. /usage (aliases /cost, /stats) shows the session's token/cost estimate plus -- on a plan -- a breakdown attributing usage to skills, subagents, plugins, and individual MCP servers (press d/w for 24h vs 7-day). /context (or /context all) shows the live breakdown of system prompt, tools, CLAUDE.md, skills, MCP, and conversation so you can see exactly where your tokens went.

Key insight

`/context` tells you what to cut; `/usage` tells you what it cost

Reach for /context when a session feels heavy -- it itemizes the window so you can spot the 40K-token MCP server or the bloated CLAUDE.md. Reach for /usage to see spend and attribute it to the skill, subagent, plugin, or MCP server responsible. One diagnoses context; the other diagnoses cost.

Watch out

Image accumulation is a silent token drain

Pasted screenshots are stored at full resolution in the session transcript and accumulate. They can quietly dominate a context window and, on long sessions, even cause 400 errors on resume. /clear or /compact image-heavy sessions, and start fresh for the next visual task.

Plan, Console, and the 2026 SDK credit pool

Individual hygiene controls per-session cost; org-level budgets cap the total. Where you set the ceiling depends on how you authenticate.

Subscription plans (Pro / Max). Set a monthly spend limit on usage credits with /usage-credits. If you hit the limit while credits remain, Claude Code prompts you to raise or remove it without leaving the CLI (changing it needs billing access). The /usage plan view shows your bars and breakdown so you can see how close you are.

Console / API. Authenticating with a Console account auto-creates a workspace named "Claude Code" for centralized cost tracking (you can't create API keys for it -- it's auth-only). Set workspace spend limits on total Claude Code workspace spend, and a workspace rate limit on its Limits page to cap Claude Code's share so it can't starve other production workloads. Admins view cost and usage reporting in the Console; for authoritative billing, trust the Console Usage page over the locally-estimated /usage figure.

The June 15, 2026 change. Starting that date, Agent SDK and claude -p usage on subscriptions draws from a separate monthly Agent SDK credit pool, distinct from your interactive limits. Practically: your headless automation and scripted runs now consume a different bucket than your interactive coding. Budget them separately, and watch that pool independently -- a fan-out job that used to share your interactive allowance now bills against the SDK pool.

Note

The `/usage` dollar figure is an estimate

The Session block's cost is computed locally from token counts and may differ from your actual bill; it's most relevant to API users (Max/Pro subscribers have usage included). Figures are computed from local session history on this machine, so usage from other devices or claude.ai isn't included. For billing truth, use the Console Usage page.

Try it: Fan out across worktrees on a budget

Practice safe parallelism and hard cost ceilings end to end. Use a throwaway git repo with a few similar files (or any small project).

  1. Trust once: in the repo, run plain claude once and accept the workspace trust dialog, then exit. Add .claude/worktrees/ to your .gitignore.
  2. Spin up two isolated sessions: in terminal 1, claude -w feature-a; in terminal 2, claude -w feature-b. Confirm each landed in .claude/worktrees/<name>/ on branch worktree-<name> with git worktree list. Make a small edit in each and verify the other's files are untouched.
  3. Writer/Reviewer: in feature-a, have Claude implement a small function and commit. Open a fresh session on that branch and prompt it to review the diff cold ("assume nothing about the author's intent; check correctness and edge cases"). Note one issue the reviewer caught that the writer didn't mention.
  4. Bounded fan-out: create files.txt listing 3-4 files, then run a loop: for f in $(cat files.txt); do claude -p --max-budget-usd 1 --max-turns 4 --allowedTools "Edit" "add a top-of-file comment to $f"; done. Confirm each run respects the caps.
  5. Measure: in an interactive session, run /context to see the window breakdown, then /usage to see the cost estimate and per-skill/subagent/MCP attribution. Run /compact and re-check /context to see the reduction.
  6. Clean up: exit the clean worktree sessions and confirm Claude auto-removes them; for any with changes, decide keep vs. remove. Remove leftover -p worktrees with git worktree remove.

Deliverable: a short note recording (a) the worktree paths and branch names git created, (b) the one issue the cold reviewer found, and (c) the /context token total before and after /compact -- plus one sentence on which cost lever (model, effort, budget cap, or context hygiene) would save you the most on your real workload, and why.

Key takeaways

  1. 1`claude -w <name>` (alias `--worktree`) creates an isolated checkout at `.claude/worktrees/<name>/` on branch `worktree-<name>`; `claude -w "#1234"` branches from a PR into `.claude/worktrees/pr-<number>`; worktrees share git history and remote.
  2. 2Sessions tie to directories, not branches -- switching branches in place changes the code a session sees, so worktrees are the clean way to run parallel branch work without edit collisions.
  3. 3Parallel quality patterns: Writer/Reviewer in a fresh-context session, fan-out across worktrees (or `/batch`), and background agents (`--bg`, `Ctrl+B`, `claude agents`/`logs`/`attach`).
  4. 4Pick the cheapest model and effort that works (Sonnet over Opus, lower `/effort`, `model: haiku` subagents) and cap automation with `--max-budget-usd`, `--max-turns`, and `--bare`.
  5. 5Practice token discipline: `/compact` at ~60%, `/clear` between tasks, subagents for exploration, MCP tool search to defer schemas; track with `/usage` (cost) and `/context` (what's in the window).
  6. 6Set budgets at the right level: `/usage-credits` on plans, workspace spend/rate limits in the Console; from June 15 2026, SDK and `claude -p` usage on subscriptions draws from a separate Agent SDK credit pool.

Quiz

Lock in what you learned

Check your understanding

0 / 4 answered

1.You run `claude --worktree feature-auth` in a repo. Where does the worktree live, what branch is it on, and what happens to your other sessions' files?

2.Why does the documentation insist that worktrees, not branch-switching, are the right way to run parallel work on different branches?

3.You're scripting a CI fan-out: `claude -p` over a list of files, applying the same fix. Which combination best bounds cost and blast radius for a single unit?

4.It's June 20, 2026. Your team runs heavy nightly `claude -p` automation on Max subscriptions and is surprised the interactive usage bars barely moved despite the automation running. What's the explanation?

Go deeper

Hand-picked sources to keep learning