A pit call has to be on the wall in 25 ms. This is the system that makes sure it always is, even when the model can't be trusted.
Companion code for my LinkedIn article
AI/ML at Race Speed: Low-Latency Inference, Confidence Thresholds & Fallback Logic โ
A model that answers in 8 ms is no use if the system around it takes 40. A model that says it's 91% sure but is right 60% of the time is worse than one that admits it doesn't know. This repo builds the parts that turn a model into something you could put on a pit wall: a latency budget per stage, a hard timeout on inference, a dual-threshold confidence gate, calibration, drift detection, a three-tier fallback chain and an audit log of every call. The model itself is deliberately small; the engineering is in everything around it.
flowchart TB
A["๐ก Telemetry frame arrives ยท 0 ms"] --> B{"Features complete?"}
B -- yes --> C{"Inside training envelope?"}
C -- yes --> D["โก Edge inference ยท hard timeout โค 10 ms"]
D -- answered --> E{"๐ฏ Confidence gate ยท โค 12 ms"}
E -- "โฅ 0.85" --> P["โ
PRIMARY: model's call"]
E -- "0.70 โ 0.85" --> V["๐ก ADVISORY: fallback call, model shown alongside"]
E -- "< 0.70" --> F
B -- "no: sensor dropout" --> F
C -- "no: out of distribution" --> F
D -- "timeout" --> F
V --> F
F["๐ Fallback chain: 1 ยท rules โ 2 ยท cached call (decays per lap) โ 3 ยท crew chief with context"]
P --> W["๐ฅ๏ธ Pit wall ยท โค 25 ms ยท every call written to the audit log"]
F --> W
| The article says | Where it lives |
|---|---|
| Five stages, each with a hard latency budget (10 / 12 / 18 / 25 ms) | pipeline.py โ STAGE_DEADLINES_MS, cumulative deadlines from frame arrival |
| A slow model must not make the call late | pipeline.py โ inference runs on a worker thread with future.result(timeout=โฆ) |
| Dual-threshold gate: โฅ0.85 primary, 0.70โ0.85 advisory, <0.70 fallback | gate.py |
| Confidence without calibration is noise | calibration.py โ temperature scaling + expected calibration error |
| Distributional shift detection | drift.py โ z-score envelope from training data |
| Four failure modes: missing features, timeout, low confidence, out-of-distribution | pipeline.py โ each is a named failure_mode on the decision |
| Tier 1 rules โ Tier 2 cached model โ Tier 3 human override | fallback.py |
| Every prediction logged with context for post-race analysis | Decision.to_dict() โ one JSON line per lap |
No dependencies beyond Python 3.10+.
git clone https://github.com/atonyhonesto/race-speed-inference.git cd race-speed-inference python demo.py # simulate a 200-lap race with injected faults python -m unittest -v # 15 tests covering every gate zone and failure mode
Real output from python demo.py:
Calibration (expected calibration error, lower is better)
model as trained ECE 0.025 (fitted T=1.05)
simulated overconfident net ECE 0.091
...after temperature scaling ECE 0.017 (fitted T=3.20)
Race report: 200 laps, seed 2026
----------------------------------------------------
Who made the call Laps Share
PRIMARY_MODEL 166 83.0%
ADVISORY 4 2.0%
RULES 10 5.0%
CACHED 19 9.5%
HUMAN 1 0.5%
----------------------------------------------------
Why the model was bypassed Laps
LOW_CONFIDENCE 14
OUT_OF_DISTRIBUTION 8
FEATURE_UNAVAILABLE 6
MODEL_TIMEOUT 6
----------------------------------------------------
Primary-model accuracy on laps it owned: 91.6%
Latency p50 0.07 ms p99 9.20 ms max 9.24 ms (budget 25 ms)
Every call inside its stage deadlines: True
The demo injects three kinds of trouble so every path gets used: 3% sensor dropouts, 3% of inferences artificially stalled for 30 ms, and an eight-lap track-temperature spike the model never trained on. The stalled inferences are cut off at the deadline and handed to the fallback chain, which is why p99 sits just under 10 ms rather than at 30.
One lap from the audit log, when the heat spike pushed the model out of its envelope and neither the rules nor the cache had an answer:
{
"lap": 157,
"action": "CREW_CHIEF_CALL",
"source": "HUMAN",
"failure_mode": "OUT_OF_DISTRIBUTION",
"reasons": ["escalated: lap=157, drifted=['track_temp_c']"],
"stage_ms": {"inference": 0.003, "gate": 0.003, "strategy": 0.022, "dashboard": 0.023},
"deadline_met": true,
"injected": ["heat_spike"]
}- The fallback is a feature, not a failure. In the sample race the model owns 83% of laps. The other 17% still get a safe, explainable call on time, and each one records why the model was bypassed.
- Calibration is shown on purpose. Logistic regression is close to calibrated out of the box (Tโ1.05), so the demo also sharpens the logits 3x to imitate an overconfident deep network. ECE jumps from 0.025 to 0.091, and temperature scaling (Tโ3.2) brings it back to 0.017.
- Deadlines are cumulative, not per-stage. Every stage is measured from the moment the frame arrived, so slack early on can't hide a slow stage later.
- The cache decays. A trusted call from three laps ago is worth less than one from last lap; once it drops below the lower threshold it expires and the chain moves to the human.
- Accuracy is capped by design. The simulated "right call" has 8% label noise, standing in for strategists who'd disagree, so ~92% is the ceiling.
| Here | On a real pit wall |
|---|---|
Synthetic per-lap telemetry (telemetry.py) |
ECU stream at ~10 ms intervals from 60+ sensors |
| Pure-Python logistic regression | INT8-quantized network on ONNX Runtime / TensorRT at the track edge |
| Thread-pool timeout | Dedicated inference process or accelerator queue with deadline scheduling |
| Z-score drift envelope | Conformal prediction / SafeML-style runtime monitors |
| JSON-lines audit log | Time-series store feeding post-race retraining |
racespeed/
telemetry.py synthetic race + fault injection
model.py primary model (pure Python logistic regression)
calibration.py temperature scaling, expected calibration error
drift.py out-of-distribution detector
gate.py dual-threshold confidence gate
fallback.py rules โ cached call โ crew chief
pipeline.py deadlines, timeout, orchestration, Decision record
demo.py race simulation + report
tests/ unit and end-to-end tests
Built by Tony Honesto, cloud & integration engineer with roots in IndyCar timing & scoring and NASCAR Race Control. More articles and companion repos: github.com/atonyhonesto ยท LinkedIn