# Build memory-aware AI agents with Agentic Retrieve on Amazon Bedrock Managed Knowledge Base

> Source: <https://aws.amazon.com/blogs/machine-learning/build-memory-aware-ai-agents-with-agentic-retrieve-on-amazon-bedrock-managed-knowledge-base/>
> Published: 2026-11-16 22:43:30+00:00

## [Artificial Intelligence](https://aws.amazon.com/blogs/machine-learning/)

# Build memory-aware AI agents with Agentic Retrieve on Amazon Bedrock Managed Knowledge Base

Users of a knowledge-grounded application expect it to remember them, not only to search well. They ask follow-up questions that make sense only in the context of the previous turn. They expect an answer about a policy or a procedure to reflect what they told the application about themselves last month. And they still expect every answer to be grounded in the documents they are allowed to read, with citations. Whether the application is a customer support assistant, a document chat inside a SaaS product, or an internal assistant over company policies, the expectation is the same.

Meeting that expectation is hard, because the context that would sharpen a search lives outside the knowledge base. A user’s location, role, or product tier was often mentioned in an unrelated conversation weeks ago. To use it, the application has to store every conversation, extract durable facts from it, decide which facts matter for the current question, and inject them into the request. When many users share the application, every one of those steps also has to be isolated per user, and so does every document.

[Amazon Bedrock Managed Knowledge Base](https://aws.amazon.com/bedrock/knowledge-bases/) and [Amazon Bedrock AgentCore memory, a capability of Amazon Bedrock AgentCore](https://aws.amazon.com/bedrock/agentcore/), now take over these steps: storing each conversation, extracting facts from it, and bringing the relevant facts into the search. You turn this on by adding a `memoryConfiguration` to the `AgenticRetrieveStream` request, which makes AgentCore memory a retrieval source alongside your knowledge base. Within the same call, the service restores the conversation, and the planner decides on its own when a fact about the user would help. It retrieves that fact from long-term memory and uses it to compose the sub-queries it sends to the knowledge base. After generating the answer, the service saves the turn back to memory. The same request carries a `userContext`, so the knowledge base returns only the documents that the user’s access control lists (ACLs) allow. The integration is opt-in and backward compatible. Your application keeps only what is genuinely yours: authenticating the user and passing who they are.

This post shows how to build such an assistant for many users who share one knowledge base and one memory resource. Each user sees only the documents they may read, and each user’s memory stays private. The running example is an internal employee assistant for AnyCompany, a fictional company. Employees sign in with Amazon Cognito and ask questions over HR policies, IT guides, engineering runbooks, and sales plans. We show how to read the user’s identity from the Cognito ID token, how to pass it to `AgenticRetrieveStream` for document ACLs and for memory, how to design memory namespaces for many users, and how to read the trace to see when the planner used memory. Finally, we make AWS Identity and Access Management (IAM) enforce per-user memory isolation even when application code gets it wrong. If you deployed the application from [Build multi-tenant agentic chat applications on enterprise data with Amazon Bedrock Managed Knowledge Base](https://aws.amazon.com/blogs/machine-learning/build-multi-tenant-agentic-chat-applications-on-enterprise-data-with-amazon-bedrock-managed-knowledge-base/), this post replaces its “persist conversation history yourself” step.

## Solution overview

AnyCompany keeps its documents in Amazon Simple Storage Service (Amazon S3). One managed knowledge base syncs them through an ACL-enabled Amazon S3 data source. A global ACL file in the same bucket says which employees may read each folder: everyone reads HR and IT, only engineers read the engineering runbooks, and only salespeople read the commission plan. One AgentCore memory resource is shared by every employee. Amazon Cognito authenticates employees, and its ID token supplies two claims: the `sub` (user ID) identifies the user’s memory, and the verified `email` identifies the user to the knowledge base’s ACL filter. Three test users sign in: two engineers, Maya and Liam, and one salesperson, Sofia. The trace excerpts in this post are taken from a run of the sample application in the [GitHub repository](https://github.com/aws-samples/sample-managed-knowledge-base-patterns/tree/main/agentcore-memory-multiuser).

1. The employee signs in with Cognito and receives an ID token, a JSON Web Token (JWT) signed by the user pool.
2. The application’s API validates the token — an Amazon API Gateway JWT authorizer in production, a small FastAPI service in the sample. Unauthenticated requests do not reach the handler.
3. The handler reads two claims from the verified token: `sub` becomes the memory`actorId` and namespace, and`email` becomes the`userContext.userId` . The client body carries only`{ question, sessionId? }` . The handler then assumes the application’s IAM role for this user, with the session tag`userId=<sub>` .
4. The handler calls `AgenticRetrieveStream` with the knowledge base as the retriever,`userContext` , and a`memoryConfiguration` bound to this user.
5. Managed Knowledge Base restores the conversation history from short-term memory (STM) in AgentCore memory for the actor and session identified by `sessionBinding` .
6. Managed Knowledge Base retrieves from the knowledge base with ACL filtering based on `userContext` . It can also retrieve the user’s configured long-term memory through speculative retrieval or memory searches selected by the planner.
7. Managed Knowledge Base streams a cited answer and trace events back to the handler. The handler records the traces with a correlation ID and forwards the answer and trace information to the UI.
8. With `persistenceMode: DEFAULT` , the service saves the question and generated answer to the session. AgentCore memory then extracts long-term records asynchronously according to the configured strategies.

*Figure 2: One `AgenticRetrieveStream` turn with `userContext` and `memoryConfiguration`. Everything between the first and last arrow is the service’s work.*

Managed Knowledge Base owns ingestion, ACL filtering, indexing, ranking, planning, retrieval, and generation. With `memoryConfiguration`, it also owns session restore, memory search, and persistence. AgentCore memory owns storage, retention, encryption, and extraction. Your application owns authentication, the UI, and reading the user’s identity from the token. There is no conversation table: when the UI lists or reopens a conversation, it reads the same memory resource.

### How identity maps to the request

Every value is derived by the application from the verified ID token. None comes from the request body.

| **Request field** | **Derived from** | **What it scopes** | **Enforced by** | 
| `userContext.userId` | ID token `email` (verified, immutable in the pool) | Which documents can be retrieved | Knowledge base ACL filtering | 
| `memoryConfiguration.sessionBinding.actorId` | ID token `sub` | Whose short-term memory is restored and written, and the `{actorId}` in long-term namespaces | AgentCore memory keying, plus an IAM condition | 
| `memoryConfiguration.sessionBinding.sessionId` | Generated by the application for a new conversation. Listed with `ListSessions` | Which conversation within that actor | Keyed under the `actorId` | 
| `memoryConfiguration.retrievalConfigs[0].namespacePath` | `/anycompany/<sub>` | Which long-term records the planner may search | IAM condition | 

Two different claims do two different jobs. The knowledge base identifies users by email, because that is what the connected data source records in its ACLs. Memory needs an `actorId` that never changes and is never reused. The Cognito `sub` is both. An email address can be changed, or reassigned to a new employee after someone leaves, and that person would then inherit the old memory. You can use another stable user ID from your own user store as the `actorId`. What matters is that it never moves to a different person.

## Prerequisites

- This walkthrough uses a managed knowledge base and an AgentCore memory resource in the US West (Oregon) Region (us-west-2). As described in the [agentic retrieval documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/kb-test-agentic-retrieve.html) , the memory resource used with this integration must be in the same AWS account as the knowledge base and in the`ACTIVE` state.
- Python 3.10 or later and Boto3 1.43.72 or later.
- Permission to create an S3 bucket, a Cognito user pool, IAM roles, a knowledge base, and a memory resource. The sample’s `infra/setup.py` creates all of these resources idempotently.

## Solution walkthrough

The following steps create the knowledge base and its data source, set up the memory resource and Amazon Cognito, build the request, and read the trace to confirm the result.

### Step 1: Create the knowledge base with an ACL-enabled data source

A managed knowledge base needs no vector store:

Configure the service role for your setup by following [Create a service role for managed Amazon Bedrock Knowledge Bases](https://docs.aws.amazon.com/bedrock/latest/userguide/kb-managed-permissions.html), and pass its ARN in `roleArn`. For model access requirements, also review the [prerequisites for managed and custom models](https://docs.aws.amazon.com/bedrock/latest/userguide/kb-managed-prereqs.html).

Documents live in the bucket under `docs/<folder>/`. Amazon S3 has no permission system of its own to crawl, so you supply the ACLs in a global ACL file in the same bucket. Each entry maps an S3 prefix to the emails that may read it:

The sample’s file also gives everyone `docs/it/`. Then create the data source. For a managed knowledge base, every connector has the type `MANAGED_KNOWLEDGE_BASE_CONNECTOR`, and `connectorParameters.type` picks the source:

`aclEnabled` cannot be changed after the data source is created. `CreateDataSource` is asynchronous, so wait for the status `AVAILABLE`. Then call `StartIngestionJob` and wait for `COMPLETE`. The sync reads the documents and the ACL file and indexes each document with its ACL. In the sample, all 11 documents were scanned and indexed with 0 failures. A document that no ACL entry covers is not ingested, and a user whose email matches no entry receives no results from this data source.

The same model applies to connectors that crawl permissions from the source system, such as Microsoft SharePoint, Google Drive, and Atlassian Confluence. There, the permissions come from the source, groups are resolved from the source, and the knowledge base also checks each result against the source in real time. Your application code does not change: it still passes the user’s email in `userContext`. For the full list, see [Access Control Lists awareness enablement](https://docs.aws.amazon.com/bedrock/latest/userguide/kb-managed-acl.html).

### Step 2: Create the memory resource and design namespaces

One memory resource serves every user. Per-user separation comes from `{actorId}` in t
