from_db Examples
Practical Agent.from_db() recipes for analysts, governed production agents, large schemas, memory, monitors, and write-enabled workflows.
#Read-Only Analyst
python
from daita import Agent
agent = await Agent.from_db(
"postgresql://user:pass@host/analytics",
mode="analyst",
prompt="Use USD for currency and UTC for dates.",
)
await agent.start()
answer = await agent.run("Show weekly revenue for the last 8 weeks.")
await agent.stop()#Governed Production Agent
python
agent = await Agent.from_db(
"postgresql://user:pass@host/warehouse",
mode="governed",
allowed_tables=["orders", "customers", "products"],
blocked_columns=["email", "phone", "address"],
query_default_limit=25,
cache_ttl=3600,
prompt="""
Revenue excludes refunds. Use order_created_at for order date.
Do not expose direct customer identifiers in final answers.
""",
)#Large Schema Agent
python
agent = await Agent.from_db(
"postgresql://user:pass@host/warehouse",
budget="retrieval",
mode="analyst",
)
print(agent.describe()["db"]["prompt"])Retrieval-style schema prompting lets the agent use schema navigation tools instead of depending on one enormous prompt.
#Memory-Enabled Metrics Agent
python
agent = await Agent.from_db(
"postgresql://user:pass@host/analytics",
memory=True,
prompt="Store durable metric definitions when the user teaches them.",
)
await agent.run("""
Remember that net revenue excludes refunds, chargebacks, and test orders.
""")
answer = await agent.run("What was net revenue last month?")#Data-Team Agent
python
agent = await Agent.from_db(
"postgresql://user:pass@host/warehouse",
mode="data_team",
)
metadata = agent.describe()
print(metadata["capabilities"])data_team enables analyst tools, lineage, history, and data-quality tools.
#Local Freshness Monitor
python
agent = await Agent.from_db(
"postgresql://user:pass@host/warehouse",
mode="data_team",
)
agent.db.register_monitors([
{
"name": "Orders freshness",
"type": "freshness",
"severity": "warning",
"entity": {"table": "orders"},
"sql": "SELECT MAX(updated_at) FROM orders",
"threshold": {"max_age_hours": 24},
"interval": "1h",
}
])#Custom Conversation History
python
from daita import ConversationHistory
history = ConversationHistory(max_turns=6)
agent = await Agent.from_db(
"postgresql://user:pass@host/db",
history=history,
)
await agent.run("How many orders last week?")
await agent.run("Break that down by channel.")#Write-Enabled Admin Agent
python
agent = await Agent.from_db(
"postgresql://user:pass@host/admin",
read_only=False,
allowed_tables=["feature_flags"],
query_timeout=15,
prompt="Only update feature flags when the user explicitly asks.",
)Keep write-enabled agents narrow. Use database permissions, table allowlists, and explicit prompts.