Strands Agents SDK Practical — Persist Conversations with Session Management
Table of Contents
Introduction
In Part 4 of the intro series, we learned that conversations continue when you keep talking to the same agent instance. But those conversations only exist in memory. Kill the process and they're gone.
Adding FileSessionManager is all it takes to save conversations to files and restore them after restarts.
In this article, we'll try:
- Save with FileSessionManager — Persist conversations to local files
- Restore after process restart — Restore the same session from a different process
- Examine stored data — Inspect the directory structure and JSON files
- Switch to S3SessionManager — Migration path to cloud storage
See the official documentation at Session Management.
Setup
Use the same environment from Part 1. All examples use the same model configuration and can be run as independent .py files. Write the common setup at the top, then add each example's code below it.
from strands import Agent
from strands.models import BedrockModel
from strands.session.file_session_manager import FileSessionManager
bedrock_model = BedrockModel(
model_id="us.anthropic.claude-sonnet-4-20250514-v1:0",
region_name="us-east-1",
)Saving Conversations with FileSessionManager
Specify a session_id and storage directory in FileSessionManager, then pass it to Agent.
session_manager = FileSessionManager(
session_id="user-123",
storage_dir="./sessions",
)
agent = Agent(
model=bedrock_model,
session_manager=session_manager,
callback_handler=None,
)
result1 = agent("My name is Taro. I'm learning Strands Agents SDK.")
print(f"Agent: {result1.message['content'][0]['text'][:80]}...")
print(f"Messages: {len(agent.messages)}")
result2 = agent("What is my name?")
print(f"Agent: {result2.message['content'][0]['text'][:80]}...")
print(f"Messages: {len(agent.messages)}")01_save.py full code (copy-paste)
from strands import Agent
from strands.models import BedrockModel
from strands.session.file_session_manager import FileSessionManager
bedrock_model = BedrockModel(
model_id="us.anthropic.claude-sonnet-4-20250514-v1:0",
region_name="us-east-1",
)
session_manager = FileSessionManager(session_id="user-123", storage_dir="./sessions")
agent = Agent(model=bedrock_model, session_manager=session_manager, callback_handler=None)
result1 = agent("My name is Taro. I'm learning Strands Agents SDK.")
print(f"Agent: {result1.message['content'][0]['text'][:80]}...")
print(f"Messages: {len(agent.messages)}")
result2 = agent("What is my name?")
print(f"Agent: {result2.message['content'][0]['text'][:80]}...")
print(f"Messages: {len(agent.messages)}")python -u 01_save.pyResult
Agent: Hello Taro! Nice to meet you. I'd be happy to help you learn about Strands Agent...
Messages: 2
Agent: Your name is Taro, as you mentioned in your first message....
Messages: 4Same multi-turn conversation as Part 4 of the intro series, but now the conversation is automatically saved to the ./sessions directory.
Restoring After Process Restart
The real value of session management is that conversations survive process termination. Create an agent with the same session_id in a different script (= different process) to verify restoration.
session_manager = FileSessionManager(
session_id="user-123",
storage_dir="./sessions",
)
agent = Agent(
model=bedrock_model,
session_manager=session_manager,
callback_handler=None,
)
print(f"Restored messages: {len(agent.messages)}")
result = agent("What is my name and what am I learning?")
print(f"Agent: {result.message['content'][0]['text']}")
print(f"Messages after: {len(agent.messages)}")02_restore.py full code (copy-paste)
from strands import Agent
from strands.models import BedrockModel
from strands.session.file_session_manager import FileSessionManager
bedrock_model = BedrockModel(
model_id="us.anthropic.claude-sonnet-4-20250514-v1:0",
region_name="us-east-1",
)
session_manager = FileSessionManager(session_id="user-123", storage_dir="./sessions")
agent = Agent(model=bedrock_model, session_manager=session_manager, callback_handler=None)
print(f"Restored messages: {len(agent.messages)}")
result = agent("What is my name and what am I learning?")
print(f"Agent: {result.message['content'][0]['text']}")
print(f"Messages after: {len(agent.messages)}")python -u 02_restore.pyResult
Restored messages: 4
Agent: Your name is Taro and you're learning Strands Agents SDK.
Messages after: 6The existing session is automatically restored when session_manager is passed to the Agent constructor. Four messages (2 turns) were restored, and the agent remembers the previous conversation.
No special restore code needed. Just specify the same session_id.
Examining Stored Data
Let's look inside the ./sessions directory.
sessions/
└── session_user-123/
├── session.json
└── agents/
└── agent_default/
├── agent.json
└── messages/
├── message_0.json
├── message_1.json
├── message_2.json
└── message_3.jsonsession.json— Session metadata (session_id, timestamps)agent.json— Agent state (agent_iddefaults todefault)messages/— Each message saved as an individual JSON file
The contents of session.json:
{
"session_id": "user-123",
"session_type": "AGENT",
"created_at": "2026-03-27T15:21:31.179132+00:00",
"updated_at": "2026-03-27T15:21:31.179145+00:00"
}Messages are automatically appended to files each time agent() is called. No explicit "save" call needed.
Switching to S3SessionManager
For production or multi-instance deployments, you'll want S3 instead of local files. The code change is minimal.
from strands.session.s3_session_manager import S3SessionManager
session_manager = S3SessionManager(
session_id="user-123",
bucket="my-agent-sessions",
prefix="production/",
region_name="us-east-1",
)
agent = Agent(model=bedrock_model, session_manager=session_manager)Replace FileSessionManager with S3SessionManager and swap storage_dir for bucket and prefix. The agent code doesn't change at all.
For S3, you'll need these IAM permissions:
s3:PutObject— Create and update session datas3:GetObject— Retrieve session datas3:DeleteObject— Delete session datas3:ListBucket— List objects in the bucket
Summary
- Just add
FileSessionManagerto persist conversations — Specify asession_idand pass it to the agent. The SDK manages save timing automatically. - Same
session_idrestores the session — Even from a different process, specifying the samesession_idautomatically restores past conversations. No special restore code needed. - Messages are saved as individual JSON files — Under
session_<id>/agents/agent_default/messages/, each message is stored as an independent file. - Switching to S3 is just a class name change — Replace
FileSessionManagerwithS3SessionManager. The agent code stays the same.
