Author's bio: ๐๐ Hi, I'm CryptoPatrick! I'm currently enrolled as an Undergraduate student in Mathematics, at Chalmers & the University of Gothenburg, Sweden. If you have any questions or need more info, then please join my Discord Channel: AiMath
๐ Important Notices
- Supports SQLite, PostgreSQL, and MySQL
- Implements the AgentDB trait from agentdb
- Includes database migration system for schema management
- Powered by SQLx for type-safe SQL
๐ค What is AgentSQL
agentsql is a production-ready SQL backend implementation for AI agent persistence. It provides a unified interface for SQLite, PostgreSQL, and MySQL databases through the AgentDB trait, enabling agents to store filesystems, key-value data, and tool call audit logs with a single API.
Built with SQLx for type-safe SQL, AgentSQL handles schema migrations automatically and provides seamless switching between database backends without code changes.
Use Cases
- Development: Use SQLite for local development with zero configuration
- Production: Deploy on PostgreSQL or MySQL for multi-agent systems
- Cloud: Seamlessly migrate between managed database services (AWS RDS, Google Cloud SQL, Azure)
- Testing: Fast in-memory SQLite databases for unit tests
- Edge: Embedded SQLite for resource-constrained environments
- Enterprise: PostgreSQL/MySQL for high-availability deployments
๐ท Features
agentsql provides complete SQL backend support for agent persistence with production-grade features:
๐พ Multi-Database Support
SQLite
- Zero Configuration: File-based or in-memory databases
- Single File: Entire database in one portable file
- Fast: Ideal for development and embedded systems
- ACID Compliant: Full transaction support
- In-Memory Mode: Perfect for testing
PostgreSQL
- Production Grade: Battle-tested for high-load scenarios
- Advanced Features: JSONB, full-text search, concurrent access
- Scalability: Handles millions of records efficiently
- Cloud Ready: Works with AWS RDS, Google Cloud SQL, Azure Database
- Replication: Built-in streaming replication
MySQL
- Wide Adoption: Industry-standard database
- Compatibility: Works with MySQL, MariaDB, and cloud variants
- Replication: Built-in master-slave replication
- Cloud Services: Compatible with AWS Aurora, Google Cloud SQL
- Performance: Optimized for read-heavy workloads
๐ง Schema Management
- Automatic Migrations: Schema applied on first connection
- Multi-Statement Support: Complex migration scripts
- Inode/Dentry Design: Unix-like filesystem structure
- Indexes: Optimized for filesystem operations
- Tool Call Auditing: Built-in audit trail table
- Version Control: Schema versioning support
๐ Production Ready
- Connection Pooling: Efficient connection management via SQLx
- Async Operations: Full async/await support with Tokio
- Error Handling: Detailed error messages with context
- Type Safety: Compile-time SQL verification (SQLx)
- NULL Handling: Proper handling of optional fields
- Performance: Optimized queries with prepared statements
๐ Architecture
- ๐ Overall Architecture
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ AgentFS High-Level APIs โ
โ (FileSystem, KvStore, ToolRecorder) โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ AgentDB Trait Interface โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ AgentSQL (SQLx) โ
โ โข Connection pooling โ
โ โข Query builder โ
โ โข Migration system โ
โ โข Type conversions โ
โโโโโฌโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโ
โ โ โ
โโโโโผโโโโโโโ โโโโโโโโผโโโโโโโ โโโโโโโผโโโโโโโ
โ SQLite โ โ PostgreSQL โ โ MySQL โ
โ Local โ โ Production โ โ Cloud โ
โโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโ
- ๐พ Database Schema
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ SQLite / PostgreSQL / MySQL โ
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ fs_inode (File Metadata) โ โ
โ โ - ino (PK, AUTO_INCREMENT) โ โ
โ โ - mode (permissions) โ โ
โ โ - uid, gid โ โ
โ โ - size, atime, mtime, ctime โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ fs_dentry (Directory Entries) โ โ
โ โ - id (PK) โ โ
โ โ - name โ โ
โ โ - parent_ino (FK โ fs_inode) โ โ
โ โ - ino (FK โ fs_inode) โ โ
โ โ UNIQUE(parent_ino, name) โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ fs_data (File Content) โ โ
โ โ - id (PK) โ โ
โ โ - ino (FK โ fs_inode) โ โ
โ โ - offset โ โ
โ โ - size โ โ
โ โ - data (BLOB/BYTEA) โ โ
โ โ INDEX(ino, offset) โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ kv_store (Key-Value) โ โ
โ โ - key (PK) โ โ
โ โ - value (TEXT) โ โ
โ โ - created_at, updated_at โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ tool_calls (Audit Trail) โ โ
โ โ - id (PK) โ โ
โ โ - name โ โ
โ โ - parameters (JSON) โ โ
โ โ - result (JSON) โ โ
โ โ - error โ โ
โ โ - status (pending/success/error) โ โ
โ โ - started_at, completed_at โ โ
โ โ - duration_ms โ โ
โ โ INDEX(name), INDEX(started_at) โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
- ๐ Migration Flow
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ SqlBackend::new(config) โ
โโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโผโโโโโโโโโโโ
โ Detect Backend โ
โ SQLite/PG/MySQL โ
โโโโโโโโโโฌโโโโโโโโโโโ
โ
โโโโโโโโโโผโโโโโโโโโโโ
โ Load Migration โ
โ SQL for backend โ
โโโโโโโโโโฌโโโโโโโโโโโ
โ
โโโโโโโโโโผโโโโโโโโโโโ
โ Execute Schema โ
โ Multi-statement โ
โโโโโโโโโโฌโโโโโโโโโโโ
โ
โโโโโโโโโโผโโโโโโโโโโโ
โ Initialize Root โ
โ inode (ino=1) โ
โโโโโโโโโโฌโโโโโโโโโโโ
โ
โโโโโโโโโโผโโโโโโโโโโโ
โ Ready for Use! โ
โโโโโโโโโโโโโโโโโโโโโ
๐ How to Use
Installation
Add to your Cargo.toml:
[]
= "0.1"
= "0.1"
# Enable the backend(s) you need:
= { = "0.1", = ["sqlite"] }
# agentsql = { version = "0.1", features = ["postgres"] }
# agentsql = { version = "0.1", features = ["mysql"] }
# agentsql = { version = "0.1", features = ["sqlite", "postgres", "mysql"] }
Or install with cargo:
Example: SQLite (Local Development)
use SqlBackend;
use AgentDB;
async
Example: PostgreSQL (Production)
use SqlBackend;
use AgentDB;
use env;
async
Example: MySQL (Cloud Deployment)
use SqlBackend;
async
๐งช Examples
See the agentfs crate for complete examples demonstrating:
- Basic SQLite usage
- PostgreSQL multi-agent systems
- MySQL cloud deployments
๐งช Testing
Run the test suite:
# Run all tests (SQLite)
# Test with PostgreSQL
# Test with MySQL
# Run with output
๐ Documentation
Comprehensive documentation is available at docs.rs/agentsql, including:
- API reference for
SqlBackend - Migration system details
- Database-specific configuration
- Performance tuning guides
- Connection pooling best practices
๐ Author
CryptoPatrick
Keybase Verification: https://keybase.io/cryptopatrick/sigs/8epNh5h2FtIX1UNNmf8YQ-k33M8J-Md4LnAN
๐ฃ Support
Leave a โญ if you think this project is cool.
๐ License
This project is licensed under MIT. See LICENSE for details.