EHRAgent — an LLM that writes, runs, and debugs Python to answer complex EHR table queries with four-shot prompts

January 13, 20247 min

Overview

Decision SnapshotNeeds Validation

EHRAgent uses generate→execute→parse-error→refine cycles with domain metadata and memory to improve multi-hop schema linking; effective empirically but depends on a capable base LLM and incurs extra runtime/API calls.

Citations2

Evidence Strength0.70

Confidence0.90

Risk Signals13

Trust Signals

Findings with numeric evidence: 3/3

Findings with evidence refs: 3/3

Results with explicit delta: 1/4

Reproducibility

Status: Code + data available

Open source: Partial

At A Glance

Cost impact: 50%

Production readiness: 60%

Novelty: 60%

Authors

Wenqi Shi, Ran Xu, Yuchen Zhuang, Yue Yu, Jieyu Zhang, Hang Wu, Yuanda Zhu, Joyce Ho, Carl Yang, May D. Wang

Links

Abstract / PDF / Code / Data

Why It Matters For Business

EHRAgent reduces dependence on data engineers by letting clinicians ask EHR questions in plain language and getting accurate answers; this can speed workflows but increases runtime/API calls and needs privacy safeguards.

Who Should Care

Summary TLDR

EHRAgent is a code-centric LLM agent that answers complex, multi-table EHR queries by generating Python plans, executing them, and iteratively debugging using parsed error traces and a small memory of past successful examples. It injects query-specific medical metadata, dynamically selects four relevant few-shot examples from long-term memory, and uses a 'rubber duck' error-explainer to guide fixes. On three real EHR benchmarks (MIMIC-III, eICU, TREQS) it raises success rate vs prior best baselines (same tools and prompts) by up to 29.6 percentage points on evaluated datasets, while requiring only four demonstrations.

Problem Statement

Clinicians need ad hoc, multi-step data from large relational EHRs but rely on data engineers. Off-the-shelf LLMs struggle with multi-table schema linking, domain specifics, and iterative debugging. EHRAgent reframes EHR QA as code-based planning plus execution feedback to let an LLM autonomously generate, run, and refine queries.

Main Contribution

An LLM agent that produces executable Python plans to solve few-shot multi-table EHR questions.

Query-specific medical information injection to improve schema linking and table/column discovery.

Key Findings

EHRAgent substantially improves EHR multi-table QA success rates versus prior LLM agent baselines.

NumbersUp to +29.6 percentage points success rate (TREQS) vs strongest baseline

Practical UseIf you need more correct answers from natural-language EHR queries, a code-execution agent with debugging and domain injection can substantially boost accuracy on evaluated EHR benchmarks.

Evidence RefTable 1; Section 4.2

Interactive coding (code generation + execution feedback) is the largest single contributor to performance.

NumbersAblation: overall SR drops from 58.97% to 24.55% without interactive coding on MIMIC-III

Practical UsePrioritize a code-execution loop (generate → run → parse errors → refine) when building agents for multi-step tabular tasks.

Evidence RefTable 2; Section 4.3

Results

MetricValueBaselineDeltaSplit / DatasetEvidenceEvidence Ref
Success rate (MIMIC-III, overall)58.97%MIMIC-III (all complexities)EHRAgent overall success rate on MIMIC-IIITable 1
Success rate (eICU, overall)53.10%eICU (all complexities)EHRAgent overall success rate on eICUTable 1

What To Try In 7 Days

Run a small prototype: wire a Python-based toolset (DB loader, SQLInterpreter) to an LLM and test 10 real queries.

Add EHR metadata injection (table/column descriptions) and 4 curated examples to your prompt; compare success vs plain prompts.

Implement a simple error parser that returns error type, location, and candidate causes to the LLM and measure iteration gains.

Agent Features

Memory
long-term memory of successful code examplessimilarity-based retrieval for few-shot selection
Planning
code-based planning (Python programs as plans)multi-step action decomposition
Tool Use
Python tool functionsSQLInterpreterWolframAlpha calculator API
Frameworks
AutoGenOpenAI API (GPT-4 / GPT-3.5)
Is Agentic

Yes

Architectures
LLM planner + code executor loopPython-centered tool interface
Collaboration
single-agent interactive loop (planner-executor)

Optimization Features

Token Efficiency
selective few-shot example retrieval to stay within context window

Reproducibility

Risks & Boundaries

Limitations

Requires multiple LLM calls per query which increases cost and latency.

Relies on closed-box models (GPT-4) in experiments; behavior may differ for open LLMs.

When Not To Use

For unsupervised, high-stakes clinical decisions without human review.

If strict cost or latency budgets forbid multiple LLM calls per query.

Failure Modes

Date/time miscalculations (uses system dates incorrectly)

Context-length truncation leading to missing info

Core Entities

Models

GPT-4 (gpt-4-0613)GPT-3.5-turbo (0613)

Metrics

success ratecompletion rate

Datasets

MIMIC-IIIeICUTREQSEHRSQL (benchmark source)

Benchmarks

EHRSQLTREQS