context-cost-audit

Measures per-request token usage and prefix components from Claude Code transcripts to rank cost reductions.

Updated Dec 20, 2025
One-click install
npx skills add https://github.com/koala-man-64/helpful-scripts --skill context-cost-audit-koala-man-64
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: context-cost-audit
Source: https://github.com/koala-man-64/helpful-scripts/tree/main/context-cost-audit
Command: npx skills add https://github.com/koala-man-64/helpful-scripts --skill context-cost-audit-koala-man-64

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) components.

What problem does it solve? Agentic coding tools send a large hidden prefix with every API request (system prompt, tool schemas, CLAUDE.md, agent and skill listings, hooks, MCP instructions), and this Skill measures exactly what each request carries from real transcripts so you know what to trim. ## Core Features & Use Cases - Real usage analysis: Parses Claude Code session transcripts, deduplicates streamed usage records, and reports first-request prefix, median/p90 context, cache share, and idle-gap cache rewrites. - Prefix anatomy: Breaks one session's prefix into exact character sizes per component (tool schemas, CLAUDE.md, agent listing, skill listing, MCP instructions, hooks) with tokens allocated from measured usage. - Ranked optimization levers: Produces threshold-based findings ordered by tokens saved per request, covering agent descriptions, instruction files, hooks, surface choice, and transcript growth. - Use Case: Run the audit after noticing high token bills, discover that oversized agent descriptions add 20k tokens per request, trim them, and verify the drop with a fresh session. ## Quick Start Ask the assistant to run the context cost audit script for the last 7 days and summarize which prefix components cost the most tokens.

Frequently Asked Questions about context-cost-audit

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I measure token usage per request in Claude Code?▼

Run the context_cost_audit.py script with --days 7 to parse transcripts under ~/.claude/projects. It deduplicates streamed usage lines by message.id and reports context per request as input plus cache creation plus cache read tokens.

What makes up the fixed prefix in a Claude Code session?▼

The prefix includes tool schemas, the static system prompt, CLAUDE.md instructions, agent and skill listings, MCP server instructions, deferred tool names, and hook-injected context. The audit's prefix anatomy section measures each component's exact characters from attachment records.

Does the context cost audit script need external Python packages?▼

No, the script uses only the Python standard library and requires Python 3.10 or later. It runs on Windows, macOS, and Linux without an API key since it reads local transcript files.

Why do token counts differ between sessions on the same machine?▼

Prefix size varies by surface and project: desktop apps bundle extra tools, and repos with hooks, local agents, or repo-level CLAUDE.md files add tokens. The by-project table in the report shows median first-request prefix per project.

How can I reduce Claude Code context costs?▼

The report ranks levers by tokens saved: cap agent descriptions near 200 characters, slim global CLAUDE.md, limit hook output, defer MCP tools, and bound transcript growth. Verify each change with a fresh session's first-request usage.