Context Management
Strategic context management for accurate, relevant responses. Learn when to add, clear, and organize information.
Premium Course Content
This lesson is part of a premium course. Upgrade to Pro to unlock all premium courses and content.
- Access all premium courses
- 1000+ AI skills included
- New content added weekly
Context Is Everything
In the previous lesson, we explored core commands. Now let’s build on that foundation. Claude Code’s responses are only as good as the context you provide. Give it the right files, it gives you great answers. Give it irrelevant clutter, it gives you confused responses.
Context management isn’t just adding files. It’s strategically curating what Claude sees to get the results you need.
How Context Works
Think of Claude’s context like a desk. Limited surface area. Everything on the desk competes for attention.
Small, organized desk: You can find things. You work efficiently.
Huge, cluttered desk: Things get buried. You lose focus.
Same with Claude. A few relevant files beat dozens of tangentially related ones.
The Minimal Viable Context Principle
Before adding anything, ask: “Does Claude need this for the current task?”
Yes, add it:
- Files you’re asking about or modifying
- Files that define interfaces being used
- Files with patterns you want followed
- Test files showing expected behavior
No, skip it:
- Entire directories “just in case”
- Files tangentially related to the topic
- Large config files Claude won’t need to change
- Documentation Claude doesn’t need to reference
When uncertain: Start without it. Add if Claude seems to be missing information.
Context Layering Strategy
Build context in layers, from general to specific.
Layer 1: Project Understanding
/add README.md package.json
High-level project info. What is this? What stack?
Layer 2: Architectural Context
/add src/index.js src/routes.js
Entry points and structure. How is this organized?
Layer 3: Feature Context
/add src/auth/*.js
The specific area you’re working in.
Layer 4: Task Context
/add src/auth/login.js src/auth/types.ts
The exact files for your current task.
You don’t always need all layers. For a simple bug fix, skip straight to layer 4.
Reading Context Signals
Claude tells you when context is off:
“I don’t see…” Claude is missing a file. Add it.
Hallucinated code references Claude is guessing because it doesn’t have the real code. Add the source files.
Inconsistent style or patterns Claude hasn’t seen your existing code. Add examples of the patterns you want.
Very slow responses
Context might be too large. Try /compact.
Responses reference old, irrelevant work
Context is cluttered with outdated info. Consider /clear and rebuilding.
Context Strategies by Task
Bug Fixing
/clear
/add [file with bug]
/add [related files that might be involved]
> Here's the error: [error message]
> Find and fix the bug.
Minimal, focused context. Just what’s needed to understand and fix the issue.
Feature Development
/clear
/add src/similar-feature/* # Pattern to follow
/add src/types/*.ts # Type definitions
/add tests/similar-feature.test.js # Test patterns
> Create a new feature that does X. Follow existing patterns.
Quick check: Before moving on, can you recall the key concept we just covered? Try to explain it in your own words before continuing.
Context shows Claude what patterns to follow.
Code Review
/add $(git diff --name-only HEAD~1) # Only changed files
> Review these changes for bugs, style issues, and potential problems.
Focused on what actually changed.
Refactoring
/add [all files involved in refactor]
> Refactor [component] to use [new pattern].
> Update all usages across these files.
All affected files upfront for coordinated changes.
Understanding Unfamiliar Code
/add src/mystery-module/*
> Explain how this module works. What's the main entry point? How do the pieces connect?
Let Claude explore and explain before you dive in.
Context Chunking for Large Tasks
Some tasks need more context than fits comfortably. Solution: chunk the work.
Instead of:
/add src/**/* # Everything—too much
> Refactor the entire codebase to use TypeScript
Do:
# Session 1
/add src/utils/*
> Convert utils to TypeScript. Here's our tsconfig.
# Session 2
/clear
/add src/models/*
> Convert models to TypeScript. Match the patterns from utils.
# Session 3
/clear
/add src/services/*
> Convert services to TypeScript.
Each session has focused context. Work is incremental.
Practical Exercise
Try this context management exercise:
- Open Claude Code in a project
- Add way too many files:
/add **/*.js - Ask a specific question about one function
- Notice how the response might reference irrelevant code
- Now
/clearand add only the relevant file - Ask the same question
- Compare the quality of responses
The focused context usually wins.
Context Management Checklist
Before starting a task:
-
/clearor/compactif previous context is irrelevant - Identify the minimum files needed
- Add files in logical order (patterns first, then target files)
- Verify with
/lsthat context looks right
During a task:
- Add files as needed when Claude seems to be missing information
- Use
/compactif responses slow down - Don’t add files “just in case”
Finishing a task:
- Consider
/compactbefore next task if context will overlap -
/clearif moving to unrelated work
Key Takeaways
- Context quality beats context quantity
- Minimal viable context: only what’s needed for the current task
- Layer context from general to specific based on task complexity
- Watch for signals that context needs adjustment
- Chunk large tasks into focused sessions
Next up: orchestrating multi-step tasks so Claude does more of the work.
Up next: In the next lesson, we’ll dive into Task Orchestration.
Knowledge Check
Complete the quiz above first
Lesson completed!