Claude Code

Claude Code's context window: what survives compaction?

TL;DR

Not everything survives compaction: root CLAUDE.md and auto memory come back, path-scoped rules don't, and long skills lose their end. Put important rules in the right place, compact with a focus at natural breaks, and check with /context.

A session runs long, Claude Code "compacts" automatically, and you notice it forgot a rule you'd given it, or the contents of a skill it just used. That's not random: each kind of content is loaded into the context window in a different way, so each survives compaction differently. Understanding this lets you put instructions in the right place and keep tokens under control.

What's in context before you type anything?

  • The system prompt and tool definitions.
  • CLAUDE.md (or AGENTS.md) and auto memory.
  • MCP tool names and skill descriptions.
  • An output style and --append-system-prompt text, if any.

As Claude works: each file read adds to context; rules with paths: load when a matching file is touched; a PostToolUse hook can add content after each edit. A subagent does research in its own context and returns only a summary.

What survives compaction?

ContentAfter compaction
System prompt, output styleStill apply
Project-root CLAUDE.md, unscoped rules, auto memoryRe-injected from disk
Rules with paths:, subdirectory CLAUDE.mdLost; reloaded when a matching file is touched
The plan written in plan modeRe-injected from disk
Files read/editedUp to 5 most recently modified are re-read; files over 5,000 tokens come back as a path only
Invoked skill bodiesRe-injected, capped at 5,000 tokens per skill and 25,000 total; oldest dropped first
Context added by hooksSummarised with the conversation

Two practical consequences: a rule that must survive compaction should drop paths: or move to the root CLAUDE.md; for long skills, put the most important instructions at the top of SKILL.md, since truncation cuts the end.

Act before context fills up

  • /compact focus on the auth bug fix: a focused compaction whose summary keeps what you need.
  • /rewind → "Summarize from here" or "up to here": summarise only part of the conversation.
  • /autocompact 500k: set the auto-compact threshold earlier.
  • /clear when switching to unrelated work; old conversation both crowds context and costs tokens every turn.
  • Delegate big file reads to a subagent so the content stays out of your main context.
  • /context: see the actual breakdown by category, with optimisation suggestions.

Risks and caveats

  • A 1-million-token window doesn't replace discipline: some models support 1M context, but longer context is pricier and more diluted.
  • Compacting with a cold cache costs the most: compact at natural breaks between tasks, while the cache is warm.
  • Hook-added context doesn't persist: to re-inject after compaction, use a SessionStart hook matching the compact source.

How to apply it

  • Dev: Run /context when Claude seems to "forget"; check where that rule lives and whether it survives compaction.
  • Tech Lead: Review your team's long skills and move core instructions to the top; move mandatory rules out of paths:-scoped rules.
  • PM: For long multi-phase sessions, ask for plan mode first: the plan is re-injected after every compaction.

Sources & references

  1. Explore the context window — Claude Code Docs