All posts

Guides · Sep 17, 2026

Deterministic and non-deterministic AI

Ask AI the same question twice and you can get two different answers. A script gives one. What deterministic and non-deterministic mean, and how an AI agent in a folder uses Python scripts for the numbers.

Built by Simon + Claude

You

What is 348 times 348?

The model reasons in words

I split 348 into 300 and 48.

348 × 300 = 104,400

348 × 48 = 16,704

Together that is 121,104.

Correct

The model runs a script

print(348 * 348)
121104

Same result every time, like a calculator

One question, worked out in words and calculated by a script

TL;DR

  • Deterministic means the same input gives the same output. A script is deterministic, a language model is not.
  • An AI agent can write and run a script, so the number in the answer is calculated and can be checked.
  • Take a file where you already know one total, ask your question, and ask for Python on every number.

Ask a language model the same question twice and you can get two different answers. When you want ideas, that is part of what you came for. When you want the sum of your invoices it is a reason to doubt the number, and it is also the reason an AI agent writes small scripts.

What deterministic and non-deterministic mean

Deterministic means the same input gives the same output, every time. A calculator is deterministic, and so are a spreadsheet formula and a Python script. Type 348 times 348 and you get 121,104 today and next year.

Non-deterministic means the output can vary, and it is also called probabilistic. A language model builds its answer one word at a time, and each word is drawn from a set of likely candidates. Ask again and the draw can land differently. The wording changes, and sometimes a number inside the wording changes with it. The width of that draw is called temperature, and in many models it is a setting you can change.

Variation and error are two separate things. Two different summaries of the same meeting can both be good, and an answer that comes back identical every time can still be wrong.

A language model can also hand a sum to a deterministic tool, the way you would reach for a calculator.

The language modelNon-deterministic348 × 348121,104The toolDeterministic121,104
A language model that hands the sum to a tool and gets the number back

2 + 2 = 5

You may remember AI answering that 2 plus 2 is 5 a few years ago, when the user insisted. The model had not calculated anything. It wrote the most likely reply to a user who keeps insisting.

The models are much better now, but a number in a sentence is still written the same way. An answer like "the average order is 14,350 kroner" looks precise. It does not say whether the number was calculated or just written like the rest of the sentence.

A number in the text can be argued with. A number from a script stays where it is until the data or the script changes.

A model that runs a script

Many people see a question and an answer in the chat, and nothing of what happens in between. Claude, ChatGPT, Copilot and Gemini can all write code, run it and use the result in the answer. In Claude the setting is called Code execution and file creation. The number in the answer then comes out of a programme that ran on your file.

Your questionPlain wordsThe modelWrites the scriptCan varytotals.pySums every rowSame every timeThe modelExplainsCan vary
Your question, the script the model writes, and the result it explains

You do not have to write "use Python" every time. Upload a spreadsheet, ask how sales developed through the year, and the model will usually decide on its own that this is a job for code. When the numbers matter, I say it anyway.

You

Use Python for the calculations, and show which rows and columns went into each number.

This does not make the whole AI agent deterministic. It can still choose a different method next time, or explain the result in other words. The sum itself is fixed.

The Python toolbox

You do not need to learn Python for any of this, as long as you know what you can ask for. Python is useful because of its libraries, finished pieces of code that each do one kind of job. The AI agent picks the library the same way it picks the method, and you never have to name one.

Every library in the figure is on Anthropic's list for the code execution tool in the Claude API, and the Claude apps use common packages like these too. Claude Code on your own machine can install whatever the job needs.

Reading and counting

What has helped us is a simple split. The model reads, sorts and explains, and anything that is counted, summed or compared goes through a script.

Take 1,000 comments from a customer survey. Finding the themes is reading. "Hard to get started", "unclear instructions" and "did not know what to do first" may be one theme in three wordings, and a language model is very good at seeing that. Ask it for one row per comment, with an id, the original text and the themes it chose.

How many comments fall under each theme is counting. You would not ask a colleague to count 1,000 rows in their head when they have a spreadsheet open, and the same goes for a model. The script counts the saved tags and works out the shares.

You now have two things to check, and they are checked in different ways. Read a sample of the rows to see whether the comments were tagged the way you would have tagged them. Hold the number of rows the script counted against the number of comments you started with. A script can count a theme perfectly when the model has put the comments in the wrong one.

Cleaning and reshaping data

An export is rarely ready to count. The same customer is spelled three ways, dates come in three formats, and an amount reads "8,900 kr" where the script expects a number. Sorting this out is called data wrangling, and it suits a script because the same rule has to hit every row.

It is mostly three kinds of work.

  • Clean. Remove duplicates, make three date formats one, turn "12 500,00" into a number, and trim the spaces nobody can see.
  • Reshape. Stack forty monthly files into one table, turn a column per month into a row per month, or split one column into two.
  • Join. Match the orders with the customer list on customer number, and list the rows that found no match.

The split from the section above still holds. Deciding that Nordvik AS and Nordvik A/S are the same customer is a judgement the model can make with you, and applying that decision to 10,000 rows is the script.

You

Clean this export with Python. Leave the original untouched and save a clean copy next to it. Tell me how many duplicates you removed, which formats you changed, and which rows you could not make sense of.

Rows the script cannot make sense of should be flagged for you, never deleted.

Deterministic is not the same as correct

A script does what the code says, and the code can say the wrong thing. It can read the wrong column, count the same customer twice or compare a full month with half of one. The result is identical on every run and still wrong for your question.

The question that catches most of this is what one row represents. If a row is an order line and you thought it was an order, every average after that is off.

Correct numbers also do not explain themselves. A chart that shows satisfaction falling does not show why. Ask the AI agent to keep three things apart in its answer, what was calculated, what might explain it, and what the data cannot tell you.

Scripts in the folder

In a chat the script is written, run once and gone with the conversation. With an agent in a folder it is a file, and it stays.

Besides cleaning, most scripts in a folder like this do one of three jobs.

  • Count. Read the month's export, sum revenue per month and product, and write the table to a file.
  • Check. Compare the totals in the report with the totals in the source, look for duplicates and empty cells, and stop the work when something does not match.
  • Convert. Draw the chart, fill the numbers into the PowerPoint template, or merge the PDFs.

The first time, you work the method out together with the AI agent, and that part is as non-deterministic as any other conversation. When the script is right, ask the AI agent to save it.

You

Save this as totals.py in the scripts folder. Add a line to CLAUDE.md that says monthly totals always come from this script, never from reading the file.

Next month the AI agent finds the instruction, runs the same script on the new export, and spends its own effort on what changed and why. In the skills guide we wrote that the markdown file is the brain and the script is the hands. This is the same division inside one folder, and a script that has run three months in a row is a good candidate for a skill.

Rules a script enforces

The same split works for rules. An instruction in CLAUDE.md is read by a language model, so it is followed almost every time, and a script is followed every time.

The folder behind this website has an example. The instructions say the AI agent drafts the newsletter and I press send. A script checks before every push that no code in the folder can send to the list. Claude Code can run scripts the same way, at fixed points in the work, and calls them hooks.

The same analysis every day

A script is also cheap to run. When the model reads a file itself, every row passes through its context window and is paid for in tokens, and tomorrow it is paid for again. When the AI agent runs a script, the script reads the rows, and the model sees the command and a few lines of result.

The first day is an investment. You and the AI agent work out the method, write the scripts and set up the folder, and that costs both time and tokens. Once the system is in place, a scheduled task can run the same analysis every morning on very few tokens. A task that needs a folder on your own machine runs there, so the computer has to be awake with the app open. The tokens go to what needs judgement, such as explaining why Tuesday looks different from Monday.

Try it on a file you know

Pick an export where you already know one number, last month's total or the number of customers. Use only data you are allowed to share with the tool. Upload it, or open the folder in Claude Cowork, and ask your actual question.

You

I want to understand how sales developed this year. Start by telling me what one row represents, which columns there are, and whether anything is missing or duplicated. Then suggest a simple analysis. Use Python for every number, and say so if you could not run the code.

Look for a sign that the code actually ran, because a code block in the answer does not prove it. Compare one number with the one you already knew. If it matches, ask for the script as a file and put it in the folder.

How to build AI capital is about scripts, instructions and files that keep working after the first use.