Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Nested lingtai-dev-guide reference for computing the recent prompt-cache hit rate from LingTai token ledgers over rolling windows (default 1h / 5h / 1d / 3d). Explains the provider-agnostic input/cached fields in logs/token_ledger.jsonl, the exact formula (sum(cached)/sum(input) per window), timestamp/timezone handling, the daemon double-count hazard, and ships a read-only stdlib script (scripts/cache_hit_rate.py) that aggregates an agent workdir, a project root, or a single ledger file. Use whe
.claude/skills/lingtai-ai-dev-guide-cache-hit-rate/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-12 | ✗→✓ | ▲ Improved | 144% | 0% |
| case-04 | ✗→✓ | ▲ Improved | 155% | 0% |
| case-06 | ✗→✓ | ▲ Improved | 104% | 0% |
| case-07 | ✗→✓ | ▲ Improved | 33% | 0% |
| case-09 | ✗→✓ | ▲ Improved | 46% | 0% |
Nested lingtai-dev-guide reference. Read this after the top-level router sends you here when you need to know how well prompt caching has been working recently for one or more LingTai agents, grounded in the token ledger rather than guessed.
This pairs with reference/runtime-self-check/SKILL.md §6: when a cache/affinity fix "should be live," the token ledger is the observable that proves it. This reference is the measurement; runtime-self-check is the did-the-object-rebuild diagnosis.
A read-only metric. It only reads append-only logs/token_ledger.jsonl files; it never writes, rotates, or mutates runtime state. Report rates without pasting private absolute paths into human-facing deliverables — the ledger holds no secrets, but its parent paths can be private, so generalize to ~/.lingtai-tui/... or <project>/.lingtai/<agent>/.
Single source of truth: logs/token_ledger.jsonl, one JSON object per LLM call, written after every call by lingtai/kernel/token_ledger.py (append_token_entry). Required fields:
| Field | Meaning | |---|---| | ts | Call time, UTC, %Y-%m-%dT%H:%M:%SZ (always Z/UTC). | | input | Total prompt/input tokens for the call. Already includes the cached portion. For the Anthropic/Claude adapters this is raw_input + cache_read + cache_write. | | output | Output tokens. | | thinking | Reasoning/thinking tokens. | | cached | Cache-read input tokens served from the provider prompt cache. A subset of input. | | model, endpoint | Attribution (which model / base_url produced the tokens). |
Optional tags on some entries: source (main, soul, tc_wake, daemon), and for daemon-attributed rows em_id / run_id / api_call_id / codex_*.
The kernel normalizes every provider's usage into these same fields before writing, so the metric is provider-agnostic (verified across gpt-5.5, mimo-v2.5-pro, deepseek-v4-pro, and the Anthropic adapters). Key invariant, confirmed in the adapters (lingtai/llm/anthropic/adapter.py, lingtai/llm/claude_code/adapter.py) and empirically over a full ledger: 0 <= cached <= input, so the hit rate is always in [0, 1].
For a window [now - W, now], over all entries whose ts lies in it:
hit_rate(W) = sum(cached) / sum(input)sum(input) already includes cached tokens, making this atoken-weighted rate (a 100k-token call counts more than a 1k-token call) — which is what "how much of the prompt volume came from cache" means.
1h, 5h,1d, 3d. An entry is in the window iff now - W <= ts <= now. Ledger timestamps are UTC and "now" is computed in UTC — no local timezone involved.
n/a, never adivide-by-zero.
ts, or non-numericinput/cached — are skipped and counted under skipped, so silent data loss is visible.
Caveat on cached. Native streaming/non-streaming adapters set cached = cache_read only; cache writes are billed into input but not counted as cached. CLI-backed daemon runs (lingtai/tools/daemon/run_dir.py) instead document cached as cache_read + cache_creation, because the CLI backend only exposes an aggregate. So CLI-backed entries lean slightly optimistic (first-write tokens read as "cached"). The cached <= input bound still holds — just don't over-interpret sub-percent differences on daemon CLI traffic.
Daemon LLM calls are written to two ledgers: the daemon's own daemons/<run>/logs/token_ledger.jsonl and the parent agent's logs/token_ledger.jsonl (tagged source="daemon" + em_id/run_id). Naively globbing **/logs/token_ledger.jsonl under a project double-counts every daemon token.
The rule this reference and the script follow:
<workdir>/logs/token_ledger.jsonl; it alreadycontains the daemon-tagged rows.
<root>/<agent>/logs/token_ledger.jsonl; never recurse into daemons/.
scripts/cache_hit_rate.pyDeterministic, read-only, standard library only. Accepts an agent workdir, a project root, or a single ledger file.
bashSCRIPT=~/.lingtai-tui/utilities/lingtai-dev-guide/reference/cache-hit-rate/scripts/cache_hit_rate.py PY="$HOME/.lingtai-tui/runtime/venv/bin/python" # or any python3.11+ # Current agent workdir (run from e.g. <project>/.lingtai/codex) "$PY" "$SCRIPT" . # A specific agent workdir "$PY" "$SCRIPT" <project>/.lingtai/codex # A whole project root: each agent's ledger, aggregated (daemons not double-counted) "$PY" "$SCRIPT" <project>/.lingtai # A single ledger file, custom windows, JSON output "$PY" "$SCRIPT" logs/token_ledger.jsonl --windows 1h 6h 1d --json # Only main-chat turns; pin the clock for a reproducible result "$PY" "$SCRIPT" . --source main --now 2026-06-22T01:00:00Z
Flags: --windows (<int><s|m|h|d|w>, e.g. 90m 1d 1w), --source (filter to one source tag), --now ISO (override the clock for deterministic runs), --json, --help.
Example text output:
window calls input cached hit_rate
----------------------------------------------------------
1h 43 3,933,316 1,627,648 41.4%
5h 43 3,933,316 1,627,648 41.4%
1d 43 3,933,316 1,627,648 41.4%
3d 43 3,933,316 1,627,648 41.4%Equal rows across windows just mean all recent activity fell inside the smallest window (e.g. one active session in the last hour).
Exit codes: 0 success (including empty windows); 1 no ledger found under the path; 2 bad argument (missing path, bad --now, bad --windows).
For a single window against one explicit ledger path:
bashPY="$HOME/.lingtai-tui/runtime/venv/bin/python" "$PY" - logs/token_ledger.jsonl 5 <<'PY' import json, sys from datetime import datetime, timedelta, timezone path, hours = sys.argv[1], float(sys.argv[2]) cut = datetime.now(timezone.utc) - timedelta(hours=hours) inp = cac = 0 for line in open(path): line = line.strip() if not line: continue try: d = json.loads(line) except ValueError: continue ts = d.get("ts","") try: t = datetime.fromisoformat(ts.replace("Z","+00:00")) except ValueError: continue if t >= cut: inp += d.get("input",0); cac += d.get("cached",0) print(f"{cac}/{inp} = {100*cac/inp:.1f}%" if inp else "n/a (no input in window)") PY
It takes one explicit path, so it cannot trip the double-count hazard — but it does not report skipped rows. Prefer the script for anything you will report.
no token_ledger.jsonl found — the path is neither an agent workdir(logs/token_ledger.jsonl) nor a project root with child agents. Point at the agent dir (e.g. .lingtai/codex), the .lingtai/ root, or the ledger file.
n/a — no input tokens in those windows: idle agent, or--now predates the activity. Widen the window, drop --source, or check ts ranges with head -1 / tail -1.
cache_creation caveatabove.
skipped is non-zero — corrupt/rotated lines or pre-schema rows. A handfulis normal (e.g. a partially written final line); a large fraction suggests schema drift.
input/cached/ts, thisreference and the script must be updated in the same spirit as the "anatomy travels with code" rule. Re-read lingtai/kernel/token_ledger.py and the active provider adapter to re-confirm field semantics before trusting numbers.
recent activity; idle colleagues contribute zero.
Sanity-check the script against a known answer with a fixture and a pinned clock:
bashT=$(mktemp -d); mkdir -p "$T/a/logs" printf '%s\n' \ '{"source":"main","ts":"2026-06-22T00:50:00Z","input":1000,"output":1,"thinking":0,"cached":800}' \ '{"source":"main","ts":"2026-06-21T22:00:00Z","input":1000,"output":1,"thinking":0,"cached":500}' \ > "$T/a/logs/token_ledger.jsonl" "$PY" "$SCRIPT" "$T/a" --now 2026-06-22T01:00:00Z # 1h -> 80.0%, 5h -> 65.0% rm -rf "$T"
reference/runtime-self-check/SKILL.md — §6 live-object lifecycle: the ledgeras proof a cache/affinity fix took effect after refresh.
reference/debug-troubleshoot/SKILL.md — broader runtime diagnostics when alow/zero hit rate points at a misbehaving session rather than a metric.
reference/architecture/SKILL.md — where runtime state (including.lingtai/<agent>/logs/) lives.
Other measured skills in the registry, with their headline benchmark lift.