Skip to content

How to fix AI agent context loss? ​

Your coding agent has forgotten a rule, a decision, or what it was doing before you stopped. This page separates three failures that look alike: losing track during a long session, starting again without the right context, and handing work to another agent. Each calls for a different first fix, so identify the boundary before changing settings. The sources below were read on 28 November 2026.

TL;DR

  • Inside one session: in Claude Code, put must-keep instructions in the root CLAUDE.md or unscoped rules, then use a focused summary to shorten the conversation (documentation).
  • Between sessions: check what loads at startup, including CLAUDE.md, AGENTS.md, and enabled memory.
  • Between agents: this page recommends writing shared decisions somewhere both agents read; a subagent returns a summary, not its full working history (documentation).
  • Shared rule: write what must survive outside the window and arrange for the agent to read it back, following Anthropic’s structured note-taking approach.

The three kinds are this page's own sorting; every tool behaviour is cited to the vendor's documentation, and the recommendations marked as this page's are ours.

Kind of lossWhat is lostFirst fix
Inside one long sessionEarlier information becomes harder to recall, or may not survive summarization (Anthropic).Put persistent rules outside chat and control the summary’s focus (Claude Code).
Between sessionsThe new session lacks context from earlier work (Claude Code).Check startup instructions and enabled memory (Claude Code, Codex).
Between agentsA summary may omit specific events or decisions (LangChain).Write shared facts outside the window and require both agents to read them; this is the page’s recommendation.

Compaction is summarizing a conversation nearing its context limit and starting a new context window with that summary (Anthropic).

Context rot is declining or uneven recall and task performance as input context grows (Anthropic, Chroma).

Which kind of context loss do you have? ​

Start with the symptom, not the memory setting:

  • It forgot something earlier in the same session. Inspect whether the conversation was summarized, then ask where the missing instruction was written.
  • It forgot after a restart. Check whether you resumed the old session or started a new one, and inspect what loaded.
  • Another agent does not know what the first one knew. Inspect the handoff and the information both agents can read.

Treat these as places to look, not proof of a cause. Before changing anything, check whether the missing instruction is absent from what the agent loaded or present but not followed. Claude Code shows the instruction files a session loaded under Memory files when you run /context all (memory). An instruction that is loaded but not followed is a different problem from one that was lost: Claude Code says how you write instructions "affects how reliably Claude follows them" (memory), and long contexts make recall harder in general, as the next section describes.

Inside one long session: the window fills and compaction summarizes ​

The problem can begin before summarization: Anthropic describes recall declining as tokens accumulate, while Chroma's evaluation across 18 LLMs reports nonuniform performance with increasing input length (Anthropic, Chroma). Compaction then introduces a selection step, and an overly aggressive summary may lose subtle but important context (Anthropic).

Claude Code summarizes long conversations automatically as they approach the window limit; the trigger depends on the model and configuration (context window). A full window therefore does not, by itself, end the session (context window).

What Claude Code reloads after compaction ​

The storage location matters:

  • Root CLAUDE.md and unscoped rules: “Re-injected from disk” (context window).
  • Rules with paths: and nested CLAUDE.md files: summarized with message history, then reloaded on demand when Claude reads, writes, or edits their trigger files (context window).
  • Context added by earlier hooks: “Summarized with the rest of the conversation” (context window).
  • Recently used files: "Right after compaction, Claude Code re-reads up to five of the files Claude has read or edited in the session, choosing the ones modified most recently" (context window).

The glossary says root CLAUDE.md and auto memory survive compaction, but instructions supplied only in conversation may not (glossary). Do not confuse a rule being reloaded from disk with its original message remaining in the active window.

System-prompt flags need separate attention ​

By default, Claude Code builds and records the system prompt on the conversation’s first request, incorporating system-prompt flags (CLI reference). Later requests use that recorded prompt until compaction, including after --resume or --continue (CLI reference).

If you resume without passing a rule supplied only by a first-launch system-prompt flag, that rule is gone from the rebuilt prompt at the first compaction unless passed again (CLI reference). Different flag text, or none, on the later launch takes effect at compaction or when starting a new conversation, not immediately on resume (CLI reference).

Documented Claude Code fixes ​

Choose the control that addresses the symptom:

  • Keep a rule across summaries. The docs say to “drop the paths: frontmatter or move it to the project-root CLAUDE.md” (context window).
  • Choose the summary’s focus. Use /compact with instructions about what to preserve: “The summary keeps what you choose instead of what the automatic pass guesses is important” (context window).
  • Summarize earlier. The documented control is “run /autocompact with a token count”; use it to compact earlier rather than assuming a universal trigger (context window).
  • Reload operational context. Use SessionStart hooks with source compact; Claude Code “runs them and adds their output to the compacted context” (context window).
  • Separate unrelated tasks. “Clear between tasks: run /clear when switching to unrelated work” (context window).
  • Keep large reads elsewhere. “Delegate large reads: send research to a subagent so the file contents stay in its context window, not yours” (context window).

A minimal example of the first fix. A rule written in the project-root CLAUDE.md is re-injected from disk after compaction, where the same sentence typed into the chat may not survive it:

markdown
# Project rules
- Run `npm test` before every commit.
- Never edit files under `generated/`.

After the next compaction, run /context all and check that CLAUDE.md is still listed under Memory files (memory).

There is also a narrower operation: Summarize in /rewind keeps you in the same session and compresses context like a targeted /compact (checkpointing). For that operation, the docs explicitly say the original messages stay in the session transcript and remain available for reference; this statement concerns /rewind Summarize, not automatic compaction (checkpointing).

Codex /compact is different from /recap ​

Codex describes /compact as “Summarize the visible chat to free tokens” and says it replaces earlier turns with a concise summary while keeping critical details (developer commands). Its documented use is after long runs, so Codex retains key points without exceeding the window (developer commands).

Use /recap only to catch yourself up: it generates a conversation summary “without compacting its context” (developer commands). To free context space, the docs direct you to /compact (developer commands).

If you are building your own agent ​

Anthropic’s platform describes context editing this way: “Context editing automatically clears stale tool calls and results from within the context window when approaching token limits” (context management). Treat that as a window-management control, separate from your decision about which task facts to persist.

Between sessions: a new session starts from what loads at startup ​

"Each Claude Code session begins with a fresh context window." Claude reads CLAUDE.md files "at the start of every session", and auto memory "lets Claude learn from your corrections without manual effort"; "Auto memory is on by default in local sessions" (memory).

Codex reads AGENTS.md before doing work (agent configuration). Its memories carry context into future work, but local memories are off by default; memories.use_memories controls whether existing memories are injected into future sessions (memories).

Resuming differs from starting fresh: Claude Code restores the saved conversation and state, including messages, tool calls, and results (sessions). If the session was compacted, it opens at a “Conversation compacted” row and continues with the last summary, messages left unsummarized, and subsequent messages; summarized messages are not loaded again (sessions).

The sessions guide says to pass launch flags again because Claude Code does not restore them (sessions). For system-prompt flags, the timing still follows the rule above: later-launch text takes effect at compaction or in a new conversation (CLI reference).

For the details, see what survives when AI agents restart and how to store agent state across restarts.

For your own agent, Anthropic describes structured note-taking as regularly writing notes outside the window and pulling them back later (context engineering). Its memory tool “enables Claude to store and consult information outside the context window through a file-based system” (context management).

Between agents: a handoff carries a summary, not the history ​

Anthropic describes specialized subagents working in clean windows and returning a “condensed, distilled summary” (context engineering). In Claude Code, large research reads stay in the subagent’s separate window; “Only the summary and a small metadata trailer come back” (context window).

That boundary reduces what the parent receives. LangChain, citing Cognition, describes summarization at agent boundaries to reduce tokens and warns that specific events or decisions can be challenging to capture (context engineering for agents).

This page’s recommendation is to write what both agents need into a file or store that both read. For a handoff, include the decision, its reason, unresolved questions, and the next action. Ask the receiving agent to read that record rather than relying only on the returned summary.

Use project instruction files for shared guidance. See shared memory for multi-agent systems when the requirement is shared working knowledge across agents or tools.

What to put where so it survives ​

Choose the destination by the information’s job. Do not make one memory mechanism responsible for everything.

InformationPut it hereWhy this location
A project-wide ruleThe root rules fileClaude Code re-injects root CLAUDE.md and unscoped rules; Codex reads AGENTS.md before work (Claude Code, Codex).
A fact the tool learned itselfThe tool’s memory, if enabledClaude Code auto memory is on locally by default; local Codex memories are off by default, and Codex calls memory a recall layer rather than the sole source of required rules (Claude Code, Codex).
The progress of a long taskNotes outside the windowAnthropic describes persisting notes and pulling them back into context later (context engineering).
What several agents or tools needA shared store both readThis page’s recommendation; see shared memory and instruction files.

A rules file is not enforcement: Claude Code treats CLAUDE.md as context, not enforced configuration, and says instruction wording affects how reliably it follows the guidance (memory).

What this page does not claim ​

No benchmark figure. The three kinds are this page's sorting. The tool behaviours cited are the vendors' documented features on the date in Sources, and the recommendations marked as this page's are ours; your version may differ. Which kind of loss you have is a diagnosis this page cannot make for you.

Common questions ​

How to fix AI agent context loss? ​

First identify the boundary: a long session, a restart, or an agent handoff. In Claude Code, keep persistent rules in the root CLAUDE.md and use focused summarization during long sessions; check startup instructions after a restart (context window, memory). For handoffs, this page recommends shared notes that both agents read, following the practice of writing context outside the window.

Fix AI agents forgetting context ​

Put project rules in CLAUDE.md or AGENTS.md, and check memory settings: Claude Code auto memory is on by default locally, while local Codex memories are off by default. For task progress, use notes that the agent writes outside the window and reads back later.

Why does my agent forget instructions after compaction? ​

Path-scoped rules and nested CLAUDE.md files are summarized with conversation history, then reload on demand when Claude touches their trigger files (context window). Root CLAUDE.md is re-injected from disk, but conversation-only instructions may not survive (context window, glossary). The recorded system prompt lasts until compaction, including across resume; different system-prompt flag text, or none, on a later launch takes effect at compaction or in a new conversation (CLI reference).

Does a subagent hand its full history back to the main agent? ​

No. Anthropic describes a subagent returning a condensed summary, and Claude Code says only the summary and a small metadata trailer come back. LangChain, citing Cognition, describes summarization at agent boundaries to reduce tokens, while warning that specific events or decisions can be difficult to capture.

Does a bigger context window fix context loss? ​

A bigger window helps by pushing compaction back; Claude Code says compaction works the same way at the larger limit. It does not remove recall problems: Anthropic describes declining recall as context length grows, and Chroma reports nonuniform performance with increasing input length. Anthropic also notes relevance concerns at every window size.

Sources ​

All sources below were read on 28 November 2026.

Context engineering and research ​

Claude Code documentation ​

  • Context window. Compaction controls, reloaded instructions, hooks, and subagent returns.
  • Glossary. Instructions and memory across compaction.
  • CLI reference. Recorded system prompts and flag timing.
  • Checkpointing. Summarize in /rewind.
  • Memory. Startup instructions, auto memory, and instruction reliability.
  • Sessions. Resume behavior and previously compacted conversations.

Codex documentation ​

Sources were read on 28 November 2026; the page describes them as of that date.

Who wrote this. Mnemoverse publishes this page; it is a persistent-memory API for AI agents, so we have an interest in the topic, and it is not offered here as a fix. Every tool behaviour is quoted or paraphrased from the vendor's documentation on the date in Sources; recommendations marked as this page's are ours. Send corrections through contact.

Edward Izgorodin · Mnemoverse · 28 November 2026