Optimization

Claude Code Context Management Guide

Manage Claude Code context windows, long sessions, project memory, compaction, and handoffs without losing the current task.

Context management is how you keep Claude Code focused during long work. The practical goal is simple: keep the current task, relevant files, decisions, and verification steps visible, while moving old or unrelated details out of the active conversation.

Quick Answer

Use CLAUDE.md for stable project rules, /compact for long conversations, and a short handoff summary when switching tasks or sessions. Do not paste the whole repository into the chat. Ask Claude Code to inspect specific files and summarize only what matters for the current task.

ProblemBest response
Claude is losing the task goalRestate the objective, files, and success criteria.
The session is very longUse /compact or write a short handoff summary.
A new task is unrelatedStart a new session with a focused brief.
Claude keeps forgetting project rulesMove durable rules into CLAUDE.md.
The repo is largeAsk for a file map before editing.

What Belongs In Context

Keep active context narrow:

  • Current objective and constraints.
  • Files already inspected and why they matter.
  • Decisions already made.
  • Commands that prove the change works.
  • Risks that must not be ignored.

Move old discussion, abandoned options, unrelated logs, and completed implementation details out of the active conversation.

Start With A File Map

For large codebases, ask Claude Code to map the relevant area first:

Find the files involved in user onboarding. Return the route, UI components, server actions, database writes, tests, and risky assumptions. Do not edit files.

Then continue with a smaller prompt:

Based on that map, propose the smallest safe change to add a required company-name step.

This pattern avoids filling the session with unrelated files.

Use CLAUDE.md For Durable Memory

Use CLAUDE.md for instructions that should survive across sessions:

## Working Rules

- Public content pages live in `content/pages`.
- Tutorial documentation lives in `content/docs`.
- Preserve existing SEO URLs unless a redirect is explicitly planned.
- After content changes, run `npm run build`.

Do not use CLAUDE.md for temporary ideas, private secrets, or every detail from a single task.

Use Compacting For Long Sessions

When the session has a lot of exploration, logs, or failed attempts, compact the conversation before continuing:

/compact

After compaction, ask Claude Code to restate the active task:

Before continuing, summarize the current objective, changed files, remaining steps, and commands still running.

This makes drift easier to catch.

Handoff Template

Use this when starting a new session or switching threads:

Current objective:

- Migrate existing tutorial URLs without changing public routes.

Already done:

- Added nested MDX page support.
- Rebuilt the pricing, FAQ, MCP, and troubleshooting clusters.

Current constraints:

- Preserve `/en`, `/zh`, and `/tutorial/*` where possible.
- Do not modify auth, payment, or template services.

Next step:

- Review high-traffic docs pages for outdated claims and update official sources.

Verification:

- Run `npm run build`.
- Check key URLs with `curl -I`.

Common Context Failures

  • Starting implementation before Claude Code knows the relevant files.
  • Mixing multiple unrelated tasks in one long session.
  • Keeping old rejected plans in active context.
  • Assuming Claude Code remembers work from another thread without a summary.
  • Letting a generated plan become outdated after files change.

Official Sources


Next: Direction Correction - Recover when Claude Code starts moving in the wrong direction.