AI Knowledge Hub

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.
What Is a Context Window?Everything the model can 'see' at once

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.

Claude 3.7 Sonnet: 200k tokens
GPT-4o: 128k tokens
Gemini 1.5 Pro: 1M tokens
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
Interactive: Context Budget SimulatorSee how fast a 200k window fills up in a real Claude Code session

Current session context

45,000 / 200,000 tokens (23%)

Pressing /compact resets to a compressed baseline (~45k tokens). In Claude Code this summarizes the conversation history.
What's Actually in Claude Code's Context?Everything the model sees during a session
System prompt
~5,000

Claude Code instructions, tool definitions, and your CLAUDE.md files.

Conversation history
grows fast

Every user message + assistant response since the session started.

File contents
up to ~60k

Files explicitly read or referenced in the session.

Tool outputs
varies

Bash stdout, search results, test output — often verbose.

Pending tool calls
small

Structured JSON for tools currently being invoked.

Bloated vs. Lean Context
GOOD
// 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 task

Model focuses on what matters. Responses are faster, cheaper, and more accurate.

Claude Code: Context Management StrategiesFive tools to keep your sessions sharp

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.

CLAUDE.md example
# 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 main
Real-World Examples: Context Gone Wrong (and Right)
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 window
With 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 dumps
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 files
With 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 failures
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 work
With context management
// .claudeignore: node_modules/ .next/ package-lock.json yarn.lock // Now Claude only sees your actual source code
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 time
With 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 session
Context Hygiene ChecklistBefore and during every Claude Code session
Project 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


Top Context Mistakes to Avoid
Quick ReferenceContext management commands at a glance
/compact

Summarize history, keep key decisions, reduce token count

After sub-task completion or >60% context
/clear

Full context reset. CLAUDE.md is re-read automatically

New unrelated task or stuck model
CLAUDE.md

Always-on context injected at session start

Project conventions, stack, key paths, do-nots
.claudeignore

Prevent Claude from reading files/dirs

node_modules, build outputs, lock files, secrets
memory/

Persistent knowledge across sessions

Long-lived decisions, user prefs, project context