Skip to content

Session persistence #302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Unshure
Copy link
Member

@Unshure Unshure commented Jun 27, 2025

Description

This PR introduces a SessionManagement class to the Strands SDK that is in charge of persisting an agent to some datastore, and re-initializing the agent. The SessionManagement feature set includes:

  • Introduces an agent id attribute used as a unique id for differentiating agents

  • SessionManager: Interface for SessionManagers. This interface introduces a append_message_to_agent_session method that is called in the Agent class when adding a message to the agent messages array, as well as an initialize_agent class for initializing an agent

  • AgentSessionManager: Implementation of the above interface for persisting a single agent to some datastore. This will also re-initialize the agent's messages and state.

  • Session models: Three new models to represent different data types for session persistence:

    • Session: The overarching container to represent a strands agent, and in the future, multi-agent systems. One session can have many agents
    • SessionAgent: A persisted version of an agent, storing the agents state and messages
    • SessionMessage: A persisted version of a message
  • SessionRepository: Repository Interface with create, read, update operations for each of the above new models

  • FileSessionManager: A implementation of the SessionRepository and AgentSessionManager for storing a session in the local file system. This introduces the following directory and file structure to represent a Session:

    • <storage_dir>/session_<session_id>/
      • session.json # Stores session related data
      • agents/agent_<agent_id>/
        • agent.json # Stores agent related data
        • messages/messages_<message_id>.json # Stores message content
  • S3SessionManager: Same as the above FileSessionDManager, but stores sessions in S3

Things that will be addressed in follow up pr's:

  • Update message after guardrail triggers
  • ser/de the conversation manager to persist the previous messages array state.

#246

Documentation PR

TODO:
PR coming, here are some initial docs: https://unshure.github.io/docs/latest/user-guide/concepts/agents/state-sessions/#session-management

Type of Change

New feature

Testing

How have you tested the change?

  • Vibecoded unit tests and local testing
  • Still needs:
    • Thorough review of unit tests
    • Integration tests

• [x] I ran hatch run prepare

Checklist

• [x] I have read the CONTRIBUTING document
• [x] I have added any necessary tests that prove my fix is effective or my feature works
• [ ] I have updated the documentation accordingly
• [ ] I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
• [x] My changes generate no new warnings
• [x] Any dependent changes have been merged and published

Additional Notes:
N/A

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the
terms of your choice.

@Unshure Unshure force-pushed the session-persistence branch from d62b09b to 74d7ed6 Compare July 1, 2025 14:48
@Unshure Unshure force-pushed the session-persistence branch from 74d7ed6 to 3b81f1b Compare July 1, 2025 14:49
@azaylamba
Copy link

azaylamba commented Jul 4, 2025

@Unshure @zastrowm Would this feature be available/merged in near future (coming weeks)? I have a use case of this to persist the context/session of an agent where the agent in deployed in a lambda function. I want to keep the context of the previous invocations of the agent based on session_id.
I am thinking to implement a custom class like DynamoDBConversationManager which would store the conversation in DynamoDB using session_id and can be retrieved/updated across the invocations. Is that a good approach?

@Unshure
Copy link
Member Author

Unshure commented Jul 7, 2025

@Unshure @zastrowm Would this feature be available/merged in near future (coming weeks)? I have a use case of this to persist the context/session of an agent where the agent in deployed in a lambda function. I want to keep the context of the previous invocations of the agent based on session_id.
I am thinking to implement a custom class like DynamoDBConversationManager which would store the conversation in DynamoDB using session_id and can be retrieved/updated across the invocations. Is that a good approach?

Hi @azaylamba , planning on getting this feature merged and available by end of this week!

@macsir
Copy link

macsir commented Jul 8, 2025

@Unshure @zastrowm Would this feature be available/merged in near future (coming weeks)? I have a use case of this to persist the context/session of an agent where the agent in deployed in a lambda function. I want to keep the context of the previous invocations of the agent based on session_id. I am thinking to implement a custom class like DynamoDBConversationManager which would store the conversation in DynamoDB using session_id and can be retrieved/updated across the invocations. Is that a good approach?

Hi @azaylamba, if it is a long conversation, would it be better to use fargate(ECS) instead of lambda to avoid potential timeout issue?

@azaylamba
Copy link

@Unshure @zastrowm Would this feature be available/merged in near future (coming weeks)? I have a use case of this to persist the context/session of an agent where the agent in deployed in a lambda function. I want to keep the context of the previous invocations of the agent based on session_id.
I am thinking to implement a custom class like DynamoDBConversationManager which would store the conversation in DynamoDB using session_id and can be retrieved/updated across the invocations. Is that a good approach?

Hi @azaylamba , planning on getting this feature merged and available by end of this week!

Thanks for the update @Unshure

@azaylamba
Copy link

@Unshure @zastrowm Would this feature be available/merged in near future (coming weeks)? I have a use case of this to persist the context/session of an agent where the agent in deployed in a lambda function. I want to keep the context of the previous invocations of the agent based on session_id. I am thinking to implement a custom class like DynamoDBConversationManager which would store the conversation in DynamoDB using session_id and can be retrieved/updated across the invocations. Is that a good approach?

Hi @azaylamba, if it is a long conversation, would it be better to use fargate(ECS) instead of lambda to avoid potential timeout issue?

Hi @macsir , it is not a long conversation and the agent run time won't be long. But the agent should be aware of past interactions and actions, so that those are not repeated (in case action has been already taken by human) and the agent has the historical context.
For the long running tasks/conversations, you are right though and ECS would be a better option.

Copy link
Member

@zastrowm zastrowm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No major blockers - a couple call-outs and questions, but otherwise looks good!

@Unshure
Copy link
Member Author

Unshure commented Jul 13, 2025

@Unshure Unshure deployed to auto-approve July 13, 2025 21:40 — with GitHub Actions Active
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: We're Working On It
Development

Successfully merging this pull request may close these issues.

8 participants