Text-to-MongoDB Analyst
Natural-language analytics and report generation over field-activity data, orchestrating several Anthropic models by task.
Context
Stakeholders needed answers about liaison progress and doctor-level activity, plus daily and monthly reports, but the data lived in MongoDB collections only engineers could query.
Challenge
Let non-technical users ask analytical questions in plain English and get correct, fast answers, without letting inference cost grow with usage.
My role
Designed and built the pipeline, then led the cost-optimisation work that halved inference spend.
Architecture
How the system fits together
Simplified for public sharing; client-specific details are omitted.
Approach
Key engineering decisions
- 01
Framed as text-to-MongoDB
Questions are translated into MongoDB aggregation pipelines against a documented schema, so answers come from the database rather than from the model’s memory.
- 02
Models routed by task
A small, fast Claude model classifies intent; a stronger model plans the query; another writes the answer or report. Each stage uses the cheapest model that meets its quality bar.
- 03
Guardrails
Generated queries are validated as read-only and checked against the schema before execution.
- 04
Prompt caching
Prompts were restructured so long, stable prefixes (schema, instructions, few-shot examples) are cached and only the question changes.
- 05
Tracing first
Langfuse traces showed which stages drove cost, which guided both the caching and the routing work.