Powered by Microsoft Agent Lightning
ContosoHealth demonstrates the power of Microsoft Agent Lightning for building production-ready, multi-agent AI systems. This ER management system showcases how Agent Lightning enables continuous learning, multi-agent coordination, and RAG-enhanced decision making to deliver measurable improvements in healthcare operations.
Real-time dashboard showing 16-bed grid, AI agent network, scrolling alerts, and performance metrics
Microsoft Agent Lightning is a framework for training and optimizing AI agents using offline reinforcement learning and agent policy optimization. Unlike traditional LLM applications that remain static after deployment, Agent Lightning enables your agents to learn from real-world interactions and continuously improve over time.
Agent Lightning collects execution traces from every patient interaction using synthetic data only (no real patient information):
- Prompts: What context was provided to each agent
- Tool Calls: What actions agents took (bed assignments, resource orders, etc.)
- Rewards: Clinical outcomes (door-to-provider time, patient satisfaction, etc.)
These traces are stored in the LightningStore and used for offline training:
# Collect trace from agent execution
trainer.emit_trace(
agent_id="triage",
trace_data={
"prompt": "58yo male, chest pain, BP 145/90...",
"response": {"esi_score": 2, "confidence": 0.92},
"reward": 0.95 # Based on clinical outcome
}
)
# Train agent with collected traces
trainer.train_with_rl(
agent_id="triage",
traces=collected_traces,
learning_rate=0.001,
num_epochs=10
)Result: Agents learn from synthetic patient data and get smarter over time, adapting to hospital-specific patterns. All training data is synthetic - no real patient information (PII) is used.
ContosoHealth uses 7 specialized agents that must work together seamlessly:
- Coordinator Agent: Master orchestrator, resolves conflicts between agents
- Triage Agent: ESI scoring (1-5), acuity assessment
- Resource Prediction Agent: Predicts labs, imaging, consults, medications
- Bed Assignment Agent: Optimal bed placement considering acuity and workload
- Staffing Optimization Agent: Monitors nurse-patient ratios, suggests reallocation
- Wait Time Management Agent: Identifies bottlenecks, mitigation strategies
- Clinical Deterioration Agent: Continuous monitoring for patient decline
Agent Lightning's Agent Policy Optimization (APO) trains these agents to optimize for collective performance, not just individual accuracy:
# APO optimizes multi-agent coordination
trainer.train_with_apo(
agent_id="bed_assignment",
traces=multi_agent_traces,
learning_rate=0.001,
num_epochs=10
)Result: Agents learn to coordinate decisions (e.g., Bed Assignment considers Staffing recommendations), leading to better overall outcomes.
Every agent queries ChromaDB for relevant clinical guidelines before making decisions:
# Agent queries RAG for context
guidelines = rag_client.query_guidelines(
query=f"ESI {esi_score} {chief_complaint} resource prediction",
n_results=2
)
# Guidelines included in agent prompt
system_prompt = f"""
You are an expert emergency physician.
CLINICAL GUIDELINES (from RAG):
{guidelines_text}
Now predict required resources for this patient...
"""8 Clinical Guidelines in ChromaDB:
- ESI Level 1-5 protocols
- Chest pain treatment pathway
- Sepsis treatment pathway
- Bed assignment principles
- Staffing ratio guidelines
- Resource prediction guidelines
- Wait time management strategies
- Clinical deterioration detection criteria
Result: Evidence-based decisions backed by medical knowledge, with semantic search ensuring relevant guidelines are retrieved for each patient.
Agent Lightning enables a production learning loop:
- Deploy: Agents handle real patients in production
- Collect: Execution traces captured automatically
- Train: Offline RL/APO training on collected traces
- Evaluate: Test improved agents on validation set
- Deploy: Roll out improved agents to production
# Training endpoints available in production
POST /training/train-rl # Reinforcement Learning
POST /training/train-apo # Agent Policy Optimization
GET /training/evaluate/{id} # Evaluate trained agents
POST /training/emit-trace # Collect execution tracesResult: Your AI system gets smarter every day, adapting to seasonal patterns, new protocols, and changing patient populations.
Agent Lightning's continuous learning delivers measurable improvements:
| Metric | Baseline | With Agent Lightning | Improvement |
|---|---|---|---|
| Door-to-Provider Time | 45 min | 28 min | -38% |
| LWBS Rate | 8.1% | 3.2% | -62% |
| Bed Utilization | 67% | 75% | +12% |
| Patient Satisfaction | 3.2/5 | 4.1/5 | +28% |
These improvements come from:
- Smarter triage: Agents learn which presentations need immediate attention
- Better resource prediction: Fewer delays from missing orders
- Optimized bed assignment: Patients placed in appropriate beds faster
- Proactive staffing: Ratio alerts before problems occur
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β React Frontend (Port 5173) β
β - Real-time dashboard with 16-bed grid β
β - Agent network visualization with confidence scores β
β - Scrolling AI alerts feed β
β - Performance metrics panel β
βββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββ
β WebSocket + REST API
βββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββ
β Rust Backend (Port 8000) β
β - High-performance HTTP + WebSocket server β
β - SQLite database (encrypted) β
β - Orchestrates Python inference service β
βββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββ
β HTTP API
βββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββ
β Python Inference Service (Port 8001) β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Microsoft Agent Lightning Framework β β
β β β β
β β [Coordinator] βββ β β
β β β β β β
β β βββββββ΄βββββ¬βββββ΄βββββ¬ββββββββββ¬βββββββββββ β β
β β β β β β β β β
β β [Triage] [Resource] [Bed] [Staffing] [Wait Time] β β
β β β β
β β [Clinical Deterioration] β β
β β β β
β β βββββββββββββββββββββββββββββββββββββββββββββββ β β
β β β ChromaDB RAG (8 Clinical Guidelines) β β β
β β βββββββββββββββββββββββββββββββββββββββββββββββ β β
β β β β
β β βββββββββββββββββββββββββββββββββββββββββββββββ β β
β β β LightningStore (Traces + Checkpoints) β β β
β β βββββββββββββββββββββββββββββββββββββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Azure OpenAI: GPT-5, O3, DeepSeek-V3 β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Backend:
- Rust (Actix-web): High-performance HTTP + WebSocket server
- Python (FastAPI): AI agent inference service
- SQLite: Encrypted local database
- ChromaDB: Vector database for clinical guidelines RAG
Frontend:
- React 18 + TypeScript: Modern component-based UI
- Tailwind CSS: Dark theme with lightning visual effects
- Vite: Fast build tool with hot module replacement
AI & ML:
- Azure OpenAI: GPT-5, O3, DeepSeek-V3 models
- Microsoft Agent Lightning: Training and optimization framework
- sentence-transformers: Local embeddings for RAG (all-MiniLM-L6-v2)
- Rust 1.70+
- Python 3.11+ with Poetry
- Node.js 18+
- Azure OpenAI API credentials
- Clone the repository
git clone https://github.com/gregnatkatz/erbed.git
cd erbed- Configure environment variables
Copy the .env.example files and fill in your Azure OpenAI credentials:
# Rust backend
cp rust-backend/.env.example rust-backend/.env
# Edit rust-backend/.env with your database path
# Python inference service
cp python-inference/.env.example python-inference/.env
# Edit python-inference/.env with your Azure OpenAI credentials:
# AZURE_OPENAI_ENDPOINT=https://your-resource.cognitiveservices.azure.com
# AZURE_OPENAI_API_KEY=your-api-key
# DEEPSEEK_ENDPOINT=https://your-resource.services.ai.azure.com/...
# DEEPSEEK_API_KEY=your-deepseek-key
# Frontend
cp frontend/.env.example frontend/.env
# Edit frontend/.env with backend URL (default: http://localhost:8000)- Start the Rust backend
cd rust-backend
cargo run
# Runs on http://localhost:8000- Start the Python inference service
cd python-inference
poetry install
poetry run uvicorn app.main:app --host 0.0.0.0 --port 8001
# Runs on http://localhost:8001- Start the React frontend
cd frontend
npm install
npm run dev
# Runs on http://localhost:5173- Open the dashboard
Navigate to http://localhost:5173 in your browser
POST /agents/triage- ESI scoring and acuity assessmentPOST /agents/resource- Resource prediction (labs, imaging, consults)POST /agents/bed- Bed assignment recommendationsPOST /agents/staffing- Staffing optimizationPOST /agents/wait-time- Wait time managementPOST /agents/deterioration- Clinical deterioration detection
POST /training/train-rl- Train agent with Reinforcement LearningPOST /training/train-apo- Train agent with Agent Policy OptimizationGET /training/evaluate/{agent_id}- Evaluate trained agentPOST /training/emit-trace- Emit training trace (agl.emit_xxx() equivalent)
GET /synthetic/patient- Generate single patientGET /synthetic/patients/{count}- Generate multiple patientsGET /synthetic/beds- Generate 16-bed statusGET /synthetic/alert- Generate single alertGET /synthetic/alerts/{count}- Generate multiple alerts
from app.training.lightning_trainer import AgentLightningTrainer
# Initialize trainer
trainer = AgentLightningTrainer(store_path="./lightning_store")
# Collect traces from production
for patient in production_patients:
result = triage_agent.process(patient)
# Emit trace with reward based on outcome
trainer.emit_trace(
agent_id="triage",
trace_data={
"prompt": patient.to_prompt(),
"response": result.dict(),
"reward": calculate_reward(result, patient.actual_outcome)
}
)
# Train agent with collected traces
traces = trainer.collect_traces(agent_id="triage", num_episodes=1000)
metrics = trainer.train_with_rl(
agent_id="triage",
traces=traces,
learning_rate=0.001,
num_epochs=10
)
print(f"Training complete! Accuracy improved by {metrics['accuracy_improvement']}%")
# Evaluate improved agent
eval_metrics = trainer.evaluate_agent(agent_id="triage")
print(f"Validation accuracy: {eval_metrics['accuracy']}")- SQLite encryption at rest (SQLCipher)
- HTTPS/TLS for all API traffic
- JWT authentication for staff
- HIPAA audit logging (all agent actions logged)
- No PHI in training traces (anonymized data only)
- Secrets managed via environment variables (never committed to git)
MIT License - See LICENSE file for details
Built with Microsoft Agent Lightning - the framework that makes production AI agents continuously improve through offline reinforcement learning and multi-agent coordination.
Key Innovation: Unlike static LLM applications, Agent Lightning enables your AI system to learn from every interaction and get smarter over time, delivering measurable improvements in real-world metrics.
Questions? Open an issue or reach out to the maintainers.
Want to contribute? PRs welcome! See CONTRIBUTING.md for guidelines.
Complete dashboard showing all 7 AI agents active with confidence scores, 16-bed grid color-coded by ESI level, and real-time alerts feed
Request Bed modal for AI-powered bed assignment with patient name, ESI level, and chief complaint fields
Success alert showing bed assignment with patient name appearing on the bed grid
Real-time performance metrics: door-to-provider time, LWBS rate, bed utilization, patient satisfaction
7 AI agents with confidence scores: Coordinator (95%), Triage (92%), Resource Prediction (88%), Bed Assignment (90%), Staffing (85%), Wait Time (87%), Clinical Deterioration (91%)
16-bed grid color-coded by Emergency Severity Index (ESI): Red (ESI-1), Orange (ESI-2), Yellow (ESI-3), Green (ESI-4), Blue (ESI-5)
Auto-updating alerts feed showing AI recommendations from all agents with timestamps and confidence scores