Claude Code Repeats Context Compaction After Large File Reads

Claude Code context compaction can keep repeating when the next file read or tool result fills the space that the previous summary just freed. The same pattern can also come from a large set of instructions loaded into the session. The useful fix is to identify what returns after compaction, then reduce that input or change how Claude retrieves it.

Start with /context, inspect the activity immediately before the next compaction, and replace broad reads with a targeted search and a small relevant excerpt. Preserve the task's decisions before clearing the conversation. Repeating /compact without changing the next operation can reproduce the same problem.

Why Claude Code Context Compaction Keeps Repeating After Large File Reads
This guide covers Claude Code. It separates ordinary context maintenance from a session that repeatedly summarizes, rereads, and fails to move on to an edit or a useful answer.

Why a successful compaction may not solve the problem

The context window is the information available to the model for a request. It includes more than your typed messages: file contents, command results, instructions, and other loaded material compete for space. A repository can be enormous without filling the window; what matters is the material actually brought into the conversation.

Claude Code manages a growing session by clearing older tool output and, when necessary, summarizing the conversation. That summary supports continued work, but it does not make subsequent file reads free. If the next action brings back a large amount of raw content, the available space can disappear again.

For background, Ars Technica's coverage of longer Claude conversations explains the broader shift toward summarizing lengthy chats. That historical product coverage is separate from the Claude Code troubleshooting steps below.

One compaction during a substantial task is not, by itself, a failure. The stronger warning is repetition without progress: the same input returns, another summary follows, and no new finding, patch, or verification result appears.

First, find what is refilling the context

Run /context and record the breakdown before repeating the troublesome operation. If you start a fresh conversation for comparison, inspect it before loading the large file. This distinguishes a large starting load from growth caused by the investigation.

Observed patternNext check
Messages grow sharply after a read or commandInspect that operation's returned content.
Non-message categories dominate a fresh sessionInspect instructions, extensions, and other startup context.
The same file range is read repeatedlyCheck whether each reread answers a new question.
Compaction reports a failure instead of finishingInvestigate the actual error, rather than assuming successful compaction followed by refill.

Current Claude Code documentation describes an Autocompact is thrashing error: after repeated rapid refills, the application stops retrying. Its recovery guidance distinguishes a large Messages category from excessive context outside that category. Older issue reports describing endless loops do not establish how your installed version behaves.

Keep a short observation record: the last operation, its file or command, whether compaction completed, and what happened next. A transcript showing the same sequence is more useful than a screenshot of the compaction label alone.

1. Interrupt the repetition and narrow the next question

In the terminal interface, press Esc to interrupt Claude and redirect the task. Do this when the repeated activity is no longer producing useful evidence, rather than waiting for another identical cycle.

Replace an instruction such as “read the entire project and fix authentication” with a bounded investigation. For example:

Investigate why an expired session remains accepted.
Search src/auth for the session validation function first.
Read its implementation and the directly relevant test.
Do not dump generated files or the full test log.
Return the failing condition and the smallest proposed change.

The paths and symptom above are illustrative; substitute the actual component and failure. The aim is to make the next read answer one question. If Claude needs a dependency, it should identify why that dependency matters before expanding the investigation.

2. Search first, then read the relevant region

Claude Code's Read tool supports offset and limit for partial reads. Ask for a function, configuration block, or line range instead of repeatedly requesting the whole file. Its search tools can also return filenames or scoped matches before loading source content.

If you use ripgrep in your own terminal, a focused search might look like this:

rg -n -m 8 -F "refreshSession" src/auth/session.ts

Here, the filename and symbol are examples. The command finds up to eight matching lines in that one file. Use the matches to choose an excerpt containing the function and enough surrounding code to understand it.

Smaller reads only help if the selection changes. Reading every part of the same enormous file into the main conversation can still accumulate a large amount of context. Start with the relevant region, and expand only when an unresolved dependency requires it.

Line counts can also mislead. A minified bundle or one-line JSON export can contain substantial text on a single line. Prefer the original source for a bundle; for structured data, extract the required keys, records, or summary statistics instead of printing the complete object.

3. Reduce command output as well as file reads

A test run, build, repository-wide search, or data-processing command can return more text than the source file under investigation. Shortening Read calls will not help if the next command dumps the same volume through another tool.

For verbose jobs, keep the complete output in a local log and inspect a bounded result: the exit status, failing test names, and a relevant error excerpt. A focused test can then check the suspected fix. Preserve the full log for follow-up instead of repeatedly pasting it into the conversation.

Do not confuse shorter terminal display with smaller model context. Claude Code documents cases where a large table is visually cut off while its full content remains in the conversation. What matters is the payload retained for the model, not how many rows you can currently see.

4. Check instructions that load again after compaction

Project-root CLAUDE.md is read again after compaction. Nested instruction files and path-scoped rules reload when Claude reads matching files. Consequently, a large instruction set can compete with the source code even when the latest read is modest.

Use /context to inspect loaded memory files and /memory to review their locations. Keep broadly applicable instructions concise. Put specialist guidance in appropriately scoped rules or material loaded when needed, while preserving essential project constraints.

Splitting a large CLAUDE.md into files that it imports with @path does not itself reduce the load: imported material is still loaded. Organization and context reduction are different goals.

Also inspect custom hooks. A SessionStart hook can match compact, and eligible hook output can add context. If that hook emits extensive repository notes or diagnostic results after every summary, it is a candidate for investigation. Compare its actual output before changing it; do not assume every hook is responsible.

Trim duplication or narrow the emitted material rather than removing project checks indiscriminately. Keep a copy of any configuration you change so that you can restore it if the comparison does not help.

5. Preserve decisions before compacting or starting fresh

A useful handoff records what the next attempt needs to do, rather than recreating the entire investigation. Ask Claude to write a short task note, or prepare one yourself if the session is no longer responding reliably.

Task: Fix expired-session validation.
Relevant files: src/auth/session.ts and its focused test.
Confirmed finding: [brief observation supported by the output].
Changes already made: [paths and purpose, or none].
Verification: [command and observed result].
Next action: [one bounded step].
Preserve: [important compatibility or scope constraints].

Those brackets are fields to fill with your actual findings, not suggested facts about your project. Check the note against the current files. Include symbol names and paths so the next session can retrieve exact details without carrying every earlier excerpt.

Then use a focused compaction instruction, such as:

/compact Preserve the task, confirmed findings, modified paths, verification results, and next action. Omit raw logs and full file contents.

Review the resulting state before resuming. If the old conversation is no longer useful, /clear starts a new conversation. Explicitly tell the new session to read the handoff note; a custom filename does not make it automatically loaded. Clearing conversation context does not undo edits already made to your project files.

6. Isolate genuinely large investigations

A subagent can inspect a self-contained area in its own context window and return findings to the main conversation. This is useful when exploration produces substantial output that implementation will not need in full.

Specify the return format: relevant paths, the responsible function, evidence for the finding, and unresolved questions. Ask for a concise report rather than copied source files or complete logs.

Delegation has limits. The subagent still consumes usage, and its returned report enters the main conversation. Several lengthy reports can recreate the pressure you were trying to avoid. For a small change in code already understood, keeping the work in the main session may be simpler.

7. Check custom compaction settings and software versions

The auto-compact window and the model's maximum context window are not always the same. On Claude Code v2.1.221 or later, /autocompact shows the configured window. Inspect it if compaction happens earlier than you expect.

If you previously set a custom value, /autocompact auto requests the window tuned for the model. Record the old value first. Higher-priority configuration or CLAUDE_CODE_AUTO_COMPACT_WINDOW can affect the result, so check what the command reports. Raising a threshold cannot make the model accept unlimited input.

For a persistent problem, record claude --version, the model, and your interface. Use the update method appropriate to your installation: native installations support claude update, while package-manager installations should follow their package's update process. Restart after updating before comparing behavior.

If a small, focused reproduction still stalls, keep its exact steps and error for a bug report. Include whether the problem occurs before any substantial file read. Review logs before sharing them, since they may contain private code or configuration.

How to tell whether the change worked

Repeat one representative task with the narrower workflow. Look for useful progress after reading: a specific finding, an appropriate edit, or a completed check. Compare the context breakdown at similar points in the task.

For example, if a full-file read previously triggered immediate compaction, test a relevant function excerpt without simultaneously changing the model, instructions, and extensions. If that works, you have evidence that the original read pattern contributed. If it still fails before substantial input, investigate the starting context or the reported error.

The target is enough room to complete the next meaningful step. A long task may still compact occasionally and work correctly.

Frequently asked questions

Does a compaction warning mean I used up my subscription allowance?

No. Context capacity and account usage limits are separate. A compaction warning concerns the current conversation; a usage-limit message concerns the allowance or budget named in that message. Diagnose the message you actually received.

Does prompt caching stop large files from occupying context?

No. Prompt caching can reduce the cost of processing repeated content, but it does not make that content disappear from the model's input. A cached large prompt can still be large.

Should I stop Claude from rereading files entirely?

No. Rereading can be necessary after a file changes or when exact implementation details are needed. The unproductive pattern is rereading the same large, unchanged material without a new reason. Request a targeted reread instead of a blanket ban.

Will a larger context window fix every compaction loop?

It may provide more room when available for your configuration, but it does not correct an investigation that continually adds unnecessary content. Verify the actual session window and keep the input focused.

Do Open WebUI compaction settings apply to Claude Code?

No. These applications manage context separately. If your conversation is running in Open WebUI, follow the Open WebUI compaction guide for its configuration and trigger checks instead of copying Claude Code commands.

Choose the next action from the evidence

If one operation causes the refill, narrow that read or output. If a fresh session is already crowded, reduce unnecessary startup material. If both are modest and the same failure remains reproducible, investigate the version and error. Keep the next task small enough to finish, and retain a concise record of what has already been established.