Append-Only Session Logs: Eliminate Agent Memory Loss and View Inconsistency

    Long-running AI agents often suffer from memory loss and inconsistent views between the model context, UI, and debugging dashboards. The root cause is rarely the model itself, but the session state management architecture—specifically, the mistaken assumption that the context window is the source of truth. This article proposes an append-only event log design where the log is the single source of fact, and the model's context is merely a projected view. This approach turns silent memory eviction into explicit projection policies and unifies all views through a single, replayable, and auditable data source.

    In long-running conversational sessions and AI agent systems, recurring issues plague development teams: the model appears to “forget” what was said in a previous turn, tool call results vanish without a trace, and the user interface displays a state that contradicts the model’s actual context. A common first reaction is to blame model capability or an insufficient context window. However, a more frequent root cause lies in session state management: the system mistakenly equates the model’s current context window with the session’s factual record. When context is truncated, compressed, or replaced, the facts are lost. Furthermore, when the model view, the UI view, and the debug view each maintain their own copy of the data, inconsistency is inevitable.

    A more robust design treats the conversation as an append-only event stream. All occurred events are written sequentially to a log, which serves as the single source of truth. The context the model sees each turn is not the storage itself, but a message list projected from the log according to a defined policy. This way, memory no longer depends on a mutable in-memory array, and multiple views are no longer stored separately but rendered from the same source.

    1. Root Cause: Mistaking “View” for “Fact”

    Many frameworks use a mutable array for the entire conversation for simplicity, feeding it directly as the model context. In short dialogues, this works. However, in multi-turn, tool-calling, and long-horizon tasks, the array overflows, and older messages are silently evicted. There is no error, no notification—the model simply “cannot see” that part of history in the next turn. It isn’t truly forgetting; it never retained the memory at all, but only read a temporary context snapshot per turn. Once that content is evicted, the model has no knowledge of what transpired.

    A deeper design flaw is that the context is both the sole carrier of facts and a mutable, lossy view. The interface needs a complete timeline, debugging needs to inspect tool arguments and results, the model requires a condensed context, and auditing demands an immutable record. If all these requirements read from separate data structures, multiple divergent truths are inevitable. The solution is not to enlarge the context array, but rather to separate storage from the view.

    2. Core Principle: Log as Fact, Context as Projection

    The key principle of an append-only session log can be summarized in four points.

    First, a session is a sequence of events, not a block of text. Each event must minimally contain a type, a monotonically increasing sequence number, a timestamp, and its payload. User messages, assistant messages, tool calls, tool results, state changes, and summarization policy updates are all recorded in the order they occur. Once an event is written, it is never modified in place; corrections are expressed by appending new events.

    Second, the model does not retain memory. Before each inference step, a projection function maps the log onto a list of model-readable messages. Projection can filter, collapse, summarize, or reorder, but it is only a view, not storage. What the model sees can change with the projection policy, while the facts remain intact in the log.

    Third, the model’s visibility is contingent on what has been recorded. Any input intended for the model must first be defined as an event type, written to the log, and then rendered from the log. It is forbidden to temporarily assemble a piece of “model-only” content in a corner of the codebase. Otherwise, that content cannot be replayed or audited, and it breaks the single source of truth.

    Fourth, all views are derived from the same source. The UI, debugging tools, audit logs, and model context all come from projecting the same log. Different views can have different granularities: the UI can show the full timeline, the model can see a compressed message list, and debugging can display full events with metadata. However, they all reference the same sequence numbers and are consistent with each other, avoiding inherent contradiction.

    3. Solving “Memory Loss”

    Under the log architecture, old content is not lost simply because the context window is full. The projection strategy can decide which events this turn’s model sees: retaining recent turns, prioritizing critical tool results, replacing lengthy history with a summary event, or using controlled retrieval to re-project relevant historical events into the context. Crucially, compression changes the view, not the underlying facts. Original events remain in the log and can be restored for replay, audit, or re-projection when needed.

    This yields two direct benefits. First, memory loss shifts from being a “silent eviction” to an “explicit policy.” If a historical detail does not enter the model’s context, it is because the projection rules chose not to display it, not because an array silently dropped it. Second, long-horizon tasks can rebuild context in stages. The system can regenerate the message list for a specific turn from the log to determine exactly what the model saw at that time. This aids in diagnosing whether an issue stems from the projection policy, tool results, or model reasoning.

    4. Solving “View Inconsistency”

    View inconsistency typically arises from keeping separate copies. The log architecture mandates that all views read only the same source of truth. The interface renders the timeline by reading the event stream; the debugging panel expands event payloads by sequence number; the model message list is generated by the projection function; the audit view validates event integrity and order. These are not four data sets, but four projections of the same log.

    For example, the user interface may show the complete chain of tool calls, the model context may only contain the summaries of tool results, and the debug view might preserve raw arguments and return values. They differ in granularity, but they all reference the same sequence numbers. If a model’s answer is incorrect, you can replay the projection result of that turn to confirm whether the model saw a specific tool result, rather than speculating across multiple log files.

    5. Engineering Implementation Notes and Sketch

    When implementing, it is advisable to make the projection function a pure function and version the projection policy. Event schema evolution must be backward compatible. Sequence numbers are used for ordering and idempotency checks. Tool calls and their results should be recorded as a pair. Sensitive information can be redacted at the projection layer, but audit permissions and storage tiering require a separate design. Append-only does not mean never archiving. Cold data can be archived to object storage or log services; snapshots can only serve as accelerators for indexing, not as a replacement for the fact log.

    1. Event = { seq, type, payload, ts }
    2. append(log, event):
    3. assert event.seq > last_seq(log)
    4. persist_append_only(log, event)
    5. project_messages(log, policy_version):
    6. events = filter_by_policy(log, policy_version)
    7. return render_messages(events)

    When a new type of model-visible input is needed, the correct path is: first, define a new event type and write it to the log; second, extend the projection rules. This ensures that new capabilities are inherently replayable, auditable, and do not break multi-view consistency.

    6. Limitations and Conclusion

    Append-only logging does not mean blindly feeding every event into the context, nor does it suggest using summaries to replace facts. Projection outputs can still exceed context limits, so summarization, retrieval, and prioritization strategies are necessary. However, summaries should also be recorded as events themselves and retain links to the original content. Privacy and compliance requirements call for tiered storage, access control, and redaction projections on the log. Concurrent writes require ordering guarantees, and event schemas require validation and migration policies.

    In summary, modeling a session as an append-only event stream fundamentally changes the architecture from “context is fact” to “log is fact, context is projection.” It transforms memory loss from an implicit eviction into an explicit policy and shifts multiple views from divergent maintenance to consistent, same-source rendering. For agent systems requiring long-running operations, frequent tool calls, and high demands for debugging and auditability, this is a more robust architectural path.