Global health analytics

Last Mile Health KPI Agent

LangGraph multi-agent text-to-SQL system for querying global health KPI data in natural language.

Program teams needed a faster way to retrieve community health worker training and operations metrics without hand-writing Redshift SQL for each analytical question.

This project turns operational public-health questions into structured database queries. The core challenge is not only generating SQL, but generating it with enough schema context, guardrails, and retry behavior that a program team can trust repeated KPI lookups.

Read this page as a systems project: the visualizations show where the agent spends time, how a question flows through the pipeline, and how the backend returns a chart-ready contract instead of a raw notebook output.

Protect analytical access

Route natural-language questions through an agent that can inspect schema context without exposing private warehouse data on the portfolio.

Constrain SQL generation

Retrieve relevant schema, validate generated SQL, and use bounded correction when warehouse errors indicate a fixable query issue.

Reduce repeated latency

Cache schema and repeated query context so common KPI questions can move from expensive discovery to faster answer synthesis.

Return frontend-ready charts

When the user asks for a chart, emit a small chart_spec object that any portal charting library can render.

Architecture moved from hand sketch to workflow mockup.

The sketch captures the original control-flow thinking: a master agent routes the request, schema retrieval decides whether cached metadata is enough, SQL generation and validation are separated, and bounded correction loops prevent uncontrolled retries.

Handwritten planning sketch showing the master agent and SQL workflow.
Early planning sketch for the multi-agent SQL flow, including schema retrieval, validation, correction, and final error handling.
Visual workflow architecture mockup for the LMD 2.0 AI agent.
Workflow mockup extracted from the deliverables PDF, used to reason about agent routing and execution order.

Natural language to guarded SQL.

The workflow retrieves relevant schema context before generation, then validates and retries SQL in a bounded loop.

01 User question

Portal sends message and optional dashboard page context.

02 Master orchestrator

Classifies whether database access and visualization are needed.

03 Schema retrieval

Searches cached Redshift schema metadata for relevant tables and columns.

04 SQL generation

Generates read-only SQL using only verified schema context.

05 Validation + retry

Checks SQL safety and retries bounded corrections when appropriate.

06 Result formatter

Summarizes query rows and prepares preview-safe response metadata.

07 Visualization agent

Builds a minimal chart_spec when the query result is plottable.

Latency exposes where the agent does real work.

Bars show average warm-request latency in seconds for representative prompts.

What is a KPI? 4.93s Show KPI metrics 16.7s Show the training KPI trend for Malawi 12.3s

Average warm latency in seconds.

QueryAvgColdRows
What is a KPI?4.93s5.43s-
Show KPI metrics16.66s21.61s6
Show the training KPI trend for Malawi12.35s12.39s0

Visualization is returned as a small frontend contract.

The backend returns a compact chart specification so a web app can render structured answers consistently.

{
  "chart_type": "line",
  "x_field": "fiscal_year",
  "y_field": "value",
  "series_field": null,
  "title": "Training KPI trend over time"
}

Last Mile Health: LMD 2.0 Agent

Final TGHI presentation covering the LMD 2.0 agent concept, implementation narrative, and project handoff.

Last Mile Health: LMD 2.0 Agent, slide 1 of 16
Slide 1 of 16