Managing Context in AI
Context is the most valuable — and most limited — resource in any AI interaction. Learn how to budget it wisely, especially in Claude Code.
A context window is the maximum number of tokens (roughly ¾ of a word each) that the model can process in a single request — both input and output combined.
Think of it as working memory. Anything outside the window is invisible to the model. Once full, older content is dropped or the request fails.
Typical token costs at a glance
| 1 word | ~1–2 tokens |
| 1 line of code | ~10–20 tokens |
| 1 page of text | ~500 tokens |
| A 500-line file | ~3,000–5,000 tokens |
| Full git diff (medium PR) | ~8,000–20,000 tokens |
| All open files in Claude Code | ~10,000–60,000 tokens |
Current session context
45,000 / 200,000 tokens (23%)
Claude Code instructions, tool definitions, and your CLAUDE.md files.
Every user message + assistant response since the session started.
Files explicitly read or referenced in the session.
Bash stdout, search results, test output — often verbose.
Structured JSON for tools currently being invoked.
// CLAUDE.md: project summary, conventions (500 tokens)
// Only the 3 files directly relevant to the task
// /compact ran after resolving the config issue
// Test run with --silent, only failures shown
// One focused conversation thread per taskModel focuses on what matters. Responses are faster, cheaper, and more accurate.
CLAUDE.md is a special markdown file Claude Code automatically reads at the start of every session. It injects always-on context without you having to repeat yourself.
Keep it under ~500 tokens. Treat it like a README for Claude, not a novel.
# Project: ai-notebook
## Stack
- Next.js 14 (App Router), TypeScript, MUI v5
- No Tailwind. Use MUI sx prop for styling.
## Conventions
- TypeScript everywhere, no .js files
- Commit prefixes: feat:, fix:, chore:, docs:
- 2-space indent in YAML / JSON
## Key paths
- Pages: src/app/pages/
- Sidebar: src/components/layout/SidebarMenu.tsx
- Router: src/app/pages/[slug]/page.tsx
## Do NOT
- Add purple gradients to UI
- Use default exports for contexts
- Force-push to mainClaude forgets the plan mid-session
Without context management
User: let's refactor the auth module
Claude: [reads 5 files, 20k tokens]
... 15 turns later ...
User: ok now rename LoginController to AuthController
Claude: "What's the project structure? I don't see an AuthController."
// The file list scrolled out of the context windowWith context management
// Use /compact after the refactor plan is agreed on:
User: /compact
Claude: [summarizes: auth refactor plan, files involved, decisions]
// Now Claude starts the rename with the summary in context
// instead of the full file dumpsTest output floods context
Without context management
// Running: npm test (verbose mode by default)
// Output: 12,000 tokens of passing test lines
// Now there's no room for the actual code filesWith context management
// In CLAUDE.md:
## Testing
- Run tests with: npm test -- --silent 2>&1 | tail -50
- Only show failures + summary, not passing lines
// Or in the session:
User: run tests, show only failuresModel reads irrelevant files
Without context management
Claude: Let me explore the project structure...
[reads node_modules/react/index.js — 3k tokens]
[reads .next/cache/... — 8k tokens]
[reads package-lock.json — 25k tokens]
// Half the context window gone before any real workWith context management
// .claudeignore:
node_modules/
.next/
package-lock.json
yarn.lock
// Now Claude only sees your actual source codeRepeated context across sessions
Without context management
// Every session:
User: "We use TypeScript, MUI, no Tailwind,
commits need feat:/fix: prefix,
don't use purple..."
// 300 tokens wasted re-explaining every timeWith context management
// CLAUDE.md (once, forever):
## Stack: TypeScript, MUI v5, Next.js App Router
## No Tailwind. No purple UI.
## Commit format: feat: | fix: | chore: | docs:
// Claude reads this automatically every sessionProject setup (once)
Create CLAUDE.md with stack, conventions, key paths
Create .claudeignore excluding build artifacts and lock files
Set up memory/ directory for long-lived knowledge
Session start
Check that CLAUDE.md is current and accurate
Start with a focused goal — one task per session
Only open files directly relevant to the task
During session
Run /compact after completing each sub-task
Pipe noisy commands through tail -50 or grep
Avoid pasting large files — use Read tool instead
Watch the context usage indicator in the UI
Session end / switching tasks
Run /compact or /clear before switching topics
Save important decisions to memory/ files if they should persist
Update CLAUDE.md if conventions changed
/compact
Summarize history, keep key decisions, reduce token count
/clear
Full context reset. CLAUDE.md is re-read automatically
CLAUDE.md
Always-on context injected at session start
.claudeignore
Prevent Claude from reading files/dirs
memory/
Persistent knowledge across sessions