Skip to main content

Context Management Best Practices for Agents

Purpose: Proactive token usage optimization through strategic compacting and subagent isolation

Overview

Claude Code agents have access to 200K token context windows, but long-running tasks can accumulate significant context. This guide provides strategies for managing context efficiently.

Two Primary Strategies

1. Manual Compacting (/compact command)

Summarize and compress conversation history at strategic points

2. Subagent Isolation (Task tool)

Spawn independent agents with fresh context windows

Strategy 1: When to Recommend /compact

Trigger Points

Recommend compacting to the user when: Transitioning Between Major Phases
Before Agent Handoffs
After Large Code Changes
Context Exceeds 100K Tokens
Starting Complex Analysis

When NOT to Compact

Mid-Implementation - Lose important working context ❌ During Active Debugging - Need full error trace history ❌ Iterating on Specific Feature - Context is still relevant ❌ Just Started Session - No accumulated context yet

How to Recommend

Use this format when recommending:

Strategy 2: Use Subagents for Context Isolation

When to Use Subagents

Use the Task tool instead of continuing in same context when: Independent Work Modules
Different Agent Roles
Exploratory Analysis
Isolated Testing
Large Document Generation

Subagent Benefits

  • Fresh Context: Each subagent starts with clean 200K tokens
  • Focused Work: Subagent only sees relevant task details
  • Natural Summary: Parent gets concise summary when subagent completes
  • Parallel Potential: Can spawn multiple subagents for different tasks
  • Automatic Cleanup: Subagent context discarded after completion

Subagent Usage Pattern

Pattern 1: Isolated Implementation
Pattern 2: Research/Analysis
Pattern 3: Agent Handoffs

Context Management in Agent Workflows

Add to All Agents

Each agent should include a context management section:

Implementation Guidelines

For Agent Developers

When updating agents with context management:
  1. Add Section: Include “Context Management” section after main workflow
  2. Strategic Points: Identify natural breakpoints in agent workflow
  3. Recommend, Don’t Command: Suggest /compact, don’t require it
  4. Provide Context: Explain WHY compacting would help
  5. Subagent Patterns: Show WHEN to use Task tool vs continue

Template for Agents


Measuring Context Usage

Rough Estimates

  • Average message: ~500-1000 tokens
  • File read (200 lines): ~1500-2000 tokens
  • Code generation: ~2000-5000 tokens per feature
  • Large analysis: ~10000-20000 tokens

High Context Indicators

You’re likely over 100K tokens if:
  • Read 20+ files in conversation
  • Generated multiple large features
  • Had extensive back-and-forth debugging
  • Performed multiple codebase searches
  • Generated comprehensive documentation

Best Practices Summary

✅ Do

  • Recommend compacting at natural breakpoints
  • Use subagents for isolated work
  • Preserve key decisions when compacting
  • Explain benefits to user
  • Monitor for high context scenarios

❌ Don’t

  • Recommend compacting mid-implementation
  • Overuse compacting (not every task)
  • Use subagents for trivial tasks
  • Interrupt user flow unnecessarily
  • Mandate compacting (always suggest)

Examples

Example 1: Phase Transition

Example 2: Using Subagent

Example 3: Agent Handoff


Monitoring and Optimization

Track These Metrics

  • How often compacting is recommended
  • User acceptance rate of recommendations
  • Subagent usage patterns
  • Context-related performance issues

Iterate Based On

  • User feedback on recommendation frequency
  • Actual token usage patterns
  • Agent performance after compacting
  • Subagent success rates

Summary

Context management is about balance:
  • Use /compact at strategic phase transitions
  • Use subagents for naturally isolated work
  • Recommend, don’t mandate
  • Explain the “why” to users
  • Keep parent context clean and focused
Goal: Optimize token usage without disrupting workflow or losing important context.