Claude Code Token Optimization: Prompt Caching & Skills
Quick Answer: Why Two Developers Spend $200 vs. $40 on the Same Sprint
Developers on r/ClaudeAI, Linux.do, and V2EX consistently report burning dozens of dollars per day on Claude Code when billed per token. In almost every audit, runaway cost comes not from generated output tokens, but from repeated prompt-cache invalidation and startup context bloat.
Applying four mechanical habits reliably cuts Claude Code API spend by 50%–75%:
- Protect prefix cache hits: Cached input reads cost roughly 0.05x–0.1x standard input pricing, whereas switching models mid-session, toggling MCP servers, or stepping away past the 5-minute API TTL triggers a costly full cache rewrite.
- Eliminate startup bloat: Oversized
CLAUDE.mdfiles and 15 idle global MCP server schemas quietly load 30k–40k tokens before your first prompt. - Move playbooks into Agent Skills (
SKILL.md): Skills keep only brief trigger descriptions in baseline context (~450 tokens) and load full instructions strictly on demand. - Route through a sticky-session
/v1/messagesgateway: Random round-robin proxies bounce consecutive turns across separate upstream accounts and destroy cache hits. Multi-protocol gateways with sticky prompt caching like Aiozium keep cache discounts intact.
1. How Claude Code's Four-Layer Prompt Prefix Works
Every Claude Code turn assembles its prompt strictly from top to bottom:
- System Prompt & Active Tool / MCP Definitions (top static prefix)
- Project Context (
CLAUDE.md, auto-memory, workspace metadata) - Conversation History & Previous Tool Outputs (grows every turn)
- Your New User Prompt (appended at the tail)
Because Anthropic prompt caching relies on exact prefix matching, mutating anything in layers 1 or 2 invalidates every token downstream.
| Action During an Active Session | Breaks Prompt Cache? | Practical Takeaway |
|---|---|---|
| Switching models mid-session (Sonnet → Opus) | Yes — instant miss | Finish the subtask before switching models or /clear cleanly |
| Adding/removing MCP servers mid-session | Yes — instant miss | Tool schemas live at layer 1; configure project MCPs before starting |
| Sitting idle > 5 minutes on API keys | Yes — TTL expired | API key cache defaults to 5m TTL (some plans extend to 1h); batch work in focused bursts |
Editing CLAUDE.md mid-session | No (deferred) | Active session keeps cached prefix; edits apply after /clear, /compact, or restart |
| Running 5 unrelated tasks in one giant chat | Hits cache, but quadratic cost | Turn 30 replays 29 turns of tool logs; run /clear between distinct features |
2. Four High-Impact Token Hygiene Tactics
Keep CLAUDE.md Lean (< 100 Lines) & Scope MCP Servers Per Repo
Treat CLAUDE.md as universal operational truth—build commands, test commands, architectural boundaries, and hard security rules—not a 2,000-line wiki dump. Disable unused global MCP servers so every turn isn't paying to serialize schemas for services your current repo never touches.
Progressive Disclosure with Agent Skills (SKILL.md)
Move deployment checklists, database migration SOPs, and PR review rubrics into modular SKILL.md folders (< 500 lines each). Claude Code indexes only concise frontmatter descriptions at startup and loads the body only when triggered, slashing baseline context weight.
Master /clear vs. /compact Timing
- Run
/clearimmediately after shipping an independent feature or whenever the agent gets trapped in 3+ failed retry loops. - Avoid over-clearing across closely related edits in the same module—cold starts force full re-reads of repository files from scratch.
- Run
/compacton deep single features once context approaches ~150k tokens to compress old tool output while preserving key decisions.
Plan First (Shift + Tab / /plan) & Read Targeted Line Ranges
Use Plan Mode on multi-file changes so the agent inspects outlines and targeted line slices instead of dumping huge logs, lockfiles, or entire 2,000-line files into conversation history.
3. Why Your Gateway Choice Decides Whether Caching Actually Works
Even perfect local prompt hygiene collapses if your API relay lacks sticky-session routing or translates /v1/messages into a generic OpenAI wrapper—both drop cache read hits to 0%.
- Sticky-Session Native
/v1/messagesGateway: Aiozium routes Anthropic/v1/messages, OpenAI/v1/responses, and Gemini/v1betanatively on one endpoint with sticky session caching, micro pay-as-you-go top-ups, and per-request cache read logs. - Unified Multi-Model & Global Alternatives: Pair AISuhui for everyday multi-model clients, OpenRouter for global model benchmarking, or self-hosted New API for internal team quotas.
# macOS / Linux (~/.zshrc or ~/.claude/settings.json env block)
export ANTHROPIC_BASE_URL="https://www.aiozium.com"
export ANTHROPIC_AUTH_TOKEN="sk-your-aiozium-key"
claude
# Windows PowerShell persistent environment variables
[Environment]::SetEnvironmentVariable("ANTHROPIC_BASE_URL", "https://www.aiozium.com", "User")
[Environment]::SetEnvironmentVariable("ANTHROPIC_AUTH_TOKEN", "sk-your-aiozium-key", "User")
Want to audit whether your current relay truly honors prompt caching and never downgrades models? Read Detect Downgraded LLM API Relays: 2026 Verification Guide, review our Custom LLM API Setup Guide, or explore more developer tools like OpenSpec and twinny in our AI Coding Directory.