Strands Agents sessions
Strands Agents persist sessions through a session manager.
strands-persql is a drop-in replacement for FileSessionManager /
S3SessionManager that stores sessions, agent state, and messages in a
PerSQL database — an isolated SQLite database you can query like any
other.
pip install strands-persqlimport osfrom strands import Agentfrom persql import PerSQLfrom strands_persql import PerSQLSessionManager
client = PerSQL(token=os.environ["PERSQL_TOKEN"])manager = PerSQLSessionManager("user-42", client.database("acme/agent-state"))
agent = Agent(session_manager=manager)agent("hi")agent("remember me?") # history and state persist across restartsPerSQLSessionRepository is also exported for use with
RepositorySessionManager or your own manager. Multi-agent (Graph and
Swarm) state persists too.
One database per agent
Section titled “One database per agent”Sessions are keyed by session id and can share one database. For hard isolation, give each tenant its own database instead:
manager = PerSQLSessionManager(session_id, client.database(f"acme/tenant-{tenant_id}"))PerSQL databases are provisioned on first write and billed by usage, so per-tenant session stores cost what they’re actually used for.
Inspect history with SQL
Section titled “Inspect history with SQL”SELECT message_id, json_extract(data, '$.message.role') AS role, created_atFROM strands_messagesWHERE session_id = 'user-42'ORDER BY message_id;