HomeAI Precision Dosing & DiagnosticsClosed-Loop Insulin/Drug Pump (AI Controller)

🧠 Closed-Loop Insulin/Drug Pump (AI Controller)

This simulation models a closed-loop insulin pump controlled by an AI algorithm that uses real-time data from a continuous glucose monitoring (CGM) sensor to adjust insulin dosing in response to post-meal glucose spikes. The goal is to maintain optimal blood sugar levels while minimizing the risk of hypoglycemia.

AI Precision Dosing & Diagnostics2DModerate60 FPS
closed-loop-insulin-pump ↗ Open standalone

CGM Sensors and the Glucose-Insulin Control Problem

The modern artificial pancreas is fundamentally a control-systems problem wrapped around human physiology: a noisy, delayed sensor (the CGM) feeds an algorithm that must compute a safe insulin dose to counteract an unpredictable, meal-driven, highly nonlinear disturbance (blood glucose) — with a actuator (subcutaneous insulin) that itself has a multi-hour delay and no way to "undo" an overdose once delivered.

  • 5 min: CGM sampling interval (Dexcom G7 / Libre 3)
  • ~10–15 min: Interstitial-blood lag (physiological delay)
  • ~8–9%: MARD (sensor accuracy) (mean absolute relative difference)
  • 70–180 mg/dL: Target glucose range (ADA/ISPAD consensus)

Why glucose control is a uniquely hard control problem

Most industrial control problems have fast, direct actuators and clean sensors. Glucose control has neither: the CGM measures interstitial glucose (a proxy, lagging true blood glucose by 10–15 minutes and carrying ~8–9% random measurement error), while the actuator — subcutaneous rapid-acting insulin — takes 15 minutes just to begin absorbing and 60–90 minutes to reach peak effect, with total action persisting 3–5 hours.

A meal can raise blood glucose by 100+ mg/dL within 30–60 minutes, faster than insulin can respond even with perfect prediction — this is why hybrid closed-loop systems still require a manual pre-meal bolus announcement in most commercial systems (Tandem Control-IQ, Medtronic 780G): the control loop alone cannot react fast enough to fully blunt a meal spike without meal-time forewarning.

The mismatch between meal-glucose speed (minutes) and insulin action speed (hours) is the single hardest unsolved problem in closed-loop insulin delivery — faster insulin analogs (Fiasp, Lyumjev) and dual-hormone systems (adding glucagon) are the two main engineering responses.

PID Control — Proportional, Integral, and Derivative Insulin Dosing

PID control is the same algorithm family used in cruise control, thermostats, and industrial process control — and its transparency, tunability, and decades of engineering track record make it the backbone of nearly every FDA-cleared hybrid closed-loop insulin pump on the market today.

  • u=Kp·e+Ki·∫e+Kd·de/dt: PID formula (dose from error terms)
  • Control-IQ, 780G: Commercial examples (Tandem, Medtronic)
  • 0–3× basal rate: Basal modulation range (typical suspend-to-boost span)
  • 3: Tuning parameters (Kp, Ki, Kd per patient)

Reading the three PID terms in a glucose controller

The proportional term (Kp·e) delivers extra insulin proportional to how far current glucose sits above target right now — a large, immediate error gets a large, immediate correction. The integral term (Ki·∫e dt) accumulates past error over time, catching persistent small deviations that the proportional term alone would under-correct — e.g., glucose sitting 20 mg/dL above target for hours due to a slightly wrong basal rate. The derivative term (Kd·de/dt) reacts to the rate of change, allowing the controller to preemptively reduce insulin as glucose is already falling fast, even before it reaches target — critical for avoiding a rebound low after a correction.

Insulin dosing PID controllers almost universally implement asymmetric gains: they act much more aggressively increasing insulin above target than decreasing/suspending below it, and most commercial systems bias toward insulin suspension (Kp effectively capped near zero net insulin) rather than negative dosing, since insulin cannot be "taken back" once infused — a fundamental actuator asymmetry that has no analogue in a thermostat.

Reinforcement Learning — Adaptive, Personalized Dosing Policies

Where PID control applies the same fixed-gain formula to every glucose trajectory, reinforcement learning approaches instead learn a dosing policy directly from data (real or simulated), adapting to an individual's unique insulin sensitivity, meal timing patterns, and circadian variation in ways a fixed PID gain set cannot easily capture.

  • TIR − λ·hypo_penalty: Reward function (typical RL objective)
  • FDA-accepted UVA/Padova: Training environment (in-silico glucose simulator)
  • Offline + periodic online: Policy update (safety-gated adaptation)
  • Investigational: Clinical RL systems (not yet widely FDA-cleared)

Why RL promises personalization — and why safety gating is non-negotiable

An RL dosing agent is trained (typically offline, on the FDA-accepted UVA/Padova type-1 diabetes simulator population, or on de-identified real patient CGM+pump data) to maximize a reward that combines time-in-range with an asymmetric penalty for hypoglycemia — reflecting the clinical reality that a low blood glucose event carries acute danger (seizure, loss of consciousness) that a moderately high reading does not.

Unlike PID, an RL policy can in principle learn patient-specific structure: that this patient tends to under-bolus for breakfast, that overnight sensitivity rises around 3 a.m. (the "dawn phenomenon" in reverse), or that a particular meal pattern predicts a delayed post-prandial rise from high-fat content. In practice, every clinically deployed RL-adjacent system wraps the learned policy in hard safety constraints — bounded maximum dose, mandatory suspension below a glucose floor, and supervisory override — because an unconstrained learned policy exploring novel dosing strategies on a live patient is not an acceptable risk; RL contributes the adaptive "how much" within limits that remain rule-based.

Every RL-based glucose controller in active clinical development runs inside a rule-based safety supervisor that can veto or clip the RL output — the learned policy proposes, but a simple, auditable safety layer always has final authority over insulin delivery.

Subcutaneous Insulin Pharmacokinetics — The Delay the Controller Must Out-Think

No dosing algorithm, however sophisticated, can outrun the physical absorption kinetics of insulin injected into subcutaneous fat. Understanding "insulin on board" — the residual glucose-lowering effect of insulin already delivered but not yet fully acted — is what separates a controller that avoids delayed hypoglycemia from one that stacks doses into a dangerous low.

  • ~15 min: Onset (rapid-acting) (Aspart, Lispro, Fiasp)
  • 60–90 min: Time to peak action (subcutaneous absorption)
  • 3–5 h: Duration of action (total insulin activity curve)
  • ~5 min faster onset: Ultra-rapid analogs (Fiasp/Lyumjev vs. standard)

Insulin-on-board and the danger of dose stacking

When the pump delivers a correction bolus, that insulin does not act instantaneously and then vanish — it follows a pharmacokinetic action curve that rises over ~60–90 minutes and then decays over several hours. "Insulin on board" (IOB) tracks how much of a previous dose is still pharmacologically active. A controller that ignores IOB and doses again purely off the current glucose reading risks "stacking" — delivering a second full correction on top of a first dose that has not yet finished acting, driving glucose down further than intended once both doses reach peak effect simultaneously.

Every closed-loop algorithm — PID or RL — must therefore subtract estimated IOB from any new dose recommendation. This is precisely why faster-acting insulin analogs (Fiasp, Lyumjev) are such a meaningful engineering lever for closed-loop performance: shrinking the action-curve delay directly shrinks the unavoidable control lag, letting the same algorithm achieve tighter time-in-range with less stacking risk.

Time-in-Range and the Real-World Verdict on Closed-Loop Performance

All the control theory in the world is ultimately judged against three simple, clinically validated metrics tracked over a 24-hour glucose trace: how much time was spent in the safe target range, how much above it, and — most consequentially — how much below it.

  • >70%: TIR target (consensus) (time 70–180 mg/dL)
  • <4%: Time below range target (time <70 mg/dL)
  • +10–15%: Hybrid closed-loop TIR gain (vs. sensor-augmented pump alone)
  • ~0.3–0.5%: HbA1c reduction (typical closed-loop trial benefit)

From control-theory elegance to hard clinical endpoints

Time-in-Range (TIR, glucose 70–180 mg/dL), Time-Above-Range (TAR, >180 mg/dL) and Time-Below-Range (TBR, <70 mg/dL) are now standardized, consensus-endorsed metrics (used alongside HbA1c) because they capture glycemic variability that a single averaged HbA1c number hides entirely — two patients can share an identical HbA1c while one spends hours in dangerous hypoglycemia and the other does not.

Randomized trials of commercial hybrid closed-loop systems (Control-IQ, 780G, CamAPS FX) consistently show a 10–15 percentage-point absolute increase in TIR compared to sensor-augmented pump therapy without automation, concentrated overnight when patients cannot manually respond to alarms — the single largest real-world benefit of automated insulin delivery is not daytime meal handling (still often manually bolused) but continuous, fatigue-proof overnight correction that no amount of patient vigilance can reliably replicate.

The overnight TIR improvement — not daytime meal control — is what nearly every closed-loop pivotal trial identifies as the dominant source of benefit, because it is the one period where a tireless algorithm has a structural advantage over an sleeping human.
⚙ Under the hood

This simulation models a closed-loop insulin pump controlled by an AI algorithm that uses real-time data from a continuous glucose monitoring (CGM) sensor to adjust insulin dosing in response to post-meal glucose spikes. The goal is to maintain optimal blood sugar levels while minimizing the risk of hypoglycemia.

CanvasBiomedicine

2D · HTML5 Canvas 2D · 60 FPS target · runs fully client-side, no install

What did you find?

Add reproduction steps (optional)