Append-Only Event Streams: Solving Agent Memory Loss and Inconsistent Views
Append-only event streams separate immutable session facts from the context sent to a model. This article explains why array-based and summary-based context management causes Agent memory loss and inconsistent views, then shows how event logs, projection functions, ordering, persistence, and lifecycle event types can provide recoverable, auditable, and consistent Agent sessions.
Many developers have encountered this scenario: after several rounds of conversation with an Agent, it suddenly “does not acknowledge” something. A tool execution result that was just provided seems to have never happened in the next round. The first reaction is often that “the model has a poor memory,” but the more common reason is that the context the model sees in each round is not the complete set of session facts. Instead, it is a temporary, trimmed projection that may be overwritten at any time.
Once you adopt this perspective, you will find that most memory problems in Agent runtimes are not model capability problems but system architecture problems. A well-designed session store should keep facts permanently in a log and let the model see only a projection of that log. This is the core problem that an append-only event stream is designed to solve.
1. Why Common Approaches Lose Memory
Many session frameworks take a simple approach: put the conversation history into an array and pass the entire array into the model context. This approach seems fine for short conversations, but once a task becomes long—especially with multi-turn tool calls, long document reading, or complex workflow orchestration—the array is truncated because of length limits.
Truncation usually happens to the “oldest” messages. The system does not report an error or tell you that content has been removed. As a result, the model’s subsequent answers are based on an incomplete context. It is not that the model has “forgotten”; it simply does not have that information in hand. If you ask, “What was the result of that step just now?” it can only answer vaguely because the record is not in its visible context.
The root cause is treating “the context currently visible to the model” as “the sole carrier of facts.” Context has a window limit and is mutable. When it is compressed, replaced, or truncated, facts disappear. A more troublesome issue is that some systems also perform summary compression: old messages are condensed into a paragraph and placed back into the context. A summary may preserve the “general idea” for a short time, but it loses key details such as specific values, raw fields returned by tools, and error stacks. If the summary itself exceeds the limit, it may be compressed again, eventually leaving only a vague “summary of a summary.”
In addition to memory loss, inconsistency is a common side effect. The context visible to the model, the chat history displayed in the frontend, and the trace information exported for debugging often come from different caches or different data sources. When one place is updated and another is not, a split state appears: the interface shows that a tool call succeeded, but the model does not know about it.
2. Append-Only Event Streams: Liberating Facts from Context
To solve both “memory loss” and “multi-view inconsistency,” a different design principle is needed: do not use context to carry the truth. Instead, write every fact that occurs during a session as an append-only event stream. This event stream is the session log and the “single source of truth” for the entire system.
Each element in the event stream is an immutable event. Each event contains at least:
- Event sequence number: unique and monotonically increasing, ensuring global order.
- Event type: such as user message, model reply, tool call, tool result, or system state change.
- Event payload: the specific content, including text, parameters, results, error information, and so on.
- Timestamp or correlation identifier: used for tracing causality and debugging.
The key constraint is “append-only”: historical events are not modified or overwritten. A record that has been written is a fact and will never be erased because of later adjustments to context space. No matter how long the session becomes, the log retains the complete original event chain.
How does the model obtain context then? The answer is “projection.” The model does not read the log directly and never “remembers” anything. Before each conversation round begins, the system runs a projection function that maps the events needed from the log into the message list that the model can read in that round. The projection function is only responsible for generating a view; it does not modify the underlying log. What the model sees is derived, not the storage itself.
There is a very important design invariant here: anything visible to the model must already have been written to the log. If a developer wants to add a new kind of visible information for the model, the correct approach is to define a new event type, write it to the log, and then render it from the log—not to secretly insert a system prompt or hidden text somewhere. This invariant ensures that everything the model sees is traceable. There is no “ghost input” that is absent from the log but visible to the model.
3. Why It Solves Both Problems at Once
3.1 Memory Is No Longer “Volatile”
In traditional approaches, once the context is truncated, the facts are completely lost. In an append-only event stream, facts do not disappear because the context window is insufficient. The fact that the model cannot currently see a piece of history does not mean it does not exist. When needed, the system can reproject from the log, generate a summary, or perform targeted retrieval to bring key information back into the visible context.
For example, a long task may first read a large amount of data returned by a tool, and then reference a specific field from that result dozens of rounds later. A traditional approach may have truncated the original result long ago. The event stream approach persists the complete result as an event. Even if the current projection renders only a summary to save tokens, the model can still obtain the original data through subsequent queries or replay. This capability is not achieved by “good model memory” but by the completeness and traceability of the log.
Furthermore, the session log is also the foundation for crash recovery. If the Agent process exits unexpectedly, the session context can be rebuilt from the log after restart. The model does not need to “remember” the past; the system only needs to reproject the historical events to restore the state.
3.2 Multiple Views Are Naturally Consistent
Interface display, model input, debugging traces, and audit reports are essentially “different views of the same facts.” As long as they are all projected from the same event log, they will not contradict one another.
Projection is a pure function: given the log and the current cursor, it outputs a deterministic view. The model sees one view, and the frontend sees another, but both can be traced back to the same event. If the frontend needs to display the status of a tool call, it can directly read the corresponding event and render it according to the view rules. If debugging needs to see what the model actually received, it only needs to run the same projection function instead of checking another cache. This eliminates the root cause of inconsistency, because the system no longer contains multiple independent copies of facts.
4. Key Points to Consider During Implementation
4.1 Event Types Should Cover the Full Session Lifecycle
Recording only conversation text is far from enough. A recoverable and analyzable session event stream should contain at least:
- Messages initiated by the user.
- Replies generated by the model.
- Tool call requests initiated by the model.
- Tool execution results or errors.
- System state changes and phase transitions.
- Strategic information that needs to be injected into the model.
Each event should be as self-contained as possible. In particular, for tool call results, it is best to write the complete return value to the log rather than querying an external service only during projection. In this way, historical facts are preserved even if the external service is unavailable.
4.2 The Order of “Writing the Log” and “Generating Context” Must Not Be Reversed
The correct flow is: append the event first, then perform projection, and finally pass the projection result to the model. If a tool returns a result, that result must be written to the log before it can enter the model-visible context in the next round. If the order is reversed, the model may see content that cannot be found in the log, breaking the invariant.
4.3 The Projection Layer Can Trim, but Trimming Is Not Discarding
The event log retains all facts, while the projection layer can decide “what should currently be displayed” based on the context window, task phase, and token budget. Projection can replace long events with summaries, or present them without truncation when the window allows. The key point is: what is trimmed is the current view, not the underlying data.
This downgrades the physical constraint of a “limited context window” from “fact loss” to a “presentation strategy problem.” Facts always remain in the log; they are simply not all brought into the model’s field of view at the current moment.
4.4 Persistence and Ordering Guarantees
The session log must be written to reliable storage. You can choose a general-purpose log service, message queue, object storage, or database, but the core requirement is support for sequential append and cursor-based reads. If the system needs parallel writes, it is best to guarantee event order through a global sequence number or distributed ID to avoid concurrency causing causality errors. For scenarios that require idempotency, event IDs can be used for deduplication so that the same tool call is not recorded repeatedly.
5. A Simplified Schematic Structure
The following pseudocode shows the basic outline of an “event log + projection” design:
class Event:seq: intevent_type: strpayload: dicttimestamp: floatclass SessionLog:def __init__(self):self.events = []self.seq = 0def append(self, event_type, payload):event = Event(seq=self.seq,event_type=event_type,payload=payload,timestamp=now())self.events.append(event)self.seq += 1return eventdef project(log, cursor, context_policy):messages = []for event in log.events[:cursor]:rendered = render_event(event, context_policy)if rendered is not None:messages.extend(rendered)return messagesdef render_event(event, policy):if event.event_type == "tool_result":# Decide whether to return the full result or a summary based on context policyreturn [{"role": "tool","text": apply_policy(event.payload, policy)}]...
A real system will of course be more complex, but the core logic remains the same: the log is the fact, projection is the view, and the model consumes only the view. All visible content must come from the log.
6. Conclusion
An Agent’s “amnesia” does not necessarily indicate a model capability weakness. More often, it is because the session architecture places facts on top of a context that changes and overflows. Append-only event streams provide a more reliable alternative: turn every interaction into an immutable event record, turn the model context into a recomputable projection, and turn the interface, debugging, and auditing into different views of the same source of truth.
This design does not change the model’s context window limit, but it changes “where information beyond the limit goes.” It no longer disappears; it remains in the log, waiting to be reprojected, retrieved, and referenced. For Agent applications that need to run for long periods, collaborate across multiple tools, and remain traceable, this is a technical path worth serious consideration.
