🏥 Hospital-OS & FHIRFlow: Multi-Agent Healthcare Operating System & Claims Engine
An end-to-end, multi-role agentic healthcare platform powered by LangGraph, Groq GPT-OSS-120B, Deepgram, Pinecone, Redis, and HL7 FHIR.
This repository contains the complete architecture and implementation of Hospital-OS — a unified platform bringing together Doctors, Hospital Administrators, Lab Technicians, and Patients. The platform combines a 5-agent claims RAG validation pipeline, real-time Deepgram AI voice patient outreach, 5-domain surgical readiness scorecards, dynamic bottleneck intelligence tracing, and an asynchronous pub/sub Event Bus.
📋 Table of Contents
- Architectural Overview & Core Idea
- System Architecture Flowchart
- Deep-Dive Agent Directory by Scope
- Decoupled Hospital Event Bus (Pub/Sub)
- NIST AI RMF Security & FHIR Audit Provenance
- Complete Technology Stack
- Database Models & Schemas
- Complete REST API Reference
- Setup & Installation Guide
Architectural Overview & Core Idea
Modern healthcare systems suffer from severe friction between clinical delivery and administrative pre-authorization:
- Medical Claim Denials: 15–20% of healthcare claims are rejected by payers due to undisclosed policy updates, dosage limits (e.g., Vitamin B12 injections $> 1000\,\text{mcg}$ requiring prior auth), or missing coding modifiers (CPT/HCPCS/SNOMED). Traditional manual appeals take up to 45 days.
- Surgical Pre-Op Delays: Operating theater (OT) schedules stall when pre-operative clinical prerequisites (ECG/Troponin lab results, chest X-rays, blood bank reservations, insurance pre-authorizations) are managed in isolated departmental silos.
Hospital-OS solves both challenges by deploying 17 specialized AI agents across 5 distinct operational scopes, synchronized via a real-time Event Bus and secured by HL7 FHIR compliance standards.
System Architecture Flowchart
flowchart TD
subgraph Ingestion ["1. Document Ingestion & RAG Indexing"]
PDF["Payer Policy PDFs"] -->|store_index.py| Pinecone["Pinecone Vector Store (fhirdb)"]
PDF -->|Agent 2| Agent2["Agent 2: Policy Change Detector"]
Agent2 -->|Diff Extraction| PolicyJSON["policy_changes.json"]
end
subgraph FHIRSync ["2. FHIR R4 Synchronization"]
PolicyJSON --> Agent3["Agent 3: FHIR Batch Updater"]
Agent3 -->|Batch Update| FHIRDB[("FHIR Procedure SQLite DB")]
end
subgraph ClaimsValidation ["3. Claims Validation Engine (LangGraph)"]
ClaimJSON["Claim JSON Upload"] --> Agent4["Agent 4: Claims Validator"]
FHIRDB -->|Patient History| Agent4
Pinecone -->|Vector Policy Retrieval| Agent4
Agent4 -->|Groq GPT-OSS-120B| Decision{"Validation Decision"}
end
subgraph Routing ["4. Approval / Rejection Routing"]
Decision -->|"APPROVED"| Agent5A["Agent 5A: Approval Router"]
Agent5A -->|Generate EDI 837| EDIFile["EDI 837 (.edi) File"]
Decision -->|"REJECTED"| TicketEmail["Raise Email Ticket (OPEN)"]
TicketEmail --> Agent5B["Agent 5B: Voice Agent"]
end
subgraph VoiceOutreach ["5. Deepgram Patient Outreach"]
Agent5B -->|Deepgram TTS / Nova-2 STT| VoiceCall(("Voice Call Session"))
VoiceCall -->|"YES (Agreed)"| AltReval["Re-validate with Alternative Code"]
AltReval -->|"APPROVED"| Agent5A
AltReval -->|"APPROVED"| CloseResolved["Close Ticket: RESOLVED"]
VoiceCall -->|"NO (Refused)"| EscalatedTicket["Close Ticket: ESCALATED"]
end
subgraph HospitalOS ["6. Multi-Role Hospital-OS Environment"]
EventBus["Event Bus (events.py)"] <-->|Pub/Sub Events| DoctorHub["Doctor Cockpit"]
EventBus <--> AdminHub["Admin Command Center"]
EventBus <--> LabHub["Lab Tech Workspace"]
RBAC["NIST RBAC & FHIR Audit (registry.py)"] -->|Audit Provenance| MainDB[("claims.db")]
end
Deep-Dive Agent Directory by Scope
Scope 1: Insurance Claims & Policy Validation Pipeline (LangGraph)
Orchestrated via LangGraph in langgraph_workflow.py.
1. Agent 2: Policy Change Detector
- File:
agents/agent2/agent2.py - Idea & Purpose: Automates the detection of policy changes between newly downloaded payer PDFs and historical policy vectors in Pinecone.
- Workflow & Execution Flow:
- Loads new policy PDFs downloaded from payer portals.
- Generates a summary of the new policy using Groq LLM.
- Queries Pinecone (
fhirdbindex) for the corresponding old policy vector using metadata filters (e.g., codeJ3420). - Prompts Groq GPT-OSS-120B with both old and new policy text to extract structured diffs (prior auth changes, dose limits, CPT code restrictions, effective dates).
- Outputs timestamped
policy_changes_<timestamp>.jsonand upserts new policy embeddings into Pinecone.
- Inputs: Policy PDF files.
- Outputs:
policy_changes.jsoncontainingcode,change,impact,affected_codes,details.
2. Agent 3: FHIR Batch Updater
- File:
agents/agent3/agent3.py - Idea & Purpose: Synchronizes policy changes directly with the patient procedure database adhering to the HL7 FHIR R4 Procedure resource schema.
- Workflow & Execution Flow:
- Consumes
policy_changes.jsongenerated by Agent 2. - Connects to
fhir_procedure_db.sqlite. - Processes updates in batches of 100 procedure records.
- Flags procedures violating new policy requirements, updates
statusReasonfields, and inserts entries intoprocedure_policy_flags. - Appends immutable audit records to
audit_log.
- Consumes
- Inputs:
policy_changes.json. - Outputs: Updated SQLite procedure records & audit logs (
total_affected,procedures_updated,procedures_flagged).
3. Agent 4: Claims RAG Validator
- File:
agents/agent4/agent4.py - Idea & Purpose: The core RAG reasoning engine. Validates incoming claims against historical patient FHIR records and live vector policy documents.
- Workflow & Execution Flow:
- Receives a claim JSON object (
patient_id,code,dose,units,diagnosis,date_of_service). - Fetches 12-month procedure history and active policy flags for the patient from the FHIR database.
- Performs semantic similarity vector search against Pinecone (
fhirdb) using HuggingFaceall-MiniLM-L6-v2embeddings to retrieve exact governing policy rules. - Prompts Groq GPT-OSS-120B (
openai/gpt-oss-120b) with patient records, claim details, policy rules, and historical frequency. - Parses JSON output returning
decision(APPROVED/REJECTED),confidence,reasoning,policy_citations, andsuggested_alternatives. - Immediately writes the decision to the
claimstable inclaims.dbto refresh UI dashboard counters.
- Receives a claim JSON object (
- Inputs: Claim JSON payload.
- Outputs: JSON decision object with reasoning, policy citations, and alternative CPT codes (e.g.,
G0008).
4. Agent 5A: Approval Router
- File:
agents/agent5/agent5a.py - Idea & Purpose: Converts approved claims into industry-standard electronic claim formats ready for clearinghouse submission.
- Workflow & Execution Flow:
- Consumes
APPROVEDdecision objects from Agent 4 or post-voice correction. - Fetches complete patient and billing provider demographics.
- Generates a standard X12 EDI 837 Professional Claim file formatted with
ISA,GS,ST,BHT,NM1,CLM, andSV1segments. - Saves
.edisubmission files to disk and logs submission records inclaim_submissions.
- Consumes
- Inputs: Approved decision dict.
- Outputs:
.edielectronic submission file & DB record.
5. Agent 5B: Voice Outreach Agent
- File:
agents/agent5/agent5b.py - Idea & Purpose: Conducts real-time interactive AI voice calls to patients for rejected claims, offering automated claim corrections.
- Workflow & Execution Flow:
- Activated when Agent 4 returns
REJECTED. Automatically callsraise_ticket()inticket_notifier.pyto send anOPENemail ticket. - Spawns an isolated daemon thread with a dedicated
asyncioevent loop to avoid deadlocking LangGraph. - Synthesizes rejection intro speech via Deepgram TTS (Aura Asteria model over WebSocket) and plays audio via PyAudio.
- Opens microphone input stream via PyAudio and sends 16kHz linear16 PCM audio to Deepgram Nova-2 STT WebSocket.
- Transcribes user speech and prompts Groq LLM to classify intent (
YESto agree to fix,NOto refuse). - If
YES: Updates procedure code to suggested alternative (G0008), triggers secondary validation with Agent 4, routes approved claim to Agent 5A, and sends aRESOLVEDemail ticket. - If
NO: Marks claim as escalated and sends anESCALATEDemail ticket.
- Activated when Agent 4 returns
- Inputs: Rejected claim dict + patient contact details.
- Outputs: Patient intent (
YES/NO), corrected claim re-validation, and closed email ticket.
Scope 2: Doctor Cockpit Agents
Located in agents/doctor_agents.py.
┌────────────────────────────────────────────────────────────────────────┐
│ DOCTOR COCKPIT AGENTS │
├────────────────────────────────────────────────────────────────────────┤
│ 1. Surgery Readiness Agent │ 5-Domain Checklist Evaluator (0-100%) │
│ 2. Clinical Clearance Agent │ ASA Physical Score & Vitals Evaluator │
│ 3. Scan Dispatcher Agent │ HL7 FHIR ServiceRequest Generator │
│ 4. Doctor AI Assistant │ Natural Language Chat with Redis Cache │
└────────────────────────────────────────────────────────────────────────┘
6. Surgery Readiness Coordinator Agent
- Core Idea: Calculates a live Surgery Readiness Scorecard (0–100%) across 5 clinical domains to clear patients for surgery without operational delays.
- Execution Loop (
Sense→Plan→Act):- Sense: Queries SQLite DB for completed lab scans (ECG/Troponin), pre-op radiology (Chest X-Ray), blood bank 2-unit RBC reservation (
#BB-902), insurance pre-authorization approval, and operating theater allocation (OT #1). - Plan: Evaluates how many checkpoints are verified. Calls Groq GPT-OSS-120B to generate a 2-sentence clinical readiness summary.
- Act: Pushes live readiness score (e.g.,
100% READY FOR SURGERY) to Doctor Cockpit and emitsSURGERY_READINESS_UPDATEDevent to the Event Bus.
- Sense: Queries SQLite DB for completed lab scans (ECG/Troponin), pre-op radiology (Chest X-Ray), blood bank 2-unit RBC reservation (
7. Clinical Pre-Op Clearer Agent
- Core Idea: Evaluates real-time EHR vitals and anesthesia risk to grant clinical pre-op clearance.
- Execution Loop (
Sense→Plan→Act):- Sense: Ingests patient vitals (Heart Rate 72 bpm, BP 120/80, SpO2 98%, Temp 37.0°C, Resp Rate 16), ASA Physical Status Score (
ASA_II), and Mallampati Airway Class (Class II). - Plan: Verifies vitals fall within cardiac risk index thresholds ($<0.9\%$ complication rate). Prompts Groq LLM for an anesthesia clearance opinion.
- Act: Issues formal clearance certificate and logs an immutable HL7 FHIR
AuditEventto SQLite viaregistry.py.
- Sense: Ingests patient vitals (Heart Rate 72 bpm, BP 120/80, SpO2 98%, Temp 37.0°C, Resp Rate 16), ASA Physical Status Score (
8. Diagnostic Scan Dispatcher Agent
- Core Idea: Formats doctor scan orders into standardized HL7 FHIR
ServiceRequestitems. - Execution Loop (
Sense→Plan→Act):- Sense: Captures scan order requests (CT Scan, MRI, Ultrasound) submitted by doctors.
- Plan: Formats order into HL7 FHIR
ServiceRequestJSON schema with priority tags (URGENT/ROUTINE). - Act: Inserts order into
scanstable and emitsSCAN_ORDEREDevent to notify Lab Technicians.
9. Doctor AI Assistant Chatbot
- Core Idea: Provides doctors with instant natural language query responses for schedules, patient history, and clearances using Redis Memory Cache.
- Execution Loop:
- Receives prompt → Computes MD5 hash cache key (
doctor_chat:<hash>). - Queries Redis (or in-memory fallback). On cache hit, returns instant response.
- On cache miss, sends prompt to Groq LLM (
openai/gpt-oss-120b), returns response, and sets Redis key with 300s TTL.
- Receives prompt → Computes MD5 hash cache key (
Scope 3: Admin Command Hub Agents
Located in agents/admin_agents.py.
10. Operation Claims Pre-Approval Agent
- Core Idea: Automatically filters and pre-approves insurance claims specifically for patients scheduled or requested for surgical operations.
- Execution Loop (
Sense→Plan→Act):- Sense: Polls
claimstable and cross-references active surgical candidates fromsurgeriestable. - Plan: Identifies surgical operation claims (e.g., CABG, Knee Arthroplasty) and prompts Groq LLM to generate an executive pre-op clearance summary.
- Act: Updates pre-op clearances in Admin Command Hub and emits
OPERATION_CLAIMS_PROCESSEDevent.
- Sense: Polls
11. OT & Bed Resource Optimizer Agent
- Core Idea: Optimizes hospital bed capacity and Operating Theater scheduling.
- Execution Loop (
Sense→Plan→Act):- Sense: Reads
hospital_capacitytable for total/occupied/available beds (e.g., 108/150 occupied) and total/in-use/available OTs (e.g., 3/8 in use). - Plan: Calculates Bed Occupancy Rate (%) and OT Utilization Rate (%). Asks Groq LLM for 2 actionable resource optimization recommendations.
- Act: Pushes metrics to Admin Command Center and emits
OT_RESOURCE_OPTIMIZEDevent.
- Sense: Reads
12. Bottleneck Intelligence Tracer
- File: Defined in
app.py(/api/bottleneck/trace). - Core Idea: Diagnoses operational delay bottlenecks across patient encounter timelines.
- Execution Loop:
- Traces patient stages: Consultation (25 mins), Pre-Op Blood Work (30 mins), CT Imaging Scan (45 mins queue delay), Insurance Auth Clearance (92 mins delay).
- Pinpoints primary cause: 3-patient queue backlog at Scanner #2 and missing pre-auth update from Agent 4.
- Recommends resolution: Reroute scan to idle Scanner #1 and trigger Voice Agent 5B patient confirmation call.
13. Admin AI Assistant Chatbot
- Core Idea: Executive assistant for hospital administrators backed by Groq LLM and Redis Memory Caching.
- Execution Loop: Hashes query → Checks Redis Cache → On miss, invokes Groq LLM with admin context → Caches result → Returns strategy response.
Scope 4: Lab Tech Workspace Agents & Workflows
Integrated in app.py, database.py, and events.py.
14. Diagnostic Work Order Processor
- Core Idea: Queue management engine for lab technicians.
- Execution Loop: Listens for
SCAN_ORDEREDevents → Populates pending work orders (SCAN001,SCAN002) in Lab Tech Workspace → Displays priority tags (URGENT/ROUTINE).
15. Lab Result Uploader & Event Publisher
- Core Idea: Processes lab test results and automatically triggers surgical readiness updates.
- Execution Loop:
- Lab Tech submits test results (ECG, Troponin < 0.01 ng/mL, CT Angiogram findings) via
/api/lab/submit. - Inserts report into
fhir_diagnostic_reportstable. - Emits
LAB_RESULT_READYevent over Event Bus. - Automatically updates Surgery Readiness Scorecard in Doctor Cockpit.
- Lab Tech submits test results (ECG, Troponin < 0.01 ng/mL, CT Angiogram findings) via
Scope 5: Patient Care Hub & Communication Agents
Located in ticket_notifier.py and agent5b.py.
16. Care Plan & Claims Status Monitor
- Core Idea: Patient-facing dashboard displaying active health records, procedure history, and claim authorization status.
17. Email Ticket Lifecycle Manager
- File:
ticket_notifier.py - Core Idea: Professional SMTP email ticketing system managing claim rejections and appeals.
- Execution Loop:
- Ticket Raised (
OPEN): Triggered when Agent 4 rejects claim. Sends HTML email containing claim ID, rejection reason, priority badge (CRITICAL/HIGH/MEDIUM/LOW), and alternative codes. - Ticket Closed (
RESOLVED): Sent after patient agrees via Voice Agent 5B and corrected claim is approved. - Ticket Escalated (
ESCALATED): Sent if patient refuses voice correction. - Ticket Failed (
FAILED): Sent if patient agrees but secondary validation still fails.
- Ticket Raised (
Decoupled Hospital Event Bus (Pub/Sub)
Implemented in events.py. Decouples micro-agent state transitions from UI rendering:
# Publish Event Example
events.publish_event(
event_type="LAB_RESULT_READY",
payload={"claim_id": "CLM001", "test_name": "Pre-Op ECG", "result": "Normal Sinus Rhythm"},
source_agent="LabAgent_Agent3"
)
- Persistence: All events are inserted into
hospital_eventstable in SQLite (id,event_type,payload,source_agent,timestamp). - Subscribers: In-memory callbacks execute synchronously to notify registered UI handlers.
NIST AI RMF Security & FHIR Audit Provenance
Implemented in registry.py:
1. Role-Based Access Control (RBAC) Matrix
ROLE_PERMISSIONS = {
"DOCTOR": ["view_patient_records", "view_surgery_readiness", "approve_surgery_clearance", "trigger_claims_agent"],
"ADMIN": ["view_command_center", "view_bottleneck_intelligence", "manage_claims_engine", "view_escalation_tickets"],
"LAB_TECH": ["view_pending_lab_orders", "upload_lab_results", "emit_lab_ready_event"],
"PATIENT": ["view_my_claims", "interact_voice_agent_5b", "view_care_plan"]
}
2. Immutable FHIR AuditEvent Logging
All sensitive agent executions call log_audit_event(), inserting records into fhir_audit_events table:
{
"agent_id": "DoctorAgent_Clearance",
"role": "DOCTOR",
"action": "GRANT_CLINICAL_CLEARANCE",
"target_resource": "pat001",
"status": "SUCCESS",
"reasoning": "Vitals stable. ASA Class ASA_II approved."
}
Complete Technology Stack
| Layer | Component | Description |
|---|---|---|
| Agent Orchestration | LangGraph (StateGraph, MemorySaver) |
Multi-agent execution workflow in langgraph_workflow.py |
| LLM Inference | Groq API (openai/gpt-oss-120b) |
Ultra-fast LLM reasoning across Agents 2, 4, 5B, Doctor, and Admin agents |
| Vector DB | Pinecone (fhirdb index) |
Medical policy vector storage |
| Embeddings | HuggingFace all-MiniLM-L6-v2 |
Policy text embedding generation |
| Voice Speech-to-Text | Deepgram Nova-2 (WebSocket) | Real-time 16kHz linear16 microphone audio transcription |
| Voice Text-to-Speech | Deepgram Aura Asteria (WebSocket) | Natural streaming audio voice synthesis |
| Audio I/O | PyAudio | Microphone reading & speaker playback |
| Caching Layer | Redis (Local + In-Memory Fallback) | Instant AI assistant prompt responses |
| Databases | SQLite (claims.db & fhir_procedure_db.sqlite) |
Persistence for claims, surgeries, doctors, capacity, and FHIR data |
| Standards Compliance | HL7 FHIR R4 & X12 EDI 837P | Medical procedure records and EDI claim format generation |
| Backend API | FastAPI + Uvicorn | Async REST server in app.py |
| Frontend UI | Next.js (React 19 + TypeScript + Tailwind CSS) | Enterprise role-governed portals in frontend/ |
| Observability | LangSmith Client | Tracing LLM execution, accuracy scores, and feedback |
Database Models & Schemas
claims.db Schema:
claims:id(PK),patient_id,procedure_code,description,dose,units,date_of_service,diagnosis,status,decision,reasoning,confidence,created_at,processed_atdoctors:id(PK),name,specialty,status,current_location,on_duty,shift_timing,phonesurgeries:id(PK),doctor_id,doctor_name,patient_id,patient_name,procedure_name,operating_theater,scheduled_time,duration_mins,readiness_score,statusscans:id(PK),doctor_id,doctor_name,patient_id,patient_name,scan_name,body_part,priority,status,result_summary,uploaded_byhospital_capacity:id(PK=1),total_beds,occupied_beds,available_beds,total_ots,in_use_ots,maintenance_ots,available_otsfhir_diagnostic_reports:id(PK),claim_id,test_name,result_value,tech_id,timestampfhir_audit_events:id(PK),agent_id,role,action,target_resource,status,reasoning,timestamphospital_events:id(PK),event_type,payload,source_agent,timestamp
Complete REST API Reference
| Scope | Method | Endpoint | Description |
|---|---|---|---|
| Claims | POST |
/api/upload |
Upload claim JSON & start LangGraph workflow |
GET |
/api/claims |
Fetch recent processed claims | |
GET |
/api/stats |
Fetch dashboard metrics (total, approved, rejected, tickets) | |
| Workflows | GET |
/api/workflow/{id} |
Get full state of a workflow |
GET |
/api/workflow/{id}/messages |
Poll live agent execution logs | |
POST |
/api/workflow/{id}/voice-response |
Submit patient UI YES/NO response | |
| Doctor | GET |
/api/doctors |
List doctors roster and duty status |
GET |
/api/surgeries |
List upcoming surgeries | |
POST |
/api/scans/order |
Doctor uploads diagnostic scan order | |
POST |
/api/doctor/agent/readiness |
Execute Surgery Readiness Coordinator Agent | |
POST |
/api/doctor/agent/clearance |
Execute Clinical Pre-Op Clearance Agent | |
POST |
/api/doctor/chat |
Execute Doctor AI Assistant Chatbot | |
| Admin | GET |
/api/admin/hospital-capacity |
Get Bed and OT availability metrics |
GET |
/api/admin/ot-requests |
Get pending OT booking requests | |
POST |
/api/admin/agent/operation-claims |
Execute Operation Claims Pre-Approval Agent | |
POST |
/api/admin/agent/resource-optimizer |
Execute OT & Bed Resource Optimizer Agent | |
POST |
/api/admin/insurance/approve |
Approve surgical pre-auth & set readiness to 100% | |
POST |
/api/admin/chat |
Execute Admin AI Assistant Chatbot | |
| Lab Tech | POST |
/api/lab/submit |
Upload lab result → update DB → emit LAB_RESULT_READY |
POST |
/api/scans/complete |
Complete diagnostic scan work order | |
| Events/Audit | GET |
/api/events |
Fetch live hospital event stream |
POST |
/api/events/publish |
Publish custom event to Event Bus | |
GET |
/api/audit-trail |
Fetch FHIR AuditEvents compliance log | |
GET |
/api/bottleneck/trace |
Trace encounter timeline to pinpoint delay bottlenecks |
Setup & Installation Guide
1. Prerequisites
- Python 3.10+
- Node.js 18+ &
npm uvpackage manager (pip install uv)portaudiosystem library (macOS:brew install portaudio, Ubuntu:sudo apt-get install portaudio19-dev)
2. Environment Variables (.env)
GROQ_API_KEY=gsk_...
PINECONE_API_KEY=pcsk_...
DEEPGRAM_STT_KEY=...
DEEPGRAM_TTS_KEY=...
# Email Ticketing
[email protected]
[email protected]
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
[email protected]
SMTP_PASSWORD=your-app-password
3. Launching Backend & Frontend
# Terminal 1: Ingest Policy Vectors & Run Backend
uv run python store_index.py
python app.py
# Terminal 2: Start Next.js Frontend
cd frontend
npm install
npm run dev
(Open http://localhost:3000 to view the Hospital-OS role gateway)
Log in or sign up for Devpost to join the conversation.