Status line shows stale token count after /compact operation

Resolved 💬 3 comments Opened Oct 1, 2025 by secondcircle Closed Oct 1, 2025

Problem Summary

After running the /compact command to compress conversation history, the status line continues to display the token count from the old transcript rather than updating to reflect the new, compacted transcript.

Expected Behavior

When /compact creates a new conversation transcript, the status line should immediately update to show the reduced token count from the compacted conversation.

Actual Behavior

  • Before compact: Status line correctly showed token usage
  • After compact: Status line shows old token count (146k/200k - 73%)
  • Actual usage (per /context command): 97k/200k (49%)
  • Root cause: Status line using old transcript file path

Investigation Findings

Token Count Comparison

Three different token counts were observed:

  1. Status line: 146k tokens (73%) - Bug: reading from old transcript
  2. /context command: 97k tokens (49%) - correct, from Claude API
  3. Token calculator on new transcript: 78k tokens (39%) - reading new file

Transcript Analysis

Two transcript files found for this project:

Old transcript (pre-compact):

  • File: 26263a5e-0722-4cff-b071-eb9eb558181a.jsonl
  • Size: 1.6M, 561 messages
  • Last update: 2025-10-01T16:33:56.376Z
  • Token count: 146,967 tokensMatches status line bug\!
  • Active chain: 449 messages

New transcript (post-compact):

  • File: eaba8dbb-c175-4465-9013-1620f156aa9f.jsonl
  • Size: 156 messages
  • Last update: 2025-10-01T16:43:55.530Z (10 minutes later)
  • Token count: 77,967 tokens
  • Active chain: 123 messages

Verification

Tested custom status line script with both transcript paths:

# Using NEW transcript (correct)
echo '{"transcript_path":".../eaba8dbb-c175-4465-9013-1620f156aa9f.jsonl"}' | ./.claude/statusline.sh
# Output: 79k/200k (39%) ✓

# Using OLD transcript (bug reproduction)
echo '{"transcript_path":".../26263a5e-0722-4cff-b071-eb9eb558181a.jsonl"}' | ./.claude/statusline.sh  
# Output: 146k/200k (73%) ← Matches user's buggy status line\!

Root Cause Analysis

After /compact operation:

  1. Claude Code creates new transcript file with compacted history
  2. Claude Code switches internal conversation to use new transcript
  3. Bug: Status line hook continues receiving old transcript path
  4. Status line calculates tokens from stale file
  5. User sees inflated token count that doesn't match reality

Status Line Implementation Details

The status line uses custom scripts:

  • .claude/statusline.sh - Reads transcript_path from JSON input
  • .claude/token-calculator.js - Parses transcript JSONL and calculates:

``javascript
contextLength = input_tokens + cache_read + cache_creation
``

The algorithm:

  1. Parses all messages from transcript JSONL
  2. Filters to main chain (isSidechain: false)
  3. Finds most recent message with usage data
  4. Returns token count from that message

The bug: Status line receives stale transcript_path value after compact, so it reads the old file.

Steps to Reproduce

  1. Start a long conversation that builds up significant context (e.g., 140k+ tokens)
  2. Note the status line token count
  3. Run /compact command to compress conversation history
  4. Observe: status line still shows old token count
  5. Run /context command
  6. Compare: /context shows much lower count than status line

Impact

  • User confusion: Status line shows 146k when actual usage is 97k (49k difference\!)
  • Premature compacting: Users may think they're running out of context when they have plenty left
  • Trust issues: Discrepancy between status line and /context command undermines confidence

Affected Components

  • [x] Status Line (receives stale transcript_path)
  • [x] Compact Operation (doesn't update status line input)
  • [ ] Token Calculator (works correctly when given right path)

System Information

  • Working Directory: /home/ikme/repos/ifs-accounting
  • Platform: Linux (WSL2) - 6.6.87.2-microsoft-standard-WSL2
  • Status Line Type: Custom command (./.claude/statusline.sh)
  • Old Transcript: 26263a5e-0722-4cff-b071-eb9eb558181a.jsonl (1.6M, 561 msgs)
  • New Transcript: eaba8dbb-c175-4465-9013-1620f156aa9f.jsonl (156 msgs)

Proposed Fix

After /compact operation completes:

  1. Ensure status line hook receives updated transcript_path pointing to new file
  2. Consider forcing status line refresh/recalculation
  3. Verify all hooks receive updated context information

Additional Context

  • Issue discovered by user after noticing status line didn't update post-compact
  • Custom status line implementation in project uses token-calculator.js
  • The /context command correctly shows updated count from API
  • Token calculator algorithm works correctly when given proper transcript path

---
Investigation conducted using custom status line debugging

View original on GitHub ↗

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