# Code and AI: deterministic vs probabilistic

> 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.

## Analogy

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 database, or enforcing a rate-limit. Given the same input, it produces the exact same result every time. Never use an AI model 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 prompt. Because the model predicts words by probability, two identical prompts can yield slightly different phrasing—and sometimes a hallucination (stating something false with confidence).

To keep AI features reliable inside a real app, your coding-agent wraps every model call in deterministic guardrails on the backend: 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 fallback, so visitors still get their answer if the primary provider is busy.

## In your project

When designing your spec, 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 preview deployment.

## Copy for your agent

```text
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.
```

---
Source: [Agentic Makers](https://agenticmakers.app/en/learn/deterministic-vs-probabilistic) · License: CC BY 4.0
