Compaction irreversibly discards context when summary API call fails
Bug
Manual /compact discards the original conversation before confirming the summary API call succeeded. When that call returns an error (401, 429, 500, etc.), the context is irreversibly lost and replaced with the raw error message as the continuation.
Steps to reproduce
- Start a long session (96K+ tokens)
- Have the API credentials expire or become invalid (or trigger rate limiting)
- Run
/compact
Expected behavior
The original conversation is preserved if the summary API call fails. Either:
- Keep the original context intact and report the compaction failure
- Retry with a fallback model/provider
- At minimum, inject a continuation that says "compaction failed, preserving original context" rather than an error message
Actual behavior
The original conversation is discarded. The continuation message contains only the API error text. All prior work context is lost.
Evidence from session transcript
Compaction history showing the failure:
| Line | Trigger | PreTokens | PostTokens | Duration | Notes |
|------|---------|-----------|------------|----------|-------|
| 473 | auto | 167,143 | 12,615 | 119,672ms | Success (120s) |
| 861 | manual | 117,678 | 11,547 | 87,790ms | Success (88s) |
| 1166 | manual | 96,606 | 8,471 | 1,498ms | FAILED — 1.5s is impossibly fast vs 88-120s for real compactions |
The compact at L1166 recorded postTokens=8471 and durationMs=1498, but the continuation message at L1167 was:
This session is being continued from a previous conversation that ran out of context.
Failed to authenticate. API Error: 401 {"type":"error","error":{"type":"authentication_error","message":"Invalid authentication credentials"}}
The postTokens and durationMs values in the compactMetadata reflect the error response, not a successful compaction. This masks the failure.
Impact
- Context loss is irreversible — the user cannot recover their working session
- The
compact_boundarymetadata is misleading (reports success when it failed) - Subsequent
/compactattempts compound the problem (429 rate limits from repeated failed calls) - Context usage percentage is irrelevant — this happens on manual compaction at any usage level
Environment
- Claude Code version: 2.1.112
- OS: macOS (Darwin 25.3.0)
This issue has 5 comments on GitHub. Read the full discussion on GitHub ↗