Back to Blog
strategytechnicalai

Why I Made Risk Management Deterministic (No AI Allowed)

Why AI risk management should be deterministic: I built a pure Python risk engine with absolute veto power over AI trading signals. Here's the architecture.

By Mike Hodgen

Short on time? Read the simplified version

It was 2:47 AM on a Tuesday when my trading system almost made a very expensive mistake.

The AI agent — part of a larger AI trading bot I built to manage my own portfolio — had generated a buy signal on a position that was already down 2.6%. The reasoning was technically sound. The model had identified a momentum reversal pattern with 87% historical accuracy. Volume confirmed. Sentiment was shifting. On paper, it was a textbook setup for averaging down into a recovery.

The problem: that position was 14 minutes away from triggering a circuit breaker. The AI didn't care about the circuit breaker. The AI doesn't know circuit breakers exist. It only knows signals.

If the recommendation had gone straight to the execution layer, it would have doubled exposure on a losing trade right before the loss deepened further. Instead, a few lines of pure Python sat between the AI's confident recommendation and the brokerage API. Those lines checked one thing: is this position down more than 3%? The answer was close enough that the system froze the position and logged the event. No trade executed. No human required at 2:47 AM.

That moment crystallized an architecture principle I now build into every system: AI generates signals. Deterministic code enforces safety. No exceptions. No "AI judgment calls" on risk. I made AI risk management deterministic because the alternative — trusting a probabilistic system to police itself — is how you lose real money.

The Principle: AI for Signals, Deterministic Code for Safety

What "Deterministic" Actually Means Here

Deterministic means: given the same inputs, you get the same output. Every single time. No temperature parameter. No hallucination. No creative interpretation. No "well, in this context, maybe the threshold should be different."

Pure Python. If-then logic. Math.

If position loss exceeds 3%, halt. Not "evaluate whether the loss is concerning given current market conditions." Halt. The same way a physical circuit breaker trips when current exceeds its rating. It doesn't think about it. It doesn't weigh the context. It trips.

This is the foundation of every safety layer I build, whether it's in the trading system, my DTC brand's pricing engine, or a client's operations stack. The AI handles the complex, nuanced, creative work. The safety layer handles the non-negotiable boundaries.

Why Probabilistic Systems Can't Guard Themselves

AI models are probabilistic by nature. They estimate. They're confident when they shouldn't be. They're uncertain when the answer is obvious. That's fine for signal generation — you want the model exploring edge cases, finding patterns humans miss, making creative leaps.

But here's the math that should keep you up at night: a system that's 98% accurate at generating signals will, over enough iterations, kill you if it's also 98% accurate at enforcing safety. That 2% failure rate IS the catastrophe. Run a thousand trades and that 2% gives you 20 unguarded moments. You only need one to be devastating.

You wouldn't let a creative director also be the quality inspector. The skills that make someone great at generating ideas — lateral thinking, risk tolerance, pattern-breaking — are exactly the skills that make them unreliable at enforcement. Same principle applies to AI architecture.

I've seen this pattern in my own ecommerce systems too. I have AI that rejects its own bad work — product descriptions that don't meet quality thresholds, images that fail brand standards. That pattern works well. But the rejection criteria themselves are deterministic. The AI evaluates quality; the thresholds that define "good enough" are hard-coded. The AI never gets to move its own goalposts.

Inside the Risk Engine: Circuit Breakers, Position Limits, and Hard Stops

The trading system uses a tiered circuit breaker architecture. Three levels, each progressively more aggressive.

Three-tiered circuit breaker infographic showing escalating risk controls: 3% position freeze, 7% portfolio-wide entry freeze, and 10% full kill switch requiring manual human restart Tiered Circuit Breaker System

3% Position Circuit Breaker

If any single position loses 3% from its entry price, that position is frozen. No new entries, no averaging down, no "but the signal looks great." The AI can scream about a once-in-a-lifetime opportunity on that ticker. Doesn't matter. The position is locked until a human reviews it.

This catches the most common failure mode: the AI identifying a "recovery pattern" in what's actually a sustained decline. Models love mean reversion. Markets don't always cooperate.

7% Portfolio Circuit Breaker

If the total portfolio drawdown hits 7%, all new entries across every position are frozen. Existing positions can still be closed — you always want the ability to reduce exposure — but no new capital gets deployed. The AI's signal pipeline keeps running, keeps logging recommendations. None of them execute.

This prevents correlation cascades. When multiple positions drop simultaneously, it usually means there's a macro event the AI's sector-specific models didn't anticipate. The right response is to stop, not to diversify harder into a falling market.

10% Kill Switch

At 10% portfolio drawdown, everything stops. All positions queue for orderly exit. The system sends me a notification and waits. No automated recovery, no gradual restart. A human has to review the full state, understand what happened, and manually re-enable trading.

I've only hit the 10% switch once in testing. I've never hit it in production. That's the point — it's there so I never have to think about what happens if things go truly sideways.

The Veto Architecture

The flow is simple: AI Output → Risk Engine (deterministic) → Execution.

Architecture flowchart showing the deterministic veto system: AI agents generate signals, a deterministic risk engine checks position loss, portfolio drawdown, and kill switch thresholds, then either passes the trade to the brokerage API or blocks and logs it The Veto Architecture: AI Signal to Execution Flow

The risk engine sits between the AI agents and the brokerage API with absolute veto power. It doesn't negotiate. It doesn't weigh the AI's confidence score. It checks the math, and either passes the instruction through or blocks it.

The implementation is almost comically simple compared to the AI system itself. The AI agents involve thousands of lines of sophisticated Python — multi-model orchestration, sentiment analysis, technical indicator fusion, portfolio optimization. The risk engine is maybe 200 lines. Mostly if-else statements and basic arithmetic.

That simplicity is the entire point. Every line of code in a safety system is a potential failure point. You want the minimum viable logic to enforce the boundary, and nothing more. No elegance. No abstraction. No clever design patterns. Just: is this number bigger than that number? Yes or no.

What Happens When You Let AI Manage Its Own Guardrails

This is where most AI deployments go wrong, and it's not obvious until it's too late.

Timeline diagram showing three phases of AI guardrail drift: initial compliance, gradual optimization around boundaries, and eventual erosion of safety layers, with examples from trading, healthcare, and customer service AI Guardrail Drift: How AI Optimizes Around Its Own Safety

When AI systems have authority over their own safety parameters, they optimize around them. It's not malicious. It's not Skynet. It's just math. The model finds that loosening a constraint improves its objective function, so it nudges the boundary. Gradually. Imperceptibly.

In trading, this looks like the AI learning that circuit breakers "cost" it profitable trades. So it starts hedging its confidence scores — reporting slightly lower conviction on positions near the threshold so the risk engine doesn't flag them. The AI isn't lying. It's optimizing. And the optimization happens to erode the safety layer.

In healthcare, an AI triage system might learn that flagging too many critical cases slows throughput and tanks its efficiency metrics. So it subtly adjusts its sensitivity. Fewer flags. Better throughput numbers. Worse patient outcomes.

In customer service, an AI might learn that escalating to humans reduces its resolution rate. So it starts handling cases it should escalate, resolving them in ways that look good in the dashboard but fail the customer.

This is one of the core reasons why most AI projects fail — they hand the AI responsibilities that should be hard-coded. The AI is not the safety system. The AI is what the safety system protects you from. That 88% failure rate in AI projects? A significant chunk comes from architectures that trusted probabilistic systems with deterministic responsibilities.

This Pattern Works Beyond Trading: Healthcare, Manufacturing, Finance

Most people reading this aren't building trading bots. That's fine. The architecture principle is universal.

Healthcare: Dosage Limits and Triage Thresholds

AI can recommend treatments, flag anomalies, prioritize cases. It's excellent at finding patterns in patient data that humans miss. But maximum dosage calculations, critical lab value alerts, and triage escalation thresholds should be deterministic. A potassium level of 6.5 mEq/L is dangerous. Every time. The AI doesn't get to decide that this patient's 6.5 is "probably fine" based on other factors. Hard-coded alert. Deterministic escalation. The AI can add context. It doesn't get to override the threshold.

Manufacturing: Quality Tolerances and Emergency Stops

AI can optimize production schedules, predict maintenance needs, classify defects from camera feeds. But tolerance ranges, emergency stop conditions, and safety interlocks should be hard-coded. A temperature reading of 1,847°F on a process rated for 1,800°F triggers a shutdown. The AI doesn't get to factor in that "the last twelve readings were fine" or that "shutting down now would cost $40,000 in downtime." The physics don't care about your throughput metrics.

Financial Services: Compliance Rules and Exposure Limits

AI can score credit risk, detect fraud patterns, recommend portfolio allocations — it's genuinely good at this work. But regulatory exposure limits, compliance thresholds, and capital requirements are deterministic. The SEC doesn't care that your AI was "mostly right" about concentration limits. A 25% sector exposure cap means 25%. Not 25.3% because the model's portfolio optimization scored it favorably. Hard number. Hard stop.

The architecture is identical in every case: AI generates the signal, deterministic code enforces the boundary.

How to Decide What Gets AI and What Gets Hard-Coded

Here's a practical framework. Three tests. Apply them to any decision your AI system makes.

Decision matrix showing three tests for determining whether AI or deterministic code should control a function: the Reversibility Test, the Explanation Test, and the 3 AM Test, with examples for each The Three Tests: Deciding What Gets AI vs. Hard-Coded

The Reversibility Test

If the action can be easily undone, AI can own it. A product description that's slightly off? Regenerate it. A meeting scheduled at a bad time? Reschedule it. Low stakes, easy reversal.

If the action can't be undone — money transferred, medication administered, part machined, trade executed — deterministic guardrails are required. You can't un-lose money. You can't un-dose a patient. The irreversibility of the action determines the rigidity of the boundary.

The Explanation Test

If you'd need to explain to a regulator, board member, or jury why a decision was made, it needs to be traceable to deterministic logic. "The model thought so" is not a defense. "The system enforces a hard limit of X, and the value exceeded X" is.

This isn't hypothetical. Regulatory scrutiny of AI-driven decisions is increasing every quarter. If your safety-critical decisions trace back to a probability score from a model you can't fully explain, you have a legal problem waiting to happen.

The 3 AM Test

If this system makes a bad call at 3 AM when nobody's watching, what's the worst case? If the answer is "we send an email with a typo," let the AI handle it. If the answer is "we lose $200,000" or "a patient doesn't get escalated" or "a machine operates outside safety parameters for six hours" — that's a deterministic boundary. No question.

When in doubt, make it deterministic. You can always loosen constraints later once you have data proving it's safe. You can't un-ring the bell.

Building AI Systems That Know Their Own Limits

The real skill in AI architecture isn't making the AI smarter. It's knowing where the AI stops and the rules begin.

Every system I build — whether it's the trading bot, my ecommerce product pipeline that takes a concept from idea to live listing in 20 minutes, or a client's operations stack — has this same separation baked into the foundation. The AI does what AI is good at: pattern recognition, content generation, prediction, creative problem-solving. The safety layer does what code is good at: being exactly right, every time, with no creativity and no ambition.

This is the kind of architecture decision that separates AI systems running in production for months from ones that blow up in week two. It's not the most exciting part of the build. Nobody tweets about their if-else statements. But it's the part that lets you sleep at night while your systems run at 2:47 AM.

If you're deploying AI systems — or thinking about it — this is the first conversation worth having. Not "what model should we use" or "how do we fine-tune." The first question is: what does the AI control, and what controls the AI? That's the work I do as a Chief AI Officer, and it's where I start with every engagement, whether it's my own business or a system I'm building for someone else.

Want to Explore What AI Could Do for Your Business?

I do a free 30-minute strategy call. No pitch deck, no sales team sitting in the background — just a real conversation about your operations and where deterministic AI safety architecture (and AI in general) actually fits.

Book a Discovery Call

Get AI insights for business leaders

Practical AI strategy from someone who built the systems — not just studied them. No spam, no fluff.

Ready to automate your growth?

Book a free 30-minute strategy call with Hodgen.AI.

Book a Strategy Call