精选

← 返回 OPC日报

AWS Machine Learning Blog · 产品 ·

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

Artificial Intelligence
Build memory-aware AI agents with Agentic Retrieve on Amazon Bedrock Managed Knowledge Base
by Peng Yang, Jason Sheng, Renya Kujirada, and Siddhant Sahu on 16 NOV 2026 in Advanced (300), Amazon Bedrock Knowledge Bases, Technical How-to Permalink Share
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 and Amazon Bedrock AgentCore memory, a capability of Amazon 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, 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.
Figure 1: Architecture of the memory-aware employee assistant. The application authenticates the user and constructs the request (steps 1–4). Amazon Bedrock Managed Knowledge Base restores the session, retrieves information, returns an answer, and saves the exchange to AgentCore memory (steps 5–8). Short-term events and long-term records belong to the same memory resource.
The employee signs in with Cognito and receives an ID token, a JSON Web Token (JWT) signed by the user pool.
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.
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>.
The handler calls AgenticRetrieveStream with the knowledge base as the retriever, userContext, and a memoryConfiguration bound to this user.
Managed Knowledge Base restores the conversation history from short-term memory (STM) in AgentCore memory for the actor and session identified by sessionBinding.
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.
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.
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, 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, and pass its ARN in roleArn. For model access requirements, also review the prerequisites for managed and custom models.
kb = agent.create_knowledge_base(
name="anycompany-assistant-docs",
description="AnyCompany internal documents: HR policies, IT guides, engineering runbooks, sales plans.",
roleArn=kb_service_role_arn,
knowledgeBaseConfiguration={
"type": "MANAGED",
"managedKnowledgeBaseConfiguration": {"embeddingModelType": "MANAGED"},
},
)["knowledgeBase"]
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:
[
{"keyPrefix": "s3://BUCKET/docs/hr/",
"aclEntries": [{"Name": "maya@example.com", "Type": "USER", "Access": "ALLOW"},
{"Name": "liam@example.com", "Type": "USER", "Access": "ALLOW"},
{"Name": "sofia@example.com", "Type": "USER", "Access": "ALLOW"}]},
{"keyPrefix": "s3://BUCKET/docs/engineering/",
"aclEntries": [{"Name": "maya@example.com", "Type": "USER", "Access": "ALLOW"},
{"Name": "liam@example.com", "Type": "USER", "Access": "ALLOW"}]},
{"keyPrefix": "s3://BUCKET/docs/sales/",
"aclEntries": [{"Name": "sofia@example.com", "Type": "USER", "Access": "ALLOW"}]}
]
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:
ds = agent.create_data_source(
knowledgeBaseId=kb["knowledgeBaseId"],
name="s3-docs-acl",
dataSourceConfiguration={
"type": "MANAGED_KNOWLEDGE_BASE_CONNECTOR",
"managedKnowledgeBaseConnectorConfiguration": {
"connectorParameters": {
"type": "S3",
"version": "1",
"connectionConfiguration": {"bucketName": BUCKET, "bucketOwnerAccountId": ACCOUNT_ID},
"filterConfiguration": {"inclusionPrefixes": ["docs/"]},
"aclEnabled": True,
"aclConfiguration": {"globalAccessControlListS3Uri": f"s3://{BUCKET}/acl/global-acl.json"},
}
},
},
)["dataSource"]
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.
Step 2: Create the memory resource and design namespaces
One memory resource serves every user. Per-user separation comes from {actorId} in the namespace templates, which AgentCore memory resolves from each event at extraction time. Two choices matter.
Strategies. The two strategies extract different things. The summary strategy summarizes each conversation, so it captures facts that a user mentions in passing, such as “I’m on a Mac in the Frankfurt office.” Those facts are what personalize a later answer about, say, PTO. The user-preference strategy extracts stated preferences into structured records (“Prefers short bullet-point answers without tables”, “Always deploys to eu-central-1 first”) and does not treat incidental mentions as preferences. Summaries give you “what we discussed.” Preferences give you “how this person wants to be answered.” Use both.
Layout. retrievalConfigs accepts one entry, so a single namespacePath must reach everything the planner should see for a user. Put {actorId} directly under the root: namespacePath: "/anycompany/<sub>" then covers the profile and every conversation summary. A layout such as /anycompany/profile/{actorId} and /anycompany/summaries/{actorId} would let one entry reach only one of them.
memory = control.create_memory(
name="anycompany_assistant_memory",
eventExpiryDuration=30, # days; raw conversation events expire after this
# encryptionKeyArn=CMK_ARN, # optional customer managed key
memoryStrategies=[
{"userPreferenceMemoryStrategy": {
"name": "EmployeeProfile",
"namespaces": ["/anycompany/{actorId}/profile"],
}},
{"summaryMemoryStrategy": {
"name": "ConversationSummaries",
"namespaces": ["/anycompany/{actorId}/summaries/{sessionId}"],
}},
],
)["memory"]
Wait for the memory to reach ACTIVE. Creating the memory does not need a user or actorId, so the order of Step 2 and Step 3 does not matter. Nothing is provisioned per user: a new Cognito user becomes a new actor the first time they ask a question.
Step 3: Set up Cognito and grant the application permissions
Create a user pool and an app client without a client secret. The email decides which documents a user may read, so make it required, verified, and not changeable by the user:
pool_id = idp.create_user_pool(
PoolName="anycompany-assistant-employees",
AutoVerifiedAttributes=["email"],
Schema=[{"Name": "email", "Required": True, "Mutable": False, "AttributeDataType": "String"}],
AdminCreateUserConfig={"AllowAdminCreateUserOnly": True},
)["UserPool"]["Id"]
Mutable cannot be changed after the pool is created. With a mutable email, a signed-in user could change their email to a colleague’s address. If your users federate from Okta or Microsoft Entra ID, map the IdP’s email to this attribute so that it matches the email in your data source’s ACLs.
The sample enables USER_PASSWORD_AUTH so the local UI and tests can sign in directly. Production applications typically use Cognito managed login or AWS Amplify.
Set up an IAM role for the application. The backend assumes it once per user, with the session tag userId=<sub>, and every memory statement is conditioned on that tag. This is the complete policy the sample runs with:
{
"Version": "2012-10-17",
"Statement": [
{ "Sid": "AgenticRetrieve", "Effect": "Allow",
"Action": "bedrock:AgenticRetrieveStream", "Resource": "*" },
{ "Sid": "KnowledgeBaseRetrieval", "Effect": "Allow",
"Action": ["bedrock:Retrieve", "bedrock:GetDocumentContent"],
"Resource": "arn:aws:bedrock:us-west-2:111122223333:knowledge-base/KB_ID" },
{ "Sid": "PlannerAndGeneration", "Effect": "Allow",
"Action": "bedrock:InvokeModelWithResponseStream", "Resource": "*" },
{ "Sid": "MemoryDescribe", "Effect": "Allow",
"Action": "bedrock-agentcore:GetMemory",
"Resource": "arn:aws:bedrock-agentcore:us-west-2:111122223333:memory/MEMORY_ID" },
{ "Sid": "MemorySessionsScopedToCaller", "Effect": "Allow",
"Action": ["bedrock-agentcore:ListEvents", "bedrock-agentcore:CreateEvent",
"bedrock-agentcore:ListSessions"],
"Resource": "arn:aws:bedrock-agentcore:us-west-2:111122223333:memory/MEMORY_ID",
"Condition": {"StringEquals": {"bedrock-agentcore:actorId": "${aws:PrincipalTag/userId}"}} },
{ "Sid": "MemoryRecordsScopedToCaller", "Effect": "Allow",
"Action": ["bedrock-agentcore:RetrieveMemoryRecords", "bedrock-agentcore:ListMemoryRecords"],
"Resource": "arn:aws:bedrock-agentcore:us-west-2:111122223333:memory/MEMORY_ID",
"Condition": {"StringLike": {"bedrock-agentcore:namespacePath": "/anycompany/${aws:PrincipalTag/userId}/*"}} }
]
}
ListEvents is required with sessionBinding, RetrieveMemoryRecords with retrievalConfigs, and CreateEvent when persistenceMode is DEFAULT. ListSessions and ListMemoryRecords are for the application’s own reads (conversation list and the “what memory knows about you” view), not for AgenticRetrieveStream. The role’s trust policy allows sts:AssumeRole, sts:TagSession, and sts:SetSourceIdentity from the backend’s own identity (an AWS Lambda execution role or an Amazon Elastic Container Service (Amazon ECS) task role in production). If the memory uses a customer managed key, add kms:Decrypt on that key.
Step 4: Read the user from the token, then build the request
Verify the ID token and read the two claims. Only the ID token carries email. The access token does not:
@dataclasses.dataclass(frozen=True)
class Principal:
sub: str # -> memory actorId and namespace
email: str # -> userContext.userId (document ACLs)
def namespace_path(self, root: str) -> str:
return f"{root}/{self.sub}" # resolved literal, e.g. /anycompany/a8e1b390-...
def verify(id_token: str) -> Principal:
key = jwks.get_signing_key_from_jwt(id_token) # public keys from {issuer}/.well-known/jwks.json
claims = jwt.decode(id_token, key.key, algorithms=["RS256"], issuer=ISSUER, audience=CLIENT_ID)
if claims.get("token_use") != "id" or claims.get("email_verified") is not True:
raise PermissionError("a verified ID token is required")
return Principal(sub=claims["sub"], email=claims["email"].lower())
jwt.decode checks the signature, the issuer (your user pool), the expiry, and the audience (your app client). For details of the claims, see Understanding the identity (ID) token. With API Gateway, a JWT authorizer does the same checks and passes the claims to your handler.
Next, get the user’s AWS credentials. The backend assumes the application role for this user and caches the session until shortly before it expires:
creds = sts.assume_role(
RoleArn=APP_ROLE_ARN,
RoleSessionName=f"user-{principal.sub}",
SourceIdentity=principal.sub,
Tags=[{"Key": "userId", "Value": principal.sub}],
)["Credentials"]
Every call for this user, including the memory calls that agentic retrieval makes on the caller’s behalf, now runs as assumed-role/anycompany-assistant-app-role/user-<sub> and carries the tag that the policy in Step 3 checks.
Decide the sessionId. For a new conversation, the application generates one, such as "conv-" + uuid4().hex, and returns it to the client. To continue a conversation, the client sends it back. To show a user their conversations, call ListSessions with the user’s actorId:
def list_conversations(principal):
try:
resp = memory.list_sessions(memoryId=MEMORY_ID, actorId=principal.sub, maxResults=50)
except memory.exceptions.ResourceNotFoundException:
return [] # the user has not asked anything yet, so there is no actor
return [{"sessionId": s["sessionId"], "createdAt": s["createdAt"].isoformat()} for s in resp["sessionSummaries"]]
You do not need your own table that maps users to sessions. Accepting a sessionId from the client is safe because short-term memory is keyed by (memoryId, actorId, sessionId), and the actorId comes from the token. If Liam sends Maya’s sessionId, it names a session inside Liam’s own space, which is empty. Agentic retrieval itself does not verify that the actorId, sessionId, or namespace you supply belong to the user who is asking, so they must never come

查看来源原文