JSON File Persistence

Zero-config file persistence for development and lightweight applications

JSON file persistence saves agent sessions as human-readable files — no database required.

python
from praisonaiagents import Agent, MemoryConfig

agent = Agent(
    name="FileBot",
    instructions="You are a helpful assistant.",
    memory=MemoryConfig(
        session_id="my-session",
    ))
agent.start("Hello — this conversation is saved to JSON files")

The user chats locally; JSON files persist the session without installing a database.

Diagram

🤖 Agent

📄 Session Store

💾 JSON Files

🗂️ ~/.praisonai/sessions

Quick Start

  1. Simple Usage

    Set session_id — PraisonAI persists to JSON automatically:

    python
    from praisonaiagents import Agent, MemoryConfig
    
    agent = Agent(
        name="FileBot",
        memory=MemoryConfig(
            session_id="dev-session",
        ))
    agent.start("Remember I prefer British English")
  2. With Configuration

    Custom session directory or the conversation JSON store:

    python
    from praisonaiagents.session import DefaultSessionStore
    from praisonai.persistence import create_conversation_store
    
    # Agent sessions — custom directory
    store = DefaultSessionStore(session_dir="./my_sessions")
    
    # Full conversation API — praisonai package
    conv_store = create_conversation_store("json", path="./data/conversations")

How It Works

Diagram
session.jsonDefaultSessionStoreAgentsession.jsonDefaultSessionStoreAgentadd_messageatomic write + file lockget_chat_historyreload under lockconversation history
StoreLocationUse case
DefaultSessionStore~/.praisonai/sessions/Agent session_id (zero config)
JSONConversationStoreConfigurable pathFull conversation API via db()

Compaction Checkpoint Key

When context compaction persists a checkpoint, the session JSON gains an optional top-level last_compaction object. It is omitted entirely when no checkpoint exists, so older session files load unchanged.

json
{
  "session_id": "user-42-chat",
  "messages": [ ... ],
  "last_compaction": {
    "summary": "Earlier: we discussed X, Y, Z.",
    "message_index": 40,
    "role": "system",
    "tokens_before": 8100,
    "tokens_after": 1200,
    "timestamp": 1751894400.0,
    "metadata": {}
  }
}
FieldTypeDefaultMeaning
summarystr(required)The condensed history the compactor produced
message_indexint0Transcript length at compaction time; anything after this index is the tail
rolestr"system"Role used when replaying the summary as an LLM message
tokens_beforeint0Token count before compaction (observability)
tokens_afterint0Token count after compaction (observability)
timestampfloattime.time()When the checkpoint was written
metadataDict[str, Any]{}Free-form extension slot

See Compacted Session Resume for how the key is produced and consumed.


Configuration Options

DefaultSessionStore

OptionTypeDefaultDescription
session_dirstr~/.praisonai/sessions/Directory for session JSON files
max_messagesint100Maximum messages kept per session
lock_timeoutfloat5.0File lock timeout in seconds

JSONConversationStore

OptionTypeDefaultDescription
pathstr"./praisonai_conversations"Directory for JSON files
prettyboolTruePretty-print JSON output

Best Practices

Use session_id for quick dev

No db= needed — set session_id and conversations persist to ~/.praisonai/sessions/ automatically.

Set a custom session_dir for projects

Point DefaultSessionStore(session_dir=...) at a project folder to keep sessions alongside your code.

Upgrade when data grows

JSON suits development and small deployments. Move to SQLite or PostgreSQL when you need querying or multi-instance scaling.

Back up the sessions directory

Copy the session folder regularly — each session is a single readable .json file.


Related