← Blog

How-to6 min read

Cutting Claude Code and Codex token usage: /context, /compact, prompt caching, and model routing

How to cut Claude Code and Codex token spend and stretch usage limits: /context, /compact, /clear, prompt caching, subagents, and local usage tracking.

Mirasim settings and local usage ledger for tracking coding-agent tokens, cache hit rate, and model routingMirasim settings and local usage ledger for tracking coding-agent tokens, cache hit rate, and model routing

The short answer

Coding-agent bills and 5-hour limits burn fastest from one thing: re-sending a bloated context window on every turn. Use /clear between unrelated tasks, /compact with explicit keep-instructions during long tasks, subagents to isolate big file reads and test logs, and cheaper models for routine edits.

HabitCommand / settingWhy it saves tokens
Start fresh on a new task/rename then /clear (Claude Code); /new (Codex)Stops old file reads from being re-sent on every message
Compact with focus/compact Focus on test output and code changesReplaces long history with a targeted summary while reloading rules
Inspect context & cache/context and /usage (Claude Code); /status (Codex)Shows what is taking space and whether prompt cache is hitting
Isolate noisy readsUse subagents (or cheaper subagent model like haiku)Only the short summary returns to your main context window
Match model to the task/model and /effortSave Opus / GPT-6.2 Astra for hard reasoning; use Sonnet / Sol for routine edits

What fills your context before you even type

In Claude Code, several pieces load before your first prompt: the system prompt, root CLAUDE.md (or AGENTS.md), auto memory, skill descriptions, and MCP tool names. Every file read, hook output, and test run adds on top — and every subsequent turn processes that growing prefix again.

  • Check /context in Claude Code to see the live breakdown of what is occupying space.
  • Check /usage › Prompt cache (main) (v2.1.251+): it reports what share of input tokens hit cache, how many cache misses happened, and from v2.1.260 the likely cause of the last miss (such as tool definitions changing).
  • Trim unused MCP servers with /mcp: Anthropic notes that plain CLI tools like gh, aws, or gcloud add zero per-tool listing overhead compared with MCP servers.

/compact vs. /clear: what actually survives

When you run /compact, Claude Code summarizes the conversation history and automatically re-injects your system prompt, root CLAUDE.md, unscoped rules, auto memory, rebuilt skill descriptions (v2.1.304+), any plan written in plan mode, and a fresh git status snapshot. Path-scoped rules and nested subdirectory CLAUDE.md files reload on demand.

Tell Claude Code what to preserve during compaction inside CLAUDE.md
# Compact instructions

When compacting, preserve failing test commands, exact error messages, and the list of files already modified.

Tracking cache hit rate and routing cheaper models in Mirasim

Provider billing pages only show your total account spend, not which repo or session burned the budget. Mirasim keeps a local ledger on your machine and lets every pane pick its own model and reasoning level.

  • Local Usage page: view call count, total tokens, output tokens, cache hit rate, and estimated cost broken down by agent, model, account, workspace, and individual session — down to single calls.
  • Mix models across panes: keep one pane on Claude Opus 5.5 or GPT-6.2 Astra for design work, while adjacent panes run Sonnet 5.5, DeepSeek, Kimi, or a local Ollama model with zero Mirasim credit.
  • Pay per token on Mirasim credit when you want discounted API access without extra subscriptions: Claude Sonnet 5.5 50% off, Opus 5.5 50% off, GPT-6.2 Sol Limited time: 90% off off official API prices.

Questions

Does /compact erase my CLAUDE.md rules?
No. Claude Code re-injects your root CLAUDE.md, unscoped rules, auto memory, and plan mode plan from disk after compaction; nested CLAUDE.md files reload on demand.
Why did my prompt cache hit rate suddenly drop?
Changing active MCP tools, switching models mid-session, letting the cache TTL expire while idle, or running compaction rebuilds the prefix. In Claude Code v2.1.260+, /usage names the likely cause on the Prompt cache (main) line.
How do I cap spend for a single Claude Code CLI run?
Pass --max-budget-usd <amount> when running Claude Code on API billing so the run stops once estimated cost reaches that cap.

Sources

Checked on November 29, 2026 against Anthropic's Claude Code docs (code.claude.com/docs: Manage costs effectively, Explore the context window) and OpenAI's Codex command docs (learn.chatgpt.com/docs). Check those pages for the latest commands and cache stats.

Keep reading