Skip to main content
Session management is experimental. APIs may change in future releases.
Sessions let your agent remember previous conversations. When you attach a SessionManager to an agent, it automatically loads history at the start of each run and saves it when the run completes — so the agent picks up exactly where the last conversation left off.

Quick start

1

Initialize a storage engine

Create a SQLite engine and set up the database tables. Do this once at application startup.
from nexau.archs.session.orm import SQLDatabaseEngine

engine = SQLDatabaseEngine.from_url("sqlite+aiosqlite:///sessions.db")
await engine.setup_models()
2

Create a SessionManager

from nexau.archs.session import SessionManager

session_manager = SessionManager(engine=engine)
3

Attach the manager to your agent

Pass session_manager, user_id, and session_id when constructing the agent.
from nexau.archs.main_sub.agent import Agent, AgentConfig

agent = Agent(
    config=agent_config,
    session_manager=session_manager,
    user_id="user_123",
    session_id="sess_456",
)

response = await agent.run_async("Hello, how are you?")
# The next run loads this conversation automatically
response2 = await agent.run_async("What did I just ask?")
If you omit session_id, a new session is created. Pass the returned session ID in subsequent requests to continue the same conversation.

Storage backends

Choose a backend based on your deployment needs.
File-based storage. No extra dependencies — good for development and small single-process deployments.
from nexau.archs.session.orm import SQLDatabaseEngine
from nexau.archs.session import SessionManager

engine = SQLDatabaseEngine.from_url("sqlite+aiosqlite:///sessions.db")
await engine.setup_models()

session_manager = SessionManager(engine=engine)
The database is a single file you can copy or back up directly.

Concurrency control

When multiple requests try to run against the same session simultaneously, SessionManager uses a distributed lock to prevent conflicting writes. Only one agent run can modify a session at a time.
session_manager = SessionManager(
    engine=engine,
    lock_ttl=30.0,           # Lock expires after 30 seconds
    heartbeat_interval=10.0, # Renew lock every 10 seconds
)
ParameterDefaultDescription
lock_ttl30.0Seconds before a lock expires automatically. Prevents deadlocks if a run crashes.
heartbeat_interval10.0Seconds between lock renewals during a long-running operation.
For operations that take longer than the default TTL, increase lock_ttl and set heartbeat_interval to roughly one-third of it.

Common errors

You haven’t called setup_models() before using the engine. Add it to your application startup:
engine = SQLDatabaseEngine.from_url("sqlite+aiosqlite:///sessions.db")
await engine.setup_models()  # Required before first use
The agent was created without a session_manager, or without a session_id to resume. Both are required to continue an existing session:
# Wrong — no session manager
agent = Agent(config=agent_config)

# Correct — session manager and IDs provided
agent = Agent(
    config=agent_config,
    session_manager=session_manager,
    user_id="user_123",
    session_id="sess_456",
)
The agent run took longer than lock_ttl. Increase the TTL and the heartbeat interval proportionally:
session_manager = SessionManager(
    engine=engine,
    lock_ttl=60.0,
    heartbeat_interval=20.0,
)