A Prescription Safety Auditor in 50 Lines of Python
34% of prescriptions in Indian hospitals contain medication errors. 47.2% of those errors are wrong dose. 37.2% involve antibiotics. These numbers come from a 2024 systematic review across multiple Indian hospital settings. NABH flagged dosage miscalculation as a key patient safety challenge in March 2024.
A study at Massachusetts General Hospital showed that clinical decision support software can prevent up to 95% of medication errors in operating rooms. The tools exist. They're just not accessible.
Here is a prescription safety auditor, written in Python, that does what a clinical pharmacist does during medication review. It takes a patient profile and a new prescription, then returns SAFE, WARNING, or REJECT with reasons.
The Tool
pip install moisscode
from moisscode.modules.med_pk import PharmacokineticEngine
from moisscode.modules.med_lab import LabEngine
def audit_prescription(patient, new_drug, new_dose, dose_unit="mg"):
"""
Audit a new prescription against patient profile.
Returns a dict with verdict (SAFE/WARNING/REJECT) and reasons.
"""
pk = PharmacokineticEngine()
lab = LabEngine()
findings = []
verdict = "SAFE"
# Step 1: Calculate eGFR from creatinine
gfr = lab.gfr(
creatinine=patient["creatinine"],
age=patient["age"],
sex=patient["sex"]
)
egfr = gfr["eGFR"]
# Step 2: Validate the dose
dose_check = pk.validate_dose(new_drug, new_dose, dose_unit)
if dose_check["level"] != "SAFE":
findings.append(f"DOSE: {dose_check['message']}")
verdict = "REJECT"
# Step 3: Check interactions against current medications
if patient.get("current_drugs"):
ix = pk.check_interactions(new_drug, patient["current_drugs"])
if ix.get("interactions"):
for interaction in ix["interactions"]:
sev = interaction["severity"]
other = interaction["drug_b"]
findings.append(f"INTERACTION: {new_drug} x {other} = {sev}")
if sev in ("MAJOR", "SEVERE"):
verdict = "REJECT"
elif sev == "MODERATE" and verdict == "SAFE":
verdict = "WARNING"
# Step 4: Renal dose adjustment
renal = pk.renal_adjust(new_drug, gfr=egfr)
if renal["adjustment_factor"] < 1.0:
pct = int((1 - renal["adjustment_factor"]) * 100)
findings.append(
f"RENAL: eGFR {egfr} mL/min ({gfr['stage']}). "
f"{renal['recommendation']}"
)
if verdict == "SAFE":
verdict = "WARNING"
return {
"drug": new_drug,
"dose": f"{new_dose} {dose_unit}",
"eGFR": egfr,
"ckd_stage": gfr["stage"],
"verdict": verdict,
"findings": findings if findings else ["No issues detected"],
}
That's the auditor. Under 50 lines of logic. Here's what it catches.
Test Case: The Prescription That Should Never Be Written
A 72 year old male. Creatinine 2.8 mg/dL. Currently on Warfarin, Metformin, Amlodipine, and Omeprazole. A new prescription comes in for Ciprofloxacin 500mg.
patient = {
"age": 72,
"sex": "M",
"creatinine": 2.8,
"current_drugs": ["Warfarin", "Metformin", "Amlodipine", "Omeprazole"],
}
result = audit_prescription(patient, "Ciprofloxacin", 500, "mg")
for key, val in result.items():
print(f"{key}: {val}")
Output:
drug: Ciprofloxacin
dose: 500 mg
eGFR: 23.2
ckd_stage: G4 - Severely decreased
verdict: REJECT
findings: ['INTERACTION: Ciprofloxacin x Warfarin = MAJOR',
'RENAL: eGFR 23.2 mL/min (G4 - Severely decreased). Reduce dose by 50%, monitor closely']
Two problems caught:
-
Ciprofloxacin and Warfarin is a MAJOR interaction. Fluoroquinolones inhibit warfarin metabolism, causing INR to spike. This is a well documented cause of bleeding events and a frequent source of adverse drug reactions in Indian hospitals.
-
eGFR of 23.2 means Stage G4 kidney disease. At this GFR, Ciprofloxacin needs a 50% dose reduction. The standard 500mg dose is too high. The engine recommends 200mg with close monitoring.
Either of these alone is a red flag. Together, this prescription is a REJECT.
What A Clean Prescription Looks Like
Same patient, different drug. Amlodipine 5mg (which the patient is already on, just a refill):
result = audit_prescription(patient, "Amlodipine", 5, "mg")
print(f"verdict: {result['verdict']}")
print(f"findings: {result['findings']}")
verdict: SAFE
findings: ['No issues detected']
No interactions with current medications. Amlodipine doesn't need renal adjustment (it's hepatically metabolized). Dose is within range. SAFE.
Where This Fits
This auditor runs in under a second. It could sit behind any prescription entry form in any EMR. Every time a doctor writes a prescription, this runs in the background and returns SAFE, WARNING, or REJECT before the order goes through.
The underlying engine has 106 drugs across 30+ categories with full PK profiles, interaction maps, and renal/hepatic adjustment protocols. The audit_prescription function above is just one way to wire them together. Adapt it, extend it, embed it.
Professional biomedical software. All clinical outputs should be validated by qualified professionals before patient care application.
