
Your AI Agent Forgot the Conversation? Persist Strands Sessions with Amazon S3
Learn how to persist AI agent conversations across application restarts using Strands Agents and Amazon S3.
Imagine building an AI-powered grocery assistant.
A user asks:
"I want to buy groceries for the weekend."
The agent recommends a shopping list. A few minutes later, the user asks:
"Can you remove the milk from that list?"
But there's a problem.
The application restarted, and the agent no longer remembers the previous conversation.
This is a common engineering challenge when developing AI agents.
An agent can have access to powerful foundation models and tools, but that doesn't automatically mean its conversation state will survive an application restart.
In this article, let's explore how to persist agent sessions using Strands Agents and Amazon S3.
1. Why do AI agents need session persistence?
An AI agent typically maintains information about its current conversation, including:
- Previous user messages.
- Assistant responses.
- Tool calls and their results.
- Agent state.
When an application runs continuously, this information can remain available in the agent's current process.
But what happens when the process stops?
Without a persistent session mechanism, the application cannot simply assume that the previous conversation will be available when the agent starts again.
Session persistence addresses this problem by storing the conversation and associated agent state so the application can restore it later.
This is particularly useful for:
- AI customer support assistants.
- Personal productivity agents.
- Shopping automation systems.
- Multi-step AI workflows.
- Applications where users return to an existing conversation.
2. What is session management in Strands Agents?
Strands Agents provides session management to persist conversation history and agent state.
For Python applications, the current recommended approach for new single-agent sessions is
SnapshotSessionManager .It works with storage backends so that the agent can save and restore its session.
Strands supports several storage options:
Storage backend | Typical use |
|---|---|
InMemoryStorage | Testing and short-lived agents |
LocalFileStorage | Local development |
S3Storage | Persistent cloud storage |
For a cloud-hosted application, Amazon S3 is a useful option because the session data can be stored outside the application's local process.
3. Understanding the architecture
Let's look at a simple architecture.
USER
↓
APPLICATION BACKEND
↓
STRANDS AGENT
↓
SNAPSHOT SESSION MANAGER
↓
AMAZON S3
When the user sends a message, the agent processes the request and session management persists the relevant conversation state.
When the application initializes the agent again with the same session identifier and storage backend, it can restore the existing session.
The important detail is that the application must reuse the correct session ID for the same conversation.
A different session ID represents a different conversation.
4. Building a persistent Strands Agent with Amazon S3
Let's implement a simple example using Python.
Step 1: Install Strands Agents
Create a virtual environment and install the SDK:
python -m venv .venv
pip install strands-agentsConfigure AWS credentials and make sure your account has permission to invoke the selected model through Amazon Bedrock.
Step 2: Configure your S3 bucket
Create an Amazon S3 bucket for storing agent session data.
For this example, assume you already have a bucket named:
my-agent-sessions The application will store session information under this prefix:
production/strands/ Make sure the bucket is private and that the application's IAM role has only the necessary permissions.
Step 3: Create the session manager
Now import the required components:
import os
from strands import Agentfrom strands.session import SnapshotSessionManagerfrom strands.storage import S3StorageRead your storage configuration from environment variables:
SESSION_BUCKET = os.environ["SESSION_BUCKET"]SESSION_ID = os.environ["SESSION_ID"]Create a reusable function:
def create_agent(session_id: str):
storage = S3Storage( SESSION_BUCKET, prefix="production/strands/" )
session_manager = SnapshotSessionManager( session_id=session_id, storage=storage )
agent = Agent( session_manager=session_manager )
return agentHere is what each component does:
S3Storage: Stores session data as objects in your S3 bucket.
SnapshotSessionManager: Manages the agent's session snapshots and restores previous state.
Agent: Uses the session manager to maintain conversation continuity.
Strands uses Amazon Bedrock as its default model provider in the current Python quickstart, so make sure your AWS credentials and model access are configured.
Step 4: Start a conversation
Let's simulate a user interacting with the agent.
agent = create_agent(SESSION_ID)
response = agent( "I'm building an AI grocery assistant " "for managing weekend shopping.")
print(response)The agent processes the request, and its session manager persists the conversation state using the configured storage backend.
Step 5: Resume the same conversation
Now imagine the application process has restarted.
Create an agent using the same session ID and the same S3 storage configuration:
agent = create_agent(SESSION_ID)
response = agent( "What application am I building?")
print(response)Because the application restores the same session, the agent can use the previous conversation context when generating its response.
That's the key idea: restore the session instead of starting an unrelated conversation.
The example demonstrates session restoration. It is not a claim that the code has been deployed or tested in a production environment.
5. Important IAM permissions
Your application's AWS credentials need permission to access the configured S3 storage.
The current Strands storage documentation identifies these required permissions:
Permission | Purpose |
|---|---|
s3:PutObject | Create and update session data |
s3:GetObject | Read stored session data |
s3:DeleteObject | Delete stored session data |
s3:ListBucket | List objects under the configured prefix |
Restrict these permissions to the specific bucket and object prefix used by your application.
Avoid granting unrestricted S3 access simply to make the integration work.
6. Sessions are not the same as long-term memory
This is one of the most important concepts to understand when designing AI agents.
Persisting a conversation does not automatically give your agent a complete long-term memory system.
There are three related but different concepts:
Session management
Preserves the conversation so the agent can resume the same interaction.
Conversation management
Controls how conversation context is maintained within the model's available context window.
Long-term memory
Stores durable information, such as user preferences, useful facts, and summaries, so an agent can recall them across different conversations.
Strands provides a separate
MemoryManager abstraction for long-term memory.Amazon Bedrock AgentCore Memory also provides short-term and long-term memory capabilities, including configurable strategies for extracting useful information from conversations.
For example:
A shopping assistant might preserve the current shopping conversation through session persistence while retaining a user's preferred grocery brands as long-term memory.
These are different engineering requirements and should be designed separately.
References:
7. Production considerations
Before using persistent sessions in production, consider the following:
Session isolation
Give each conversation a unique session ID. Associate it with the authenticated user on your backend. Never let an untrusted user select another user's session simply by supplying its ID.
Concurrency
The built-in Strands session managers do not provide distributed locking. The SDK is designed around one live writer per conversation. Concurrent requests targeting the same session can overwrite or merge conversation history unexpectedly.
For applications with concurrent requests, implement appropriate request serialization, locking, or routing.
Data retention
Conversation data can contain personal or sensitive information. Define retention and cleanup policies, and configure appropriate S3 security controls.
Storage permissions
Use least-privilege IAM policies and protect the bucket from public access.
Cost and performance
Consider the number and size of session snapshots, the frequency of writes, S3 storage costs, and retrieval latency when designing a production system.
Final thoughts
Building an AI agent is not only about connecting a model to tools.
It's also about designing how the agent maintains state, handles interruptions, and continues work across application requests.
With Strands Agents,
SnapshotSessionManager and S3Storage provide a practical way to persist agent sessions using Amazon S3.And remember:
Session persistence helps an agent continue a conversation. Long-term memory helps it remember what matters across conversations.
Understanding that distinction is an important step towards engineering more reliable AI applications.
#AWS #StrandsAgents #AIEngineering #AmazonS3 #AmazonBedrock #AIAgents #Python #Serverless
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article