Lesson 18Memory

Context Management

Window optimization

Context Window

Context is Claude's "working memory". Everything Claude knows about the task must fit in context (~200K tokens for Claude Sonnet).

~10%
System prompt

Claude Code instructions

~5%
Memory (CLAUDE.md)

Project instructions

~40%
Chat history

Previous messages

~30%
Tool results

Read files, searches

~15%
Current task

Your request and context

Auto-compaction

When context fills up (~80%), Claude automatically compresses history:

Context 50% — normal operation
Context 80% — warning
Context 95% — auto-compaction

⚠️ Auto-compaction may lose details. Use /compact manually for control.

/compact Command

Use /compact for manual context compression:

> /compact
Compressing context...
Context compressed: 85% → 35%
Preserved: current task, important files
Removed: intermediate search results

💡 Use /cost to see current context usage

Sub-agent Context Isolation

Each sub-agent works in isolated context, which helps:

Main agent

Preserves overall task context

Sub-agent

Receives only necessary information

# Example:
Main context: task + important files
→ Task(Explore, "find auth files")
Sub-agent: only search instruction
Returns: brief result
Context Optimization

Use /compact regularly

Compresses history while preserving important info

Break large tasks

Each subtask — separate session

Avoid unnecessary files

Read only necessary code parts

Use sub-agents

Isolated context for subtasks

When to Start New Session

  • Task completed — start new session for clean context
  • Topic changes — don't drag old context
  • Context overflow — better to start fresh
  • Claude got confused — reset helps

Lesson Quiz

1 of 1

1.What happens when a Claude Code conversation approaches the context limit?

This lesson is part of a structured LLM course.

My Learning Path