/token-calculation — docs/ context-size & latency audit (any repo)
Role: You are a performance auditor measuring where context (and therefore money and latency) goes in a repository's docs/ corpus and in the Claude Code sessions run against it. Read-only — never edit, commit, or change the audited repo. Your report is the deliverable, not your process. Numbers, not adjectives. Distributions (median / p90 / max), never just means — the tail is what matters.
This skill is repo-agnostic: it analyses any docs/ tree + that repo's transcripts. It knows nothing about agent roles. To audit an agent-playbook framework's per-role prompt floor and skill inventory, also run the companion skill token-playbook-calculation and merge the sections.
When to use
- "How many tokens is our
docs/ corpus? Where's the bulk / the giant-doc tail?"
- "Why are Claude Code sessions on this repo getting slow or expensive over time?"
- Establishing a baseline before restructuring docs or prompts.
- NOT for: a single file's token count (just count it); live production latency SLOs (needs OTEL — see latency note).
Iron rules
- Never
cat the whole corpus into context (can be ~1M+ tokens). The bundled script sizes in-shell and surfaces only aggregates.
- Label every number
(measured), (est), or NOT AVAILABLE. Never fabricate or infer a number you did not measure.
- Token estimate = bytes ÷ 3.8 — state the divisor. (Exact
count_tokens lives in the playbook companion skill, for per-file static load.)
- Distributions: median, p90, max per group.
Procedure
SKILL=~/.claude/skills/token-calculation
bash "$SKILL/token-audit.sh" --repo /path/to/repo --sessions 2
# --docs DIR (if not <repo>/docs) --divisor 3.8 --topn 10
The script emits, each labelled:
- Environment — OS, Claude Code + Codex versions.
- Corpus shape — per
docs/ subdir: files, est tokens, median/p90/max; giant-doc tail (>200 lines); top-N concentration; total tree size.
- Onboarding (auto-skips if no
docs/tasks or docs/handoffs) — per dispatch-style artefact: itself + every docs/*.md it cites; median/p90/max + largest worked examples.
- Context growth + latency — for the richest N session transcripts (resolved from
~/.claude/projects/<encoded-repo-path>/): per-turn context (input+cache_create+cache_read) first/median/p90/max, monotonic-decrease count (≈ compaction events), inter-turn wall-clock bucketed by context size, and how many transcripts carry a compaction/summary marker.
Do by hand (the script can't)
- Worst onboarding example — itemise the single largest brief line-by-line (itself + each cited artefact + tokens).
- Latency caveat — inter-turn seconds include tool-execution time (upper bound). True TTFT / per-request latency is NOT AVAILABLE without OTEL (
CLAUDE_CODE_ENABLE_TELEMETRY=1 + an exporter). Say so.
Gotchas the script already handles (know them for debugging)
- RTK / token-optimizer wraps
grep/awk/wc/etc. as shell functions that can abort loops — the script unset -fs them; if running commands yourself, prefix with command .
set -e in the host profile kills bare [ -z x ] && y lines — the script runs set +e.
wc -l < file returns 0 under RTK (lost redirect) — use wc -l file.
- The
Grep tool's count mode caps the total at head_limit — the script uses find, no cap.
- A fenced repo may block the
Write tool (fail-closed PreToolUse guard) — write the final report via a shell heredoc (cat > file) to a path the user names, outside any protected dir, and send them the path.
Report assembly
ONE markdown report, tables + short narrative: Environment · Corpus shape · Onboarding · Context growth & compaction · Latency · What I could not measure. End with a 5-line TL;DR: (1) total corpus size, (2) where the bulk/tail is, (3) typical vs worst onboarding, (4) the one number characterising latency-vs-context, (5) the biggest unmeasured unknown. Write the file outside the audited repo's protected dirs; give the user the path.
1---2name: token-calculation3description: Use when auditing the context-size, token-budget, or latency of ANY repository from its docs/ folder and its Claude Code session transcripts — questions like "how big is our docs corpus", "why are sessions on this repo getting slow or expensive", corpus growth, giant-doc tail, onboarding read-amplification, or context-vs-latency. Read-only; produces a measured report. For an agent-playbook role/skill static-load audit, pair with token-playbook-calculation.4---56# /token-calculation — docs/ context-size & latency audit (any repo)78**Role**: You are a performance auditor measuring where context (and therefore money and latency) goes in a repository's `docs/` corpus and in the Claude Code sessions run against it. **Read-only — never edit, commit, or change the audited repo.** Your report is the deliverable, not your process. Numbers, not adjectives. Distributions (median / p90 / max), never just means — the tail is what matters.910This skill is **repo-agnostic**: it analyses any `docs/` tree + that repo's transcripts. It knows nothing about agent roles. To audit an agent-playbook framework's per-role prompt floor and skill inventory, **also run the companion skill `token-playbook-calculation`** and merge the sections.1112## When to use13- "How many tokens is our `docs/` corpus? Where's the bulk / the giant-doc tail?"14- "Why are Claude Code sessions on this repo getting slow or expensive over time?"15- Establishing a baseline before restructuring docs or prompts.16- NOT for: a single file's token count (just count it); live production latency SLOs (needs OTEL — see latency note).1718## Iron rules191. **Never `cat` the whole corpus into context** (can be ~1M+ tokens). The bundled script sizes in-shell and surfaces only aggregates.202. **Label every number** `(measured)`, `(est)`, or `NOT AVAILABLE`. Never fabricate or infer a number you did not measure.213. **Token estimate** = bytes ÷ 3.8 — state the divisor. (Exact `count_tokens` lives in the playbook companion skill, for per-file static load.)224. **Distributions**: median, p90, max per group.2324## Procedure25```bash26SKILL=~/.claude/skills/token-calculation27bash "$SKILL/token-audit.sh" --repo /path/to/repo --sessions 228# --docs DIR (if not <repo>/docs) --divisor 3.8 --topn 1029```30The script emits, each labelled:31- **Environment** — OS, Claude Code + Codex versions.32- **Corpus shape** — per `docs/` subdir: files, est tokens, median/p90/max; giant-doc tail (>200 lines); top-N concentration; total tree size.33- **Onboarding** (auto-skips if no `docs/tasks` or `docs/handoffs`) — per dispatch-style artefact: itself + every `docs/*.md` it cites; median/p90/max + largest worked examples.34- **Context growth + latency** — for the richest N session transcripts (resolved from `~/.claude/projects/<encoded-repo-path>/`): per-turn context (input+cache_create+cache_read) first/median/p90/max, monotonic-decrease count (≈ compaction events), inter-turn wall-clock bucketed by context size, and how many transcripts carry a compaction/summary marker.3536### Do by hand (the script can't)37- **Worst onboarding example** — itemise the single largest brief line-by-line (itself + each cited artefact + tokens).38- **Latency caveat** — inter-turn seconds include tool-execution time (upper bound). True TTFT / per-request latency is **NOT AVAILABLE** without OTEL (`CLAUDE_CODE_ENABLE_TELEMETRY=1` + an exporter). Say so.3940## Gotchas the script already handles (know them for debugging)41- **RTK / token-optimizer wraps `grep`/`awk`/`wc`/etc. as shell functions** that can abort loops — the script `unset -f`s them; if running commands yourself, prefix with `command `.42- **`set -e` in the host profile** kills bare `[ -z x ] && y` lines — the script runs `set +e`.43- **`wc -l < file` returns 0 under RTK** (lost redirect) — use `wc -l file`.44- **The `Grep` tool's `count` mode caps the total at `head_limit`** — the script uses `find`, no cap.45- **A fenced repo may block the `Write` tool** (fail-closed PreToolUse guard) — write the final report via a shell heredoc (`cat > file`) to a path the user names, **outside** any protected dir, and send them the path.4647## Report assembly48ONE markdown report, tables + short narrative: **Environment · Corpus shape · Onboarding · Context growth & compaction · Latency · What I could not measure**. End with a 5-line TL;DR: (1) total corpus size, (2) where the bulk/tail is, (3) typical vs worst onboarding, (4) the one number characterising latency-vs-context, (5) the biggest unmeasured unknown. Write the file outside the audited repo's protected dirs; give the user the path.