Repository navigation
Open
Description
Reminder for 2026-11-27 (two weeks after #622 merged): measure whether #622 actually cut Claude Code spend in this repository, and keep or adjust its settings.
Baseline (before #622)
Measured from local transcripts, 2026-09-25 to 2026-11-12, at Anthropic list prices:
| Measure | Before |
|---|---|
| Spend | $11,808 over 33 active days ($358 per active day), 140,570 requests |
| Split | cache reads 48%, cache writes 35%, output 17% |
| Subagents | 90% of spend |
| Agents with 150+ turns | 46% of spend |
| Sonnet | 2.3% of spend |
| Subagent peak context | median 151k, max 967k tokens |
Expected after #622: 21-25% lower spend from compaction at 400k plus the implementer's one-hour cache (an upper bound from a replay), plus 2-4% from Sonnet routing and a little from the LSP tool and a smaller CLAUDE.md.
How to measure
- Save the script below and run
python3 measure.py 2026-11-14. It prints the same measures for the periods before and after that date. - Compare:
- Cost per active day and per request. Spend varies with the workload, so read these alongside what kind of work happened in each period.
- Share of 150+-turn agents. It should drop.
- Maximum subagent peak context. It should be about 400k, not 967k. If it isn't,
CLAUDE_CODE_AUTO_COMPACT_WINDOWis not reaching subagents. - Sonnet share. It should rise.
- Check quality as well as cost. Did long agents repeat work or lose track after compacting? If so, raise the window to
500000in.claude/settings.json. - Record the result in
docs/agent/RATIONALE.md("Token cost of long-running agents"), then close this issue.
Do this before mid-December. Claude Code deletes transcripts after 30 days by default (cleanupPeriodDays), so the "before" data disappears and only the table above will remain.
measure.py
"""Compare Claude Code spend in this repo before and after a cutoff date.
Usage: python3 measure.py [CUTOFF_DATE] (default 2026-11-14, the day after #622)
Reads ~/.claude/projects/-Users-mac-repos-citry. List prices per million
tokens: Opus 5.5 $4 in / $20 out / $0.20 cache read, Sonnet 5.5 half that;
cache writes cost 1.25x input (5-minute) or 2x (1-hour).
"""
import collections
import datetime
import glob
import json
import os
import sys
CUTOFF = sys.argv[1] if len(sys.argv) > 1 else "2026-11-14"
ROOT = os.path.expanduser("~/.claude/projects/-Users-mac-repos-citry")
PRICES = {"opus-5-5": (4, 20, 0.20), "sonnet-5-5": (2, 10, 0.10), "haiku-5-5": (0.10, 0.50, 0.01)}
def price(model):
return next((v for k, v in PRICES.items() if k in model), None)
def mtime(path):
return os.path.getmtime(path) if os.path.exists(path) else 0
# Main sessions first, so a request copied into a fork or resumed transcript
# is credited to the file that made it.
files = sorted(glob.glob(ROOT + "/*.jsonl"), key=mtime) + sorted(
glob.glob(ROOT + "/*/subagents/*.jsonl"), key=mtime
)
owner, agents = {}, {}
for path in files:
try:
fh = open(path)
except OSError:
continue
requests, order = {}, []
for line in fh:
if '"usage"' not in line:
continue
try:
entry = json.loads(line)
except ValueError:
continue
msg = entry.get("message") or {}
usage, mid, model = msg.get("usage"), msg.get("id"), msg.get("model", "")
if not usage or not mid or not price(model):
continue
if owner.setdefault(mid, path) != path:
continue
if mid not in requests:
order.append(mid)
cc = usage.get("cache_creation") or {}
# Streaming writes several lines per request; the last holds the final usage.
requests[mid] = (
requests[mid][0] if mid in requests else entry["timestamp"][:10],
model,
usage.get("input_tokens", 0),
usage.get("output_tokens", 0),
usage.get("cache_read_input_tokens", 0),
cc.get("ephemeral_5m_input_tokens", 0),
cc.get("ephemeral_1h_input_tokens", 0),
)
if order:
agents[path] = [requests[m] for m in order]
def cost(model, inp, out, read, w5, w1):
p = price(model)
return (inp * p[0] + out * p[1] + read * p[2] + w5 * 1.25 * p[0] + w1 * 2 * p[0]) / 1e6
for label, keep in (("before", lambda d: d < CUTOFF), ("after", lambda d: d >= CUTOFF)):
parts, n, days = collections.Counter(), 0, set()
sonnet = sub = long_agents = 0.0
peaks = []
for path, reqs in agents.items():
reqs = [r for r in reqs if keep(r[0])]
if not reqs:
continue
agent_cost = 0.0
for day, model, inp, out, read, w5, w1 in reqs:
p = price(model)
c = cost(model, inp, out, read, w5, w1)
parts["output"] += out * p[1] / 1e6
parts["cache read"] += read * p[2] / 1e6
parts["cache write"] += (w5 * 1.25 + w1 * 2) * p[0] / 1e6
agent_cost += c
sonnet += c if "sonnet" in model else 0
n += 1
days.add(day)
if "/subagents/" in path:
sub += agent_cost
peaks.append(max(r[2] + r[4] + r[5] + r[6] for r in reqs))
long_agents += agent_cost if len(reqs) >= 150 else 0
total = sum(parts.values())
if not n:
print(f"{label}: no requests")
continue
peaks.sort()
print(f"\n{label} {CUTOFF}: {n} requests over {len(days)} days, ${total:,.0f} (${total / len(days):,.0f}/day)")
print(" " + ", ".join(f"{k} {100 * v / total:.0f}%" for k, v in parts.items()))
print(f" subagents {100 * sub / total:.0f}%, 150+-turn agents {100 * long_agents / total:.0f}%, Sonnet {100 * sonnet / total:.1f}%")
if peaks:
print(f" subagent peak context: median {peaks[len(peaks) // 2]:,}, max {peaks[-1]:,}")Metadata
Metadata
Assignees
Labels
No labels
Activity