Strands Valkey Session Manager
Section titled “Strands Valkey Session Manager”{{ community_contribution_banner }}
The Strands Valkey Session Manager is a high-performance session manager for Strands Agents that uses Valkey/Redis for persistent storage. Valkey is a very-low latency cache that enables agents to maintain conversation history and state across multiple interactions, even in distributed environments.
Tested with Amazon ElastiCache Serverless (Redis 7.1, Valkey 8.1), ElastiCache (Redis 7.1, Valkey 8.2), and Upstash.
Installation
Section titled “Installation”pip install strands-valkey-session-managerBasic Setup
Section titled “Basic Setup”from strands import Agentfrom strands_valkey_session_manager import ValkeySessionManagerfrom uuid import uuid4import valkey
# Create a Valkey clientclient = valkey.Valkey(host="localhost", port=6379, decode_responses=True)
# Create a session manager with a unique session IDsession_id = str(uuid4())session_manager = ValkeySessionManager( session_id=session_id, client=client)
# Create an agent with the session manageragent = Agent(session_manager=session_manager)
# Use the agent - all messages are automatically persistedagent("Hello! Tell me about Valkey.")
# The conversation is now stored in Valkey and can be resumed later using the same session_id
# Display conversation historymessages = session_manager.list_messages(session_id, agent.agent_id)for msg in messages: role = msg.message["role"] content = msg.message["content"][0]["text"] print(f"** {role.upper()}**: {content}")Key Features
Section titled “Key Features”- Persistent Sessions: Store agent conversations and state in Valkey/Redis
- Distributed Ready: Share sessions across multiple application instances
- High Performance: Leverage Valkey’s speed for fast session operations
- JSON Storage: Native JSON support for complex data structures
- Automatic Cleanup: Built-in session management and cleanup capabilities
Configuration
Section titled “Configuration”ValkeySessionManager Parameters
Section titled “ValkeySessionManager Parameters”session_id: Unique identifier for the sessionclient: Configured Valkey client instance (only synchronous versions are supported)
Storage Structure
Section titled “Storage Structure”The ValkeySessionManager stores data using the following key structure:
session:<session_id> # Session metadatasession:<session_id>:agent:<agent_id> # Agent state and metadatasession:<session_id>:agent:<agent_id>:message:<message_id> # Individual messagesAvailable Methods
Section titled “Available Methods”The following methods are used transparently by Strands:
create_session(session): Create a new sessionread_session(session_id): Retrieve session datadelete_session(session_id): Remove session and all associated datacreate_agent(session_id, agent): Store agent in sessionread_agent(session_id, agent_id): Retrieve agent dataupdate_agent(session_id, agent): Update agent statecreate_message(session_id, agent_id, message): Store messageread_message(session_id, agent_id, message_id): Retrieve messageupdate_message(session_id, agent_id, message): Update messagelist_messages(session_id, agent_id, limit=None): List all messages
Requirements
Section titled “Requirements”- Python 3.10+
- Valkey/Redis server
- strands-agents >= 1.0.0
- valkey >= 6.0.0
References
Section titled “References”- PyPI: strands-valkey-session-manager
- GitHub: jeromevdl/strands-valkey-session-manager
- Issues: Report bugs and feature requests in the GitHub repository