← All case studies
LLM & AgentsHealthcareQPharma · 2025

Text-to-MongoDB Analyst

Natural-language analytics and report generation over field-activity data, orchestrating several Anthropic models by task.

−50%inference cost via caching & routing

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

Text-to-MongoDB Analyst architecture diagramStakeholderplain-English questionIntent routerfast Claude modelCached prefixschema · few-shotQuery plannerClaude → aggregationValidatorread-only · schemaMongoDBfield activityLangfusecost · latencyAnswer writerClaude · reportsAnswer / reportdaily · monthlyqueryrows
User / outputModel / AIData storeServiceInfra / opsTelemetry / async

Simplified for public sharing; client-specific details are omitted.

Approach

Key engineering decisions

  1. 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.

  2. 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.

  3. 03

    Guardrails

    Generated queries are validated as read-only and checked against the schema before execution.

  4. 04

    Prompt caching

    Prompts were restructured so long, stable prefixes (schema, instructions, few-shot examples) are cached and only the question changes.

  5. 05

    Tracing first

    Langfuse traces showed which stages drove cost, which guided both the caching and the routing work.