Long-running AI agents: just-in-time context, compaction, case facts and progress files
A long-running agent outlives its context window: the conversation gets compacted, sessions end, the next one starts with no memory. Four habits keep it on track. Load data just in time through pointers instead of preloading everything. Know what compaction keeps and loses. Keep exact facts (amounts, dates, IDs) in a block outside the summary. And save progress in files (a progress log, a JSON task list, git) that the next session reads first.
Video · Just-in-Time Context for AI Agents: Pointers, Not Payloads (CCAR-F) · 3:46
Video · Claude Code /compact: What Compaction Keeps and What It Loses (CCAR-F) · 4:34
Video · Case Facts Block: Keep Amounts, Dates and IDs Exact in Long LLM Chats (CCAR-F) · 4:32
Video · Long-Running AI Agents: Progress Files, Scratchpads, Git and Manifests (CCAR-F) · 4:41
Why long-running agents lose the thread
Context is "a finite resource with diminishing marginal returns", says Anthropic's guide to context engineering: every token spends some of the model's attention. A long task runs into that limit in three ways:
- The window fills up, so older turns get compacted into a summary.
- The session ends. "Each new session begins with no memory of what came before", like engineers on shifts.
- Details blur. Summaries keep the gist and lose the exact numbers.
Each problem has its own fix. For the basics of the window itself, see the LLM context window.
1. Load context just in time
Instead of preloading every file and record, give the agent pointers. Just-in-time agents "maintain lightweight identifiers (file paths, stored queries, web links, etc.) and use these references to dynamically load data into context at runtime using tools".
- In a codebase: start with Grep to find entry points, then Read to follow them, rather than reading every file up front.
- In Claude Code: CLAUDE.md loads at the start; skills, path-scoped rules and MCP tool definitions load only when needed.
- The trade-off: exploring is slower than preloaded data, and a badly guided agent can chase dead ends. The usual answer is a hybrid: a little up front, the rest on demand.
2. Know what compaction keeps and loses
Compaction is "taking a conversation nearing the context window limit, summarizing its contents, and reinitiating a new context window with the summary". In Claude Code it clears older tool outputs first, then summarizes; your requests and key code snippets survive, but detailed instructions from early on may not.
What to do about it:
- Rules that must last go in CLAUDE.md, which is reloaded from disk after compaction, not in the chat.
- Steer the summary:
/compacttakes focus instructions, such as "keep the refund amounts and booking IDs". - Start fresh when the state lives on disk. Anthropic's prompting guide notes that Claude's latest models are "extremely effective at discovering state from the local filesystem", so a new window that reads the progress files can beat a long compacted one.
3. Keep exact facts outside the summary
The exam guide names the risk precisely: summarization condenses "numerical values, percentages, dates, and customer-stated expectations into vague summaries". "The customer wants a partial refund" is useless when the facts were a $35 refund for booking B-3107's unused day, promised by staff.
The fix is a case facts block, kept outside the summarized history and sent with every prompt:
<case_facts>
customer: C-2817 (verified)
booking B-3107: e-bike, returned 2026-10-18, one unused day
promised by staff: $35 refund for the unused day
booking B-3112: city bike, no issue
</case_facts>
Your code fills it from tool results, for example with a PostToolUse hook, so the model never has to remember the numbers.
4. Save progress in files that outlive the session
For work that spans sessions, Anthropic's harness for long-running coding agents writes state to disk and reads it back first:
| File | Holds | Why this format |
|---|---|---|
progress.txt |
What was done, what's next, in plain words | Notes are easiest as text |
tests.json or a feature list |
Task status, test results | The model is less likely to rewrite JSON by mistake than Markdown |
| git history | Checkpoints with descriptive commits | Revert bad changes, recover a working state |
init.sh |
How to start the app and run the tests | No repeated setup each session |
Each new session starts the same way: read the progress file and git log, run a basic test, pick the next unfinished task, and leave things clean. Two related patterns from the exam guide:
- Scratchpad files of key findings, consulted for later questions instead of relying on a long context.
- Manifests that each agent exports, so a coordinator can resume a multi-agent job after a crash instead of starting over.
Claude's memory tool works the same way, with its own reminder: "ASSUME INTERRUPTION: Your context window might be reset at any moment".
Long-running agents on the Claude Certified Architect exam
Domain 5 of the CCAR-F exam covers this in task 5.1 (progressive summarization risks and the case facts block) and task 5.4 (scratchpad files, /compact during long explorations, manifests for crash recovery). Task 1.7 adds the session side: start fresh with a structured summary rather than resume with stale tool results; see Agent SDK sessions.
More in all articles.
Sources: Effective context engineering for AI agents; Effective harnesses for long-running agents; How the agent loop works; Prompting best practices; Memory tool; Claude Certified Architect Foundations exam guide v1.0, task statements 1.7, 5.1 and 5.4.
FAQ
What is a long-running AI agent?
An agent whose work spans more than one context window or session: a long support case, a multi-day coding task, a research job. It needs ways to keep the right information when the conversation is compacted or restarted.
What does context compaction lose?
Compaction summarizes older history to free space. Anthropic warns that overly aggressive compaction can lose subtle context whose importance shows up later, and the exam guide names exact numbers, dates and customer expectations as the usual victims.
What is a case facts block?
A short, structured block of exact facts (amounts, dates, booking or order IDs, statuses, promises) kept outside the summarized history and included in every prompt, so compaction can never blur them.
How do agents keep progress across sessions?
By writing state to files the next session reads first: a progress log in plain text, task status or test results in JSON, and git commits as checkpoints. Claude's memory tool and scratchpad files follow the same idea.
Read next
- LLM Context Window Explained: Claude's Working Memory
- Human in the Loop AI: Approvals, Handoffs and Review
- LLM Confidence Calibration and Human Review That Scales
- CLAUDE.md Guide: What to Put in It and Where It Goes
- Authentication vs Authorization: Sessions, JWT, OAuth, SSO
- Software Engineering Unpacked: home
- The CCAR-F video playlist on YouTube