Documentation Index
Fetch the complete documentation index at: https://docs.praison.ai/llms.txt
Use this file to discover all available pages before exploring further.
Session Persistence
PraisonAI Agents provides automatic session persistence with zero configuration. Simply provide asession_id to your Agent and conversation history is automatically saved and restored.
Quick Start
How It Works
When you provide asession_id to an Agent:
- Automatic Persistence: Conversation history is automatically saved to disk after each message
- Automatic Restoration: When a new Agent is created with the same
session_id, history is restored - Zero Configuration: No database setup required - uses JSON files by default
Default Storage Location
Sessions are stored in:~/.praisonai/sessions/{session_id}.json
Behavior Matrix
| Scenario | Behavior |
|---|---|
session_id provided, no DB | JSON persistence (automatic) |
session_id provided, with DB | DB adapter used |
No session_id, same Agent instance | In-memory only |
No session_id, new Agent instance | No history |
In-Memory Memory (Default)
Even withoutsession_id, the same Agent instance remembers previous messages:
In-memory memory is lost when the Agent instance is garbage collected or the process ends.
Use
session_id for persistence across processes.Persistent Sessions
Basic Usage
Resuming Sessions
Session File Format
Sessions are stored as JSON files with automatic metadata tracking:Session Metadata Fields
The following metadata is automatically populated after each assistant turn:| Field | Type | Description |
|---|---|---|
model | string | LLM model used in the session |
total_tokens | int | Cumulative input+output tokens |
cost | float | Estimated USD cost |
agent_id | string | Gateway or registry agent id |
source | string | Origin: chat, gateway, cli, api |
agent_name | string | Human-readable agent name |
Multi-Process Safety
The session store uses file locking to ensure safe concurrent access:- Unix: Uses
fcntl.flock()for file locking - Windows: Uses
msvcrt.locking()for file locking - Atomic Writes: Uses temp file + rename to prevent corruption
Direct Session Store Access
For advanced use cases, you can access the session store directly:Custom Session Directory
Using with DB Adapter
When a DB adapter is provided, it takes precedence over JSON persistence:Context Caching
For cost optimization with Anthropic models, usecaching=True:
Bot Session Persistence
Bots now use the same session store as agents. Each user gets a persistent session that survives bot restarts:Without a
store parameter, BotSessionManager falls back to in-memory-only mode for backward compatibility.Session Store Protocol
All session stores implementSessionStoreProtocol — a lightweight interface that enables swapping backends:
Learn more about building custom session stores
Best Practices
-
Use meaningful session IDs: Include user ID or context in the session ID
- Handle session limits: Default max messages is 100. Older messages are trimmed.
-
Clean up old sessions: Use
store.delete_session()to remove unused sessions. -
Use prompt caching: Enable
caching=Truefor Anthropic models to reduce costs.
API Reference
Agent Parameters
| Parameter | Type | Description |
|---|---|---|
session_id | str | Session identifier for persistence |
db | DbAdapter | Optional database adapter (overrides JSON) |
prompt_caching | bool | Enable Anthropic prompt caching |
DefaultSessionStore Methods
| Method | Description |
|---|---|
add_message(session_id, role, content) | Add a message |
add_user_message(session_id, content) | Add a user message |
add_assistant_message(session_id, content) | Add an assistant message |
get_chat_history(session_id, max_messages) | Get chat history |
get_session(session_id) | Get full session data |
clear_session(session_id) | Clear all messages |
delete_session(session_id) | Delete session completely |
list_sessions(limit) | List all sessions |
session_exists(session_id) | Check if session exists |

