Skip to content
Back to Advanced agents

Advanced agents · Chapter 10 of 10

Long-horizon agents

A job that spans sessions needs compaction inside a session, hand-off files and memory between sessions, and a check of its whole trajectory.

25 minutes0 of 4 goals done

Goals

  • Run session 1 and read the files it leaves for the next session.To do
  • Start session 2 from the files alone, and watch compaction replace older messages with a summary.To do
  • Grade the coding sessions' tool calls, and open a rule that session 3 broke.To do
  • Pass the check.To do

Step 1 of 4: The read

Longer than one context

A long-horizon job needs more model calls than one context window holds, or more than one session: building an app feature by feature, say. The context window is the most a model reads in one call, counted in tokens. A session here is one run of the agent on a new thread. Two things keep such a job on track: shrinking the context inside a session, and handing the work over between sessions.

Offloading and compaction

LangChain's post on context management for Deep Agents (2026-01-28) describes three layers. A tool result over 20,000 tokens goes to a file, and the agent gets its path and a preview. Past 85% of the window, the inputs of old file writes and edits become pointers to their files. When that is not enough, compaction: a model writes a summary of the intent, the artifacts and the next steps, the summary replaces the older messages, and the full transcript stays on disk. This lab's re-created harness offloads at 20,000 tokens and compacts at 85% of the window (summarize: { window, fraction, keep }), with no middle layer.

Hand-off files and memory

Anthropic's post on harnesses for long-running agents (2025-11-26) starts each job with an initializer session. It writes a JSON feature list with every feature failing, a progress file, an init.sh script and a first git commit. Every later session reads the progress file, the git log and the feature list, runs a basic end-to-end test, works on one feature, sets its passes field to true only after testing it, and ends with a commit and a progress note. Nothing else in the list changes. For facts every thread needs, LangGraph's Store keeps long-term memory under a namespace, outside any thread.

Reset or compaction

Starting a session from these files is a context reset: a clean context and a written hand-off. Compaction keeps one context and shrinks it. In Anthropic's post of 2026-03-24, Sonnet 4.5 wrapped up early as its context filled, so that harness reset; with Opus 4.5 it switched to compaction.

Grade the trajectory

A trajectory is the sequence of tool calls a run made. An answer can say "done" when no test ran, so grade the calls with rules in code, like the code checks in Agent systems, chapter 8. Read the calls from the trace: after compaction, the messages no longer hold the early ones.

Step 2 of 4: Try it: Three sessions, one job

Three sessions, one job

Run session 1, then session 2 from its files alone, then session 3, and grade the coding sessions' trajectories.

Mode:RealRe-createdSimulatedReal: LangGraph.js 1.5.1 runs in this page, with its InMemoryStore. Re-created: the deepagents 1.16 harness rebuilt on LangGraph.js 1.5.1, with its tool names, defaults and messages, because the real package does not install next to this site's LangSmith 0.11; it estimates tokens as characters / 4. Simulated: the model's decisions and its summaries are scripted, and git, the app, its smoke test and its tests are stand-ins that run in the page.

Step 3 of 4: Check

Check

A coding session of a long job starts on a new thread. Put its steps in order, as the harness in Anthropic's post runs them.

Drag the items, or use the arrow buttons, to put them in order.

  1. Position 1: Set that feature's passes to true in feature_list.json.
  2. Position 2: Run that feature's tests.
  3. Position 3: Run the smoke test to check that the app still works.
  4. Position 4: Read progress.txt, the git log and feature_list.json.
  5. Position 5: Add a line to progress.txt and commit.
  6. Position 6: Write the code for one feature.

Step 4 of 4: What you can do now

What you can do now

  • Set up a long job with a feature list, a progress file and commits, so each session can start from the files.
  • Compact a context with summarize at a threshold, and offload large tool results to files.
  • Keep long-term memory in a LangGraph Store that every thread reads.
  • Grade a run by its tool calls, with rules in code read from the trace.