from_db Runtime Context
How Agent.from_db() adds compact database context, selects tools per prompt, records audit entries, and compacts tool results during each run.
#Overview
Agent.from_db() wraps run() and stream() with DB-specific runtime behavior. The public API stays the same:
answer = await agent.run("Which products grew fastest last month?")Under the hood, each prompt gets a compact DB context block and a scoped tool set.
#Context Contents
The runtime context can include:
- database type and name
- selected mode
- table, column, and relationship counts
- query policy
- available capabilities
- memory status and relevant DB memory snippets
- data-health summary
- candidate metrics
- schema hints
- drift summary
This keeps each run grounded without requiring the full schema in every prompt.
#Per-Run Tool Selection
from_db() selects a focused set of tools for each prompt. Schema tools are available for navigation, query tools are available for data questions, and specialized tools are added when the prompt calls for them.
Examples:
- schema-only questions emphasize schema navigation tools
- analytic questions include
db_plan_queryanddb_query - quality questions include data-quality tools when enabled
- memory-write prompts include
db_rememberwhen DB memory is enabled - write prompts can include
db_executeonly whenread_only=False
Use agent.describe() and agent.db for stable inspection of the configured capabilities, tools, prompt strategy, and query policy.
#Final Synthesis
DB agents are configured to allow a final answer after terminal DB tools such as db_query, db_count, and db_sample. The model can query, receive compacted results, and then synthesize a user-facing answer.
#Audit Log
from_db() records a redacted audit log for DB-agent runs:
await agent.run("Show total orders by status")
last = agent.db.audit.last()
print(last["prompt"])
print(last["tool_calls"])Raw row values and parameter values are not stored verbatim in the audit log. Use this for debugging and observability, not for reconstructing result data.
#Tool Result Compaction
Result compaction keeps tool output small before it is sent back to the model:
from daita.agents.db.config import ToolResultPolicy
agent = await Agent.from_db(
"postgresql://user:pass@host/db",
tool_result_policy=ToolResultPolicy(max_rows_inline=6),
)Compaction can:
- keep only a preview of rows
- summarize large JSON values
- truncate long strings
- omit noisy columns by pattern
- preserve row counts and truncation metadata
#Streaming
stream() receives the same DB runtime behavior as run():
async for event in agent.stream("Find unusual order spikes this week"):
print(event)The agent still uses compact DB context, selected tools, result compaction, and audit behavior.