ai-powered-conversational-intelligence

Prompt 33 – AI Data Analyst Agent

A governed, multi-step analyst that investigates a business question by running bounded sub-questions through the existing Text-to-SQL pipeline, then returns an evidence-linked report. Off by default (ENABLE_DATA_ANALYST_AGENT=false).

Pipeline

understand -> execute <-> analyze -> recommend -> explain -> END
Node Does Charged to budget
understand check_input on the question; one LLM call decomposes it into sub-questions (agent/analyst/planner.py). Any planner failure falls back to the single original question. step + 1 LLM call
execute Runs ONE sub-question through agent.graph.run_agent, the full governed pipeline. Maps the result to evidence (agent/analyst/evidence.py). step + sub-question
analyze Queues one deterministic follow-up per anomalous period found in the evidence. step (+ follow-up when queued)
recommend Dedupes the sub-runs’ recommendations and links each to the evidence it cites. none
explain Writes the final status and the evidence-linked report. Always runs, even after a budget stop. none

Mapping of the prompt’s stages to nodes: Understand = understand; Plan = understand’s decomposition; Retrieve, Query, Validate, Execute = inside each execute via run_agent; Analyze = execute’s evidence mapping plus analyze; Investigate = analyze’s follow-ups; Explain = explain; Recommend = recommend.

What is reused (not duplicated)

API

POST /analyst/investigate (api/analyst.py, schemas in api/analyst_schemas.py)

Requires Permission.ASK and Permission.EXECUTE_SQL. Returns 404 when the feature flag is off.

Request (extra="forbid"):

{ "question": "Why did revenue fall in April?",
  "conversation_history": [],
  "model": null }

The request cannot carry a tenant, roles or budget. Any such field is a 422. Tenant and roles come from the authenticated identity. The budget comes from Settings.

Response:

Configuration

Setting Env var Default Meaning
enable_data_analyst_agent ENABLE_DATA_ANALYST_AGENT false Route is 404 when off
analyst_max_steps ANALYST_MAX_STEPS 12 Charged node executions
analyst_max_subqueries ANALYST_MAX_SUBQUERIES 4 Sub-questions run (planner + follow-ups)
analyst_max_llm_calls ANALYST_MAX_LLM_CALLS 2 The analyst’s own LLM calls
analyst_max_followups ANALYST_MAX_FOLLOWUPS 2 Anomaly follow-ups per analysis
analyst_timeout_seconds ANALYST_TIMEOUT_SECONDS 180 Wall-clock deadline, checked between nodes
analyst_max_recommendations ANALYST_MAX_RECOMMENDATIONS 10 Recommendations kept in the report

Controls

Tests

tests/test_data_analyst_agent.py (40) and tests/test_api_analyst.py (8), fully mocked at run_agent and call_ollama. The graph, budget, evidence mapper and report renderer all run for real.

Covered: multi-step decomposition through the governed pipeline; caller roles and tenant forwarded to every sub-run; recommendations linked to evidence; observed/AI sections kept apart; planner failure falling back to the single-question path; ambiguity (alone and mixed); insufficient data; a single anomaly triggering exactly one bounded follow-up; a hostile period label never reaching a question; the sub-query budget blocking a follow-up and saying so; a timeout stopping before new work; injection in the question (rejected before any SQL) and in planner output (dropped); restricted data blocked without leaking the column; a sub-run exception recovered from; the route’s feature flag; the request rejecting smuggled tenant/roles/budget fields; the model allowlist.

Results: analyst tests 48 passed; full backend suite tests/ 3776 passed; tests/security/ 128 passed. ruff/black clean on new files. mypy clean on new files (one pre-existing error in db/schema_introspection.py, not touched).

Limitations (honest)

Risks

Next prompt

Recommended: Prompt 34 – a live evaluation harness for the analyst. Run the analyst against the existing eval/benchmark questions and a set of multi-step business questions on the real database and model. Measure decomposition quality, sub-question execution accuracy, budget stop rates and p95 latency. Then decide whether to add a hard cancel (a per-sub-run timeout through the execution layer) and a per-caller limit before enabling the flag.