本文へスキップ
Pathrule

記事

記事
エンジニアリング
/Sertan Helvacı/7分で読めます

このページは英語でのみご覧いただけます。

What Survives a Claude Code Compaction?

Claude Code's docs list what it reloads after a compaction, mechanism by mechanism.

Read against that list, the rule that vanished an hour ago stops being a mystery, and the fix is usually a change of where it lives.

結論

Claude Code re-reads the project-root CLAUDE.md, unscoped rules, auto memory's MEMORY.md and the plan-mode plan from disk, and re-attaches invoked skills up to 5,000 tokens each. Everything said only in the conversation is reduced to a summary. Rules with `paths:` and nested CLAUDE.md files return only when Claude touches a matching file again.

Pathrule
Pathruleは、あなたとワークスペース、そしてコーディングエージェントをつなぐインテリジェンスレイヤーです。

この記事の内容

  • Rules with paths: frontmatter and CLAUDE.md files in subdirectories are summarized away and reload only when Claude next reads, writes or edits a matching file; Claude Code's docs advise dropping paths: or moving the rule to the root CLAUDE.md if it must persist.
  • Invoked skills are re-attached within 5,000 tokens per skill and 25,000 tokens in total, oldest dropped first, and truncation keeps the start of SKILL.md; the files a skill read are not re-attached with it, since Claude Code re-reads only up to five of the session's most recently modified files.
  • Instructions given only in the conversation, and context that hooks added earlier, survive only as far as the summary keeps them; a SessionStart hook with the compact matcher runs after every compaction and adds its output to the new context.
  • Idle compaction, on by default in interactive sessions, compacts a conversation whose context has passed 200,000 tokens after about 54 minutes idle on the one-hour prompt cache; setting idleCompaction to false turns it off.
  • Codex compacts with /compact and an automatic token threshold, lets compact_prompt override the prompt the summary is written with, and runs SessionStart hooks matching the compact source before the next model request.
  • One developer on r/ClaudeCode reported that after 62 compactions the summary held 0 of 12 older decisions, their memory files about 8, and 4 were lost entirely.
  • Pathrule files decisions, rules and skills at the path they apply to, outside the conversation; on Claude Code and Codex its hooks deliver them when a prompt is submitted and before a tool call, and a strict rule with an approved block pattern stops a matching edit whatever the summary kept.

比較

WhatAfter a compactionIf it has to stay
Project-root CLAUDE.md and rules without paths:Re-read from diskNothing to do
Auto memory (MEMORY.md)Re-read from disk; its topic files only when Claude opens themKeep the must-know line in MEMORY.md itself
The plan from plan modeRe-read from diskNothing to do
Rules with paths: and nested CLAUDE.md filesGone until Claude touches a matching fileDrop paths:, or re-inject it with a compact SessionStart hook
Files Claude read or editedUp to five re-read, most recently modified first; over 5,000 tokens as a path onlyAsk Claude to read the file again
Invoked skillsRe-attached: 5,000 tokens each, 25,000 in total, oldest droppedPut the must-follow lines at the top of SKILL.md; invoke it again
What a skill read when it startedNot re-attached with the skillInline small reference files into SKILL.md
Decisions and corrections said in chatOnly what the summary keptWrite them to the root CLAUDE.md or a file a compact hook re-injects
Context that hooks added earlierSummarized with the conversationHave the hook also match compact
Background commands and subagentsKeep runningNothing to do

Before a long session compacts

  • Every decision that must outlive the session is in the root CLAUDE.md or an unscoped rule, with its date and its reason.
  • The root CLAUDE.md has a Compact Instructions section that names what a summary must keep.
  • A SessionStart hook with the compact matcher re-injects your short decision log.
  • Each skill's must-follow lines sit at the top of SKILL.md, and small reference files are inlined.
  • A PostCompact hook appends compact_summary to a log, so you can check what a summary dropped.
  • You know whether idle compaction is on, and set "idleCompaction": false if you would rather keep the full history.

The rule was there before the summary

Two hours into a session you told Claude Code to leave the generated API client alone and change the OpenAPI spec instead. It did, for an hour. Then a line about compacting the conversation scrolled past, and the next fix went straight into src/generated/client.ts. Nothing in your setup changed. The instruction had lived in the conversation, and the conversation is now a summary.

One developer on r/ClaudeCode counted this in a session that had compacted 62 times. They listed 12 decisions made about ten compactions back: the current summary held 0 of them, their memory files held about 8, and 4 were nowhere. The most recent stretch was fine, 20 of 21 items. It is one person's count, not a benchmark, but it matches Anthropic's own warning: the summary keeps your requests and key code snippets, while detailed instructions from early in the conversation may be lost.

The useful part is that Claude Code publishes what it reloads after a compaction, mechanism by mechanism. Read against that list, most "it forgot" moments stop looking random. They come down to where the instruction lived.

What Claude Code reads back from disk

Anything Claude Code loaded from a file at startup is read again, not summarized. The project-root CLAUDE.md and rules without paths: frontmatter are re-injected from disk, and so is auto memory. The system prompt and an output style still apply. The plan Claude wrote in plan mode is re-injected from disk, skill descriptions are rebuilt from your current skills (Claude Code v2.1.304 or later), and if the session started with a git status snapshot, a fresh one is read from the repository.

Two details matter. Auto memory means MEMORY.md, of which the first 200 lines or 25KB load; the topic files it points to are read on demand with the normal file tools, so after a compaction they are once again files Claude has to think of opening. And re-read from disk means the file as it is now, so a line you added to CLAUDE.md mid-session is part of what comes back.

Background commands and background subagents keep running through a compaction, and Claude Code reminds Claude which ones are still going so it does not start a duplicate.

What comes back only when Claude touches the file again

Rules scoped with paths: and CLAUDE.md files in subdirectories work differently. They load into the message history when Claude reads, writes or edits a file that triggers them, so a compaction summarizes them away with everything else. They return the next time Claude touches a matching file.

That leaves a gap. If Claude plans the next change to services/billing from the summary before it opens a billing file, the billing rule is not in front of it while the plan is made. The docs' own advice for a rule that must persist is to drop the paths: frontmatter or move it to the root CLAUDE.md. That works, and it also means every session in every folder now carries that rule.

Files are a smaller version of the same thing. Claude Code re-reads up to five of the files Claude has read or edited, the most recently modified first, and a file over 5,000 tokens comes back only as a path, shown as Referenced file. The schema you read at the start of the session and never changed is back only if it is one of those five.

Skills come back cut short

An invoked skill is re-attached after a compaction, but within a budget: 5,000 tokens per skill and 25,000 tokens across all of them, filled from the most recently invoked. A long skill keeps only its start, and an older skill can drop out entirely once the total is spent. Invoking the skill again restores its full content.

This is the pattern behind threads like "after compaction it would just... stop." The skill text is often fine. What is missing is what the skill read when it started: a profile, a style guide, a reference file. Those were tool results, and they are not re-attached with the skill.

The docs' fix is to put the most important instructions near the top of SKILL.md, since truncation keeps the start. Inlining a small reference file into the skill does the same job for what it used to read.

Everything said in the chat becomes a summary

The rest of the session is summarized: your messages, Claude's answers, tool results, and also context that hooks added earlier. A hook that injected your conventions at startup is summarized like anything else, unless it also runs on the compact source. A correction like "not that library, we dropped it in March" is one sentence in a long transcript, and the summary keeps what the summarizer judged important.

A compaction can also happen while you are away. With idle compaction, on by default in interactive sessions, a conversation whose context has passed 200,000 tokens and that sits idle for about 54 minutes on the one-hour prompt cache is compacted before the cache expires. In the best-practices page's words, after lunch you continue from a summary. Set "idleCompaction": false in settings if you would rather pay for a cache rebuild and keep the full history.

How to keep what matters

Most fixes move the instruction out of the conversation and into a file Claude Code re-reads or a hook it runs after the summary.

For decisions, write the ones that must outlive the session where Claude Code re-reads them, the root CLAUDE.md or an unscoped rule, with the date and the reason. A Compact Instructions section in the root CLAUDE.md tells every summary what to keep, and before a long new task /compact focus on the billing migration beats letting the automatic pass guess.

For anything longer, or anything that changes, a SessionStart hook with the compact matcher runs after every compaction and adds its output to the new context. cat .claude/decisions.md or git log --oneline -5 there brings back a short file without making every session start heavier.

To see what you lost, a PostCompact hook receives the summary as compact_summary: append each one to a log and check, the way the r/ClaudeCode count did by hand, which decisions made it. After a compaction, /context shows which CLAUDE.md and memory files are loaded, and the InstructionsLoaded hook reports files re-loaded with the load reason compact. A PreCompact hook can block an automatic compaction with exit code 2, but when the compaction was triggered to recover from a context-limit error, blocking it makes the request fail.

Codex: the same hooks, no published list

Codex compacts too. /compact replaces earlier turns with a summary, model_auto_compact_token_limit sets the threshold for the automatic pass, and compact_prompt overrides the prompt the summary is written with, the closest thing to Claude Code's Compact Instructions.

Codex builds its AGENTS.md chain once per run, and its docs do not publish a list of what is re-sent after a compaction the way Claude Code's do. What they do document is the hook: SessionStart hooks matching the compact source run before the next model request, also when automatic compaction happens in the middle of a turn. If something must be back after a Codex compaction, that hook is the documented way to be sure.

Knowledge that never lived in the conversation

Every fix above has the same shape: the instruction stops depending on the conversation. For one person on one project, a short root CLAUDE.md plus a compact hook is genuinely enough. The cost shows up later, when the root file fills with rules that matter for one folder, because the root file is the place that survives.

This is the problem Pathrule was built around. Decisions, rules and skills are filed at the path they apply to, outside every conversation, so a compaction has nothing of theirs to summarize away. On Claude Code and Codex, Pathrule's hooks hand the agent what is filed for a folder when you submit a prompt and right before it uses a tool, and a strict rule with an approved block pattern is checked in the hook before the edit runs: a hand edit to src/generated/ is stopped whether or not the summary kept the sentence. On Cursor, Copilot and Windsurf the same knowledge arrives over MCP as guidance, not a block.

It does not make the summary keep more. A decision that exists only as a sentence in chat is still a sentence in chat until someone files it.