Append-Only LLM Context for Agents: A Practical Guide

    This article explains why mainstream LLM services treat the conversation prefix as immutable, how rewriting earlier context invalidates cached reasoning state and raises cost and latency, and how to translate configuration changes into appended records. It includes a before-and-after pattern table, an example, notes on fixed instruction placement, and a migration checklist.

    1. An Underestimated Constraint: Context Has Entered the Append-Only Era

    In multi-turn conversations and agent runtimes, mainstream LLM services are converging on a clear engineering constraint: a conversation prefix that has already been sent cannot be rewritten, and new information can only be added by appending to the end. This looks like an implementation detail of caching and billing, but it affects cost, latency, output stability, and safety policies at the same time. For developers used to dynamically rewriting context, it is a paradigm shift that has to be addressed head-on.

    The constraint is not a proprietary design of any single vendor. It follows naturally from the general technical approach of prefix caching plus inference state reuse: the server can only hit the cache and reuse previously computed intermediate state when the request prefix is byte-for-byte identical. Once the prefix changes, every derived state after the change point has to be recomputed.

    2. Why Going Back to Change One Line Is So Expensive

    2.1 Inference State Is a Function of the Prefix

    Across multiple turns, the model retains intermediate inference state (commonly referred to in the industry as reasoning items or intermediate chain-of-thought artifacts). This state is computed from the prefix as it was at that time and is tightly bound to the preceding text. When you rewrite the system-level instruction at the beginning, truncate a tool call result, replace earlier turns with a summary, switch models, or change the end user or organization ownership, you are effectively telling the server that the preceding text has changed. All inference state after the change point loses its validity and must be discarded and recomputed in full.

    2.2 Three Typical Categories All Hit “Prefix Modification”

    Common context management techniques accumulated by the agent community over the past two years have mostly turned into anti-patterns under the new constraint:

    • Rewriting the system prompt at the beginning of every turn based on retrieval results;
    • Going back to truncate tool logs after a few turns when they grow too long;
    • Compressing early content into a summary and replacing the original text when the conversation gets too long;
    • Switching the structured output schema as the task moves between phases;
    • Dynamically adding or removing entries in the tool list based on permissions.

    All five practices modify the historical prefix, which causes inference state to be discarded, token consumption to rise, end-to-end latency to grow, and output quality to degrade when inference cannot be recovered.

    2.3 It Is Also a Safety Policy

    One point worth noting: discarding inference state is often explicitly described as part of the safety mechanisms, not merely a billing choice. This means that even if you are willing to pay for recomputation, you cannot expect to bypass the policy by reusing old inference — inheriting state across the change point is disallowed by design.

    The core principle is a single sentence: append to the end of the context in chronological order, and write configuration changes as a new record at the end as well.

    Past practice Append-only practice
    Changing the reasoning effort parameter Append a configuration update record at the end and keep request-level parameters unchanged
    Switching the structured output schema Append it as a configuration update record instead of replacing the old schema
    Adjusting the available tool list Append “tools newly available this turn”, or narrow the scope with “call scope allowed this turn”
    Rewriting the fixed instruction at the beginning Append a new developer message at the end that declares the override
    Truncating old tool output Complete the trimming before the content is first sent to the model, and never modify it afterwards

    Example: From Rewriting to Appending

    1. # Anti-pattern: going back to rewrite history
    2. [developer] You are a code review assistant focused on security vulnerabilities # rewritten in turn 2
    3. [user] Please review module A
    4. [assistant] ...
    5. [tool] full log, 20000 lines # truncated in turn 5
    6. # Recommended: append-only
    7. [developer] You are a code review assistant focused on security vulnerabilities
    8. [user] Please review module A
    9. [assistant] ...
    10. [tool] trimmed key log (trimmed before the first send)
    11. [configuration_update] reasoning_effort=high, output_schema=v2
    12. [developer] Additional constraint: from this turn on, also watch for performance regressions
    13. [user] Continue reviewing module B

    4. An Easily Overlooked Detail: Where Fixed Instructions Sit

    Many runtimes place the top-level system instruction field after the input sequence. If you want fixed instructions to hit the cache reliably, move them to the position of the first developer message in the input sequence; otherwise that content is billed as uncached input, and a long-running agent will incur a noticeable amount of avoidable overhead. As an engineering practice, write “stable parts first, volatile parts last” into the review checklist for prompt templates as a hard rule.

    5. Migration Checklist

    1. Audit prefix dependencies: list every code path that goes back and modifies history, and convert each one to appending.
    2. Move trimming earlier: perform truncation, denoising, and field filtering on tool results before they enter the context, and keep a reproducible summary rather than deleting or editing content after the fact.
    3. Make changes explicit: use configuration update records and appended developer messages to express “override”, so that both the model and the server can tell exactly where a change takes effect.
    4. Adjust the summarization strategy: do not replace the original text; append the summary at the end and annotate the range of turns it covers.
    5. Observability: instrument cache hit rate, recomputation volume, and time to first token in log services and monitoring alerts, and use the data to verify the results of the migration.
    6. Cost control: use the load-testing environment on the container platform to compare token and latency curves before and after the change, then roll out gradually.

    6. Conclusion

    Append-only is not a step back in capability; it moves context management from arbitrary rewriting toward engineering discipline that is verifiable, cacheable, and auditable. For developers, the real payoff is a traceable context evolution path, reusable inference state, and predictable cost. For enterprise users, it means agents have more stable quality and safety boundaries under high concurrency. If you are building similar capabilities, you can rely on general cloud product capabilities such as the model services, log service, and monitoring alerts offered by Baidu AI Cloud to hard-code these rules into your development process.