# Append-only context for long-running coding agents

Author: Chris Chabot
Canonical URL: https://chabot.dev/blog/append-only-context-for-long-running-coding-agents/
Published: 2026-08-22

How to lay out a coding agent's prompt so file edits keep the prompt cache warm, compact a conversation without losing the source, and keep token counts honest across providers.

Rewrite a workspace file near the top of a coding agent's prompt, and the cached conversation behind it is invalidated and billed again at full input rates.

A long agent session needs context to change as files are edited, transcripts outgrow the window, and requests fail over between providers, while prompt caching and reliable recall depend on an unchanged prefix and an intact record. Mutating in place, by updating the file inside the prompt, summarizing the summary or overwriting the provider's token count, discards the original each time. Log-structured storage and event sourcing settled this decades ago by keeping an immutable append-only record, deriving the working view from it, appending deltas, and merging only at chosen checkpoints.

I applied that rule across three layers of a TypeScript runtime for our production coding agent so nothing derived overwrites its source. The runtime assembles each LLM request from a system prompt, conversation history, and workspace files, runs the returned tool calls, and reaches models through a gateway that can fall back across provider families.

## Separating resident files from mid-turn edits

In the runtime's agent loop, I built and tested a snapshot-and-tail layout that decouples pinned workspace files from the bytes serialized into the cached prefix. A frozen snapshot of resident files sits after the system prompt, followed by conversation history and an uncached tail past the last cache breakpoint, where mid-turn edits and newly pinned files are appended until a checkpoint absorbs them.

```text
System prompt                        \
Frozen snapshot of resident files     | cached prefix
Conversation history                 /
--- cache breakpoint ---
Mid-turn edits and newly pinned files  uncached tail
```

Suppose the agent rewrites `src/routing.ts` mid-turn while it sits in the snapshot. Rather than invalidating cached history for one edit, the loop leaves the resident bytes untouched, appends the new content to the uncached tail, and adds a note that this copy is current. Between checkpoints the prompt carries both versions of `src/routing.ts`, so correctness rests on the model honoring that note over stale bytes higher in its context.

Because advancing the snapshot invalidates the cached suffix behind it, refreshing eagerly during an edit-heavy turn spends more re-caching the conversation than subsequent hits recover. The snapshot advances only at turn start, after compaction, after context recovery, or on a mid-turn refresh that pays back its re-caching cost and runs at least two steps after the previous one.

At a checkpoint, pinned files are reconciled against disk by content digest so out-of-band edits are absorbed alongside the agent's own writes. A failed read marks a file unavailable rather than deleting its pin, and because the updated snapshot alters code above earlier turns, the next turn opens with a note listing what changed.

## Compaction without overwriting the conversation log

Compaction fails the same way when a runtime replaces older turns with a prose summary and feeds that summary into the next pass. Constraints soften, specifics disappear, and on LongMemEval, a benchmark for long-term conversational memory, substituting summaries for raw history hurts detailed recall. Keeping the conversation log as the authoritative record lets compaction build a derived view instead.

In our compaction ledger, implemented and contract-tested in the runtime, each pass appends structured facts citing content-addressed log entries. Deterministic code copies system, developer, and user instructions verbatim so operating constraints are never paraphrased. A writer model proposes the remaining facts from the compacted turns, and a separate verifier checks each claim against its cited entries. When the writer proposes "All tests pass" citing one test file, the verifier rejects it even though the citation resolves; the repaired claim, "eight tests in that file passed," is accepted. The verifier is a second opinion, not proof, though separating extraction from entailment checking catches scope inflation that hash validation alone misses.

Because the log stays intact, dropped details remain recoverable: a bounded search re-reads the original entries directly, and an earlier summary is never accepted as evidence.

## Keeping raw token counts alongside normalized usage

Both cache-refresh payback and compaction triggers depend on accurate prompt sizing, which breaks down when a gateway fails over across provider families. Providers still disagree on whether input counts include cached tokens: OpenAI's total includes them, while Anthropic's excludes both cache reads and writes. On an illustrative response reporting 1,000 input tokens, 500 cache reads, and 300 cache writes, an inclusive convention puts the prompt at 1,000 tokens and an exclusive one puts it at 1,800, an 800-token disagreement on the same numbers.

The same rule applies to usage accounting: normalize by the provider that actually answered, not the one requested, and keep the raw count beside the normalized total. When an inclusive provider returns contradictory numbers, with raw input below cache reads plus writes, clamping the uncached share to zero hides the discrepancy from both context safeguards and cost analysis. The runtime retains the raw observation and flags the contradiction explicitly.
