Note
AI SecurityGuardrailsDeterministic ControlsPrompt InjectionLLM Pipelines

Let Code Decide

The numbers here are ones I noted while building and testing pipelines, over many runs. They aren't from a controlled study, I didn't keep run counts, and they depend on the models and prompts I used. I'd expect the direction to hold for anyone who measures it.

I was building a workflow that took data, sent it to a model, and had the model write a search query from it. Partway through I realized the query was a language with a grammar. Given the same data it should come out the same every time. I was sending it to something slower and more expensive than a function, and the accuracy was a dice roll. So I wrote the query builder in Python, and the problem went away.

That's the whole idea, and it applies far more widely than I first thought. If the output has a grammar, or the decision has a rule, code should produce it. A model belongs where the input really is unstructured, and even there, code should check what the model says.

How consistent a model judge is

I measured consistency across repeated runs, because a control that gives different answers to the same input can't be trusted with either answer.

Using a small model (3 billion parameters) as a guardrail to rule pass or block on prompt injection, I got about 60 percent consistency. The model was small on purpose: the guardrail had to run locally and inline on any user's machine, shipped with a coding tool. That's the normal constraint for an inline guardrail, and it's the model size you get. Four in ten prompts could be blocked on one run and let through on the next, depending on the ruling. As a security gate, that's a coin flip for a large share of inputs, and an attacker only needs to retry.

Having a model write queries and then checking them against verified query syntax rules did much better, in the low 90s. A Python script that built the query from the data was right every time, because there was nothing left to vary.

That spread, from about 60 to the 90s to 100, is the reason for the rule.

What I use models for now

I still combine syntax checks and model detection in pipelines. The syntax side handles everything that has a known shape: query structure, fields, patterns, allowed values. The model handles what's left, the text that has no structure to check, and its output goes into something deterministic rather than straight to a decision.

When I do need a model's judgment, I don't take one answer. I send the same input through several models, or through one model several times, and compare the outputs. Sometimes I pass a result only when enough of the runs agree. Sometimes I attach a confidence score based on the share of runs that agreed, and let that score decide what happens next. This turns the model's inconsistency from a hidden risk into a number I can set a threshold on.

The judge problem

For a long time I had a model judge the agreement as well: here are five outputs, how similar are they? That puts a second dice roll on top of the first.

What worked far better was changing what the models return. I moved most of my model outputs to structured, JSON-style responses with defined fields, and had a script compare them. Field-by-field comparison in code is exact. It's also cheap and fast, and it's been far more accurate for my assessments than a model reading prose and deciding whether two answers mean the same thing.

The general move is to shrink the model's job to filling in fields, and let code do the comparing, validating and deciding.

The ladder

I now think of every step in a pipeline as sitting on a ladder, and I try to push each one as high as it will go:

  1. Code produces the output. Queries, transforms, anything with a grammar
  2. A model produces it and code checks it. Schema validation, syntax rules, allowed values
  3. Several model runs produce it and code measures agreement. Pass on a threshold or attach a confidence
  4. A model produces it alone. Only where a wrong answer is cheap

Most steps I first built at rung 4 turned out to belong at rung 1 or 2. The model was doing work a function could do, because asking the model was the fastest way to get something running.

Probability compounds

Every model call in a workflow adds probability, and a chain of calls multiplies it. If each step in a four-step chain gives the right answer 95 percent of the time, the chain is right about 81 percent of the time (0.95 to the fourth power). At 90 percent per step it's about 66 percent. Three steps at 90 percent come to about 73 percent. A step that looks reliable on its own becomes a workflow that's wrong a third of the time.

That arithmetic assumes the errors are independent. In practice they're often worse, because a wrong answer early in the chain becomes the input to the next call, which then reasons confidently from it. And when the final output is wrong, it's hard to tell which step caused it.

Every step moved to code drops out of the multiplication. Replace two of four model calls with deterministic steps and you're multiplying two probabilities instead of four.

Why this matters for security

A security control is a decision that has to come out the same way every time for the same input. A model at rung 4 can't promise that. My 60 percent figure for injection rulings is what that looks like in practice, and the attacker chooses which inputs to send.

Deterministic steps can also be tested properly. You can list a script's failure modes and break each one on purpose, which the post Fail open by default argues every control needs. You can't list every input a model misjudges.

And they don't change under you. A model update can move a judge's behavior without any change on your side. A query builder means the same thing next year.

The cost

Writing the builder takes longer than writing the prompt. Rules go stale and edge cases pile up. Some inputs really are unstructured, and forcing them into a grammar loses information. Ensembles multiply model calls, so a five-run agreement check costs five times as much as one call, which is another reason to push steps up the ladder before reaching for an ensemble.

For me the trade has been worth it every time. The pipelines got faster and cheaper, and I could say how often they were right.

How confident I am

High that code beats a model for anything with a grammar or a fixed rule, since the consistency numbers above were large and repeatable. Medium that my specific figures carry over to other models and prompts. A larger model than the one behind the 60 percent would do better. But the point isn't the exact figure: a model adds probability wherever it sits, and code doesn't. I'd expect the gaps to narrow as models improve, though not to close.

What would change my mind is a model judge that, run repeatedly on the same inputs, gave the same verdict at the rate a script does, and kept doing so across model versions. I haven't seen one.

← All Writing