Brief IA

AI Agents: 5 Models for Optimized Memory and State

🛠️ AI Tools·Tom Levy·

AI Agents: 5 Models for Optimized Memory and State

AI Agents: 5 Models for Optimized Memory and State
Key Takeaways
1LLMs, by nature stateless, require solutions to manage memory and state effectively.
2The context work buffer allows for managing the ephemeral state of an AI session, which is essential for multi-step reasoning.
3Semantic and episodic memory ensures the persistence of knowledge and actions across distinct sessions.
💡Why it mattersThese models are crucial for enhancing the performance and reliability of AI agents over the long term.
Le brief IA que lisent les pros

Le brief IA que les pros lisent chaque soir

Les 7 actus IA du jour, décryptées en 5 min. Gratuit.

Inclus dès l'inscription : notre sélection des meilleurs guides & comparatifs IA.

Choisis ton rythme

Gratuit · Pas de spam · Désabonnement en 1 clic

📄
Full Analysis

Memory and State for AI Agents

Creating an artificial intelligence agent is a complex task that becomes even more challenging when it comes to maintaining its effectiveness over an extended period, such as six months. Large language models, or LLMs, are designed to be stateless. This means that each interaction with the model starts from scratch, with no memory of previous exchanges. Initially, developers attempted to work around this issue by integrating the complete history of conversations into the context window, hoping that the model could effectively utilize it.

However, this method quickly showed its limitations. Models suffer from increased latencies, and their ability to use contextual information degrades. Relevant facts can get lost in a flood of information, and when multiple versions of the same fact exist, there is no guarantee that the model will choose the most recent version. Additionally, token costs increase significantly, although caching prompts has helped mitigate this issue for stable prefixes. The solution does not lie in increasing the size of the context window, but rather in a deliberate architectural approach to managing memory and state.

Before diving into the different models, it is crucial to clarify the terms "memory" and "state," which are often confused. The state is a snapshot of what the agent currently knows about a given task: the current step, the last tool call made, and the tracked variables. It can be compared to a whiteboard that is constantly updated as the task progresses. At the end of the session, this state disappears unless one chooses to retain it, which is the purpose of Model 2.

Memory, on the other hand, is the mechanism that allows information to be carried beyond immediate boundaries: to the next turn, the next session, or even a different agent executed later. Working memory concerns the short term (turn by turn), while semantic and episodic memory extends over multiple sessions.

These two concepts interact in a specific cycle. At the beginning of a task, the agent consults memory to establish its initial state, loading relevant facts, applicable behavioral rules, and records of past failures on similar tasks. During the task, the state is continuously updated. As the task progresses and concludes, the agent writes selected fragments of this state into memory so that the next turn or session can benefit from past events. Memory feeds the state, and the state enriches memory.

This distinction is crucial because the modes of failure differ. A failing state means that the agent loses track of its ongoing task. A failing memory prevents the agent from learning, personalizing, and forces it to treat each interaction as a blank slate. Both types of failures are common in production systems and require distinct solutions.

The following five models address these issues: Models 1 and 2 focus on state management, while Models 3 and 4 build the memory layer that persists between sessions, and Model 5 constrains both.

1. Contextual Working Buffer (Short-Term Execution)

The Concept

Working memory is responsible for managing the ephemeral state of the current session. It includes the active prompt, recent exchanges, and live tool outputs. It can be compared to the agent's short-term workspace, which is cleared at the end of the session.

How It Works

Instead of allowing the message list to grow indefinitely, the working buffer operates as a sliding window. The agent records immediate reasoning steps on a notepad. When the buffer reaches a token limit, a summarization process compresses older exchanges into a dense summary, retaining logical conclusions and eliminating raw tool outputs. At the end of the task, the buffer is cleared: valuable information is extracted to long-term storage, while the rest is deleted.

It is important to note that this ongoing summarization process can alter the prompt prefix, invalidating the KV cache and causing a spike in latency during the next call. This trade-off must be considered during design.

When to Use It

Every agent needs this mechanism. It is the foundation for managing multi-step reasoning within a session.

2. Execution Checkpoint (Fault Tolerance and Pause)

Once a strategy is in place for managing what the agent retains in memory during a session, the next question is how to handle session interruptions.

The Concept

Long-duration tasks are prone to failure. An agent may time out, hit a rate limit, or be paused awaiting human approval for an action. The checkpoint saves the agent's working state in a database, allowing it to resume execution exactly where it left off, without having to repeat the work already completed.

How It Works

Graph-based frameworks model workflows as nodes and edges. After each step, the framework records the state of the workflow, including variables, history, and current position, in durable storage such as PostgreSQL or SQLite. If the agent crashes, it reloads the last checkpoint and resumes from there.

A common challenge for practitioners is that resuming does not guarantee unique execution. If a node was partially executed before crashing (for example, sending an email or writing a line to the database), it may be re-executed upon resumption. Nodes with side effects must be idempotent. It is also important to note that open file handles and client objects cannot be saved, limiting what can be secured in the state.

When to Use It

Essential for systems requiring human intervention, regulated workflows where actions need approval, and any long-duration task prone to network failures.

3. Semantic Memory (Inter-Session Knowledge)

The checkpoint manages continuity within a task. But what about knowledge that must survive completely distinct sessions?

The Concept

Semantic memory represents what the agent knows: facts, user preferences, and domain knowledge that persist across independent sessions.

How It Works

Facts are extracted asynchronously and stored in an external database, typically a vector store with metadata filtering, sometimes associated with a knowledge graph where traversing relationships is crucial. When a query arrives, the system retrieves the most relevant facts and injects them into the prompt before the model processes them. It is important to note that extraction may require one or more additional LLM calls, depending on the architecture, often one per turn.

A challenge to consider during design is managing contradictory facts: if a user mentions "I use Postgres" in March and "we migrated to Snowflake" in July, both facts end up in the store. Retrieval could surface either one. Invalidating facts, through recency weighting, supplantation logic, or TTL, is essential to resolve the issue of outdated facts.

It is also crucial to clarify that identifiers and secrets are not part of semantic memory. Do not store API keys in a retrievable store. Prompt injection or excessive retrieval could expose them in a model response. Secrets should be stored in a secrets manager, where the agent obtains a credential identifier without ever seeing the value.

The reverse risk is also significant: unreliable content (a retrieved page, a user message, a tool output) extracted into semantic memory as a "fact" can persistently mislead the agent. Since there is no prompt equivalent for parameterization, no strict separation between instructions and content, provenance tagging is crucial: track where a fact comes from and limit its influence accordingly.

When to Use It

Personal assistants, coding copilots, or enterprise agents that need to remember a user's preferred coding style, architectural guidelines, or database schema conventions between sessions.

4. Episodic Event Logs (Historical Reflection)

Semantic memory stores what the agent knows; episodic memory stores what the agent has done.

The Concept

Episodic memory acts as a chronological record of the agent's execution trajectory: Goal, Plan, Tool Calls, Outcome.

How It Works

When the workflow concludes, a background process records this complete trajectory. Before the agent tackles a similar task, it queries this log. If it previously failed a database query due to a syntax error, episodic memory brings up this context so that the agent does not repeat the mistake.

A caveat: retrieved failure traces are consultative, not binding. The model may ignore them. There is also a risk of contamination: if a unique environmental failure is recorded as a strategy failure, you are persistently teaching the agent the wrong lesson. Keep this in mind.

When to Use It

Autonomous coding agents, data engineering pipelines, and planning systems that need to learn from past mistakes without human intervention.

5. Multi-Scope Segregation (Enterprise Privacy)

Once memory persists, the question is who can see it. As soon as your system serves more than one user, memory must be isolated.

The Concept

Memory is not a single shared bucket. A fact learned while assisting User A should never appear for User B.

How It Works

Each memory write is tagged with identity scopes: user_id, session_id, org_id. Retrieval strictly filters based on the active user's authentication token. When possible, apply this at the storage level, through tenant namespaces or row-level security, rather than relying solely on application-level query filters. A forgotten WHERE clause fails in open mode; storage-level isolation fails in closed mode.

This is a prerequisite for data privacy compliance, not the finish line. The more challenging issue is deletion: when a user exercises their right to erasure, you must delete not only their raw data but also the embeddings, summaries, and derived facts that result from it.

When to Use It

Any SaaS product, multi-tenant system, or enterprise deployment where data boundaries must be respected.

Summary

These models alone do not cover the limits of growth. Over a six-month deployment, semantic and episodic stores will accumulate near-duplicates, outdated entries, and noise. Retrieval quality degrades as stores fill up, and costs increase with them. TTLs, consolidation jobs, and size policies are essential; they are part of operating memory at scale.

The context window is not a database. When you decouple memory into distinct components, short-term buffers for execution, episodic and semantic memory must be carefully managed to avoid performance and cost issues.

Brief IA — L'actualité IA en français

L'essentiel de l'actualité de l'intelligence artificielle, décrypté et expliqué chaque jour.