Skip to main content
Storage backends persist agent sessions, chat history, and state. Use any database that fits your infrastructure.
from agno.agent import Agent
from agno.db.postgres import PostgresDb

agent = Agent(
    db=PostgresDb(db_url="postgresql://ai:ai@localhost:5532/ai"),
    add_history_to_context=True,
)

agent.print_response("Hello!")
# Chat history persists across sessions
agent.print_response("What did I just say?")

Supported Databases

DatabaseImportBest For
PostgreSQLfrom agno.db.postgres import PostgresDbProduction, teams
SQLitefrom agno.db.sqlite import SqliteDbLocal development
MongoDBfrom agno.db.mongodb import MongoDbDocument stores
Redisfrom agno.db.redis import RedisDbCaching, speed
DynamoDBfrom agno.db.dynamodb import DynamoDbAWS serverless
Firestorefrom agno.db.firestore import FirestoreDbGCP serverless
MySQLfrom agno.db.mysql import MysqlDbMySQL users
SingleStorefrom agno.db.singlestore import SingleStoreDbReal-time
SurrealDBfrom agno.db.surrealdb import SurrealDbMulti-model
GCSfrom agno.db.gcs import GCSDbCloud storage
JSONfrom agno.db.json import JsonDbSimple files
In-Memoryfrom agno.db.memory import InMemoryDbTesting

Examples by Database

PostgreSQL

Production-ready persistence.
cookbook/06_storage/postgres/postgres_for_agent.py
from agno.agent import Agent
from agno.db.postgres import PostgresDb
from agno.tools.duckduckgo import DuckDuckGoTools

db = PostgresDb(db_url="postgresql+psycopg://ai:ai@localhost:5532/ai")

agent = Agent(
    db=db,
    tools=[DuckDuckGoTools()],
    add_history_to_context=True,
)

agent.print_response("How many people live in Canada?")
agent.print_response("What is their national anthem called?")

SQLite

Simple local persistence.
cookbook/06_storage/sqlite/sqlite_for_agent.py
from agno.agent import Agent
from agno.db.sqlite import SqliteDb

agent = Agent(
    db=SqliteDb(db_file="agent.db"),
    add_history_to_context=True,
)

agent.print_response("Remember: My favorite color is blue")
agent.print_response("What is my favorite color?")

MongoDB

Document-based storage.
cookbook/06_storage/mongo/mongodb_for_agent.py
from agno.agent import Agent
from agno.db.mongodb import MongoDb

agent = Agent(
    db=MongoDb(
        uri="mongodb://localhost:27017",
        database="agno",
        collection="agents",
    ),
    add_history_to_context=True,
)

Redis

High-speed caching.
cookbook/06_storage/redis/redis_for_agent.py
from agno.agent import Agent
from agno.db.redis import RedisDb

agent = Agent(
    db=RedisDb(url="redis://localhost:6379"),
    add_history_to_context=True,
)

DynamoDB

AWS serverless storage.
cookbook/06_storage/dynamodb/dynamo_for_agent.py
from agno.agent import Agent
from agno.db.dynamodb import DynamoDb

agent = Agent(
    db=DynamoDb(
        table_name="agno-agents",
        region="us-east-1",
    ),
    add_history_to_context=True,
)

Firestore

GCP serverless storage.
cookbook/06_storage/firestore/firestore_for_agent.py
from agno.agent import Agent
from agno.db.firestore import FirestoreDb

agent = Agent(
    db=FirestoreDb(
        project_id="your-project",
        collection="agents",
    ),
    add_history_to_context=True,
)

In-Memory

For testing and development.
cookbook/06_storage/in_memory/in_memory_storage_for_agent.py
from agno.agent import Agent
from agno.db.memory import InMemoryDb

agent = Agent(
    db=InMemoryDb(),
    add_history_to_context=True,
)

Storage for Teams and Workflows

Storage works the same way for teams and workflows.

Team Storage

cookbook/06_storage/postgres/postgres_for_team.py
from agno.team import Team
from agno.db.postgres import PostgresDb

team = Team(
    agents=[...],
    db=PostgresDb(db_url="postgresql://..."),
)

Workflow Storage

cookbook/06_storage/postgres/postgres_for_workflow.py
from agno.workflow import Workflow
from agno.db.postgres import PostgresDb

workflow = Workflow(
    db=PostgresDb(db_url="postgresql://..."),
)

Run Examples

git clone https://github.com/agno-agi/agno.git
cd agno/cookbook/06_storage

# PostgreSQL
python postgres/postgres_for_agent.py

# SQLite (no deps)
python sqlite/sqlite_for_agent.py

# In-Memory (no deps)
python in_memory/in_memory_storage_for_agent.py