Curtis FosterCurtis Foster
All posts

AI memory architecture: the five layers behind reliable LLM products

AI memory is not one database or a longer context window. It is five separate layers, each with a different job, cost, and failure mode.

September 29, 2026· 15 min read· Curtis Foster Team
Hero image for blog post: AI memory architecture: the five layers behind reliable LLM products

At 2:13 a.m., I was staring at a kitchen table covered in notebook pages, API logs, and one cold cup of coffee. The AI assistant I was testing remembered a user's name, forgot a decision made ten minutes earlier, and confidently repeated an outdated preference.

That is the real AI memory problem. Reliable products need five separate memory layers, not one giant context window: working context, session state, episodic memory, semantic memory, and procedural memory.

Each layer answers a different question, has a different storage pattern, and needs its own rules for retrieval, expiration, correction, and deletion. Once you separate those jobs, memory becomes something you can test and operate instead of a vague promise that the model will "remember."

Separate memory layers make failures visible, so you can fix the right system instead of rewriting the prompt.
Separate memory layers make failures visible, so you can fix the right system instead of rewriting the prompt.

I've built multi-model AI products where this separation was not an academic exercise. When a product serves real users, memory affects cost, latency, privacy, answer quality, and the user's willingness to trust the next response. The model is only one part of the system.

The five layers of AI memory

Think of an AI product as an assembly line. The context window is the workbench, not the warehouse. It holds what the model can inspect right now, but it does not decide what deserves to be saved for later.

Here are the five layers:

  • Working context contains the messages, retrieved documents, tool results, and instructions sent with the current model call.
  • Session state contains the active task, such as an unfinished checkout, an open support case, or a plan being edited.
  • Episodic memory contains dated events from prior interactions, such as a user rejecting a recommendation on March 4.
  • Semantic memory contains durable facts and preferences, such as a company's billing address or a user's preferred programming language.
  • Procedural memory contains rules for how the system should behave, including workflows, policies, tool permissions, and response formats.

This division prevents a common design error. Teams often put every prior message into a vector database and call the result memory. That creates a retrieval problem, a privacy problem, and a maintenance problem all at once.

A context window has a fixed capacity, even when the advertised number is large. OpenAI, Anthropic, and Google have all released models with context windows measured in hundreds of thousands of tokens or more, but a larger window does not guarantee the model will use every detail well. The 2023 paper "Lost in the Middle: How Language Models Use Long Contexts" found that models often perform worse when relevant information sits in the middle of a long input.

So the deeper lesson is simple: AI memory is a selection system. Your product must decide what enters the model's view, what stays outside it, and what gets deleted.

Layer one: working context is the model's desk

Working context is everything assembled for one model request. It may include the latest user message, a short conversation window, retrieved records, tool output, system instructions, and a structured summary of the task.

This is the only layer the model can directly read during the call. That makes it useful and dangerous. Add too much material and you increase token cost while giving the model more ways to follow the wrong instruction.

A support assistant handling a refund request is a good example. The model may need:

  • The customer's current message.
  • The order record from Postgres.
  • The refund policy for the customer's region.
  • The last two support turns.
  • A tool result showing whether a refund was already issued.

It does not need the customer's entire three-year conversation history. It does not need every product announcement ever written. It does not need a vector search result that happens to mention the same product name but belongs to a different policy version.

A practical context builder should have an explicit budget. For each request, measure the tokens allocated to instructions, recent turns, retrieved facts, tool results, and the model's answer. If the answer budget is 1,000 tokens and the model context is 32,000 tokens, do not quietly fill all 31,000 remaining tokens with retrieved text.

At the platforms I build (Ziggyloo and Zentoko, both running in production), I treat the context builder as a separate service boundary. It logs what was selected, why it was selected, and what was left out. That record is often more useful than the final prompt when an answer goes wrong.

Here's the part most people miss: the conventional wisdom says more context makes an AI product smarter. It does not. More context gives the model more material. Good context gives it the right material. Those are completely different problems.

Layer two: session state keeps the task coherent

Session state is not the same as conversation history. It is the current state of a task, usually stored in structured fields rather than raw text.

Imagine a travel booking assistant helping Jordan arrange a flight from Denver to Boston. The conversation may contain dozens of turns, but the active state can be compact:

  • Origin: Denver.
  • Destination: Boston.
  • Travel date: October 18.
  • Passenger count: one.
  • Preferred cabin: economy.
  • Search status: three options returned.
  • User selection: pending.

That structure is easier to validate than a transcript. It also lets the system resume after a browser refresh, a model timeout, or a handoff to a human agent.

Session state belongs in a transactional store such as Postgres or Redis, depending on durability and latency needs. The important part is not the brand of database. The important part is that state changes are explicit. A tool call should not silently change the destination because the model inferred a new preference from a casual sentence.

Use an event or command model where decisions have a clear source. "User selected flight 482" is different from "assistant suggested flight 482." The first can update booking state. The second cannot.

Session state also needs versioning. If two browser tabs update the same support case, the product must detect stale writes rather than let the last model response win. AI systems are probabilistic, but the records they change should not be.

When a session ends, decide what survives. The selected flight may become an episodic event. The user's preference for morning departures may become semantic memory, but only after repeated evidence or an explicit confirmation. Most session details should disappear.

That deletion is healthy. Memory that never expires becomes a junk drawer with an API.

Layer three: episodic memory records what happened

Episodic memory stores events with time, source, and outcome. It answers questions like: What did the user try? What did the system recommend? What was rejected? When did the policy change?

A useful event might look like this:

```json

{

"type": "recommendation_rejected",

"subject": "beginner_python_course",

"occurred_at": "2026-08-21T19:42:00Z",

"source": "user_message",

"confidence": 0.98,

"retention_until": "2026-11-19"

}

```

The date matters. So does the source. A fact extracted from a user's direct statement should rank differently from a guess made by the model.

Suppose a tutoring product recommends a Python exercise to a 14-year-old learner. The learner says, "I already did this one yesterday." That event is useful for the next session, but it should not become a permanent statement that the learner dislikes Python. One event describes what happened. A durable preference requires stronger evidence.

This is where many memory systems overreach. They convert every sentence into a permanent fact, then retrieve those facts months later as if they were still true. People change plans. Businesses change prices. A user may say they prefer email during a work trip and prefer text after returning home.

Store episodic records with:

  • A timestamp and time zone.
  • The source of the claim.
  • A confidence value or evidence count.
  • A retention rule.
  • A link to the session or transaction that produced it.

Retrieval should favor recent, relevant events, but recency alone is not enough. A failed payment from yesterday may matter more than a successful payment from this morning if the user is asking why their account is locked.

I also keep event memory separate from user-facing memory controls. If a customer asks what the system remembers, the product should be able to explain the stored event in plain language, correct it, or remove it. That is not just a compliance task. It is basic product quality.

Layer four: semantic memory stores durable facts

Semantic memory contains facts that should remain useful beyond one interaction. Examples include a company's preferred deployment region, a customer's approved billing contact, or a user's explicit preference for concise answers.

This layer often uses Postgres with structured columns, JSON fields, or pgvector for fuzzy retrieval. Vector search can help find related facts, but it should not be the source of truth for facts that affect money, permissions, safety, or legal obligation.

For example, an AI sales assistant may store:

  • Acme Robotics uses Salesforce for customer records.
  • Acme Robotics requires security review before production access.
  • The approved procurement contact is Priya Shah.
  • The account operates in the European Union.

The first two facts may support planning. The third and fourth can affect a workflow, so both need an owner, an update path, and an audit record. A vector embedding cannot tell you whether the procurement contact changed last week.

Semantic memory needs conflict handling. If a user says "My timezone is Pacific" while an account profile says Eastern, the system should not silently choose. Ask for confirmation when the difference changes behavior. If the conflict is low impact, use the newest explicit statement and record the update.

You also need a write policy. A model should not be allowed to save every inferred preference. Use categories such as:

  • Explicit facts: safe to save after validation.
  • Repeated patterns: save only after a defined evidence threshold.
  • Sensitive attributes: avoid saving unless the product has a clear, lawful reason.
  • Temporary details: keep in session or episodic memory only.

In an adaptive learning product, the same principle applies: a learner's stated goal can guide future sessions, while a single wrong answer should not become a permanent ability label.

Treat semantic memory as a maintained record, not a magical personality layer. Give users a way to inspect and change it.

Layer five: procedural memory controls behavior

Procedural memory is the layer teams most often forget, because it does not look like user data. It contains the instructions and operational rules that determine how the AI acts.

Examples include:

  • A refund assistant must verify order status before offering a refund.
  • A scheduling agent must ask for confirmation before sending an invitation.
  • A coding agent may read production logs but cannot deploy without approval.
  • A healthcare intake assistant must route urgent symptoms to a human process.

Some of these rules belong in system instructions. Others belong in application code, tool permissions, policy tables, or workflow engines. Do not place a rule in a prompt when the product can enforce it outside the model.

If an agent is allowed to call a payment tool, the permission check should happen in the tool service. A sentence saying "never issue a refund over $500" is not enough protection when a model can select arbitrary arguments.

Procedural memory should be versioned like software. When a refund policy changes on September 1, store the policy version used for each decision. When a workflow changes, run the same evaluation set against both versions. This lets you answer a simple operational question: why did the assistant do that on Tuesday?

The five layers meet inside a Guardrail Surface, the set of places where the product limits what the model can see and do. That surface includes prompt construction, retrieval filters, schema validation, tool permissions, and human approval steps.

The mistake I keep watching teams make is treating procedural memory as prompt copy. Prompts explain behavior. Application controls enforce it. Those are different jobs.

How the layers work together in production

Consider a support assistant for a hotel group handling a late-night message from a guest in Chicago. The guest writes, "My room still has no heat. I called earlier and was told someone was coming."

The system can use the layers like this:

  • Working context includes the latest message, the property's current maintenance policy, and the open ticket.
  • Session state records that the case is active, the guest has already called, and a maintenance dispatch is pending.
  • Episodic memory records the earlier call and the missed response time.
  • Semantic memory provides the guest's loyalty status and preferred contact channel, if those facts are valid and relevant.
  • Procedural memory requires escalation when a heating issue remains unresolved after a defined interval.

The assistant does not need the guest's complete hotel history in the prompt. It needs the right current facts and the authority to take the next permitted action.

Build a Memory Router that decides which layer to read and which layer may be written. The router should apply rules such as:

  • Read session state for active workflow fields.
  • Read semantic memory for validated, durable facts.
  • Read episodic memory for recent events and unresolved outcomes.
  • Read procedural memory before tool use.
  • Write new memory only when the event passes a defined policy.

Then instrument the router. Track retrieval hit rate, stale-memory rate, write rejection rate, tokens added per layer, and user correction rate. A memory system that saves thousands of facts but rarely retrieves the right one is not working.

Privacy belongs in the design from the start. Encrypt stored memory, separate tenant data, apply retention limits, and support deletion. In Europe, the General Data Protection Regulation gives people rights related to access and erasure. California's Consumer Privacy Act creates its own obligations around personal information. Your legal team will have their own view, but your architecture still needs an actual delete path.

Once you frame it this way, memory becomes an operational system with boundaries, not a bag of chat history.

A build plan for an AI memory system

Start with one workflow, not a universal memory platform. Pick a task where continuity matters and mistakes are visible, such as a support case, an onboarding checklist, or a recurring tutoring session.

Write down the five layers for that workflow. If you cannot describe what belongs in each layer, the product is not ready for long-term memory. Then define the read and write rules before connecting a vector database.

A practical build sequence looks like this:

  • Define the session-state schema and the allowed transitions.
  • Build a context assembler with token budgets and logging.
  • Add episodic events with timestamps, source fields, and retention dates.
  • Add semantic facts only for validated information with a clear owner.
  • Put procedural rules in code or policy services where possible.
  • Add user controls for inspection, correction, and deletion.
  • Test memory with cases involving stale, conflicting, missing, and sensitive information.

Run failure tests that resemble real use. Ask what happens when a user changes their email address, when two agents update the same support case, when a policy changes halfway through a session, or when retrieved text contains an instruction aimed at hijacking the system.

Watch the Context Budget as a product metric. It should show how much information each layer contributes, what it costs, and whether that material actually improves the answer. A long prompt is not evidence of a thoughtful system.

I've shipped enough technical systems to know that the uncomfortable work is usually the work that keeps production calm, and yes, it is always the unglamorous stuff. Schema design, deletion, audit logs, and permission checks are not exciting. They are what prevent a bad memory from becoming a repeated product behavior.

Start with a spreadsheet if you need to. List each memory type, its source, retention period, read conditions, write conditions, and owner. Then implement only the rows the product can defend.

FAQ

What is AI memory in an LLM product?

AI memory is the set of mechanisms that lets a product retain, retrieve, update, or discard information across model calls and user sessions. It includes more than chat history: working context, session state, episodic events, semantic facts, and procedural rules all serve different purposes.

Is a larger context window the same as long-term memory?

No. A context window is the amount of information a model can inspect during one request. Long-term memory requires storage, retrieval, write policies, expiration, correction, and deletion outside the model call.

Should I use a vector database for AI memory?

Use vector search when semantic similarity helps retrieve relevant material, such as support articles or related past events. Do not use it as the only source of truth for permissions, account records, financial facts, or policy versions that require exact updates.

How do I prevent an LLM from remembering wrong information?

Separate temporary events from durable facts, record the source and time of each claim, and require validation before writing important information. Add user correction and deletion controls, then test conflicts and stale records as part of the product's evaluation set.

What should an AI product team build first?

Build a structured session-state model and a logged context assembler for one workflow. Once you can see what the model received and why, add episodic and semantic memory with explicit retention and write rules.

Pick one workflow this week. Draw its five memory layers on a page, assign every field an owner and expiration rule, then log the exact context sent to the model. The model is not your memory architecture. The system around it is.

0 comments

Join the conversation

Subscribe to comment

Join Curtis Foster's newsletter to leave a comment and get new posts in your inbox.

Read next

Hero image for blog post: Multi-agent systems: when they help, and when one good agent wins

Multi-agent systems: when they help, and when one good agent wins

Oct 13, 2026
Hero image for blog post: What a solo founder learned shipping AI from 0 to 1

What a solo founder learned shipping AI from 0 to 1

Oct 6, 2026
Hero image for blog post: The eval loop is the product: measuring whether your AI is actually right

The eval loop is the product: measuring whether your AI is actually right

Sep 22, 2026
AI memory architecture: five layers explained