[BUG] /branch inherits full context after compaction — immediately hits "Context limit reached" despite 18% actual usage

Resolved 💬 5 comments Opened Mar 19, 2026 by michaelxu2288 Closed Apr 21, 2026

Preflight Checklist

  • [x] I have searched existing issues and this hasn't been reported yet
  • [x] This is a single bug report (please file separate reports for different bugs)
  • [x] I am using the latest version of Claude Code

What's Wrong?

After compaction has occurred, /branch creates a branch that immediately reports "Context limit reached" and refuses input. The status bar still shows the correct 18% usage. Resuming the exact same branch session w/ claude -r <branch-id> in a new terminal works perfectly, proving the session data is intact but the in-memory context counter is wrong. This effectively kills the parent conversation instead of creating an independent fork as intended.

This effectively makes /branch unusable for any long-running session where compaction has occurred... which is exactly the use case where branching is most valuable.

What Should Happen?

/branch should create an independent session that correctly reflects the
actual post-compaction context usage. The branch should be immediately
usable in the same terminal process — not just when resumed via
claude -r in a fresh process. The context counter for the new branch
should be recalculated from the actual data, not inherited from the
parent's stale pre-compaction counter.

Error Messages/Logs

Context limit reached · /compact or /clear to continue

Steps to Reproduce

Steps to Reproduce:

  1. Start a Claude Code session with Opus 4.6 (1M context)
  2. Work until compaction triggers (raw messages exceed context window, get compacted down to ~18%)
  3. Run /context -- note Messages shows 100%+ while total shows ~18%
  4. Run /branch
  5. Type anything in the branch -> "Context limit reached"
  6. Note the status bar still shows "Context: 18%"
  7. Open new terminal, run claude -r <branch-id> -> works fine

Claude Model

Opus

Is this a regression?

No, this never worked

Last Working Version

_No response_

Claude Code Version

2.1.79 (Claude Code)

Platform

Anthropic API

Operating System

macOS

Terminal/Shell

Terminal.app (macOS)

Additional Information

  • The branched session works perfectly -- the bug is that the original session's context tracking gets corrupted/inflated after branching.
  • Root Cause Hypothesis: /branch copies the raw pre-compaction message token count (~1255k) instead of the actual post-compaction count (~185k). A fresh claude -r process recalculates from the JSONL

and gets the correct number.

<img width="652" height="615" alt="Image" src="https://github.com/user-attachments/assets/da105e70-1945-40e2-b31e-0a1065383264" />

<img width="567" height="313" alt="Image" src="https://github.com/user-attachments/assets/39dd5190-24d3-4e6f-ac1c-73ee01eda7c1" />

View original on GitHub ↗

This issue has 5 comments on GitHub. Read the full discussion on GitHub ↗