The Status button now shows true context usage by tracking tokens from all parent sessions in the chain, not just the current session. This gives accurate context window percentage and prevents surprises when context limits are reached.
Before: Status showed only current session tokens (e.g., 5%), but Claude Code actually loads ALL parent sessions into context, potentially using 95% of the context window.
After: Status shows cumulative tokens from entire session chain (e.g., 95% actual usage), giving you the real picture.
-
In-Memory Cache (
cumulativeTokenCache)- Stores calculated cumulative tokens for session chains
- Prevents expensive recalculation on every status check
- Maps sessionId → cumulative token totals
-
Recursive Token Calculator (
calculateCumulativeTokens)- Walks up the session chain using
parentUuidin JSONL files - Sums tokens from all parent sessions
- Handles circular references and infinite loops
- Caches results for future use
- Walks up the session chain using
-
Session Chain Parser (
getParentSessionId)- Reads first line of JSONL files to extract
parentUuid - Follows the session parent chain to root
- Handles missing or invalid session files gracefully
- Reads first line of JSONL files to extract
- Session Creation: When continuing a session, calculates and caches cumulative tokens from all parents
- Status Display: Shows "Total Context" with breakdown of previous vs current session tokens
- Bot Restart: Automatically recalculates cumulative tokens if cache is empty
🎯 Context: 1,200 / 200,000 (0.6%)
↳ 800 in, 400 out
↳ 3 transactions
🎯 Total Context: 85,400 / 200,000 (42.7%)
↳ 65,200 in, 20,200 out
↳ 45 transactions
↳ 🔄 Previous sessions: 75,800 tokens
↳ 📝 Current session: 9,600 tokens
🎯 Total Context: 180,500 / 200,000 (90.3%)
↳ 145,200 in, 35,300 out
↳ 125 transactions
↳ 🔗 Includes all parent sessions in chain
⚠️ Close to limit - consider /compact soon
- Cache Key: Session ID of the stored/continued session
- Cache Value: Cumulative token totals from entire chain
- Cache Lifecycle: Populated on session creation, cleared on bot restart
- Cache Miss: Automatically calculates and stores on first access
// Example session chain: C → B → A (root)
sessionC.parentUuid = "sessionB-id"
sessionB.parentUuid = "sessionA-id"
sessionA.parentUuid = null // root session
// calculateCumulativeTokens("sessionC-id") sums:
// sessionC.tokens + sessionB.tokens + sessionA.tokens- Missing Session Files: Gracefully handled, continues with available sessions
- Circular References: Detected and prevented using visited session tracking
- Invalid JSON: Skipped lines, continues processing
- File Access Errors: Logged but don't crash the bot
Comprehensive test suite covers:
- ✅ Parent session ID parsing from JSONL files
- ✅ Cumulative token calculation across chains
- ✅ In-memory caching behavior
- ✅ Empty/missing session handling
- ✅ Circular reference detection
- ✅ Session chain walking logic
- Minimal: Calculations only run on session creation and cache misses
- Fast: In-memory cache provides instant status updates
- Efficient: Single pass through session chain, results cached
- Scalable: Handles long session chains without performance degradation
- ✅ Existing sessions continue to work normally
- ✅ New sessions without parents show traditional display
- ✅ No breaking changes to existing APIs
- ✅ Graceful degradation when session files unavailable
- Token Usage Analytics: Track token consumption patterns over time
- Auto-Compaction Triggers: Automatically compact when approaching limits
- Session Chain Visualization: Show session hierarchy in status
- Memory Optimization: Implement LRU cache for very large session histories