Skip to main content
Database persistence enables agents to maintain conversation history and application state across restarts, supporting production deployments and long-running applications.

Quick Start

1

Simple SQLite Setup

from praisonaiagents import Agent, db

agent = Agent(
    name="Assistant",
    instructions="You are a helpful assistant.",
    db=db(database_url="sqlite:///conversations.db"),
    session_id="my-session"
)

agent.chat("Hello!")  # Automatically persisted
2

Production PostgreSQL

from praisonaiagents import Agent, db

agent = Agent(
    name="Assistant", 
    instructions="You are a helpful assistant.",
    db=db(database_url="postgresql://user:pass@localhost/mydb"),
    session_id="production-session"
)

response = agent.chat("Remember my preferences")
# Conversation automatically saved to PostgreSQL

How It Works

ComponentPurposeStorage Type
ConversationStoreMessages, sessions, metadataSQL databases (SQLite, PostgreSQL, MySQL)
StateStoreApplication state, key-value dataNoSQL stores (Redis, MongoDB)
DefaultSessionStoreFile-based sessionsJSON files on disk

Storage Backend Options

Choose the right backend for your use case:

SQLite

Local file database - perfect for development and single-instance apps

PostgreSQL

Production SQL database with advanced features and scalability

MySQL

Popular SQL database with excellent tooling and ecosystem

Redis

Fast in-memory state store for high-performance applications

MongoDB

Flexible document store for complex state and metadata

ClickHouse

Analytics database for large-scale data processing

JSON Files

Simple file-based storage for lightweight applications

Common Patterns

Multi-Backend Setup

Use different backends for conversations and state:
from praisonaiagents import Agent, db

# SQL for conversations, Redis for state
agent = Agent(
    name="Assistant",
    db=db(
        database_url="postgresql://localhost/conversations",
        state_url="redis://localhost:6379"
    ),
    session_id="hybrid-session"
)

Session Resume Workflow

# Initial session
agent = Agent(name="Assistant", db=db_instance, session_id="user-123")
agent.chat("My name is Alice")

# Later session - automatically resumes
agent2 = Agent(name="Assistant", db=db_instance, session_id="user-123") 
agent2.chat("What's my name?")  # Knows it's Alice

Production Configuration

import os
from praisonaiagents import Agent, db

# Environment-based configuration
agent = Agent(
    name="ProdAssistant",
    db=db(
        database_url=os.getenv("DATABASE_URL"),
        state_url=os.getenv("REDIS_URL")
    ),
    session_id=f"user-{user_id}"
)

Best Practices

  • SQLite: Development, prototypes, single-user apps
  • PostgreSQL/MySQL: Multi-user production applications
  • Redis: High-frequency state updates, caching
  • MongoDB: Complex metadata, document storage
  • JSON Files: Simple scripts, minimal dependencies
  • Use meaningful session IDs (user-123, conversation-abc)
  • Consider session expiration for privacy compliance
  • Group related conversations under the same session
  • Clean up old sessions periodically
  • Always handle database connection failures gracefully
  • Implement retry logic for transient network issues
  • Provide fallback to in-memory storage if database unavailable
  • Monitor database performance and connection pools
  • Use connection pooling for production deployments
  • Configure appropriate timeouts for your use case
  • Consider read replicas for high-read workloads
  • Monitor database metrics and query performance

Session Management

Learn about session lifecycle and management

Memory vs Context

Understand the difference between persistence and memory