3 min readPart 1

Context Is Not Memory: Why Durable Agent Memory Must Be Infrastructure

LLMs reason over working context, but durable memory, idempotency, and state persistence belong in deterministic infrastructure.

J

José Vásquez

Founder & Lead Engineer @ Invariant

#AI Agents#Memory#Infrastructure#Event Sourcing
Article SeriesReliable Agent Architecture
Part 1Context Is Not Memory: Why Durable Agent Memory Must Be Infrastructure

Large Language Models are extraordinary reasoning engines. Given structured context, they can synthesize ambiguous intents, plan multi-step workflows, and select relevant tools.

However, in the rush to build autonomous systems, the industry has conflated two fundamentally different concepts: Working Context and Durable Memory.

When you treat conversation history as your application database, your agents inevitably become brittle, expensive, and non-deterministic.

Core Thesis

Context is transient working memory for inference. Durable memory is an infrastructure concern governed by deterministic state machines.


The Context Illusion

In simple agent demos, developers typically append every event into a monolithic prompt array:

// The naive agent pattern: Storing the world in the prompt
const messages = [
  { role: "system", content: "You are an autonomous assistant..." },
  { role: "user", content: "Process batch invoice #892" },
  { role: "assistant", tool_calls: [...] },
  { role: "tool", content: JSON.stringify(rawDatabaseDump) },
  // ... 40 turns later ...
];

This pattern creates an illusion of memory. The model appears to "remember" because past tokens are resent on every inference pass. But this approach creates critical failure modes in production:

1. Token Bloat & Latency Degeneracy

As workflows progress, prompt size grows linearly with time and quadratically with tool responses. A 50-step financial reconciliation workflow can easily consume 90,000 tokens per step, turning sub-second operations into 12-second roundtrips costing dollars per run.

2. Loss of State Authority

If the model hallucinates a past transaction ID or misinterprets a nested JSON payload from 30 turns ago, there is no validation layer to correct it. The prompt is untyped, unstructured text.

3. Catastrophic Recovery Failures

If the Node.js process crashes, where does execution resume? If the server restarts halfway through a 10-step operation, re-running the prompt triggers duplicate side effects (e.g., charging credit cards or sending duplicate emails twice).

Context vs. Durable MemoryInvariant Architecture
CONTEXT (Transient Working Memory)

What the model attends to during inference. Ephemeral, token-constrained, and expensive.

// Vulnerable to token bloat

Prompt = SystemPrompt + FullHistory + ToolDumps

→ High latency, lost progress on crash

DURABLE MEMORY (Infrastructure State)

The immutable record of truth. Stored in durable storage (PostgreSQL), hydrated on demand into clean projection views.

// Deterministic Event Sourcing

State = reduce(Events, initialState)

→ Compact prompt projection + full audit trail

Figure 1: Context is an ephemeral lens over a projection; Memory is the immutable system of record.

The Separation of Concerns

To build production-grade agent systems, we must decouple probabilistic reasoning from deterministic persistence.

Working Context (Probabilistic)

Context is the ephemeral slice of data necessary for the model to make one specific reasoning step. It should be:

  • Minimal and focused (reducing distraction and token cost)
  • Dynamically synthesized from current system state
  • Strictly discarded or compressed after inference

Durable Memory (Deterministic)

Durable memory is the auditable, replayable source of truth. It should be:

  • Backed by relational or event-sourced persistence (such as PostgreSQL)
  • Governed by strict schemas and state machine transitions
  • Independent of any specific LLM provider or context window size
Key Architectural Tenet

Models should never own state. Models reason over projections of state, and emit intent payloads that infrastructure validates and records.


How Invariant Solves This: Session Hydration & Event Sourcing

In the Invariant state model, authoritative workflow state is not stored in prompt strings. It is derived from committed execution facts:

import { invariant } from '@invariant-tech/sdk';
import { sqlite } from '@invariant-tech/sqlite';
import { z } from 'zod';

const app = invariant({ storage: sqlite('./data/invoices.db') });

export const invoiceReconciliation = app
  .workflow('invoice-reconciliation', {
    inputSchema: z.object({
      invoiceId: z.string(),
      matchedItems: z.array(z.string()),
    }),
  })
  .step('record-reconciliation', ({ input }) => ({
    invoiceId: input.invoiceId,
    matchedItems: input.matchedItems,
    status: 'VERIFIED' as const,
  }))
  .capability('persist-reconciliation', {
    idempotencyKey: 'invoice-reconciliation:{{runId}}',
    handler: async ({ state }) => persistReconciliation(state),
  });

export const invoiceProjection = app.projection('invoice-summary', ({ runtime }) => ({
  status: runtime.activeExecution?.status ?? null,
  currentNodeId: runtime.activeExecution?.currentNodeId ?? null,
  allowedActions: runtime.validActions.map((action) => action.name),
}));

When an agent needs to reason, Invariant can expose a clean, compact view derived from durable Session and execution truth (Projections).

Persisted history can reconstruct committed truth after a restart. The current Beta does not automatically attach a new process to an active execution or claim pending commands, and external effects remain at-least-once when redispatched. Providers must honor stable idempotency keys.


What Comes Next

Treating context as memory was a necessary stepping stone during the early days of generative AI. But as we build autonomous systems that handle real money, customer data, and mission-critical workflows, our infrastructure must mature.

In Part 2 of this series, we examine the execution boundary: Why Agents Need a Deterministic Runtime.

J

José Vásquez

Founder & Lead Engineer @ Invariant

Building the durable execution engine for TypeScript AI applications. Keep reasoning probabilistic. Make execution predictable.