Agent.from_db()
Build a database-backed agent from a connection string or database plugin. The agent discovers schema, applies guardrails, plans queries, navigates large schemas, and exposes a public agent.db context.
#Overview
Agent.from_db() creates a database analyst agent in one call. It connects to a database, discovers schema, configures safe query tools, adds schema navigation for large databases, and wraps each run with DB-specific context.
from daita import Agent
agent = await Agent.from_db(
"postgresql://user:pass@localhost:5432/analytics",
mode="analyst",
)
await agent.start()
result = await agent.run("What were the top 5 products by revenue last quarter?")
await agent.stop()Use from_db() when you want an agent to answer natural-language questions over a database without manually writing prompts, registering database tools, or describing schema by hand.
#What It Provides
- Automatic schema discovery for supported database plugins
- A stable DB tool surface:
db_plan_query,db_validate_sql,db_query,db_count,db_sample, and optionaldb_execute - Metadata-only schema navigation tools for large or ambiguous schemas
- Read-only defaults, SQL validation, table and column restrictions, row limits, and result compaction
- Mode presets for simple, analyst, governed, and data-team workflows
- Optional lineage, memory, history, and data-quality integrations
- A public
agent.dbcontext for schema, summary, audit, findings, monitors, and configuration metadata
#Supported Sources
Pass either a connection string or a database plugin instance:
from daita import Agent
from daita.plugins import postgresql
# Connection string
agent = await Agent.from_db("postgresql://user:pass@host:5432/mydb")
# Existing plugin instance
db = postgresql(host="localhost", database="analytics")
agent = await Agent.from_db(db)Connection strings are supported for PostgreSQL, MySQL, SQLite, and MongoDB. For Snowflake, create a Snowflake plugin instance and pass that plugin to Agent.from_db().
#Common Paths
- Quick Start - Build and run your first DB agent
- Configuration - Full public parameter reference
- Modes - Choose
simple,analyst,governed, ordata_team - Schema Discovery - Discovery, sampling, caching, and drift
- Query Tools - The public
db_*tool surface - Schema Navigation - Tools for large schemas and join discovery
- Guardrails - Read-only mode, SQL validation, limits, and access controls
- Runtime Context - What gets injected into each run
- Memory - DB semantic memory and
db_remember - Monitors & Findings - Local DB monitors and finding records
- agent.db Context - Public inspection and debugging API
- Examples - Practical recipes
#Minimal Production Setup
agent = await Agent.from_db(
"postgresql://user:pass@host:5432/warehouse",
mode="governed",
prompt="Revenue means paid order total excluding refunds. Use UTC dates.",
allowed_tables=["orders", "customers", "products"],
blocked_columns=["email", "phone", "address"],
cache_ttl=3600,
)This creates a read-only agent with bounded query results, schema-aware SQL checks, history and lineage enabled by the governed mode, and table/column restrictions for safer production use.
#Next Steps
- Start with Quick Start
- Review Guardrails before production use
- Use Schema Navigation for large schemas
- Use agent.db Context to inspect what the agent discovered