

# Retain conversational context across multiple sessions using memory
<a name="agents-memory"></a>

Memory provides your agent the ability to retain conversational context across multiple sessions and to recall past actions and behaviors. By default, your agent retains conversational context from a single session. To configure memory for your agent, enable the memory setting for your agent and specify the storage duration to retain the memory.

The conversational context is stored in the memory as sessions with each session given a session identifier (ID) that you provide when you invoke the agent. You can specify the same session Id across requests to continue the same conversation. 

After you enable memory for your agent, the current session gets associated with a specific memory context when you invoke agent with same `sessionId` as the current session and with `endSessions` set to '`true`', or when the `idleSessionTimeout` configured for the agent has timed out. This memory context is given a unique memory identifier. Your agent uses the memory context to access and utilize the stored conversation history and conversation summaries to generate responses. 

If you have multiple users, make sure to provide the same memory identifier (memoryId) for the same user. The agent stores the memory for each user against that memoryId and the next time you invoke the agent with the same memoryId, the summary of each session stored in the memory gets loaded to the current session. 

You can access the memory at any time to view the summarized version of the sessions that are stored in the memory. You can also, at any time, clear the memory by deleting all the sessions stored in the memory. 

**Memory summarization**

Your agent uses the memory summarization [Enhance agent's accuracy using advanced prompt templates in Amazon Bedrock](advanced-prompts.md) to call the foundation model with guidelines to summarize all your sessions. You can optionally modify the default prompt template or provide your own custom parser to parse model output.

Since the summarization process takes place in an asynchronous flow after a session ends, logs for any failures in summarization due to overridden template or parser will be published to your AWS accounts. For more information on enabling the logging, see [Enable memory summarization log delivery](agents-memory-log-delivery-enable.md). 

**Memory duration**

If memory is enabled, your agent retains the sessions in the memory for up to 365 days. You can optionally configure the retention period by specifying a duration between 1 and 365 days. All session summaries beyond this duration will be deleted.

# Enable agent memory
<a name="agents-configure-memory"></a>

To configure memory for your agent, you must first enable memory and then optionally specify the retention period for the memory. You can enable memory for your agent when you [create](https://docs.aws.amazon.com//bedrock/latest/userguide/agents-create.html) or [update](https://docs.aws.amazon.com//bedrock/latest/userguide/agents-manage.html#agents-edit) your agent. 

**Note**  
You can enable memory for Agents on all the models *except* the following:   
Amazon Titan Text Premier
Anthropic Claude Instant
Make sure that the model you are planning to use is available in your Region. For more information, see [Model support by AWS Region.](https://docs.aws.amazon.com//bedrock/latest/userguide/models-supported.html)

To learn how to configure memory for your agent, select the tab corresponding to your method of choice and follow steps.

------
#### [ Console ]

**To configure memory for your agent**

1. If you're not already in the agent builder, do the following:

   1. Sign in to the AWS Management Console with an IAM identity that has permissions to use the Amazon Bedrock console. Then, open the Amazon Bedrock console at [https://console.aws.amazon.com/bedrock](https://console.aws.amazon.com/bedrock).

   1. Select **Agents** from the left navigation pane. Then, choose an agent in the **Agents** section.

   1. Choose **Edit in Agent Builder**

1. In the Agent details section, for **Select model**.

1. In the **Memory** section, do the following:

   1. For **Enable session summarization**, select **Enabled**.

   1. (Optional) For **Memory duration**, enter a number between 1 and 365 to specify the memory duration for your agent. By default, agent retains conversational context for 30 days. 

   1. For **Maximum number of recent sessions**, select a number for maximum number of recent sessions to store as memory.

   1. (Optional) You can optionally make changes to your session summarization prompt. To make changes, in the **Session summarization prompt**, choose **View and edit**.

1. Make sure to first **Save** and then **Prepare** to apply the changes you have made to the agent before testing it. 

------
#### [ API ]

To enable and configure memory for your agent, send an [CreateAgent](https://docs.aws.amazon.com//bedrock/latest/APIReference/API_agent_CreateAgent.html) or [UpdateAgent](https://docs.aws.amazon.com//bedrock/latest/APIReference/API_agent_UpdateAgent.html) request with an [Agents for Amazon Bedrock build-time endpoint](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#bra-bt).

In the Amazon Bedrock API, you specify the `memoryConfiguration` when you send a [CreateAgent](https://docs.aws.amazon.com//bedrock/latest/APIReference/API_agent_CreateAgent.html) or [UpdateAgent](https://docs.aws.amazon.com//bedrock/latest/APIReference/API_agent_UpdateAgent.html) request. 

The following shows the general format of the `memoryConfiguration`: 

```
"memoryConfiguration": {
"enabledMemoryTypes": [ "SESSION_SUMMARY" ],
"storageDays":30,
"sessionSummaryConfiguration": {
    "maxRecentSessions": 5
    }
}
```

You can optionally configure the memory retention period by assigning the `storageDays` with a number between 1 and 365 days.

**Note**  
If you enable memory for the agent and do not specify `memoryId` when you invoke the agent, agent will not store that specific turn in the memory.

------

# View memory sessions
<a name="agents-memory-view"></a>

The agent stores the memory for each session against the unique memory identifier (`memoryId`) provided for each user when you invoke the agent. The next time you invoke the agent with the same `memoryId`, the entire memory is loaded to the session. After you end the session, the agent generates a summarized version of the session and stores the session summary.

**Note**  
It can take several minutes after you end your session for the session summaries to appear in the console or in the API response.

To learn how to view the session summaries, choose the tab for your preferred method, and then follow the steps:

------
#### [ Console ]

**To view session summaries,**

1. Sign in to the AWS Management Console with an IAM identity that has permissions to use the Amazon Bedrock console. Then, open the Amazon Bedrock console at [https://console.aws.amazon.com/bedrock](https://console.aws.amazon.com/bedrock).

1. Select **Agents** from the left navigation pane. Then, choose an agent in the **Agents** section.

1. In the **Test** window, choose the expand icon and choose **Memory** tab.

   If you are in **Agent builder** page, in the **Memory** section, choose **View memory**.

1. You can also view memory sessions when you are testing your agent. To view sessions stored in the memory when you are testing,

   1. In the test window, choose **Show trace** and then choose **Memory** tab.
**Note**  
If you are viewing memory sessions when you are testing your agent, you can view the session summary only after the latest session has ended. If you try to view memory sessions when the current session is in progress you will be informed that session summary is being generated and it will take time to generate the sessions. You can force end the current session by choosing the broom icon. 

------
#### [ API ]

To view memory sessions of your agent, send a [GetAgentMemory](https://docs.aws.amazon.com//bedrock/latest/APIReference/API_agent-runtime_GetAgentMemory.html) request (see link for request and response formats and field details) with an [Agents for Amazon Bedrock build-time endpoint](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#bra-bt).

The following fields are required:


****  

| Field | Short description | 
| --- | --- | 
| agentId | The identifier of the agent | 
| agentAliasId | The identifier of the agent alias | 
| memoryId | The identifier of the memory that has the session summaries | 
| memoryType | The type of memory. Valid value: SESSION\$1SUMMARY | 

**Note**  
If you are viewing memory sessions when you are testing your agent, you can view the session summary only after the latest session has ended. If you try to view memory sessions when the current session is in progress you will be informed that session summary is being generated and it will take time to generate the sessions. You can force end the current session by sending an [InvokeAgent](https://docs.aws.amazon.com//bedrock/latest/APIReference/API_agent_InvokeAgent.html) request and specifying `Y` for the `endSession` field. 

------

# Delete session summaries
<a name="agents-memory-delete"></a>

To learn how to delete the session summaries, choose the tab for your preferred method, and then follow the steps:

------
#### [ Console ]

**To delete session summaries,**

1. Sign in to the AWS Management Console with an IAM identity that has permissions to use the Amazon Bedrock console. Then, open the Amazon Bedrock console at [https://console.aws.amazon.com/bedrock](https://console.aws.amazon.com/bedrock).

1. Select **Agents** from the left navigation pane. Then, choose an agent in the **Agents** section.

1. Choose **Edit in Agent Builder**

1. In the **Memory** section, choose **View memory** and choose **Memory** tab.

1. 

**To choose the session summaries you want to delete,**

   1. In the **Find memory sessions**, select the filter you want to use to search for the sessions summaries you want to delete.

   1. Specify the filter criteria.

1. Choose **Delete alias memory** and then choose **Delete**.

------
#### [ API ]

To delete session summaries, send a [DeleteAgentMemory](https://docs.aws.amazon.com//bedrock/latest/APIReference/API_agent-runtime_DeleteAgentMemory.html) request (see link for request and response formats and field details) with an [Agents for Amazon Bedrock build-time endpoint](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#bra-bt).

The following fields are required:


****  

| Field | Short description | 
| --- | --- | 
| agentId | The identifier of the agent. | 
| agentAliasId | The identifier of the agent alias. | 

The following field is optional.


****  

| Field | Short description | 
| --- | --- | 
| memoryId | The identifier of the memory that has the session summaries | 

------

# Disable agent memory
<a name="agents-memory-disable"></a>

You can disable memory for your agent at any time. You cannot access memory sessions after you disable memory for your agent. 

**Note**  
If you enable memory for the agent and do not specify `memoryId` when you invoke the agent, agent will not store that specific turn in the memory.

To learn how to disable memory, choose the tab for your preferred method, and then follow the steps:

------
#### [ Console ]

**To disable memory for your agent,**

1. Sign in to the AWS Management Console with an IAM identity that has permissions to use the Amazon Bedrock console. Then, open the Amazon Bedrock console at [https://console.aws.amazon.com/bedrock](https://console.aws.amazon.com/bedrock).

1. Select **Agents** from the left navigation pane. Then, choose an agent in the **Agents** section.

1. Choose **Edit in Agent Builder**

1. In the **Memory** section, choose **Disable**.

------
#### [ API ]

To disable memory, send a [UpdateAgent](https://docs.aws.amazon.com//bedrock/latest/APIReference/API_agent_UpdateAgent.html) request (see link for request and response formats and field details) with an [Agents for Amazon Bedrock build-time endpoint](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#bra-bt). Send the request without specifying the `memoryConfiguration` structure. This will disassociate the memory from the agent.

------

# Enable memory summarization log delivery
<a name="agents-memory-log-delivery-enable"></a>

To enable logging for your Amazon Bedrock agent alias, use the [PutDeliverySource](https://docs.aws.amazon.com//AmazonCloudWatchLogs/latest/APIReference/API_PutDeliverySource.html) CloudWatch API. Make sure to specify the following:
+ For `resourceArn`, provide the Amazon Resource Name (ARN) of the agent alias that is generating and sending the logs
+ For `logType`, specify `APPLICATION_LOGS` as the supprted log type.

You will also need the add `bedrock:AllowVendedLogDeliveryForResource` permission for the user signed into the console. This permission allows logs to be delivered for the agent alias resource. 

To view an example IAM role/permissions policy with all the required permissions for your specific logging destination, see [Vended logs permissions for different delivery destinations](https://docs.aws.amazon.com//AmazonCloudWatch/latest/logs/AWS-logs-and-resource-policy.html#AWS-vended-logs-permissions-V2). Use the example to provide details for your logging destination, including allowing updates to your specific logging destination resource (CloudWatch Logs, Amazon S3, or Amazon Data Firehose).