Context Management
Prerequisites
- Completed Modules 1-10
- Understanding of LLM token limits
- Active Copilot CLI session experience
Learning Objectives
- Understand how context works in Copilot CLI
- Use
/contextto monitor token usage - Use
/compactto compress session history - Optimize context for better responses
- Manage large codebases efficiently
Concepts
What is Context?
Context is everything Copilot "remembers" during a session:
┌────────────────────────────────────────────────┐
│ Context Window │
├────────────────────────────────────────────────┤
│ System Instructions (AGENTS.md, etc.) │
├────────────────────────────────────────────────┤
│ Conversation History (prompts + responses) │
├────────────────────────────────────────────────┤
│ File Contents (read during session) │
├────────────────────────────────────────────────┤
│ Tool Results (command outputs, etc.) │
├────────────────────────────────────────────────┤
│ Available Space for Response │
└────────────────────────────────────────────────┘
Token Limits
| Model | Approximate Limit |
|---|---|
| GPT-4 | ~128K tokens |
| GPT-4.1 | ~128K tokens |
| Claude Sonnet 4.6 | ~200K tokens |
Model auto-migration (v0.0.413): Users previously on claude-sonnet-4.5 are automatically migrated to the current default model on startup.
Use /model to select a model and /context to see context window usage. Current models include:
- Claude Opus 4.6
- Gemini 3 Pro
- GPT-5.3-Codex
- GPT-5 mini
Model availability may vary by Copilot subscription tier.
Auto-Compaction
When context reaches ~95% capacity, Copilot automatically compresses history to continue the session.
Hands-On Exercises
Exercise 1: Monitor Context Usage
Goal: Learn to track context consumption.
Steps:
-
Start a fresh session:
copilot -
Check initial context:
/contextYou'll see:
- Total tokens available
- Tokens used
- Percentage filled
- Breakdown by category
-
Have a conversation that uses context:
Explain the concept of dependency injection -
Check context again:
/context -
Read some files:
Show me the contents of package.json -
Check how file reading affects context:
/context -
Continue building context:
Now explain how TypeScript interfaces workGive me examples of generics in TypeScript -
Monitor the growth:
/context
Expected Outcome: You understand how different actions consume context.
Exercise 2: Manual Compaction
Goal: Use /compact to compress session history.
Steps:
-
Continue from Exercise 1 or start a session with significant history.
-
Check current context:
/context -
Run manual compaction:
/compact -
Check context after compaction:
/context -
Notice:
- Token count decreased
- Core information preserved
- Detailed conversation history summarized
-
Verify context was preserved:
What were we discussing earlier?Copilot should remember the key topics.
Expected Outcome: Context reduced while preserving important information.
Exercise 3: Context-Efficient Prompting
Goal: Learn to use context efficiently.
Steps:
-
Inefficient approach (uses lots of context):
copilotRead all the files in the src directory and tell me what each one doesThis loads all files into context at once.
-
Check context:
/context -
Start a new session with efficient approach:
copilotList the files in src directoryThen selectively:
Show me just the main entry point file -
Compare context usage:
/context -
Best practices for efficient context:
- Load files on-demand, not all at once
- Use specific queries instead of broad exploration
- Compact regularly during long sessions
- Clear context when switching topics
Expected Outcome: You can manage context efficiently.
Exercise 4: Working with Large Codebases
Goal: Strategies for large projects without exhausting context.
Steps:
-
Use the Explore agent for overview:
copilot@explore give me an overview of this codebase structureThe Explore agent doesn't pollute main context. As of v0.0.414, it can also use GitHub MCP tools when available.
-
Focus on specific areas:
I want to understand the authentication flow. What files should I look at? -
Read selectively:
Show me only the auth middleware file -
Use grep instead of reading entire files:
Search for "authenticate" in the codebase -
Clear when switching tasks:
/clearNow let's look at the database layer -
Leverage file-specific questions:
In src/db/connection.ts, how is the connection pool configured?Copilot reads only what's needed.
Expected Outcome: Large codebases manageable without hitting limits.
Exercise 5: Context Window Optimization
Goal: Configure for optimal context usage.
Steps:
-
Choose the right model:
copilot/modelSelect a model with larger context if available.
-
Use specific instructions:
Instead of:
Tell me everything about this fileUse:
What does the processPayment function in payment.ts do? -
Batch related questions:
Instead of:
What does function A do?
What does function B do?
What does function C do?Use:
Explain functions A, B, and C in auth.ts -
Use references instead of copies:
Look at the PaymentService class - don't repeat the code, just explain the flow -
Summarize when appropriate:
Summarize what we've learned so far in 3 bullet pointsThen clear and continue with the summary as context.
Expected Outcome: Maximum utility from available context.
Exercise 6: Understanding Auto-Compaction
Goal: See auto-compaction in action.
Steps:
-
Start a session and fill context deliberately:
copilot -
Generate lots of content:
Write a detailed explanation of microservices architecture with examplesNow explain event-driven architecture in detailCompare REST vs GraphQL with code examples for both -
Continue until you see auto-compaction trigger:
Explain the CQRS pattern with implementation details -
Watch for the auto-compaction message.
-
After compaction:
/context -
Verify continuity:
What architectural patterns have we discussed?
Expected Outcome: Auto-compaction preserves session continuity.
Exercise 7: Context-Aware Workflow
Goal: Build a workflow that manages context intelligently.
Steps:
-
Phase 1: Discovery (low context)
copilot@explore what are the main components of this application? -
Phase 2: Focus (targeted context)
/clearLet's focus on the API layer. Show me the main router file. -
Phase 3: Deep dive (specific context)
I want to add a new endpoint. Show me an existing endpoint as a template. -
Phase 4: Implementation (working context)
Create a new endpoint for user preferences based on that pattern -
Phase 5: Cleanup (compact)
/compactNow let's write tests for the new endpoint -
Monitor throughout: After each phase:
/context
Expected Outcome: Systematic workflow keeps context under control.
Context Commands Reference
Slash Commands
| Command | Description |
|---|---|
/context | Show detailed context usage |
/compact | Compress session history |
/clear | Clear all context (start fresh) |
/cwd | Change working directory (affects context scope) |
/add-dir | Add directory to accessible scope |
Context Categories
| Category | Description | Impact |
|---|---|---|
| System | Instructions, agents | Fixed overhead |
| History | Conversation turns | Grows with conversation |
| Files | Read file contents | Can be large |
| Tools | Command outputs | Varies by tool |
| Response | Space for answer | Reduces with context |
Optimization Strategies
| Strategy | When to Use |
|---|---|
/clear | Switching to unrelated topic |
/compact | Long session, need to continue |
@explore | Codebase overview without context cost |
| Selective reading | Large files, specific needs |
| Summarization | Preserve knowledge, reduce tokens |
Context Usage Tips
Do ✅
- Check
/contextregularly - Compact before running out
- Clear when switching topics
- Use targeted queries
- Leverage
@explorefor overview
Don't ❌
- Read entire codebase at once
- Ask broad questions in large projects
- Let auto-compaction be your only strategy
- Repeat information already in context
- Ignore context warnings
Summary
- ✅ Context is limited, monitor with
/context - ✅
/compactcompresses while preserving key info - ✅
/clearresets for topic changes - ✅ Auto-compaction triggers at ~95% capacity
- ✅ Efficient prompting extends useful session length
- ✅
@exploreagent preserves main context
Next Steps
→ Continue to Module 12: Advanced Topics