Guide

Manage Claude Code Tokens and Context

Monitor context usage, compact conversations effectively, and prevent runaway token costs during long development sessions.

~8 min read

One of the most noticeable differences between using Claude in a browser and using Claude Code in a terminal is conversation duration. In a terminal, a single session might span four hours of continuous coding, thirty test runs, twelve file edits, and several deep refactors.

Every file read, terminal command output, error traceback, and assistant response stays in the context window. As that transcript grows, two things happen: your token burn rate increases on every turn, and the model's attention begins to diffuse across thousands of lines of historical clutter.

Understanding how to manage your context window is not just about saving money: it is about maintaining high reasoning quality throughout an entire workday.

The economics of context window bloat

Modern frontier models support 200,000 tokens of context, and prompt caching significantly reduces repeated input costs. But prompt caching is not free, and context size directly impacts latency.

When your session holds 120,000 tokens of history, every subsequent prompt requires Claude Code to process that entire context. If that history contains 80,000 tokens of failed build logs and discarded code snippets from three hours ago, you are paying the model to read obsolete information that no longer matters to your current task.

How /compact actually works

Claude Code provides the `/compact` command to distill long conversations without losing current progress. When you run `/compact`, Claude Code generates a structured summary of the session history, discards the raw turn-by-turn logs, and resets the active context with only the distilled summary.

You type: /context
Output: Current session context: 142,600 / 200,000 tokens (71%)
Output: - System & CLAUDE.md: 2,100 tokens
Output: - Skill definitions: 3,400 tokens
Output: - Conversation history: 137,100 tokens
You type: /compact focusing on the auth refactor in src/auth and pending test fixes
Output: Compacting conversation...
Output: Context compressed from 142,600 tokens to 18,200 tokens (87% reduction).
Note: Adding an explicit note to /compact ensures critical active state is retained in the summary.
Inspect token distribution and compact conversation history.

Notice the technique in the terminal session above: passing an instruction directly to `/compact`. If you simply type `/compact`, Claude summarizes the entire transcript generically. By adding a specific focus ("focusing on the auth refactor..."), you tell the summarizer exactly which decisions and active threads must be preserved.

Choosing between /compact, /clear, and /rewind

You type: /rewind
Output: Select turn to rewind to:
Output: [1] Turn 28: Initial auth handler rewrite
Output: [2] Turn 34: Attempted microservices split (failed)
You type: 1
Output: Rewound to Turn 28. Discarded 6 turns of failed attempts from context.
Note: Rewinding purges conversational dead-ends while preserving your chosen baseline.
Use /rewind to branch back before a failed refactor.

Experienced operators navigate between three distinct memory management commands depending on what their workflow demands:

  • /compact: Shrinks transcript volume while preserving active task context, architectural decisions, and current goals. Use this when you are halfway through a complex task and context exceeds 50 percent.
  • /clear: Completely wipes the conversation history clean. Your working directory files remain untouched, but Claude forgets everything said earlier. Use this when shifting to an entirely unrelated feature or bugfix in the same repo.
  • /rewind: Steps backward in conversation history, discarding turns that went down an unproductive path. Use this when an approach failed and you want to branch from an earlier point without retaining the error logs.

Surgical reads vs whole-directory dumps

The single fastest way to blow through your context budget is asking Claude to read whole directories. Asking "look through src/ and find where users are created" can load forty files into context in one turn.

Comment: Avoid: broad reads that flood context
You type: read all files in src/services to find where Stripe customer IDs are updated
Note: This burns 25,000 tokens loading every service file into memory.
Comment: Better: surgical grep inspection
You type: use grep to search for "stripeCustomerId" in src/services
Output: Found 2 matches in src/services/customer.ts:42 and src/services/webhook.ts:88.
Note: Consumes under 300 tokens, pinpointing the exact lines to open.
Use grep tools first before reading full files.

Monitoring token thresholds throughout the day

Make checking `/context` a routine habit. When context crosses 60 percent, run a focused `/compact`. If you finish a task, commit your changes and run `/clear`. Treating context window space as an active resource keeps Claude fast, accurate, and cost-effective.

Stay updated

Get new guides in your inbox

One task, one guide, done fast. Practical Claude Code skills, zero noise.