Append-Only Context for LLMs: A Practical Guide to Never Rewriting Your Prompt Prefix

    This article explains why many modern LLM APIs treat conversation history as append-only, invalidating all downstream reasoning whenever earlier content is modified. It breaks down the cost, latency, quality, and safety implications of prefix rewriting, shows why five common context-management habits violate this constraint, and presents an append-only context protocol with a migration checklist covering trimming, configuration updates, tool lists, fixed instructions, caching, and monitoring.

    1. How the Problem Starts with Context Trimming

    When building multi-turn chat and agent applications, developers tend to accumulate a set of context-management habits: assembling system prompts dynamically per scenario, rewriting the opening instruction based on retrieval results, compressing long tool outputs, replacing early conversation turns with summaries, switching structured output modes by task stage, and adjusting the available tool list based on permissions. Over the past two years, these practices have been de facto best practice.

    In the interface semantics of a new generation of large models, however, there is a constraint that is easy to overlook but has far-reaching impact: conversation history can only be appended to along the time axis, and cannot be modified retroactively. Once any content before a change point is altered, all reasoning intermediate states after that change point are treated as invalidated and discarded, and the model has to reason again. This is not only a cache hit-rate issue; it is explicitly described as part of a safety mechanism.

    This article is not about any specific vendor product. Instead, it abstracts the phenomenon into a general engineering problem: how to design an append-only, no-rewrite context protocol, along with a transferable set of practices.

    2. Why Rewriting the Prefix Triggers a Chain of Costs

    2.1 Reasoning Intermediate States Are Tightly Bound to the Prefix

    During multi-turn interaction, the model retains structured reasoning intermediate states used to carry forward prior computation. These states correspond one-to-one with the input prefix at that time. Once the prefix is rewritten, the semantic basis for subsequent reasoning changes, and reusing it would cause logical misalignment, so the system invalidates it entirely.

    2.2 The Triangle of Cost, Latency, and Quality

    The direct consequence of invalidated reasoning is recomputation: higher token consumption, longer time-to-first-token and overall latency, and reduced output stability. For long-chain agent tasks, this duplication is amplified stage by stage.

    2.3 A Safety Policy, Not a Billing Policy

    Folding reasoning invalidation into safety guarantees means prefix immutability is a requirement of “semantic integrity”: it prevents bypassing existing constraints or injecting inconsistent context by quietly rewriting history. Therefore, it cannot be treated as a cache detail to be optimized.

    3. Why Five Common Practices All Cross the Line

    • Dynamically assembling the system prompt and rewriting the opening every turn: prefix rewriting.
    • Truncating long tool logs in the history: prefix rewriting.
    • Replacing early multi-turn conversation with a summary: prefix rewriting.
    • Switching structured output modes by stage: a configuration change written at the front.
    • Adding or removing tools by permission: prefix rewriting.

    A typical code-review agent hits four of these at once, which means nearly every turn triggers reasoning invalidation.

    The core principle is a single sentence: express every change as a new record at the end, and keep everything before it byte-for-byte stable.

    Past Practice Append-Only Alternative
    Modify reasoning effort parameter Append a configuration update record at the end; keep request-level parameters unchanged
    Switch output structure mode Append it as a configuration update record as well
    Change the tool list Append available tools, or constrain scope with the current turn’s allowed list
    Rewrite the fixed opening instruction Append a new developer instruction at the end
    Truncate old tool output Complete trimming before the data first enters the model, and never modify it afterward

    Illustrative pseudocode:

    1. input = [
    2. {role: developer, content: fixed specification}, # never modified
    3. {role: user, content: task},
    4. {role: tool, content: already-trimmed result}, # trimmed once
    5. ]
    6. # when a change is needed, only append
    7. input.append({type: configuration_update, effort: high})
    8. input.append({role: developer, content: new constraint for this turn})

    5. Easily Overlooked Billing and Caching Details

    When a top-level fixed instruction field is placed after the input, it cannot hit the prefix cache and is billed as uncached input. Moving the fixed specification to the first developer message in the input lets it enter the stable prefix and benefit from caching. This is a low-cost, high-return change.

    6. Implementation Checklist

    1. Audit the context constructor and mark every place that “writes back to history.”
    2. Move trimming logic ahead of the point where data enters the context.
    3. Abstract configuration-type changes uniformly as append events.
    4. Move fixed instructions to the front and freeze them.
    5. Add monitoring: track the number of reasoning invalidations, repeated tokens, and latency percentiles.
    6. Persist the immutable prefix in object storage, and use logging services plus monitoring alerts for regression validation, with canary releases via a container platform when necessary.

    7. Conclusion

    “Append-only, no rewrite” may look like an interface detail, but it is a structural shift in agent context engineering. Only by expressing changes as an event stream and treating the prefix as an immutable baseline can you balance cost, latency, quality, and compliance. For teams building agent applications on Baidu AI Cloud, this paradigm also applies to message-queue-driven multi-turn orchestration and full-chain tracing supported by logging services.