Append-Only Event Streams for AI Agents: Fixing Session Memory Loss and View Consistency

    When an AI agent "forgets" mid-conversation or its UI shows something different from what the model actually saw, the root cause is rarely the model. It's an architecture flaw: treating the mutable context as the source of truth. This article explains why context-as-truth frameworks silently drop memory as their message arrays overflow, and presents an append-only event log (Session Log) as the fix. The core idea is to store facts in an immutable, ordered event stream, then derive the model's context through a projection function on demand. A hard invariant—model-visible means logged—keeps every input traceable. Together, these principles eliminate memory loss at the source and keep the model view, UI view, and debug replay view consistent, since they all render from the same log. Read on for the design rationale, TypeScript examples, and practical implementation guidance.

    1. Agents Also “Forget”—and the Model Is Usually Not to Blame

    In multi-turn conversations, especially long tasks with tool calls, a familiar failure shows up: partway through, the agent suddenly acts as if earlier events never happened. Ask it “what did the tool return in the previous step?” and it can only answer vaguely.

    Most people’s first instinct is “the model just has a bad memory.” But blaming the model usually means looking in the wrong place.

    Start by clearing up a widely misunderstood fact: in the vast majority of agent runtimes, the model does not “hold” memory at all. What it can read at each inference step is simply a temporary list of context messages that the framework assembles and feeds it. This list is not persistent storage—it’s a snapshot that can be recomputed, compressed, or truncated at any time. Once it’s replaced or trimmed, the parts that were cut off can never re-enter the model’s view.

    So the “forgetting” it appears to do is really just this: that content is no longer in the context it can see. It’s not that the model can’t recall it—it’s that it never had it in hand. This should remind us that when we discuss agent memory, we must first separate two distinct concepts.

    • Memory: the factual record of what happened. It should be persistent, complete, and trustworthy.
    • Context: what the model actually sees in a given step. It’s a projection of memory that can grow or shrink depending on strategy.

    When a framework conflates the two and uses the “current context” as if it were “the facts themselves,” memory loss becomes inevitable.

    2. Why Traditional Frameworks Lose Memory: Context as Truth

    To save effort, many frameworks adopt a very direct implementation: they maintain the entire conversation as an array and use that whole array as the model context. At runtime, new messages are appended to the end of the array; when it fills up, old messages get pushed out from the front.

    This works perfectly fine for short conversations—few messages, plenty of room. But once the task grows long—say, a complex flow spanning dozens of turns with repeated tool calls—the array quickly hits its limit, and earlier messages get silently pushed out of the queue.

    The key word is silently: no error, no warning, no hint of any kind. The old content simply vanishes, and the caller has no idea it was ever dropped. Later, when you ask “what was that initial parameter again?”, the model’s answer is naturally vague—because that data really is no longer in the array.

    The root of the problem is a design choice: letting the context—which mutates and loses data—serve alone as the sole carrier of truth. Once the truth carrier can be written to, deleted from, compressed, and overwritten, the completeness and consistency of memory are out the window. This is a structural defect. Enlarging the array or tuning the compression strategy can only mitigate it, never cure it.

    3. A Different Approach: Truth Lives in a Single Append-Only Log

    A more robust approach is to fully decouple “facts” from “projection.” The core idea: don’t pack truth into the context—write it into an append-only event stream, commonly called a Session Log.

    Three design pillars support this log.

    3.1 A session is a sequence of ordered events

    A session is no longer viewed as “a pile of messages” but as “a sequence of events recorded in the order they occurred.” Each event carries a type, a monotonically increasing sequence number, a payload, and a timestamp. Every user input, every step the model generates, and every tool call and tool result is appended as an independent event:

    1. // Base structure of an event: ordered and immutable
    2. interface SessionEvent {
    3. seq: number; // monotonically increasing sequence number, guarantees global ordering
    4. type: EventType; // turn | step | user_message | tool_call | tool_result ...
    5. payload: unknown; // event payload
    6. ts: number; // timestamp of when it occurred
    7. }

    Append-only means that once written to the log, an event is never modified or deleted. To “correct” a fact, the right move is to append a new event that overrides its effect—not to go back and rewrite history. This gives you natural auditability: the entire timeline is fully preserved.

    3.2 The model sees a “projection,” not the “storage”

    The model does not read the log directly. Before each inference step, the system runs a projection function—something like deriveMessages()—that maps the event stream into the “list of messages the model can read this turn” according to defined rules:

    1. // Append-only log: writes are allowed only at the tail
    2. class SessionLog {
    3. private events: SessionEvent[] = [];
    4. append(type: EventType, payload: unknown): SessionEvent {
    5. const e: SessionEvent = { seq: this.events.length, type, payload, ts: Date.now() };
    6. this.events.push(e); // append only, no modify, no delete
    7. return e;
    8. }
    9. read(): readonly SessionEvent[] { return this.events; }
    10. }
    11. // Projection: render the log into the messages visible to the model this turn
    12. function deriveMessages(log: SessionLog, policy: RenderPolicy): Message[] {
    13. const out: Message[] = [];
    14. for (const ev of log.read()) {
    15. switch (ev.type) {
    16. case "user_message": out.push({ role: "user", content: ev.payload }); break;
    17. case "assistant_message":out.push({ role: "assistant", content: ev.payload }); break;
    18. case "tool_result": out.push({ role: "tool", content: ev.payload }); break;
    19. // whether other events are projected, and how they are compressed, is up to the render policy
    20. }
    21. }
    22. return policy.apply(out); // e.g. summarize and compress early events
    23. }

    The model still receives “one context,” but this context is a product derived in real time from the complete log, not the truth itself. The log is complete and persistent; the projection can be trimmed, compressed, and re-rendered for different purposes, yet it never corrupts or erodes the underlying facts.

    3.3 A hard invariant: model-visible means logged

    The most important constraint in this design is: anything the model can see must first be written to the log (model-visible means logged).

    The meaning is straightforward: any content that enters the model’s context must have a corresponding source event you can find in the log. If you want to add a new visible input for the model—say, a system prompt, retrieved documents, or metadata from an external tool—the correct approach is to first define a new appendable event type, then have the projection function render it into the context, rather than sneaking a string into some corner.

    This invariant may look like mere discipline, but it’s actually a safeguard: it guarantees that “what the model saw” is always traceable. When debugging reveals that the model saw something it shouldn’t have, you can locate exactly which event it was and when it was appended just by inspecting the log.

    4. How This Architecture Solves Both Problems

    Solving “memory loss”

    In the traditional approach, memory disappears when the context array overflows. In the event-stream approach, memory rests in the log and is never lost because of trimming at the projection layer. Even if the render policy summarizes and compresses early events to control tokens, the original events remain intact in the log—ready to be re-projected and replayed one by one whenever needed. What’s lost is no longer the facts, but merely “this turn’s decision not to show something to the model.”

    Solving “multi-view inconsistency”

    An agent system usually has several “eyes” looking at the same session: the model needs to see it, the frontend UI needs to display it, debugging tools need to replay it, and analytics need to measure it. If each maintains its own copy of the data, they will eventually fall out of sync and contradict each other—what the interface shows and what the model actually saw may no longer match.

    A single source of truth ends this divergence: the truth is stored in exactly one place, in the log; whoever needs it projects from this log on demand. The model view, the UI view, and the replay view are all different renderings of the same log, so they stay consistent by nature. During debugging you can replay the log as-is and precisely reproduce every word the model saw at the time.

    5. Practical Implementation Points

    • Make events immutable and orderable: use a monotonically increasing sequence number to establish global ordering, and avoid relying on clock precision; once written, an event is never changed.
    • Separate the render policy from the facts: trimming, summarizing, and formatting are all the job of the “projection layer.” Don’t write them into the storage layer, or you’ll slip back into the old “context as truth” trap.
    • Make projections replayable and reproducible: the same log plus the same render policy should reliably produce the same model context—this is the foundation of debuggability.
    • Distinguish the rendering needs of different views: the model’s context pursues information density, the UI view pursues readability, and the debug view pursues full fidelity—let them share one log while each takes what it needs.
    • Hold the line on model-visible means logged: treat it as a hard constraint in code review. Any newly added visible input must go through the path of “first define an event, then render it via projection.”

    6. Summary

    An agent’s “memory loss” and “view inconsistency” look like model problems on the surface, but they’re architecture problems at heart: mistaking mutable context for immutable facts. By pushing the truth down into a single append-only event log, deriving the model context through projection on demand, and enforcing “anything model-visible must be logged first,” you eliminate memory loss at the source and keep the model view, UI view, and debug view permanently consistent. The key to this approach isn’t any specific implementation—it’s a clear boundary: facts belong to the log; context is a projection.