Software Engineeringunpacked

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:

  1. The window fills up, so older turns get compacted into a summary.
  2. The session ends. "Each new session begins with no memory of what came before", like engineers on shifts.
  3. 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".

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:

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:

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.