Skip to main content
ai-query provides built-in storage backends for different use cases. Storage backends implement the Storage protocol and are injected into agents via the storage parameter.

Import

Storage Protocol

All storage backends implement this protocol:

MemoryStorage

In-memory storage for development and testing. Data is lost when the process exits.

Constructor

No parameters required. Creates an empty in-memory store.

Usage

Notes

  • Data is shared across all agents using the same MemoryStorage instance
  • Thread-safe for concurrent access
  • Fast for development and testing
  • Not suitable for production persistence

SQLiteStorage

Async SQLite persistence using aiosqlite. Non-blocking database operations that won’t freeze your event loop.

Constructor

str
required
Path to the SQLite database file. Use :memory: for in-memory database.

Usage

Methods

sql

Execute a SQL query against the storage database asynchronously. Parameters:
  • query: SQL query string with ? placeholders
  • *params: Query parameters
Returns: List of rows as dictionaries. Example:

Notes

  • Uses lazy connection initialization
  • Connection is automatically closed when the agent stops
  • Tables are created automatically on first use
  • State and messages are stored as JSON

Creating Custom Storage Backends

Implement the Storage protocol to use any backend:

DynamoDB Example

PostgreSQL Example


Comparison

Choosing a Backend

  • Development/Testing: Use MemoryStorage - fast, no setup
  • Local Deployment: Use SQLiteStorage - simple persistence, works out of box
  • Multi-Instance/Scaling: Create a custom backend with Redis, DynamoDB, or PostgreSQL
  • Serverless: Create a custom backend with DynamoDB or other serverless databases