Description
Problem Statement
Currently, the Strands SDK requires developers to manually implement session persistence logic for agents. This presents several challenges:
- Developers must write boilerplate code to save and load agent state
- Session management logic is tightly coupled with application code
- No standardized approach for implementing different storage backends
- Difficult for the community to share and reuse storage implementations
- Increased cognitive load when managing complex applications with multiple agents
Docs for current strategy for saving and loading state
Proposed Solution
Decorator-Based Session Management
I propose implementing a decorator-based session persistence layer that:
- Automatically handles saving and loading agent states
- Provides an extensible interface for storage providers
- Simplifies application code by separating concerns
- Enables community contributions of storage backends
Use Case
Developers building chat applications need to persist conversations across page refreshes, server restarts, and user sessions. With the decorator approach, they can simply:
@redis_session_manager(redis_url="redis://localhost:6379", ttl=3600)
def create_agent(system_prompt="You are a helpful assistant", messages=None):
return Agent(system_prompt=system_prompt, messages=messages or [])
@app.post("/chat/{user_id}")
async def chat_endpoint(user_id: str, request: ChatRequest):
# Create or restore agent using the user_id as the session ID
agent = create_agent(
session_id=f"user:{user_id}",
system_prompt="You are a helpful AI assistant."
)
# Process message - state automatically saved to Redis
response = agent(request.message)
return {"response": response}
Alternatives Solutions
Instead of decorators, one possible alternative solution would be to directly enhance the Agent class with built-in session management capabilities.
from strands.storage import RedisStorage
# Create agent with Redis persistence
agent = Agent(
system_prompt="You are a helpful assistant",
session_id="user123",
storage_provider=RedisStorage("redis://localhost:6379")
)
# Use normally - state automatically managed
response = agent("Hello!")
It would couple the storage logic more with the core Agent class, but also allow for the community to add storage providers by implementing this interface:
class StorageProvider(ABC):
@abstractmethod
def session_exists(self, session_id): pass
@abstractmethod
def load_session(self, session_id): pass
@abstractmethod
def save_session(self, session_id, state): pass
Additional Context
No response