Prompt 11 of the 32-prompt Enterprise AI Analytics & Recommendation
Platform initiative (00_MASTER_IMPLEMENTATION_CONTRACT.md), following
10_GOVERNED_METRICS_CONTRACT.md. INSPECT/PLAN/IMPLEMENT/TEST/
REVIEW/FIX/DOCUMENT — one new typed-contract module, a new LangGraph
node that runs on every question, a widened (never narrowed)
plan_query_node gate, and additive config/eval surface. No existing
route, node, or Settings field changed behavior for a caller whose
question classification is disabled, unreachable, or unparseable.
02_TARGET_ARCHITECTURE.md §2 named this exact gap back in Prompt 02:
the only “intent”-shaped classification in this codebase was
agent.followup.classify_followup (standalone/followup/ambiguous — a
conversational classification, the one node allowed to short-circuit
the graph to END) and agent.complexity’s four regex trip-wires
(top_n_per_group/growth_comparison/ranking_window/multi_metric —
a narrow “is this complex enough to widen the retry budget / bother
planning” signal, not a classifier). Neither answers “what kind of
analytical question is this” in any general sense. recommendation
/models.py’s RecommendationKind is a different axis entirely (what
kind of suggestion to make, not what kind of question was asked) and
was confirmed unrelated.
plan_query_node (gated LLM call, fails open on OllamaUnavailableError
only, threads selected_model/retrieved_context, shares the existing
retry_count/max_retries/AttemptRecord/error_history machinery) was
confirmed as the exact template every new advisory LLM-calling node in
this series clones — but critically, agent.complexity.compute_max_retries
and state["complexity_signals"] are computed once in run_agent()
before build_graph()/.invoke() even run (verified via direct
line-anchored inspection), so no graph node — including a new one — can
retroactively widen the retry budget itself. This forced the design
decision that a new intent classifier must be purely advisory to
plan_query_node’s own gate/prompt, never to the retry budget.
Permission.ASK is enforced by a FastAPI dependency strictly before
run_agent/the graph are even constructed (verified directly in
api/main.py), which is the structural proof that any new node deep
inside the graph cannot bypass authorization, by construction.
agent/intent.py (new)Mirrors the established “typed contract module, separate from the
LLM-calling module” split (agent/followup.py, agent/provenance.py,
semantic/catalog.py):
AnalyticalIntentType(str, Enum) — exactly the 13 named categories
(LOOKUP, AGGREGATION, TREND, COMPARISON, RANKING, DISTRIBUTION,
SEGMENTATION, FUNNEL, COHORT, FORECAST, ANOMALY, ROOT_CAUSE,
RECOMMENDATION). A closed set — an unrecognized value from the LLM
makes the whole response unparseable, never silently coerced.ExpectedResultShape(str, Enum) — SINGLE_VALUE/TIME_SERIES/
RANKED_LIST/TABLE/DISTRIBUTION/COMPARISON_TABLE, an
independent judgment the model makes alongside intent, not
programmatically derived from it.AnalyticalIntentClassification(BaseModel, frozen=True) — intent
and confidence (Field(ge=0.0, le=1.0)) are the only two
required fields, deliberately with no default for confidence:
an omitted value is malformed, not silently assigned a fabricated
mid-confidence score (master rules 9/10’s “never silently…”
spirit). Every other field (metric_candidates, dimensions,
time_requirement, comparison, filters,
expected_result_shape, ambiguity_flags) defaults to empty/None.
truth_level: DataTruthLevel = AI_INFERENCE — reuses
agent.provenance’s existing vocabulary exactly like
SemanticLabel/InferredRelationship already do; never promoted._INTENT_TYPES_IMPLYING_PLANNING: frozenset[AnalyticalIntentType] —
every intent except LOOKUP/AGGREGATION (the two simplest, most
direct categories) — the single source of truth both
plan_query_node’s gate and this module’s own tests read directly.intent_implies_planning(classification: dict) -> bool — deliberately
takes a plain dict, not the pydantic model, matching AgentState’s
established “plain dicts, not model instances” convention for
retrieved_context/query_plan-shaped fields. classification["intent"]
is re-parsed via AnalyticalIntentType(...) explicitly, correct
regardless of whether the caller’s dict holds the enum member or a
plain string (e.g. after a JSON round-trip). Returns True when the
classified intent is in _INTENT_TYPES_IMPLYING_PLANNING or
ambiguity_flags is non-empty — an ambiguous question benefits from a
plan to work through the ambiguity regardless of which intent was
guessed.agent/llm_client.py (extended)New _INTENT_SYSTEM_PROMPT (the same security-override boilerplate
every system prompt in this file repeats; the 13 intent values and 6
result-shape values are interpolated directly from
AnalyticalIntentType/ExpectedResultShape, never duplicated as a
separate literal list that could drift). New
_build_intent_governing_metrics_block renders governing-metric
names only (never the full approved expression
_build_mandatory_metrics_block renders for generation) — enough
grounding to let the model name a real governed metric in
metric_candidates when one plainly applies, without conflating it with
this call’s own output. New _build_intent_user_prompt — deliberately
lean, no full schema DDL (classification is about the question’s own
wording, not the schema it will eventually be matched against — keeping
this call cheap matters more than schema-grounding it, since it runs on
every question). New _parse_intent_response(raw_response) -> dict |
None — reuses the existing _JSON_FENCE_RE constant (not
duplicated), json.loadss, then AnalyticalIntentClassification
.model_validate(...) inside a try/except (ValidationError,
json.JSONDecodeError, ValueError) → None on any failure, returning
.model_dump(mode="json") on success. New
generate_analytical_intent_from_llm(question, settings, model=None,
retrieved_context=None, governing_metrics=None) -> dict | None — clones
generate_query_plan_from_llm’s exact call shape (temperature=0.0, the
new Settings.intent_classification_max_tokens, the same
OllamaUnavailableError re-raise, the same _log_ollama_timing).
New _build_plan_intent_block threads the classification into
plan_query_node’s own prompt (mirrors _build_plan_business_concept_
block’s exact shape: DATA framing, None when absent), framed as an
AI_INFERENCE, never confirmed, never an instruction.
generate_query_plan_from_llm gained an analytical_intent parameter
threading this through — deliberately scoped to planning only, never
injected into generate_sql_from_llm’s own prompt directly (it reaches
generation only indirectly, through whatever plan it causes to be
written), matching the acceptance criterion’s literal wording (“intent
influences planning”).
New classify_analytical_intent_node (agent/nodes.py), wired
retrieve_business_context → classify_analytical_intent → plan_query
(straight edges only, see §3’s “never short-circuits” proof below).
Gated only by Settings.enable_intent_classification (default True)
— runs on every question when enabled, unlike plan_query_node’s
own complexity-gated call, since intent classification is meant to be
foundational (per the prompt’s own “before SQL generation” framing).
Fails open exactly like plan_query_node: a disabled flag, an
unreachable Ollama server, or an unparseable response all resolve to
state["analytical_intent"] = None, logged but never a reason the
question can’t be answered, and never a terminal status.
The one real behavioral change, and the acceptance-criterion
mechanism: plan_query_node’s gate became enable_query_planning and
(complexity_signals or intent_triggers_planning), where
intent_triggers_planning = analytical_intent is not None and
intent_implies_planning(analytical_intent) — reading
agent.intent._INTENT_TYPES_IMPLYING_PLANNING indirectly through that
helper, never re-deriving its own copy. When analytical_intent is
None for any reason, this reduces to exactly complexity_signals
alone — byte-identical to this node’s pre-Prompt-11 behavior,
preserved by construction, not convention (directly verified by
test_none_analytical_intent_reduces_to_pre_prompt_11_behavior and
test_lookup_intent_with_no_signals_or_ambiguity_still_skips_planning).
Settings.enable_intent_classification: bool = True,
Settings.intent_classification_max_tokens: int = Field(default=250,
gt=0) — same on-by-default-but-escapable posture and naming convention
as enable_query_planning/query_plan_max_tokens.
overall_pass)eval/schema.py’s BenchmarkCase gained optional expected_intent: str
| None / expect_ambiguity: bool = False; CaseRunResult gained
observed_intent/observed_ambiguity_flags (populated by
eval/runner.py from the real state["analytical_intent"]) and a new
verdict field intent_classification_correct: bool | None. New
eval/evaluators.py::evaluate_intent_classification mirrors
evaluate_sql_exact_match’s “None if not applicable, diagnostic only,
never referenced by compute_overall_pass” shape exactly — a wrong
intent classification never fails an otherwise-correct result (verified
by test_intent_classification_correct_never_gates_overall_pass). New
eval/metrics.py metric intent_classification_accuracy (same None-
means-“not measured” contract as every other category-scoped metric).
eval/dataset_loader.py::_parse_case was extended to actually map the
two new YAML fields through — a real risk this loader’s own explicit-
field-mapping style creates for any new BenchmarkCase field, closed
here and covered by a dedicated regression test
(test_expected_intent_and_expect_ambiguity_are_parsed).
Six already-live-verified cases across eval/benchmark/easy.yaml/
real_world.yaml were tagged with expected_intent/expect_ambiguity
(pure metadata additions, zero new SQL, so no new live-DB verification
was needed): easy_filter_red_products (LOOKUP),
easy_agg_count_products (AGGREGATION), rw_top5_customers_by_spend
(RANKING), rw_gender_breakdown (DISTRIBUTION), and the two already-
documented genuinely-ambiguous cases ambig_bikes_top_territory/
ambig_best_selling_product (RANKING + expect_ambiguity: true).
Permission.ASK is enforced by a FastAPI
dependency before run_agent/the graph are even built (api/main.py).
classify_analytical_intent_node runs deep inside an already-
authorized request and never itself reads or writes caller_roles,
max_retries, or any field validate_sql_node’s restricted-column
authorization check reads — its only consumer is plan_query_node’s
own gate/prompt.test_classify_analytical_intent_has_no_conditional_routing
(tests/test_sql_agent_integration.py) confirms classify_
analytical_intent has exactly one outgoing edge (a straight
add_edge to plan_query, conditional=False), unlike
classify_followup, which does carry a conditional, possibly-END
routing table. Deliberately distinct from classify_followup_node’s
own conversational “ambiguous” classification (the one path in this
codebase actually allowed to end the graph early) — this prompt’s
analytical ambiguity_flags is advisory metadata only, feeding the
planning gate, documented explicitly in both modules’ docstrings to
prevent future confusion between the two different “ambiguous”
concepts.plan_query_node’s gate reduces to
exactly complexity_signals alone — verified directly, not just
claimed (see §2 above).generate_sql_node’s own prompt directly — per
the acceptance criterion’s literal wording (“intent influences
planning”), the classification only ever reaches generation
indirectly, through whatever plan plan_query_node is triggered to
write.scripts/test_db_connection.py), not
the DimProduct/FactInternetSales-family base tables a genuine new
TREND/COMPARISON question would need to reference and live-verify.
Writing new benchmark SQL without being able to execute and confirm
it against the real database would violate this project’s own
“every expected_sql was executed against the live, configured
database during dataset authoring” invariant (eval/benchmark/
easy.yaml’s own header comment) — so, like
FUNNEL/COHORT/FORECAST/ANOMALY/ROOT_CAUSE/RECOMMENDATION, TREND and
COMPARISON get contract-level (pytest) coverage only in this pass, not
a new live-verified YAML case. tests/test_agent_intent.py’s
golden-parse test and tests/test_agent_nodes.py’s
test_intent_alone_triggers_planning_with_zero_complexity_signals/
test_ambiguity_flags_alone_trigger_planning_even_for_lookup still
directly exercise both categories’ effect on the planning gate at the
unit level.Permission.ASK is the only gate, same as generation itself.classify_analytical_intent_node reads
state["selected_model"], resolved once by run_agent(), never
re-selected — same established pattern as every other per-question
LLM call.6 test files touched (2 new, 4 extended), ~102 new tests, plus 2 pre-existing tests fixed for the new node’s wiring/signature:
tests/test_agent_intent.py (new, 49) — golden-parse coverage for
all 13 AnalyticalIntentType values and all 6 ExpectedResultShape
values, required-field enforcement (intent/confidence missing →
ValidationError; confidence out of [0.0, 1.0]), optional-field
defaults, frozen-model immutability, _INTENT_TYPES_IMPLYING_PLANNING’s
exact membership (LOOKUP/AGGREGATION excluded, all 11 others included),
and intent_implies_planning’s full behavior (per-intent, ambiguity-
alone, and plain-string-vs-enum-member dict-input equivalence).tests/test_llm_client_intent.py (new, 29) — _parse_intent_response’s
fail-open contract (valid/fenced JSON, invalid enum value, missing
required field, malformed JSON, out-of-range confidence — each → the
correct dict or None), _build_intent_governing_metrics_block/
_build_intent_user_prompt’s DATA-framing and metric-name-only
grounding, _build_plan_intent_block’s rendering (every optional line
present/absent correctly), and generate_analytical_intent_from_llm’s
mocked-Ollama call shape, OllamaUnavailableError propagation, and
governing-metrics-grounding-without-conflation proof.tests/test_agent_nodes.py (+10, +1 fixed) — TestClassifyAnalyticalIntentNode
(disabled-flag skip, runs with zero complexity signals, successful
classification stored as a plain dict, fail-open on unreachable
Ollama, fail-open on an unparseable response — never a terminal
status in any case), plus 5 new TestPlanQueryNode cases proving the
widened gate (TREND alone with zero complexity signals triggers
planning; LOOKUP with zero signals/no ambiguity still skips — the
explicit preserved-fallback regression test; ambiguity_flags alone
triggers planning even for LOOKUP; analytical_intent=None reduces to
pre-Prompt-11 behavior; analytical_intent is correctly threaded into
the plan call) — plus the one pre-existing test
(test_calls_llm_and_stores_plan_for_a_complex_question) fixed for
generate_query_plan_from_llm’s new analytical_intent parameter.tests/test_sql_agent_integration.py (+1 new, +1 fixed) — the new
test_classify_analytical_intent_has_no_conditional_routing
structural proof (§3 above), and the pre-existing
test_retrieve_business_context_is_wired_between_golden_examples_and_
plan_query corrected for the new node sitting between
retrieve_business_context and plan_query.tests/test_eval_evaluators.py (+8) — TestEvaluateIntentClassification
(no-hint not-applicable, matching/mismatched intent, a labeled case
with no observed intent fails rather than being skipped, ambiguity-
flag matching, both-hints-must-both-pass), plus the diagnostic-only
regression test in TestComputeOverallPass.tests/test_eval_metrics.py (+3) — intent_classification_accuracy’s
“None when unmeasured,” correct aggregation over only the labeled
cases, and the diagnostic-only (never-affects-final_accuracy) proof.tests/test_eval_dataset_loader.py (+2) — the two new YAML fields
actually reach BenchmarkCase (not silently dropped by _parse_case’s
explicit mapping), and correctly default when omitted.Full suite: 2877 tests pass, zero regressions (pytest -q, re-run
after every change in this prompt). ruff check/black --check/mypy
clean on every new/touched file.
agent/llm_client.py already
requires. classify_analytical_intent_node is never itself a security
or authorization control — see §3’s structural proof. No new
user-controllable field reaches agent.sql_validator/execution; the
classification only ever influences which planning prompt is built,
and the plan itself was already treated as advisory, injection-framed
DATA before this prompt (_build_plan_block’s existing framing,
unchanged).classify_analytical_intent_node reads only question,
retrieved_context, governing_metrics, and selected_model, all
already resolved/scoped by earlier nodes in the same run. No new
database query of any kind.intent_classification_
max_tokens=250 by default) to minimize the added latency of running
on every question rather than only a complexity-flagged subset, the
real cost tradeoff of making this foundational rather than opt-in per
question-shape. Disabling Settings.enable_intent_classification
removes this cost entirely with zero other behavior change.agent
.sql_validator/execution are completely unaffected by this prompt.easy.yaml/real_world.yaml are loader-verified
(the fields parse and reach BenchmarkCase/CaseRunResult correctly)
but not yet exercised against a real Ollama server in this session —
scripts/run_benchmark.py against a live model is the natural
follow-up, same manual/real-infra-required posture every other
benchmark run already has.expected_intent/expect_ambiguity exist only on BenchmarkCase,
not FollowUpTurn — evaluate_intent_classification’s getattr
defaulting handles this gracefully (resolves to “not applicable” for
any follow-up turn), but a future prompt wanting to golden-label a
follow-up turn’s intent would need to extend FollowUpTurn too.Per 02_TARGET_ARCHITECTURE.md §5’s own table: wire analytics/
(ResultSummaryAnalyticsProvider) into generate_insight_node and
surface AnalyticsResult in AskResponse — unchanged recommendation
carried forward from Prompts 04-10, still pending explicit sign-off
since it changes a live response shape. Alternatively, a live-LLM run of
scripts/run_benchmark.py against the six newly-tagged golden-intent
cases to empirically confirm this prompt’s classification accuracy
end-to-end (not just the deterministic unit-level proof already in
place), or revisiting the DB role/schema configuration in this
development environment so a future prompt can live-verify new
TREND/COMPARISON benchmark SQL against the real base tables.