SessionStoreProtocol defines the methods any session backend must implement — swap JSON files, Redis, or your own store without changing agent code.
How It Works
Quick Start
Use the built-in JSON store (zero config)
~/.praisonai/sessions/chat-123.json.Protocol Methods
The protocol requires exactly five methods:| Method | Returns | Purpose |
|---|---|---|
add_message(session_id, role, content, metadata) | bool | Store a single message |
get_chat_history(session_id, max_messages) | list[dict] | Retrieve messages in LLM format |
clear_session(session_id) | bool | Remove all messages (keep metadata) |
delete_session(session_id) | bool | Delete session completely |
session_exists(session_id) | bool | Check if a session exists |
set_chat_history(session_id, messages) | bool | Recommended. Atomically replace chat history for idempotent saves |
update_session_metadata(session_id, **fields) | bool | Merge metadata fields without touching messages |
get_working_history(session_id, max_messages=None) | list[dict] | Optional. Compacted resume path — summary + tail when a checkpoint exists, else raw history |
For persistent backends (DB / JSON),
clear_session() and delete_session() remove the underlying stored messages too, not just in-memory data. This ensures cleared history does not reappear after a reload or restart.Backward compatible resume. On resume the agent prefers
get_working_history(...) when the store implements it (checked via hasattr), so it can replay a compacted summary + tail. Stores that implement only get_chat_history continue to work unchanged — they simply resume from the raw transcript. See Compacted Session Resume.set_chat_history (Recommended)
set_chat_history(session_id, messages) (recommended). Atomically replaces the chat history for a session. If your store omits this method, Session.save_state() falls back to add_message() per message and may produce duplicates when called repeatedly. The built-in DefaultSessionStore implements this with reload-under-lock atomic-write semantics.
update_session_metadata() API
Theupdate_session_metadata() method enables safe metadata updates across processes without touching messages:
Parameters
| Parameter | Type | Description |
|---|---|---|
session_id | str | Unique session identifier |
**fields | Any | Metadata fields to merge (e.g., model, total_tokens, cost) |
Reserved Metadata Fields
The SDK uses these reserved fields internally:| Field | Type | Description | Auto-populated |
|---|---|---|---|
model | str | LLM model name (e.g., “gpt-4o-mini”) | ✅ |
total_tokens | int | Cumulative input + output tokens | ✅ |
cost | float | Estimated USD cost | ✅ |
agent_id | str | Gateway or registry agent ID | ✅ |
source | str | Origin: “chat”, “gateway”, “cli”, “api” | ✅ |
Example Usage
Concurrency Safety
InDefaultSessionStore, update_session_metadata() reloads under a cross-process file lock so concurrent metadata updates and message appends are both preserved.
Built-in Implementations
DefaultSessionStore
JSON file-based persistence with atomic writes and file locking. Mutating and read methods reload from disk under lock so multiple processes can share one directory.
HierarchicalSessionStore
Extends
DefaultSessionStore with forking, snapshots, and revert.Using with Bots
Bots use the sameSessionStoreProtocol for persistent per-user sessions:
bot_telegram_12345, stored in the same ~/.praisonai/sessions/ directory as agent sessions.
Building a Custom Store
Full example: In-memory store for testing
Full example: In-memory store for testing
Verifying protocol conformance
Verifying protocol conformance
Checkpoint Query Protocol
For session stores that support checkpoints and rollback functionality:Checkpoint Usage
Best Practices
Implement set_chat_history for idempotent saves
Implement set_chat_history for idempotent saves
Without it, repeated
Session.save_state() calls may duplicate messages via the add_message fallback.Use runtime isinstance checks
Use runtime isinstance checks
SessionStoreProtocol is @runtime_checkable — verify custom stores with isinstance(store, SessionStoreProtocol).Related
Session Persistence
Automatic session save and restore
Sessions Overview
Sessions and remote agents

