Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

perf(subscription): skip transcripts older than the window in compute_window_tokens (#2861)

Romulo Reis91d6bf33cde7
diff --git a/headroom/subscription/session_tracking.py b/headroom/subscription/session_tracking.py
index 9ace709fa..e9a1cacfd 100644
--- a/headroom/subscription/session_tracking.py
+++ b/headroom/subscription/session_tracking.py
@@ -136,6 +136,16 @@ def compute_window_tokens(start_ts: float, end_ts: float) -> WindowTokens:
unattributed = WindowTokens()
for path in find_transcript_files():
+ # Skip transcripts that cannot contain entries inside the window.
+ # Transcripts are append-only and chronological, so a file whose mtime is
+ # older than the window start has no entry within [start_ts, end_ts).
+ # Without this guard every poll json.loads()es every line of every
+ # transcript under ~/.claude/projects.
+ try:
+ if path.stat().st_mtime < start_ts:
+ continue
+ except OSError:
+ continue
for line in _read_transcript_lines(path):
try:
entry: dict[str, Any] = json.loads(line)