🎯Core

Context Strategies

Four approaches to managing context: write, select, compress, isolate.

The Four Pillars

Claude Code applies four context engineering strategies that work together to keep the agent effective across long sessions. These same principles apply to any agentic system you build.

Four Context Engineering Strategies
✍️
Write
Long-term memories
Scratchpad (session)
State (session)
🔍
Select
Retrieve relevant tools
Retrieve from memory
Retrieve knowledge
🗜️
Compress
Summarize context
Trim irrelevant tokens
Retain key decisions
🧊
Isolate
Partition in sandbox
Hold in environment
Multi-agent scoping

Strategy 1: Write Context — Persistent Memory

The first strategy is writing context to a persistent memory architecture. Claude Code implements a three-tier memory hierarchy that persists context across sessions:

  • Project memory (./CLAUDE.md) — shared context for the entire team: architecture, standards, conventions. Version-controlled and available to all collaborators.
  • User memory (~/.claude/CLAUDE.md) — personal preferences, code style, workflow shortcuts. User-specific, not committed to the repository.
  • Dynamic memory imports — reference other memory files using the @ symbol syntax. Allows dedicated context files to be loaded on demand.
markdown
# Project Context (./CLAUDE.md)

## Architecture Overview
This is a microservices application using:
- Node.js with Express for API services
- React with TypeScript for frontend
- PostgreSQL with Prisma ORM

## Coding Standards
- Use functional components with hooks
- Implement error boundaries for all route components
- Follow RESTful API conventions

Strategy 2: Select Context — Intelligent Retrieval

The second strategy is intelligent context retrieval through dynamic discovery. Claude Code automatically looks through folders to find relevant context files, pulls context from parent folders, and prioritizes recently used information. Users can also add persistent context with the /memory command.

💡Tip

Different tools trigger different context retrieval. When editing a file, the system checks existing code style. When running a command, it checks for existing scripts. The context selected depends on the operation being performed.

Strategy 3: Compress Context

The third strategy is compression — efficient representation of accumulated context. Two built-in commands handle this:

CommandBehaviorWhen to Use
/clearResets conversation history completely, keeps project + user memoryStarting fresh, conversation went in wrong direction
/compactSummarizes conversation, keeps key decisions, drops minor detailsLong session, want to reduce cost/latency without losing progress

Strategy 4: Isolate Context — Subagents

The fourth strategy is context isolation through sub-agents. Each sub-agent runs in its own context window and does not inherit the full conversation history. This enables specialized agents — a code reviewer with security focus, a testing agent with test-writing expertise, a research agent with broad search capabilities — each working with only the context they need.

markdown
# .claude/agents/code-reviewer.md
---
name: code-reviewer
description: Reviews diffs for security and performance issues
tools: Read, Grep, Bash
---

You are a senior reviewer focused on security and performance.
For each issue: file, line, severity, fix.
bash
# Invoke from main session — runs in isolated context window
> @code-reviewer review the staged diff

# Or let main agent auto-delegate based on the subagent description
> review my last commit for security issues
ℹ️Info

Without isolation, an agent trying to do everything at once gets confused by too much context. With isolation, each specialist has a specific knowledge scope and uses it effectively.

Before you continue

We use analytics cookies to understand how the documentation is used and improve the experience. Privacy Policy.