Daita Logo

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.

python
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 optional db_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.db context for schema, summary, audit, findings, monitors, and configuration metadata

#Supported Sources

Pass either a connection string or a database plugin instance:

python
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

#Minimal Production Setup

python
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