prakashrajk/antibiotic-stewardship
๐ฆ Antibiotic Stewardship โ OpenEnv Environment
Built for the Meta ร PyTorch OpenEnv Hackathon 2026 Organised by Scaler School of Technology in collaboration with Meta, Hugging Face, and PyTorch.
An RL environment where an LLM agent acts as a clinical pharmacist, treating patients while managing antimicrobial resistance (AMR). AMR is a WHO-priority global health crisis responsible for ~700,000 deaths per year โ projected to reach 10 million by 2050.
The agent must balance curing today's patient without destroying the antibiotic's effectiveness for future patients. This is the core stewardship challenge.
๐ Why This Matters
Antimicrobial resistance (AMR) occurs when bacteria evolve to resist antibiotics through overuse and misuse. Once a drug becomes ineffective, patients die from infections that used to be easily treatable. This environment trains an AI agent to prescribe antibiotics responsibly โ using the weakest effective drug to preserve stronger ones for when they are truly needed.
๐๏ธ Architecture
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ inference.py โ
โ (LLM Agent / AI Doctor) โ
โ - Qwen/Qwen2.5-72B-Instruct via HF Router โ
โ - Deterministic fast-path for obvious cases โ
โ - Episode memory (last 6 patients) โ
โ - Retry logic + safe fallback โ
โโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ HTTP (reset / step / grade)
โโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ app.py โ
โ (FastAPI Web Server) โ
โ - OpenEnv-compliant REST API โ
โ - Per-task session management โ
โ - 503 retryable errors for cold-start handling โ
โโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ environment.py โ
โ (Hospital Simulation Engine) โ
โ - 3 tasks: easy / medium / hard โ
โ - Resistance tracking per antibiotic โ
โ - Side-effect penalties for vulnerable patients โ
โ - Overkill detection (Vancomycin on mild cases) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ๐ฏ Tasks
๐ Antibiotics
Key rule: Every antibiotic use increases bacterial resistance for ALL future patients in the episode. The agent must think long-term, not just about the current patient.
๐ค Model & Approach
LLM: Qwen/Qwen2.5-72B-Instruct accessed via Hugging Face's OpenAI-compatible router at https://router.huggingface.co/v1
Agent Design:
The agent uses a two-layer decision strategy:
- Deterministic fast-path โ For crystal-clear cases (MRSA always needs Vancomycin, severity 3 always needs the strongest available drug), the agent decides instantly without an LLM call. This saves API credits and speeds up evaluation.
- LLM reasoning โ For nuanced cases, the agent sends the full patient context to Qwen2.5-72B-Instruct, including:
- Current patient details (age, infection, severity)
- Live resistance status for all 3 drugs with OK / WARNING / FAILED labels
- Summary of the last 6 patients treated and their outcomes
- Episode totals (cured / failed / partial / overkill counts)
Prompt Engineering: The system prompt teaches the LLM the scoring rules, resistance growth rates, decision rules per severity level, and the key stewardship insight: "Using stronger drugs unnecessarily burns resistance for ALL future patients." The LLM responds only with structured JSON {"antibiotic": 0|1|2, "reasoning": "one sentence"} for reliable parsing.
Robustness:
- Retry logic with exponential backoff (up to 10 attempts for connection errors, 3 for LLM failures)
wake_up_space()pings the HF Space before evaluation to handle cold starts- Hard fallback to severity-based rules if all LLM retries fail
- 503 errors treated as retryable (not fatal) for HF Space warm-up delays
๐ Evaluation Results
Scores achieved by the deterministic agent (fast-path rules only, no LLM):
With Qwen2.5-72B-Instruct handling nuanced cases on top of the fast-path, scores on medium and hard tasks improve further โ particularly for moderate severity patients where resistance levels require careful drug selection.
Score formula: score = max(0, total_reward) / (patients ร 10) โ normalized 0.0โ1.0
๐ Scoring Rules
๐ API Reference
๐ Quick Start
Run with Docker
# Build and start
docker build -t abx-env .
docker run -p 7860:7860 abx-env
# Verify it's running
curl http://localhost:7860/health
# โ {"status": "ok"}Play through an episode manually
# Start a hard episode
curl -X POST http://localhost:7860/reset \
-H "Content-Type: application/json" \
-d '{"task_id": "hard"}'
# Treat a patient (give Vancomycin)
curl -X POST http://localhost:7860/step \
-H "Content-Type: application/json" \
-d '{"antibiotic": 2}'
# Check current state
curl http://localhost:7860/state
# Get final score (after all patients treated)
curl http://localhost:7860/gradeRun the LLM agent
# Against the live HF Space
export ENV_URL=https://prakashrajk-antibiotic-stewardship.hf.space
export API_BASE_URL=https://router.huggingface.co/v1
export MODEL_NAME=Qwen/Qwen2.5-72B-Instruct
export HF_TOKEN=hf_your_token_here
python inference.py
# Against local Docker
python inference.py --url http://localhost:7860๐ Project Structure
antibiotic-stewardship/
โโโ app.py # FastAPI server โ all OpenEnv endpoints
โโโ environment.py # Hospital simulation โ patients, resistance, scoring
โโโ inference.py # LLM agent โ Qwen2.5 + fast-path + retry logic
โโโ models.py # Pydantic data models โ Action, Observation, StepResult
โโโ client.py # Typed Python client for the API
โโโ openenv.yaml # OpenEnv spec declaration
โโโ Dockerfile # Container config for HF Spaces (port 7860)
โโโ requirements.txt # Python dependencies
โโโ README.md # This file๐ฎ Future Work
- Interactive GUI โ A Gradio or Streamlit frontend where judges and clinicians can run episodes, visualise resistance curves in real time, and inspect the LLM's reasoning for each decision.
- Multi-model comparison โ Benchmark smaller models (Qwen2.5-7B, LLaMA-3-8B) against the 72B variant under the same environment and scoring protocol to study the cost/performance tradeoff.
- Richer clinical parameters โ Add comorbidities, allergy history, culture results, and PK/PD-inspired dosing to make the simulation more clinically realistic.
- Resistance dynamics research โ Use the environment's detailed treatment logs to study how different prescribing policies affect long-term population-level resistance โ directly relevant to antibiotic stewardship research.
- Clinical validation โ Collaborate with infectious disease specialists to validate environment design and scoring rules against real hospital antibiotic stewardship guidelines.
๐ฆ Dependencies
๐ OpenEnv Compliance
This environment fully implements the OpenEnv specification:
- โ
openenv.yamlwith specversion, tasks, endpoints, actionspace, observation_space - โ
POST /resetโ typed Pydantic models, returns Observation - โ
POST /stepโ takes Action, returns StepResult (obs, reward, done, info) - โ
GET /stateโ full environment state - โ
GET /gradeโ normalized score 0.0โ1.0 with breakdown - โ 3 tasks with easy โ medium โ hard progression
- โ Meaningful reward function with partial progress signals
- โ Deployed on Hugging Face Spaces with working Dockerfile
- โ
inference.pywith[START][STEP][END]structured logging
AMR kills 700,000 people per year. Teaching AI to prescribe responsibly is not just a hackathon problem โ it's a global health necessity.
