# Token spend and context growth: diagnosis recipe Use when chats "crash", quotas run out, or the user asks why agents burn so many tokens. ## 0. Triage "all chats crashed" first (2 min) - `hermes gateway status` (main gateway alive?) then `hermes auth list`: if every Anthropic credential shows a 429/rate-limit countdown, it is quota exhaustion, not a crash. Bots pinned to one model return "couldn't connect to the AI model service"; the main session may survive via automatic model fallback. Report the countdown, do not restart the gateway (it drops every live session). - grep the affected profile's `logs/agent.log` for `429|rate.limit|AuthError` to find which session drove it. ## 1. Measure spend (logs) and fixed prompt - `python3 scripts/token_usage_from_logs.py` -> `ham.json`: per profile 7-day calls, input, cache read/write, fresh input, output, max context per session, model mix; plus `hermes -p

prompt-size --platform telegram --json` (offline) for system prompt, tool-schema JSON and per-skill SKILL.md bytes. - Check the regex matched every "API call #" line on one log before trusting totals (log format drifts). - Typical finding: fixed prompt 12-25k tokens/profile depending on active tools and descriptions (system ~3.5-7k, tool schemas ~9-34k, skill index ~8k); SOUL/MEMORY/USER are small. Once loaded via `skill_view`, a skill's full body is carried in context on every subsequent turn of that session until compressed away, so large skills (50-90 KB) compound rapidly across multi-turn chats. ## 2. Find what fills the history (state.db) - `python3 scripts/context_growth_from_statedb.py ` -> `baglam.json`. Weight each message by the number of later model calls in its session; raw size alone hides the cost because a message is resent on every call until compressed away. - Categories that dominated (8 busy profiles, 7 days): reasoning blocks 22-38% (Opus signed thinking is replayed in later turns, see `agent/anthropic_thinking_policy.py`; `reasoning_effort: high` makes it worse), the agent's own tool arguments 20-29% (full `write_file` contents, long patches/scripts), terminal output 15-22%, `read_file` 2-18%, `skill_view` 3-8% (big skills re-opened after each compression), vision/user messages small. - `reasoning`, `reasoning_content`, `reasoning_details` columns duplicate the same thinking: take max, never sum (summing triple-counts). - Compression telemetry: grep `context compression attempt telemetry` for `current_estimated_tokens`, `effective_threshold`, `method` (`deterministic_fallback` = summary failed). - Server-side implicit cache eviction: at deep context windows (400k-500k tokens, e.g. Gemini 4 Neon), server-side cache eviction can cause cache miss spikes (~65% miss rate), forcing ~95% redundant full-token reprocessing and draining weekly quotas in minutes. - Context rot: empirical testing confirms instruction following and critical rule adherence degrade significantly in long contexts (~300k chars) compared to short, clean prompts. ## 3. Levers, in the order to propose them 1. Split oversized skills (>~75 KB) into SKILL.md (~20 KB of always-on rules) + `references/`; send as a proposal file to the owner's `workspace/`, never edit an owner's skill directly. 2. Offload auxiliary summarization: compression passes consume ~20-25k+ tokens per trigger. Set `auxiliary.compression` to Sonnet 5 or Gemini Flash with an explicit task `fallback_chain` rather than burning Opus on maintenance summaries. 3. Lower `reasoning_effort` for routine profiles. 4. End a session when its job is done; long-lived Telegram sessions accumulate for days. 5. Agent habits: write big files in pieces, trim terminal output with `tail`/`head`, read file ranges instead of whole files. Threshold/config knobs (compression threshold, model tier) are the user's call and come last; this user explicitly prefers fixing the growth source over tuning the threshold. ## Report Vault note `Token Analysis .md` with method, per-profile table, category shares, decisions/pending; one line in the open-tasks note. Keep ham.json/baglam.json in scratch and name the path.