How to Manage Context Window in Claude Code: Practical Strategies for Token Management
Problem
I lost hours of work because Claude Code compacted my context at the wrong moment.
I was debugging a complex authentication flow. I had spent 45 minutes reading files, tracing through the codebase, and building a mental model of how the OAuth integration worked. Then suddenly, Claude asked me a basic question:
“What authentication system are you using?”
I had already explained this three times. All that exploration context was gone. The auto-compaction had wiped my research from Claude’s memory.
But the worst part? I had no idea when the compaction happened. There’s no sidebar showing token count. No warning before context is cleared. No way to know if my important findings survived.
A Reddit user captured this frustration perfectly:
“No sidebar like OpenCode that shows you how many tokens your context window has consumed. All compactions happen automatically when LLM wants, so experienced users that know how to manage that window, when to compact, when to write a plan file and execute with a fresh session, gets impossible to be applied.”
I needed to figure out how to manage context when there’s no visibility into context.
What I discovered
I researched strategies from experienced Claude Code users and found a fundamental principle: Context window is RAM, files are disk.
Context Window = RAM (Volatile, Limited)- Holds recent conversation, file reads, tool outputs- ~200K tokens capacity for Claude models- Gets compacted automatically when approaching limits- Important information CAN BE LOST during auto-compaction
Files = Disk (Persistent, Unlimited)- Plans survive context compaction- Findings can be referenced later- Progress tracking persists across sessions- No practical limit on storageThe problem is Claude Code provides no visibility into:
| What You Can’t See | Why It Matters |
|---|---|
| Current token count | Don’t know how close to limit |
| When auto-compaction triggers | Can’t prepare for context loss |
| What gets preserved | May lose critical findings |
| Context health status | Can’t proactively manage |
Strategy 1: Strategic Compaction
Instead of waiting for auto-compaction, I trigger /compact at logical boundaries.
When to compact
| Timing | Reason |
|---|---|
| After exploration, before execution | Research context is summarized, implementation plan preserved |
| After completing a milestone | Fresh start for next phase |
| Before major context shifts | Clear old context before different task |
| After debugging session | Remove error-resolution noise |
When NOT to compact
- Mid-implementation of related changes
- During multi-step refactoring
- While maintaining complex state
# After reading several files and understanding the codebase/compact
# Now start fresh with only your plan in context# The AI will ask clarifying questions based on your summaryThe key difference: I control when context is compressed, not the model.
Strategy 2: File-Based Planning
I create three planning files in every project:
| File | Purpose | Update Frequency |
|---|---|---|
task_plan.md | Phases, progress, decisions | After each phase |
findings.md | Research, discoveries | After ANY discovery |
progress.md | Session log, test results | Throughout session |
Template for task_plan.md
# Task: [Your Task Name]
## Goal[Clear, specific goal statement]
## Phases- [ ] Phase 1: [Name] - Status: pending - Key steps: ...- [ ] Phase 2: [Name] - Status: pending - Key steps: ...
## Current StatePhase: [current phase]Next Action: [what to do next]
## Decisions Made| Decision | Rationale | Date ||----------|-----------|------|| ... | ... | ... |
## Errors Encountered| Error | Attempt | Resolution ||-------|---------|------------|| ... | ... | ... |Template for findings.md
# Findings
## Codebase Structure[Key discoveries about the code]
## APIs/Functions[Important APIs discovered]
## Constraints[Technical constraints found]
## Risks[Potential issues identified]When context is compacted, I just read these files to restore my place.
Strategy 3: The 2-Action Rule
After every 2 view/browser/search operations, I save key findings to text files.
This prevents:
- Multimodal information loss (screenshots, images)
- Context overflow from large file reads
- Loss of research during exploration
1. Read file A (context used: 1 read)2. Read file B (context used: 2 reads) -> WRITE findings.md3. Read file C (context used: 1 read)4. Read file D (context used: 2 reads) -> WRITE findings.mdI learned this from a user on Reddit:
“One thing that has helped me is treating the agent like a system you instrument: explicit plans, tool-call boundaries, small sessions, and checkpoints so it cannot drift for hours.”
Strategy 4: Read Before Decide
Before major decisions, I read my planning files:
- Read
task_plan.md- Re-orient to goals - Read
findings.md- Recall what I learned - Read
progress.md- Check what’s done
Why? Context window may have been compacted. Reading brings current state back into attention.
This simple habit has saved me multiple times from making decisions based on stale context.
Strategy 5: Session Boundaries
I work in small, focused sessions instead of marathon runs:
| Session Type | Duration | When to End |
|---|---|---|
| Exploration | 15-30 min | After mapping codebase |
| Planning | 10-20 min | After creating task_plan.md |
| Implementation | 30-60 min | After completing a feature |
| Debugging | 15-30 min | After finding root cause |
End-of-session checklist
- Update task_plan.md with current state
- Write findings to findings.md
- Log progress in progress.md
- Note next action for next session
Strategy 6: The 5-Question Reboot Test
If I can answer these questions, my context management is solid:
| Question | Answer Source |
|---|---|
| Where am I? | Current phase in task_plan.md |
| Where am I going? | Remaining phases |
| What’s the goal? | Goal statement in plan |
| What have I learned? | findings.md |
| What have I done? | progress.md |
If I can’t answer: I read my planning files immediately.
Advanced: Hook-Based Reminders
For users comfortable with hooks, create a strategic compact reminder:
{ "hooks": { "PreToolUse": [{ "matcher": "tool == \"Edit\" || tool == \"Write\"", "hooks": [{ "type": "command", "command": "~/.claude/skills/strategic-compact/suggest-compact.sh" }] }] }}This tracks tool calls and suggests compaction at thresholds.
Context Management Decision Matrix
When I face specific situations, I follow this decision tree:
| Situation | Action | Reason ||-----------|--------|--------|| Starting complex task | Create task_plan.md | Plan persists across compaction || After reading 3+ files | Write to findings.md | Prevents information loss || Before major decision | Read planning files | Re-orient if context stale || Session > 1 hour | Consider /compact | Fresh context for next phase || Error occurred | Log to progress.md | Prevents repeating mistakes || Switching tasks | /compact + new plan | Clear previous context |Workarounds for Missing Features
Since Claude Code lacks visibility features, I adapted my workflow:
Missing: Token Counter
- Workaround: Track tool calls manually or via hooks
- Metric: ~50 tool calls = consider compaction
- Proxy: 1 hour session = high context usage
Missing: Context Health Indicator
- Workaround: Can’t answer the 5-question test? Context is stale
- Metric: AI asks questions about things already explained = context lost
Missing: Manual Compaction Control
- Workaround:
/compactcommand exists but timing is manual - Strategy: Compact at MY boundaries, not the model’s
Missing: Sidebar Visibility
- Workaround: Use external terminal or note-taking
- Metric: Track session duration and complexity
Comparison: OpenCode vs Claude Code
| Feature | OpenCode CLI | Claude Code |
|---|---|---|
| Token Counter | Yes (sidebar) | No |
| Context Health | Visible | Hidden |
| Compaction | Manual control | Auto + /compact |
| Visibility | High | Low |
| Workaround Needed | No | Yes |
For OpenCode users transitioning: Your token-watching habits remain valuable. Apply them to session duration and tool call counts instead of a visible counter.
Summary
In this post, I showed how to manage context window in Claude Code despite the lack of visibility. The key points are:
- Embrace file-based planning - Files are unlimited persistent memory
- Compact strategically - At logical boundaries, not arbitrary points
- Small sessions - Work in focused bursts, not marathon sessions
- Write immediately - The 2-action rule prevents information loss
- Read before decide - Re-orient from files when context may be stale
The fundamental insight: You can’t see the context window, but you can manage it through external discipline. Treat context as finite RAM and files as infinite disk. Plan, persist, and compact on your terms.
Related knowledge
Why context compaction matters
Context compaction in LLMs happens because:
- Token limit economics: Each token costs money and memory. Models can’t hold infinite context.
- Attention decay: Even within limits, older context gets less “attention” than newer tokens.
- Summarization trade-offs: Auto-compaction summarizes old context, but summarization loses detail.
How auto-compaction likely works
While Anthropic hasn’t published exact details, auto-compaction typically:
- Keeps the most recent N tokens
- Summarizes older conversation into a compressed form
- Preserves system instructions and key file contents
- May lose specific details from earlier exploration
Future developments
Anthropic is actively working on context management. Follow the Anthropic blog and release notes for:
- Potential token counter feature
- Better visibility into context health
- Manual compaction controls
- Improved summarization algorithms
Final Words + More Resources
My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
- 👨💻 Reddit Discussion: My brief (and bad) experience with Claude Code
- 👨💻 Anthropic Claude Documentation
- 👨💻 Claude Code CLI Documentation
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!
Comments