L11 · Spec · 5 min

Code and AI: deterministic vs probabilistic

In one sentence

Ordinary code is deterministic, meaning the same input always produces the exact same output, whereas AI models are probabilistic, meaning they predict likely responses that can vary or contain mistakes—so your app uses code for rules and validates every AI answer.

Think of it like this

Ordinary code is like a pocket calculator: type `7 × 8` a thousand times and it says `56` every single time. An AI model is like a well-read colleague brainstorming at a whiteboard: great at summarizing a messy note or suggesting recipes, but occasionally confident about a wrong detail.

How it works

Deterministic code follows exact rules step by step: checking whether a user is signed in, adding up a shopping cart, saving a row in your DefinitiondatabaseWhere your app remembers things between visits, like accounts or saved recipes.Open in the glossary →, or enforcing a Definitionrate limitA cap on how often something can be done, like sign-in attempts per minute, to stop abuse.Open in the glossary →. Given the same input, it produces the exact same result every time. Never use an AI DefinitionmodelThe AI system that produces answers, like the one behind your coding agent.Open in the glossary → for work that ordinary code can do with 100% certainty.

Probabilistic AI models shine where rigid rules fail: summarizing long text, extracting structure from messy notes, translating tone, or drafting suggestions from a DefinitionpromptThe message you give an AI model. Clear prompts get better results.Open in the glossary →. Because the model predicts words by probability, two identical prompts can yield slightly different phrasing—and sometimes a DefinitionhallucinationWhen an AI states something false with confidence. It's why you check its work.Open in the glossary → (stating something false with confidence).

To keep AI features reliable inside a real app, your Definitioncoding agentAn AI that works in your project folder: it reads files, writes code and runs commands, with your permission.Open in the glossary → wraps every model call in deterministic guardrails on the DefinitionbackendThe part of an app that runs on a server: it keeps data, checks logins and talks to other services.Open in the glossary →: it sets max_completion_tokens (around 2× the longest expected answer) so a runaway response cannot waste money, and treats all model output as untrusted until ordinary code validates and escapes it.

Finally, because external AI providers can run out of capacity (503) or hit rate limits (429), your app configures Tokentide first (maxRetries: 0) with Vercel AI Gateway as an automatic DefinitionfallbackA backup that answers when the main provider can't, so your app keeps working.Open in the glossary →, so visitors still get their answer if the primary provider is busy.

1 · Deterministic guardCode checks inputChecks login session, rate limit, input schema, and sets `max_completion_tokens`.
2 · Probabilistic stepAI Model + FallbackTokentide (or Vercel AI Gateway on fallback) drafts or extracts the answer.
3 · Deterministic checkValidate & escapeOrdinary code checks the structure and escapes the output before showing it.
FIG. 1 · Sandwich the probabilistic AI call between deterministic code checks: validate input and cap tokens before, and validate and escape the output after.

What it looks like in your project

When designing your Definitionspec`SPEC.md`: what you're building, who it's for, what's in and out, and how you'll know it works.Open in the glossary →, your agent separates deterministic tasks (logins, permissions, math, database writes) from probabilistic AI tasks. If your app doesn't need AI features, it adds no AI providers at all. If it does, it writes the Tokentide-to-Gateway fallback, validates the model output with a schema, and tests the fallback in a DefinitionPreviewA private test link Vercel makes for each change, so you can try it before it goes live.Open in the glossary → deployment.

Copy for your agent

Review my SPEC.md and tell me which features should be written as ordinary deterministic code and which (if any) need a probabilistic AI model, and explain how we will validate the model's output.

Quick check · 1 question

How should your app handle the output returned by an AI model?

Go deeper

Next lessonHow to write a good spec

Last reviewed Nov 8, 2026