The Agent Writes Its Own Context

Explain this chapter with AI

Copy this prompt into ChatGPT, Claude, Gemini, a local model, or another AI.

Apply this chapter with AI

Copy this prompt into ChatGPT, Claude, Gemini, a local model, or another AI.

A debugging agent hits a failing migration test, guesses duplicated tenant identifiers as the cause, and records the guess in its progress file. Twenty turns later the window has reset. A fresh invocation reads the file, finds “Root cause: duplicated tenant IDs,” and plans the rest of its work around a fact nobody ever established. The test output that prompted the guess is long gone. The guess survived; the evidence did not. Nothing in Chapters 10 through 14 prevents this, because every mechanism so far assumed the candidate universe was given. It is not. The agent manufactures it as it works, and this chapter governs what it manufactures.

The candidate universe does not stand still

Chapter 14 drew a pipeline from available information through candidates and admission into context. That drawing omitted the arrow that matters most once an agent starts acting:

CONTEXT_t
    โ†“
 MODEL
    โ†“
 ACTION
    โ†“
 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
 โ”‚               โ”‚
 โ†“               โ†“
OBSERVATION   GENERATED STATE
 โ”‚               โ”‚
 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
        โ†“
 CANDIDATE SET
        โ†“
 ADMISSION
        โ†“
CONTEXT_t+1

Every action changes the future candidate set. A search query creates search results. A file read creates a file observation. A test run creates logs. A subagent call creates a report. A summary creates compact state. A plan update creates new coordination state. Agent execution is therefore a context-production process, and the first durable principle follows:

Agent execution changes the information environment of future agent execution.

Two identical starting contexts can diverge on the first action. One agent searches the cache subsystem, observes a stale entry, and forms a hypothesis about invalidation. Another reads the migration log, observes a timeout, and forms a hypothesis about connection pooling. Later contexts now differ in observations, hypotheses, and plans. Context is path-dependent in an agent loop โ€” unremarkable as a claim about processes generally, consequential as engineering, because early decisions alter later information availability and there is no replay button on a production trajectory.

Generated text is not yet context

Precision first, because loose language here causes real design errors:

Self-generated context is agent-produced information that is preserved or reintroduced so that it can influence a later model computation.

Writing PLAN.md makes the plan available information and session state. It becomes context only when a later invocation receives it, through selection, admission, and rendering. Until then it is a candidate at most. The chapter covers observable, persistable state only: plans, task lists, progress records, summaries, hypotheses, critiques, reflections, subagent reports, tool-derived notes, decision records, intermediate files, structured working state. It excludes private hidden chain-of-thought, unavailable internal activations, and provider-private reasoning traces unless a product explicitly surfaces some representation as application-visible state. One architectural decision, stated as law:

Context engineering should govern observable state, not depend on private reasoning traces.

The application can govern tool calls, written plans, status fields, decision records, and explicit reflections. It cannot treat hidden reasoning as persistent application state, an auditable context item, or a stable API. Nothing in this chapter’s experiments seeds, reads, or manipulates hidden reasoning. Only observable state is written; only observable state is measured.

Three producers can fill the current context, and the distinction is load-bearing rather than taxonomic. The external world produces files, databases, pages, user instructions, tool outputs. The agent produces plans, hypotheses, summaries, critiques, reports, subagent instructions. The harness produces task status, progress records, checkpoints, evaluation outcomes, structured state. The working rule:

An item generated by the model does not automatically deserve the same evidential status as an observation generated by the environment.

A later section turns that rule into provenance requirements. Chapter 19 will use them for authority. Here they are recorded, not adjudicated.

Observation is not interpretation

The central mechanism. A test returns FAIL: expected 4, got 5. The agent writes “The cache invalidation logic is broken.” These are not equivalent. The first is an observation: what the environment reported. The second is an interpretation: what the agent currently thinks, carrying a hypothesis that exists precisely to be wrong sometimes. If both later appear as anonymous context, future computations treat a hypothesis as evidence. So the firewall reads:

tool observation
        โ‰ 
agent interpretation
        โ‰ 
established fact

Chapter 11 named one route by which possibility hardens into fact: epistemic collapse under compaction, where summaries promote the unresolved to the resolved. This chapter names the second route: an agent writes a hypothesis into state, the state is reused without its provenance and status, and a later computation treats it as fact. Same collapse, different machinery, same term consumed rather than reinvented. A useful future data model keeps observation, belief, decision, and status visibly apart โ€” what the environment reported, what the agent thinks, what it chose, what work remains โ€” but the chapter uses only as many categories as each experiment needs to stay interpretable.

Hypotheses therefore carry their uncertainty with them or they travel as counterfeits. A persisted hypothesis should keep, at minimum, its status and its evidence: unverified, based on observation-17, evidence against none yet. Later, falsified by observation-31. The exact schema is not prescribed; the principle is:

If temporary interpretations are persisted, their uncertainty must persist with them.

And confirmation must mean something checkable. A structured-state condition that lets the model mark its own hypothesis confirmed with no evidence lets the model win by declaring itself correct, which destroys the experiment. In deterministic fixtures a confirmed fact is backed by a fixture or tool-observation identifier, and the evaluator checks the backing. Production criteria come later; experimental validity comes now. Nor is the answer a confidence number on everything. Chapter 7 rejected one-dimensional importance; this chapter rejects one-dimensional confidence with the same argument. Producer, status, evidence references, scope, creation and update times: use only what the mechanism needs.

A hypothesis that becomes evidence

The failure mode, developed concretely. Weak evidence suggests hypothesis H. The agent records H in progress state. Future search queries assume H. Retrieval returns evidence related to H. The summary strengthens H. Each step is locally reasonable; the trajectory is self-reinforcing. This is not necessarily hallucination. It is a context feedback failure: a wrong item repeatedly selected, persisted, and amplified until a guess steers the work it was meant to serve. The durable statement stays measured:

Persistence can convert an error in one computation into an input assumption for many future computations.

Can, not must. The same loop is why agents work at all: a test result informs a decision, the decision updates the plan, the plan targets the next action, and correct state accumulates. The question is never whether agents should write state. It is what generated state deserves to survive, in what form, and under what evidential status. Impact scales with repetition โ€” a small error reintroduced every turn can matter more than a large one seen once โ€” which is why readmission frequency belongs in the policy alongside the persist-or-not decision. Chapter 14 owns admission generally; this chapter consumes it: a progress note need not be resident merely because the agent wrote it, and write-then-permanently-pin is recorded as the accidental policy to avoid.

Plans go stale in the same way hypotheses do. A plan that reads reproduce, inspect cache, add test, patch invalidation is temporary coordination state, useful until evidence clears the cache, after which keeping it resident constrains action toward a cleared suspect. Goal, plan, and current step are three different objects with three different lifetimes: the goal may persist while the plan changes and the step completes. Summaries steer trajectories too, which gives Chapter 11’s fidelity failures a second life as trajectory-control failures: a summary that drops the decisive qualification does not merely misrepresent the past, it redirects the future. Critiques and reflections earn the same treatment. “Previous attempt failed because we changed too many files” may be true or not; once persisted it biases later work, so a critique retains its source, the trial it refers to, its status, and its evidence. The experiment needs only enough of this to test the mechanism, never a grand schema.

Every action manufactures more context

Tool observations deserve special attention because they look environment-made while being agent-caused. The model chooses the tool call; the environment emits the observation; the observation becomes a candidate. The agent therefore controls not only what it writes but which information the environment produces for it โ€” choosing queries, choosing files, choosing tests is choosing future context. This is the bridge from Chapter 14, and Chapter 17 owns everything on the tool-design side of it. Here only the mechanism travels: action choice manufactures future observation.

Subagents generalise the mechanism across agent boundaries. A lead agent delegates an investigation; the subagent produces a report; the report becomes a parent candidate that can compress useful work, introduce error, lose provenance, or duplicate other reports. Self-generated context does not have to return to the same model instance. No multi-agent architecture is built from this; the boundary example is enough. But two consequences follow that belong here. First, generated artifacts outlive their producing computation: progress files, feature lists, and reports shape fresh sessions, other agents, future models, and human reviewers the original invocation never sees. Agent-generated context can have a longer lifetime than the computation that created it. Second, write quality separates from answer quality as an evaluation target. A model can produce a good immediate answer while leaving poor persistent state, or a mediocre reply alongside excellent continuation state. Systems that only score the current output cannot see the difference, and the experiments score both.

What production harnesses already do

The historical line starts with ReAct, used here the way the book uses all early mechanisms: for the observable architecture, not the internals. Yao and colleagues interleave generated reasoning traces with task actions and environment observations across question answering, fact verification, and interactive decision tasks, reporting large absolute gains on the decision benchmarks. The transferable lesson is the trajectory shape itself โ€” prior generated state plus environment observations influencing later action โ€” while the reasoning traces stay on the private side of this chapter’s boundary. Nothing in the mechanism requires them as an application primitive.

Reflexion is the sharper precedent: Shinn and colleagues have agents verbally reflect on task feedback signals, keep the reflective text in an episodic memory buffer, and reuse it in subsequent trials, reporting large gains including 91 per cent pass-at-one on HumanEval against an 80 per cent baseline in the authors’ measurements. That is generated text becoming future input by explicit design, which is exactly this chapter’s subject. The chapter’s contribution is the question the paper’s ablations do not centre: what happens when the reflection is wrong. Self-reflection is not intrinsically corrective, and persistence decides how far an incorrect reflection travels.

Anthropic’s long-running-agent work supplies the deliberate-state counterpart. The November 2025 harness carries continuity in artifacts the agents write and read: feature lists with pass/fail status, progress notes, init scripts, git history, with each session starting from the recorded state and ending by updating it. The March 2026 harness extends the pattern into planner, generator, and evaluator agents communicating through files โ€” sprint contracts negotiated before code, structured handoff artifacts between sessions, evaluator findings specific enough to act on. Three of its lessons transfer directly. Planner specifications constrain everything downstream, so errors in generated plans cascade: write quality governs answer quality before either is measured. Communication through files with lightweight references, rather than copying full outputs through every coordinator, preserves fidelity across agent boundaries. And the evaluator’s early leniency toward its own generator’s work had to be tuned out over several rounds โ€” a production sighting of self-authored evidence wearing a lab coat. The multi-agent research system adds the cross-agent form: a lead agent’s plan persisted to memory against truncation, subagents returning condensed findings, and subagent outputs written to a filesystem with references passed back, explicitly to avoid the multi-stage game of telephone. Its appendix adds two more earned details: state-mutating agents are evaluated on end state rather than prescribed paths, and errors compound across long stateful runs. All three posts are first-party engineering evidence of shipped design experience, not controlled studies, and every quantitative claim stays vendor-reported.

SWE-agent is used for one narrow point and no more: Yang and colleagues’ thesis that agents are a distinct category of end user whose interface shapes their behaviour, with a custom agent-computer interface changing what trajectories and observations look like. The form of tool observations, the available actions, and what gets written back into context are interface products. Benchmark figures stay in the paper. Voyager is deliberately omitted: an expanding executable skill library would overlap memory, skills, and tooling questions the book has assigned elsewhere, and nothing in its mechanism adds to the persistence problem beyond what the harness and subagent cases already cover. Omission with a stated reason beats padding with a famous name.

Structured state, bounded state

The experimental question the chapter earns: free-form progress narrative against bounded structured working state โ€” goal, confirmed facts, open hypotheses, decisions, remaining work, evidence references, using only the fields the fixture needs. Structure may reduce status collapse, provenance loss, and accidental reinterpretation; it may also lose nuance, demand schema design, and consume management effort. No winner is assumed.

Boundedness is a validity condition, not a feature. A structured record that only appends becomes another unbounded transcript wearing fields as costume, so the structured condition defines update, replace, invalidate, and close semantics up front. State carries versions โ€” v1, v2, v3, with recorded changes โ€” because correction delay and invalidation cannot be measured without them. When contradiction arrives, the system updates the live record rather than appending “H was false” beside a prominent “H is probably true” and letting future context contain both. That conflict problem belongs to Chapter 19 for precedence; here the requirement is only that working state has explicit invalidation semantics instead of append-only accumulation. The append-only evidence log still exists, but it lives with the evaluator as audit: what happened, what was observed, what the active state recorded at the time. Mutable active state represents what the agent currently needs. The old record stays auditable somewhere without staying active context โ€” the book’s available-versus-current distinction applied to the agent’s own past.

Generated state goes stale two ways, and the chapter keeps the book’s vocabulary straight. Trajectory-obsolete means the task moved on: current step still reads “run migration test” after the test finished. World-stale means the external fact changed, which belongs to Chapter 20. Only the first is governed here. Scope is controlled the same way: a plan for one task does not quietly enter another, with isolation deferred to Chapter 21. Self-generated state is not exempt from compaction either โ€” a generated summary may itself be summarised later under Chapter 11’s fidelity constraints โ€” and agent state never outranks its producer: a self-written claim carries no authority for being self-written, with Chapter 19 to adjudicate.

Proposed experiments

Deterministic multi-stage fixtures carry both experiments: an initial goal, several plausible hypotheses, tool observations, one misleading early clue, one later decisive correction, subtasks, a decision point, and a completion criterion, all with exact ground truth. Synthetic task fixtures, never a real repository where truth is ambiguous. The state writer at step two must not see the decisive evidence introduced at step five, under the same hidden-future discipline as Chapters 11 through 14. The model sees only selected state; the evaluator retains every observation, every state version, every action, and the ground truth outside live context. Experimental evidence is never destroyed.

Experiment 1: persistence versus no persistence. A, full raw trajectory as the high-context baseline. B, environment observations only โ€” tool and environment evidence persisted, no agent-authored plans, hypotheses, summaries, or reflections โ€” testing what self-generated state adds. C, free-form generated state written at milestones and reused later. D, bounded structured state with explicit update and invalidation semantics. E, oracle minimal correct intermediate state as ceiling. Optionally F, structured state plus a raw recent tail, only if the continuity question needs it. Raw trajectory naturally costs more tokens, so the comparison reports the difference honestly and runs a matched-budget arm capping the persistent-state conditions at similar budgets. Cost differences are displayed, never hidden.

Experiment 2: seeded self-reinforcing error. Early evidence supports two plausible hypotheses; the fixture deliberately seeds the false one as observable state, then introduces decisive evidence against it. A, the wrong hypothesis appears once and is never persisted. B, it persists as unqualified free-form state. C, it persists with explicit unverified status and an evidence reference. D, contradictory environment evidence arrives but old state is never updated. E, structured state with explicit invalidation on contradiction. F, oracle corrected state. The question is singular: does persistence change the probability that an early error propagates into later actions. Nothing depends on the model erring spontaneously, and nothing touches hidden reasoning.

A compact third extension reuses the first fixture across the subagent boundary: free-form subagent report against structured report with evidence references, passed to a parent agent. It earns its place only because cross-agent handoff is a genuinely different mechanism from same-agent persistence โ€” producer change, not just format change.

Measurement tracks the lifecycle the chapter defined: created, persisted, admitted, used or influential, correct or incorrect โ€” the deliberate mirror of Chapter 14’s retrieved, admitted, used, helpful. Experiment 1 reports task success, persistent-state tokens, total context tokens, actions, tool calls, repeated work, state exactness under Chapter 11’s local metrics (omission, mutation, addition, epistemic collapse, provenance loss โ€” applied to state defects feeding actions, never a second compaction study), and status survival. Experiment 2 reports wrong-state persistence, readmission counts, promotion from hypothesis to treated-as-fact, correction delay in steps, actions taken before correction, and final success. Used is defined conservatively: presence is not influence, and bounded remove/restore comparisons โ€” same trajectory with the state item removed โ€” supply the causal evidence where claimed. Overhead is measured whole: tokens to generate state, tokens it occupies, extra calls, write costs. Candidate-universe growth over time is tracked as its own number, showing why Chapter 14’s admission problem hardens as trajectories lengthen.

Falsification is stated in advance. Free-form summaries matching structured state, observations-only matching everything, plans adding little, structure costing more than its continuity, fast correction regardless of persistence, uncertainty tags changing nothing, raw trajectory winning under realistic budgets, self-generated state proving rare once real traces exist: each simplifies the runtime. If observations-only suffices, the capstone sheds state machinery. If free-form matches structured, no schema is added for elegance. If structured state substantially improves correction and continuity, it earns its role. The architecture keeps shrinking where evidence permits.

Context Lab and corpus implications

Nothing is implemented; the report records the assessment the prompt requires. Project Context already has ContextItem, and the chapter’s needs โ€” producer type, status, evidence references, versioning, supersede and invalidate links โ€” read as provenance and status metadata on the existing record rather than a separate GeneratedStateItem hierarchy. A distinct StateTransition log duplicates what run manifests and evaluation logs already capture. The recommendation is reuse: extend the item model, do not proliferate schemas, and admit new fields only as experiments consume them. The production implication survives regardless: external candidates, memory candidates, tool observations, retrieved artifacts, and agent-generated state should eventually flow through one general candidate interface instead of bespoke admission machinery per source. Chapter 22 is where that unification pays off.

The corpus position is unchanged. Project Context holds zero genuine ecological sessions at drafting time, and the chapter’s causal experiments are synthetic by design: fixtures establish propagation, the corpus will later establish prevalence. Future captures may ask how much assistant-generated content survives across invocations, how often plans and task lists repeat, how much context arrives as previous assistant messages, how frequently hypotheses and progress summaries return, and how often tool observations dominate generated state โ€” subject to what the V1 partial boundary can actually distinguish, which the chapter does not overclaim. Absence of a corpus delays prevalence, never mechanism.

When working state becomes memory

Self-generated state persists across steps, tasks, sessions, and sometimes longer, and at some horizon it starts to resemble memory. But not every progress note is memory, and this chapter does not decide where working state ends and durable past influence begins. That boundary is Chapter 16’s entire subject: if agent-generated state may outlive its trajectory, what distinguishes working context state from durable memory. The handoff question is asked here and answered there. What this chapter established is everything the answer will assume: an agent does not merely consume context but manufactures its future information landscape; persisted output is not evidence for having been written; and errors grow more dangerous the more often a system reintroduces them as context. The compiler’s eventual candidate interface โ€” retrieval, memory, tools, artifacts, and agent-generated state arriving through one door โ€” is now fully motivated. What remains is to define the strangest source of all: the durable past.

References

  • Yao, S., Zhao, J., Yu, D., et al. “ReAct: Synergizing Reasoning and Acting in Language Models.” ICLR 2023 (arXiv:2210.03629, v3 camera-ready). Interleaved generated reasoning traces with task actions and environment observations; reported gains on HotpotQA, Fever, ALFWorld, and WebShop. https://arxiv.org/abs/2210.03629
  • Shinn, N., Cassano, F., Berman, E., et al. “Reflexion: Language Agents with Verbal Reinforcement Learning.” NeurIPS 2023 (arXiv:2303.11366). Verbal reflection on task feedback stored in an episodic buffer and reused across trials; reported 91% pass@1 on HumanEval. https://arxiv.org/abs/2303.11366
  • Yang, J., Jimenez, C. E., Wettig, A., et al. “SWE-agent: Agent-Computer Interfaces Enable Automated Software Engineering.” Preprint, arXiv:2405.15793, May 2024. Agent-computer interface thesis: interface design shapes agent trajectories and observations. https://arxiv.org/abs/2405.15793
  • Anthropic (Young, J., et al.). “Effective harnesses for long-running agents.” First-party engineering essay, November 2025, verified September 2026. Feature lists, progress notes, init scripts, and git history as deliberately shaped future context. https://www.anthropic.com/engineering/effective-harnesses-for-long-running-agents
  • Rajasekaran, P. “Harness design for long-running application development.” First-party engineering essay, Anthropic Labs, March 2026, verified September 2026. Planner/generator/evaluator with sprint contracts and file-based communication; structured handoff artifacts; planner-error cascade; evaluator leniency tuned out; simplification principle. https://www.anthropic.com/engineering/harness-design-long-running-apps
  • Hadfield, J., et al. “How we built our multi-agent research system.” First-party engineering essay, Anthropic, June 2025, verified September 2026. Lead plan persisted to memory; subagent findings as cross-agent context; filesystem artifact outputs with lightweight references against multi-stage information loss; compounding stateful errors. https://www.anthropic.com/engineering/multi-agent-research-system