View a markdown version of this page

Scenario: A customer support AI agent using AgentCore Memory - Amazon Bedrock AgentCore
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Scenario: A customer support AI agent using AgentCore Memory

In this section you learn how to build a customer support AI agent that uses AgentCore Memory to provide personalized assistance by maintaining conversation history and extracting long-term insights about user preferences. The topic includes code examples for the AgentCore CLI and the Amazon SDK.

Consider a customer, Sarah, who engages with your shopping website’s support AI agent to inquire about a delayed order. The interaction flow through the AgentCore Memory APIs would look like this:

Memory AgentCore Memory

Step 1: Create an AgentCore Memory

First, you create a memory resource with both short-term and long-term memory capabilities, configuring the strategies for what long-term information to extract.

Example
AgentCore CLI
  1. Create an AgentCore project if you don’t have one yet:

    agentcore create --name ShopSupport --template empty cd ShopSupport
  2. Create memory with summary and user preference strategies:

    agentcore add memory --name ShoppingSupportAgentMemory --strategies SUMMARIZATION,USER_PREFERENCE agentcore deploy
  3. Get the memory ID. Run the following command and copy the memory arn. The memory ID is the part of the ARN after memory/:

    agentcore status --json
    Note

    To write events, use the Amazon SDK. To inspect events and records, you can also use agentcore memory event list --id <memory-id> --actor-id <actor-id> --session-id <session-id> and agentcore memory record list --id <memory-id>.

Interactive
  1. In your AgentCore project directory, run agentcore add memory to open the Memory wizard.

  2. Enter a name for the memory, such as ShoppingSupportAgentMemory.

  3. Use the arrow keys and Space to select SUMMARIZATION and USER_PREFERENCE, then press Enter.

  4. Keep the default event retention or enter a value from 3 to 365 days.

  5. Review the configuration and press Enter to add the memory.

  6. Press Ctrl+C to exit the TUI, then deploy the project to create the memory:

    agentcore deploy
  7. Get the memory ID. Run the following command and copy the memory arn. The memory ID is the part of the ARN after memory/:

    agentcore status --json
Amazon SDK
  1. import boto3 import time from datetime import datetime # Initialize the Boto3 clients for control plane and data plane operations control_client = boto3.client('bedrock-agentcore-control') data_client = boto3.client('bedrock-agentcore') print("Creating a new memory resource...") # Create the memory resource with defined strategies response = control_client.create_memory( name="ShoppingSupportAgentMemory", description="Memory for a customer support agent.", eventExpiryDuration=30, memoryStrategies=[ { 'summaryMemoryStrategy': { 'name': 'SessionSummarizer', 'namespaceTemplates': ['/summaries/{actorId}/{sessionId}/'] } }, { 'userPreferenceMemoryStrategy': { 'name': 'UserPreferenceExtractor', 'namespaceTemplates': ['/users/{actorId}/preferences/'] } } ] ) memory_id = response['memory']['id'] print(f"Memory resource created with ID: {memory_id}") # Poll the memory status until it becomes ACTIVE while True: mem_status_response = control_client.get_memory(memoryId=memory_id) status = mem_status_response.get('memory', {}).get('status') if status == 'ACTIVE': print("Memory resource is now ACTIVE.") break elif status == 'FAILED': raise Exception("Memory resource creation FAILED.") print("Waiting for memory to become active...") time.sleep(10)

If you created the memory with the AgentCore CLI, run the following code before the next steps. Replace <memory-id> with the memory ID from the agentcore status --json output.

import boto3 import time from datetime import datetime data_client = boto3.client('bedrock-agentcore') memory_id = "<memory-id>"

Step 2: Start the session

When Sarah initiates the conversation, the agent creates a new, and unique, session ID to track this interaction separately.

# Unique identifier for the customer, Sarah sarah_actor_id = "user-sarah-123" # Unique identifier for this specific support session support_session_id = "customer-support-session-1" print(f"Session started for Actor ID: {sarah_actor_id}, Session ID: {support_session_id}")

Step 3: Capture the conversation history

As Sarah explains her issue, the agent captures each turn of the conversation (both her questions and the agent’s responses). This populates the full conversation in short-term memory and provides the raw data for the long-term memory strategies to process.

print("Capturing conversational events...") full_conversation_payload = [ { 'conversational': { 'role': 'USER', 'content': {'text': "Hi, my order #ABC-456 is delayed."} } }, { 'conversational': { 'role': 'ASSISTANT', 'content': {'text': "I'm sorry to hear that, Sarah. Let me check the status for you."} } }, { 'conversational': { 'role': 'USER', 'content': {'text': "By the way, for future orders, please always use FedEx. I've had issues with other carriers."} } }, { 'conversational': { 'role': 'ASSISTANT', 'content': {'text': "Thank you for that information. I have made a note to use FedEx for your future shipments."} } } ] data_client.create_event( memoryId=memory_id, actorId=sarah_actor_id, sessionId=support_session_id, eventTimestamp=datetime.now(), payload=full_conversation_payload ) print("Conversation history has been captured in short-term memory.")

Step 4: Generate long-term memory

In the background, the asynchronous extraction process runs. This process analyzes the recent raw events using your configured memory strategies to extract long-term memories such as summaries, semantic facts, or user preferences, which are then stored for future use.

Step 5: Retrieve past interactions from short-term memory

To provide context-aware assistance, the agent loads the current conversation history. This helps the agent understand what issues Sarah has raised in the ongoing chat.

print("\nRetrieving current conversation history from short-term memory...") response = data_client.list_events( memoryId=memory_id, actorId=sarah_actor_id, sessionId=support_session_id, maxResults=10 ) # Reverse the list of events to display them in chronological order event_list = reversed(response.get('events', [])) for event in event_list: print(event)

Step 6: Use long-term memories for personalized assistance

The agent performs a semantic search across extracted long-term memories to find relevant insights about Sarah’s preferences, order history, or past concerns. This lets the agent provide highly personalized assistance without needing to ask Sarah to repeat information she has already shared in previous chats.

# Wait for the asynchronous extraction to finish print("\nWaiting 60 seconds for long-term memory processing...") time.sleep(60) # --- Example 1: Retrieve the user's shipping preference --- print("\nRetrieving user preferences from long-term memory...") preference_response = data_client.retrieve_memory_records( memoryId=memory_id, namespace=f"/users/{sarah_actor_id}/preferences", searchCriteria={"searchQuery": "Does the user have a preferred shipping carrier?"} ) for record in preference_response.get('memoryRecordSummaries', []): print(f"- Retrieved Record: {record}") # --- Example 2: Broad query about the user's issue (across sessions with the help of namespacePath) --- print("\nPerforming a broad search for user's reported issues...") issue_response = data_client.retrieve_memory_records( memoryId=memory_id, namespacePath=f"/summaries/{sarah_actor_id}/", searchCriteria={"searchQuery": "What problem did the user report with their order?"} ) for record in issue_response.get('memoryRecordSummaries', []): print(f"- Retrieved Record: {record}")

With this approach, the agent keeps context across sessions, recognizes returning customers, and recalls important details without asking the customer to repeat them.