Help us improve
Share bugs, ideas, or general feedback.
From claude-code-expert
Guides managing Claude Code context window with /compact, /clear commands, auto-compaction config, sub-agents, targeted reads, background tasks, and conversation flows for long sessions.
npx claudepluginhub markus41/claude --plugin claude-code-expertHow this skill is triggered — by the user, by Claude, or both
Slash command
/claude-code-expert:context-managementThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Complete guide to managing context window, compression, and conversation flow.
Master four context engineering operations—Write, Select, Compress, Isolate—to manage token budgets, compaction strategies, and context partitioning for efficient AI coding sessions.
Manages Claude Code context windows with token arithmetic, anchor budget math, compact strategies, progressive loading, and large codebase partitioning.
Optimizes Claude Code session context by explaining major consumers like metadata, images, file reads, and bash output, with strategies including screenshot compression, /compact usage, and reduced re-reads.
Share bugs, ideas, or general feedback.
Complete guide to managing context window, compression, and conversation flow.
Claude Code manages a conversation context window that accumulates messages, tool calls, and results throughout a session.
// settings.json
{
"autoCompact": true,
"contextWindow": {
"compactThreshold": 0.8,
"warningThreshold": 0.9
}
}
/compact # General compression
/compact focus on authentication # Preserve auth-related context
What gets preserved:
What gets summarized/dropped:
/clear
Complete reset — use between unrelated tasks.
Offload research to sub-agents to keep main context clean:
// Instead of reading 20 files in main context:
Agent(subagent_type="Explore", prompt="Find all API endpoint definitions")
// The agent researches and returns a summary
// Main context only gets the summary, not all file contents
// Bad: Read entire large file
Read(file_path="/path/to/large-file.ts")
// Good: Read specific section
Read(file_path="/path/to/large-file.ts", offset=100, limit=50)
// Good: Search first, then read specific matches
Grep(pattern="function authenticate", path="/path/to/")
Long-running operations in background don't consume main context:
Agent(run_in_background=true, ...)
Bash(command="npm test", run_in_background=true)
| Tool | Context Impact | Mitigation |
|---|---|---|
| Read (large files) | Very High | Use offset/limit |
| Bash (verbose output) | High | Pipe through head/tail |
| Grep (many matches) | High | Use head_limit |
| Agent (results) | Medium-High | Agent summarizes internally |
| Tool | Context Impact |
|---|---|
| Glob | Low (just file paths) |
| Write | Low (content sent, not echoed back) |
| Edit | Low (just the diff) |
| TodoWrite | Very Low |
| AskUserQuestion | Very Low |
1. /clear (fresh start)
2. State the objective clearly
3. Claude researches and plans
4. Claude implements
5. Claude tests
6. Session ends
1. Task A work
2. /compact (preserve Task A context)
3. Task B work
4. /compact (preserve A+B context)
5. Task C work
6. ...
1. Spawn research agents (background)
2. Work on other tasks while agents research
3. Agents return summaries
4. Synthesize findings
5. Implement based on research
Claude Code can automatically save important context across sessions:
~/.claude/projects/<project>/memory/MEMORY.md — Auto-loaded# Disable auto-memory
export DISABLE_AUTOMEMORY=1
# Or in settings.json
{ "autoMemory": false }
/memory # View current memories
/memory add <text> # Add memory entry
/memory clear # Clear all memories
# In conversation
"Remember that we always use pnpm, not npm"
"Forget the previous instruction about yarn"
Shows current session costs:
/cost
Output includes:
/compact to reduce repeated contextclaude-haiku-4-5 for simple tasks (/model claude-haiku-4-5-20251001)Grep to find specific content instead of reading everything# Resume last conversation
claude --continue
claude -c
# Resume specific session
claude --resume <session-id>
# Set specific session ID
claude --session-id my-session-123
# Set conversation ID within session
claude --conversation-id conv-456
/clear for unrelated work/cost periodicallyrun_in_background for slow operations