Part 1 of 7 · Enterprise AI Agent Engineering

Context Engineering and Memory Architecture: How to Keep Enterprise AI Agents Grounded

How to design context and memory for enterprise AI agents: what to load, what to remember, and a scoring framework for design reviews.

Part 1 of 7 in the Enterprise AI Agent Engineering guide

Why this matters now

Picture an insurance claims agent running inside a GCC. On Monday, a human reviewer denies a claim and records the reason. On Wednesday, the same claim returns through a different channel. The agent starts a fresh session, retrieves the policy document, finds nothing about the earlier denial, and approves it. Nothing was wrong with the model. The denial simply never reached the context. (This is an illustrative scenario, not a customer case.)

Enterprises hit this pattern because their conditions are unforgiving. Work spans several teams, each owning a different system. Records live in legacy platforms with inconsistent formats. Compliance requires that every decision can be explained later. Volumes are high enough that a small error rate becomes a daily operational cost.

In demos, none of this shows up. A single conversation fits comfortably in the model's view, and the agent looks capable. In production, the agent must work across sessions, users and systems, and it has to decide what to carry forward. That decision is engineering, not prompting.

This is the problem context engineering addresses. It is the first topic in our series because every later topic, from tools to guardrails to evals, depends on the agent seeing the right information at the right moment.

The core concept

Context is everything the model sees on a single step: instructions, the user request, retrieved documents, tool results and any remembered facts. Context engineering is the discipline of deciding what goes into that window, in what order, in what form and at what cost. A prompt is only one part of context.

Memory is information kept beyond a single step. We separate two kinds. Working memory holds state within one run, such as which documents the agent has already checked. Long-term memory holds information across runs, such as a customer's earlier decision or a team's approved exceptions. Memory architecture is the set of choices about what gets stored, where, for how long, who can read it and how it is brought back into context.

The two depend on each other. Memory decides what is available. Context engineering decides what is used. A rich memory with poor context selection floods the model with noise. A tight context with no memory forgets the denial from Monday.

Larger context windows do not remove the problem. Research on long-context models, such as "Lost in the Middle" (Liu et al., 2023), found that models often use information placed in the middle of a long input less reliably than information at the start or end. More tokens also add cost and latency. Treat the window as a budget, not a bucket.

How the pieces connect:

Sources (policies, tickets, records)
      |
      v
[Retrieval + filters]  <---  Long-term memory (stored decisions, preferences)
      |
      v
[Context assembly in the harness]  <---  Working memory (this run's state)
      |
      v
Model step  ->  tool call / answer  ->  write-back rules  ->  memory

The harness is the code around the model that builds context, exposes tools and enforces limits. We cover it in tool design and harness engineering.

A starting order for assembling context. Because models tend to use the start and end of a long input more reliably than the middle, the order matters. A reasonable first design, to be tested against your own evals:

  1. Non-negotiable rules and the agent's role, first.
  2. Prior decisions for this case, loaded by identifier from long-term memory.
  3. Retrieved source passages, capped and ranked, not whole documents.
  4. Working-memory state for this run: what has already been checked or tried.
  5. The current request, last, so it sits where the model attends most reliably.

How it works in real systems

Scenario 1: Claims processing. The agent needs the claim, the policy, any prior decisions and the current regulatory rules. A reliable design retrieves the policy clause rather than the whole policy, always loads prior decisions for the same claim identifier from long-term memory, and places non-negotiable rules at the start of context. The denial from Monday is stored as a structured record with a reason code and reviewer, so it can be retrieved by identifier rather than by similarity. Similarity search can miss it. A key lookup cannot.

What the stored record looks like. A structured entry, not a free-text note, is what makes key lookup, expiry and review possible. An illustrative shape for Monday's denial:

{
  "record_type": "claim_decision",
  "claim_id": "CLM-000123",
  "decision": "denied",
  "reason_code": "POLICY_EXCLUSION_7",
  "decided_by": "reviewer:r-204",
  "source_version": "policy-2026-09",
  "written_at": "2026-10-26T10:12:00+05:30",
  "review_by": "2027-04-26",
  "visibility": ["claims-team"]
}

Each field answers a question that a reviewer or auditor will ask later: who decided, on which version of the policy, when the entry expires, and who may read it. This is an example, not a customer record.

Scenario 2: IT service desk. The agent handles password resets, access requests and incident triage. Working memory tracks what it has already tried in this ticket, so it does not repeat steps. Long-term memory holds team-approved fixes. A rule limits write-back: only resolved tickets confirmed by a human enter memory, which keeps failed or disputed fixes out of future context.

Impact. Good context design improves reliability, because the agent sees the facts it needs. It reduces cost and latency, because it does not carry irrelevant tokens on every step. It lowers risk, because sensitive data is loaded only when a task needs it. It also supports adoption, because reviewers can see which sources informed a decision.

Design decisions and trade-offs

Decision Option A Option B Choose A when Choose B when
Retrieval Semantic search over documents Exact lookup by identifier The question is open-ended The record has a known key, such as a claim ID
Memory write Automatic after every run Gated by human or rule Content is low risk and reversible Content affects decisions or compliance
Context size Load broadly, trust the model Load narrowly, retrieve on demand Tasks are short and exploratory Tasks are long, costly or regulated
Memory scope Shared across users Isolated per user or team Facts are organisation-wide, such as policy Facts are personal or access-controlled

When not to add long-term memory: if tasks are independent and each can be answered from current source systems, memory adds a second copy of the truth that can drift. Start without it, and add it only when you can name the failure it fixes.

What breaks in production

Failure mode Symptom Root cause Mitigation
Context overload Answers get vague or miss key facts on long tasks Too many documents loaded, relevant items buried mid-context Cap retrieval, rank and trim, place critical rules at the start
Stale memory Agent cites a superseded policy or old decision No expiry or versioning on stored facts Add timestamps, source versions and review dates; expire by default
Memory poisoning A wrong or malicious entry keeps influencing later runs Unvalidated write-back from user input or tool output Gate writes, record provenance, allow review and deletion
Cross-user leakage One user sees another's data in an answer Shared memory with no access scoping Scope memory by user and role, filter at retrieval, test with cross-user cases
Lost state between steps Agent repeats work or contradicts itself within a run Working memory not carried between steps Keep an explicit run state object that the harness passes forward
Unexplained answers Reviewers cannot tell why the agent decided Context contents were not recorded Log the assembled context for each step (see observability and tracing)

Enterprise Decision Framework

Use this table in a design review. Score each row 0 (no), 1 (partly) or 2 (yes) for the agent in question. The thresholds are recommended starting points; calibrate to your environment.

Criterion 0 1 2
Every context source is named and owned None named Some named All named, with owners
Retrieval has caps and ranking None Caps only Caps and ranking
Memory writes are gated and logged Open writes Logged only Gated and logged
Memory has expiry and provenance Neither One of the two Both
Memory access is scoped by user and role Shared Partly scoped Fully scoped and tested
Assembled context is recorded per step Not recorded Sampled Always recorded

Reading the score (maximum 12):

  • 0 to 5: do not put the agent in front of real data. Fix scoping and write gates first.
  • 6 to 9: suitable for a limited pilot with human review of every decision.
  • 10 to 12: suitable for staged rollout, subject to eval results in evals for agents.

One rule overrides the score: if memory scoping is 0 and the agent touches personal or regulated data, stop regardless of the total.

Worked example. Suppose a claims agent is reviewed. Its sources are all named and owned, retrieval has caps but no ranking, memory writes are logged but not gated, entries have neither expiry nor provenance, access is only partly scoped, and the assembled context is always recorded. This is an illustrative review, not a customer result.

CriterionScoreWhy
Every context source is named and owned2All named, with owners
Retrieval has caps and ranking1Caps only
Memory writes are gated and logged1Logged only
Memory has expiry and provenance0Neither
Memory access is scoped by user and role1Partly scoped
Assembled context is recorded per step2Always recorded
Total7 of 12Limited pilot, with human review of every decision

The total allows a limited pilot, but the plan is set by the weakest rows. Expiry and provenance score 0, so they are fixed first. Scoping is only partial and the agent touches personal data, so completing it comes before any widening of the pilot.

What to do in the next 30 days

  1. Inventory context sources. Owner: AI architect. Outcome: a named, owned list of every source the agent can load, completed within week one.
  2. Add a context log. Owner: platform engineer. Outcome: the assembled context is recorded for 100 percent of test runs by week two.
  3. Gate memory writes. Owner: engineering lead. Outcome: every write has a rule, a provenance field and an expiry date by week three.
  4. Run a cross-user test. Owner: QA or security lead. Outcome: a written test set confirming that one user's memory never appears in another's answers, with zero failures before pilot.
  5. Score the design review table. Owner: head of AI. Outcome: a score and a go or no-go decision by day 30.

Closing thoughts

Our practitioner's view is that many agent problems are context problems in disguise. The remedy is simple: decide what the agent should see, decide what it should remember, and be able to show both to an auditor. If useful, download the checklist below, or talk to our architects about a context review for your agent.

Free checklist: Context and Memory Design Review

Download the Context and Memory Design Review checklist

A one-page, 14-point checklist your architects and risk team can use in the next design review.

Download PDF

Quick self-check

Tick the statements that are true for your agent today. The same checklist is available as a PDF above.

Context sources

Memory design

Testing

Governance

Frequently asked questions

What is context engineering for AI agents?

Context engineering is the discipline of deciding what goes into the model's view at each step: instructions, retrieved documents, tool results and remembered facts, in what order and at what cost. A prompt is only one part of context. Good context design keeps the agent grounded in the facts a task needs and out of the noise.

What is the difference between context and memory?

Context is everything the model sees on a single step. Memory is information kept beyond a single step. Working memory holds state within one run, and long-term memory holds information across runs, such as an earlier decision. Memory decides what is available; context engineering decides what is actually used.

Should an enterprise AI agent use RAG or memory?

They solve different problems. Retrieval brings in documents from source systems at the moment of the question, while memory holds decisions and state the agent was given or produced earlier. Use semantic retrieval for open-ended questions over documents, and an exact lookup by identifier when the record has a known key, such as a claim ID. Add long-term memory only when you can name the failure it fixes.

Do larger context windows remove the need for context engineering?

No. Research such as Lost in the Middle (Liu et al., 2023) found that models often use information in the middle of a long input less reliably than information at the start or end. More tokens also add cost and latency. Treat the context window as a budget, not a bucket.

How should enterprises govern agent memory?

Gate memory writes by a rule or a person, record provenance and expiry on every entry, and scope access by user and role. Log the assembled context for each step so reviewers can see what informed a decision, and test with cross-user cases before any pilot.

What is memory poisoning in AI agents, and how do you prevent it?

Memory poisoning happens when a wrong or malicious entry is written to memory and keeps influencing later runs. It usually comes from unvalidated write-back of user input or tool output. Prevent it by gating writes with a rule or a person, recording provenance on every entry, and allowing entries to be reviewed and deleted.

When should you not add long-term memory to an agent?

When tasks are independent and each can be answered from current source systems. In that case memory becomes a second copy of the truth that can drift out of date. Start without it, and add it only when you can name the failure it fixes, such as a prior decision that never reached the context.

References
  1. Liu et al., "Lost in the Middle: How Language Models Use Long Contexts" (2023)

Scenarios, the example memory record and the example score in this guide are illustrative, not customer cases. Thresholds and intervals are recommended starting points; calibrate to your environment. Last reviewed: October 2026.

Share

Put this into practice

Tell us about the workflow you want an agent to own. We map the controls, the failure modes and the measurement before any build starts.