◆ Case study · OphyCare · Healthcare
We replaced a sprawl of clinical endpoints with one conversational API
A LangGraph ReAct orchestrator over GPT-4o that routes clinicians’ natural-language requests to specialized sub-agents for clinical notes, insurance eligibility, records RAG and routing, collapsing scattered endpoints into a single API with persistent per-user memory.
4
sub-agents unified
80%
code cut in refactor
0
citation hallucinations
The problem
The product had grown a separate endpoint for every clinical capability: one for note generation, one for insurance eligibility, one for searching patient records, one for routing. Each had its own request shape, its own conventions and its own copy of the prompt logic. The client-side code that had to know which endpoint to call for which phrasing had become the most fragile part of the system.
Clinicians do not think in endpoints. A request like “summarise this visit and check whether her plan covers the follow-up” spans two capabilities, and there was no clean way to express it. Adding a fifth capability meant touching the routing logic in every caller.
Underneath that, the records search had a credibility problem. Any answer about a patient that cannot be traced to the document it came from is unusable in a clinical setting, and a plausible-sounding unsourced answer is an active liability.
What we built
-
One entry point, a router behind it
We built a single conversational endpoint backed by a LangGraph ReAct orchestrator over GPT-4o. The orchestrator reads the clinician’s request and decides which specialised sub-agent handles it — clinical notes, insurance eligibility, records RAG, or routing. Callers stopped needing to know the topology at all.
-
Keep the sub-agents narrow
Each of the four sub-agents owns exactly one capability, with its own tools and its own tightly scoped prompt. This is the decision that made the system testable: a narrow agent has a small enough behaviour surface to write real assertions against, whereas one prompt attempting all four jobs can only be evaluated by vibes.
-
Ground records retrieval and force citation
The records agent runs retrieval over pgvector and is constructed so that answers are assembled from retrieved passages with their sources attached. If retrieval returns nothing that supports an answer, the agent says so rather than filling the gap from the model’s prior. That constraint is why the system carries zero citation hallucinations — it is an architectural property, not a prompt instruction we hoped would hold.
-
Give it memory that survives restarts
Per-user conversation state persists in PostgreSQL rather than process memory, so context survives deploys and the service scales horizontally without pinning a clinician to an instance. Follow-up requests that depend on the previous turn keep working.
-
Delete the duplication
Consolidating four parallel implementations behind one orchestrator cut roughly 80% of the code in the refactor. Most of what disappeared was prompt logic, request handling and routing that had been copied and lightly edited four times over.
The outcome
- Four specialised sub-agents unified behind a single conversational API.
- 80% of the code removed in the refactor, almost all of it duplication.
- Zero citation hallucinations, because unsupported answers are structurally prevented rather than discouraged.
- Adding a fifth capability became a change in one place instead of a change in every caller.
Why this stack
- LangGraph
- Explicit graph state and control flow. Agent behaviour you can inspect and test, rather than an opaque chain.
- GPT-4o
- Reliable tool-calling and low enough latency for interactive clinical use.
- pgvector
- Vector search alongside the relational data, in one database. No second system to keep consistent.
- PostgreSQL
- Durable per-user agent memory that survives restarts and scales horizontally.
- AWS
- Deployment target, with the whole service containerised behind CI/CD.
Frequently asked
Why a multi-agent system instead of one large prompt?
Because narrow agents are testable and one large prompt is not. When a single prompt handles four jobs, every change risks regressing the other three and you have no way to detect it. Four scoped agents behind a router let you assert real behaviour per capability, and a routing mistake is visible and cheap to fix rather than diffuse.
How do you guarantee an AI system does not hallucinate citations?
You do not guarantee it with instructions — you make it structurally impossible. Answers are assembled from retrieved passages with sources attached, and when retrieval surfaces nothing supporting, the agent returns that rather than generating prose. Asking a model politely not to invent sources is not a control.
Does this architecture work outside healthcare?
Yes. The shape applies wherever one interface has to cover several distinct capabilities over sensitive data — the healthcare constraints simply make the grounding requirements explicit earlier than most domains do.
What happens when the router picks the wrong agent?
It surfaces as a wrong-but-coherent answer from the wrong capability, which is exactly why routing decisions are logged as part of graph state. A misroute is diagnosable from the trace and fixable in the router, rather than being buried inside one monolithic prompt.
More on clinical documentation & ai scribing
- GuideAI clinical documentation: how to automate notes without adding risk
- ServiceAI medical scribe development
- Case studyAI scribe auto-filling 10+ clinical forms
- IndustryAI development for healthcare
- ArticleHIPAA-compliant AI architecture: PHI boundaries and audit trails
- ArticleDictating SOAP notes from an Apple Watch: what actually works
- ArticleAI for medical notes: how it works and what it saves
- ArticleWhat is an AI medical scribe? A clinician's guide
- ArticleBuilding an AI medical scribe: audio to structured notes
- ArticleAI medical scribe notes: a real audio-to-SOAP example
- ArticleBuild vs buy an AI medical scribe: an honest comparison
- ArticleWhat AI medical scribes actually cost: DAX, Abridge and building your own
- ArticleAmbient clinical intelligence: what ambient AI scribes actually do
- ArticleMedical dictation software: Dragon, ambient scribes and when to build
- ArticleDAX Copilot explained: what it is and the rename to Dragon Copilot
- ArticleEpic AI scribe integration: what it takes to write into the chart
- ArticleVirtual scribe services vs AI scribes: cost and accuracy
Have a project like this?
Production-grade, owned end to end. Usually a reply within a day.