Issue 08 | The Art of Context Window (context) Management
🎯 Learning Objectives
After completing this issue, you will master:
- How the context window works and the concept of its capacity
- When to correctly use
/compactand/clear - How to maintain Claude's output quality in long conversations
- Context "occupancy" visualization and monitoring
📖 Core Concepts Explained
8.1 What is the Context Window?
The Context Window is the total amount of information Claude can "remember" in the current conversation. Think of it as a whiteboard:
╭─ Context Window Whiteboard ────────────────────────────────╮
│ ┌──── Occupied ───────────────────────┐ ┌── Remaining ──┐ │
│ │ CLAUDE.md | Chat History | File Content │ │ Available Space │ │
│ │ Tool Call Results | Code Context │ │ │ │
│ └───────────────────────────────────┘ └───────────┘ │
│ │
│ Occupancy: ████████████████░░░░░░░ 68% │
│ │
│ ⚠️ Above 80%, Claude's response quality significantly degrades │
│ 🔴 Near 100%, system automatically triggers /compact │
╰──────────────────────────────────────────────────────╯
8.2 Three Key Commands
| Command | Function | Use Case |
|---|---|---|
/compact |
Compresses long conversations into a summary, freeing up space | Context is too long but needs to continue |
/clear |
Completely clears context, starts fresh | Switching to a completely unrelated task |
/memory |
View persistent memory | Confirm which preferences have been saved |
8.3 Golden Rule: 60% Context Occupancy
Experience shows that Claude's output quality is highest when context occupancy is below 60%.
Occupancy Status Chart:
0% ─────── 30% ─────── 60% ─────── 80% ─────── 100%
│ ✅ Optimal │ ✅ Good │ ⚠️ Caution │ 🔴 Degraded │
│ (New Session) │ │ Consider /compact │ Must Address │
💻 Simulating Claude TUI Interaction
Scenario 1: /compact in a Long Conversation
> (After 30 rounds of conversation...)
>
> Add rate limiting to the login module
╭─ Claude ─────────────────────────────────────────────╮
│ │
│ ⚠️ Context usage: 78% │
│ Tip: Use /compact to free up space │
│ │
│ Let me add the rate limiting feature... │
│ (But the answer might not be as precise as before) │
│ │
╰──────────────────────────────────────────────────────╯
> /compact
╭─ Compacting ─────────────────────────────────────────╮
│ │
│ 📦 Summarizing conversation... │
│ │
│ Summary of work done so far: │
│ • Analyzed auth module structure │
│ • Implemented JWT refresh token flow │
│ • Added password reset endpoint │
│ • Fixed CORS configuration │
│ • Updated Prisma schema with session table │
│ │
│ Key decisions: │
│ • Using bcrypt with 12 salt rounds │
│ • Refresh tokens stored in DB (not cookie) │
│ • Rate limiting will use sliding window algo │
│ │
│ ✅ Context reduced: 78% → 25% │
│ You can continue working normally. │
│ │
╰──────────────────────────────────────────────────────╯
> Continue, add rate limiting to the login module
╭─ Claude ─────────────────────────────────────────────╮
│ │
│ (Based on the compacted summary, Claude knows the previous work context) │
│ │
│ Okay, based on our previous discussion, we chose the sliding window algorithm. │
│ Let me implement it... │
│ │
╰──────────────────────────────────────────────────────╯
Scenario 2: /clear to Switch to a Completely Different Task
> (Just finished developing the auth module)
> /clear
╭─ Context Cleared ────────────────────────────────────╮
│ │
│ ✅ Context cleared. Starting fresh. │
│ 📝 CLAUDE.md and auto memory are still loaded. │
│ │
╰──────────────────────────────────────────────────────╯
> Help me optimize the CI/CD pipeline, builds are currently too slow
╭─ Claude ─────────────────────────────────────────────╮
│ │
│ (Fresh context, no interference from auth module memory) │
│ │
│ 🔍 Tool: Read (.github/workflows/ci.yml) │
│ 🔍 Tool: Read (turbo.json) │
│ ... │
│ │
╰──────────────────────────────────────────────────────╯
💻 Code Demo: Best Practices for Context Management
# ✅ Good habit: Use different sessions for different tasks
claude # Task A: Fix bug
# Exit after completion
claude # Task B: Write new feature
# ✅ Good habit: Name sessions for easy recovery later
claude -r "auth-refactor" "Continue auth module refactoring"
claude -r "ci-optimize" "Continue CI optimization"
# ✅ Good habit: Regularly compact during long tasks
# /compact after completing each milestone
# ❌ Bad habit: One session for all tasks
# Context quickly overflows, quality plummets
# ❌ Bad habit: Never compact
# It's too late when the system automatically triggers it
Context Management Strategy Quick Reference
Task Type Recommended Strategy
────────── ──────────────────────
Minor fixes Single session, no management needed
Medium feature development /compact after each step
Large refactoring Isolate each module with /clear
Completely different tasks Directly /clear or new session
Multi-day work Name session with -r + /compact
🔧 Tools / Commands Involved
| Command | Function | Underlying Mechanism |
|---|---|---|
/compact |
Compresses session into summary | Calls model to generate summary |
/clear |
Clears context | Retains CLAUDE.md + memory |
-c / --continue |
Resumes most recent session | Reloads historical messages |
-r "name" |
Resumes named session | Looks up history by name |
--fork-session |
Forks session | Copies context to new branch |
📝 Key Takeaways from this Issue
- The context window has a capacity limit; consider
/compactwhen exceeding 60% /compactretains a work summary, suitable for long conversations on the same task/clearcompletely resets, suitable for switching to unrelated tasks- Use named sessions (
-r) to manage multiple parallel development lines - CLAUDE.md and Auto Memory are unaffected by
/clear(persistent)
🔗 References
- Resume previous conversations
- Name your sessions
- Context window visualization
- WeChat Official Account "新时代的老登": Vibe Coding feels good for a moment, but leads to project abandonment and disaster.