omp packages

cachescope0.1.0

Prompt cache forensics, waste attribution, and 5m vs 1h TTL break-even analysis for Claude Code and Pi sessions

tranhoaiphu Published 1 day ago MIT ~0 downloads/mo

Untested on omp

This is a pi package. No one has reported whether it works on omp yet. If you try it, add your result below.

Did it work for you?

Result

One report per person. A new report replaces your old one.

Reports 0

No reports yet.

Readme

cachescope

Prompt cache forensics, waste attribution, and 5m vs 1h TTL break-even analysis for Claude Code and Pi sessions.

Zero runtime dependencies. Works offline directly on local JSONL session logs (~/.claude/projects/ and ~/.pi/agent/sessions/), and doubles as an installable Pi package.

Why

Aggregate token counters tell you how much you spent, not why your prompt cache broke. cachescope inspects every turn in your session transcripts and answers three concrete questions:

  1. How much money was wasted on cache misses (re-billed at full input / cache_write price instead of cache_read), vs how much caching saved you.
  2. Why each miss happened:
    • ttl_expired — idle gap exceeded the cache TTL (> 5m or > 1h)
    • prefix_mutation — previously cached prefix invalidated within TTL on the same model (dynamic system prompt, tool schema churn, mid-session edits, or eviction)
    • uncached_tail — large uncached input suffix from the previous turn re-billed without a cache breakpoint (e.g., proxy stripping cache_control or unplaced message breakpoint)
    • model_switch — model changed mid-session
  3. Whether switching to 1-hour cache TTL saves money on your real usage: replays your actual session timestamps and prompt sizes under 5m (1.25x write) vs 1h (2.0x write) pricing and gives a dollar verdict.

Quick start

Run with npx (Node.js >= 20):

npx cachescope

Filter by tool, time window, or project:

npx cachescope --source claude-code --since 7d
npx cachescope --source pi --project my-app
npx cachescope ./path/to/session.jsonl --json
npx cachescope --since 24h --max-waste 2.00   # exits with code 2 if waste > $2.00

Install as a Pi package

pi install npm:cachescope

Inside Pi, cachescope adds:

  • A live footer indicator (cache 94.2% | waste $0.18 | ttl 3m 45s) so you can see cache health and remaining TTL before it expires.
  • /cache-audit to audit the active session branch, or /cache-audit all to scan all local Pi and Claude Code transcripts.

Example output

cachescope — Prompt Cache Audit
Sessions: 42 (scanned 48 files) | Turns: 618

Cache hit rate: 88.4%  (41.20M read / 46.60M prompt)
Saved by cache: $118.40  |  Cache waste: $14.82 (29 misses, 3.85M tokens re-billed)

Waste by Cause
  TTL expired (>5m idle)            19 misses    2.65M tok   68.8%     $10.21
  Prefix mutation (within TTL)       7 misses   840.0k tok   21.8%      $3.19
  Model switch                       3 misses   360.0k tok    9.4%      $1.42

5m vs 1h TTL Simulation (Claude models)
  Simulated 5m TTL prompt spend : $34.90
  Simulated 1h TTL prompt spend : $28.15
  Misses in 5m..1h idle window  : 17 (2.31M tokens)
  Recommendation                : Switch to 1h TTL saves ~$6.75 on this workload

CLI options

Flag Description
--source <claude-code|pi|all> Limit scan to Claude Code, Pi, or both (default: all)
--since <duration|date> Filter files by mtime (30m, 24h, 7d, 4w, or ISO date)
--project <substring> Match project name substring
--top <n> Rows shown for top projects and largest misses (default: 5)
--ttl <5m|1h> Assumed default TTL when logs do not specify 1h cache creation (default: 5m)
--noise-floor <tokens> Ignore misses at or below this size (default: 1024)
--max-waste <usd> Exit with code 2 when total waste exceeds <usd>
--json Emit full machine-readable JSON report
--no-color Disable ANSI formatting

How accounting stays accurate

  • Claude Code streaming deduplication: Claude Code appends multiple JSONL records per message.id as blocks stream; cachescope keeps the final usage record per (stream, message.id) and separates sidechain subagents from the main thread.
  • Pi session trees: Pi logs sessions as an append-only tree (id -> parentId); cachescope walks the active leaf branch, honors compaction / branch_summary resets, and counts cache_warm entries as TTL refreshes.
  • Honest 1h TTL simulation: Only idle misses in the (5m, 1h] window are credited to 1h TTL; in-TTL prefix mutations are penalized equally under both tiers.

Programmatic API

import { discoverSessionFiles, parseDiscoveredFile, analyzeSessions } from "cachescope";

const files = discoverSessionFiles({ source: "all" });
const sessions = files.map(parseDiscoveredFile).filter((s) => s !== undefined);
const report = analyzeSessions(sessions);
console.log(report.totalWastedCost, report.ttlSimulation);

License

MIT