Models shouldn't do arithmetic.
Every loomcycle agent now has a calculator that is exact where it can be, and says so when it is not.
1. The number that looks right
Ask a language model what three laptops at 1,249.99 cost with 20% tax and you will usually get the right answer. That is the problem. A model does arithmetic the way it writes prose: one likely token after another. When it slips, it slips by a little, and the wrong total looks exactly like a right one. A tax figure that is off by a cent, or a deadline that is off by one working day, is worse than no figure at all, because nobody checks it.
So between November 21 and 24 we gave every loomcycle agent a tool called Calc. The idea is simple. The model writes the formula. The runtime computes it.
The model writes the formula. The runtime computes it.
2. One call, one expression
Calc takes an expression and a few optional fields. The important ones are expr (the formula) and vars (the figures from the conversation, bound to names so nothing gets retyped wrong):
{"expr": "round(sum(hours .* rates), 2)", "vars": {"hours": [7.5, 8, 6.25], "rates": [42, 42, 55.5]}}
And the answer:
{"result": 997.88, "type": "number", "exact": true, "text": "997.88"}
result is the value as JSON, text is the same value written for reading, and the model is told to quote text. The field we care most about is exact.
Calc keeps numbers exact wherever the mathematics allows it. Decimals are stored as exact fractions, so 0.1 + 0.2 is 0.3, not the 0.30000000000000004 you get from ordinary floating point. Integers keep every digit: 2^100 comes back as 1267650600228229401496703205376. Literals, + - * /, integer powers, rounding and the integer functions all stay exact.
Some things cannot be exact. A square root that does not come out even, exp, log, trigonometry and the constants like pi give a 64-bit float with about 16 significant digits, and anything computed from one stays approximate. Calc does not hide that. sqrt(2)^2 == 2 is false, and the result says "exact": false. For money, the guidance in the help is short: keep it exact and round once at the end with round(x, 2).
3. Money and decimals
Here is the invoice from the top of this post, as Calc sees it:
{"expr": "subtotal = 1249.99 * 3; tax = round(subtotal * 0.2, 2); subtotal + tax"}
{"result": 4499.96, "type": "number", "exact": true,
"assigned": {"subtotal": 3749.97, "tax": 749.99},
"text": "4499.96"}
The assigned field lists every name the expression set, so the intermediate steps are visible too. The tax is 749.994 before rounding; it is rounded once, where the formula says, and nowhere else.
A small language detail that matters here: % in Calc is modulo, not percent. Fifteen percent of x is written x * 15 / 100. We chose that on purpose, and the help says it in bold, because "percent or modulo" is exactly the kind of ambiguity that produces a plausible wrong answer.
4. Dates and working days
Dates are where mental arithmetic quietly goes wrong. What is one month after January 31? Calc answers the way a contract usually means it, clamping to the month's end:
{"expr": "addmonths(date(\"2026-01-31\"), 1)"}
Result: 2026-02-28.
Working days are worse, because they need a calendar. Ten working days after Monday, November 23, 2026, and the ISO week that lands in:
{"expr": "due = addworkdays(date(\"2026-11-23\"), 10); [due, isoweek(due)]"}
Result: [2026-12-07, 50].
Working days count Monday to Friday, both ends included, less any holidays you pass in. Time zones work too, with the zone database built into the binary. Calc reads only ISO 8601 dates: 11/12/2026 is refused, because it means two different days depending on who wrote it.
5. Units, loans and a standard deviation
Unit conversion is a place where a model will happily remember a conversion factor that is almost right. The help tells the model never to type a factor itself:
{"expr": "round(convert(500, \"g\", \"oz\"), 2)"}
Result: 17.64. convert covers length, mass, time, temperature, area, volume, speed, force, pressure, energy, power, angle, frequency and data size, with SI and binary prefixes, so GiB and GB are different things, as they should be.
A loan payment is a formula most people half remember. Here is 250,000 at 6% a year over 30 years, paid monthly, with the total interest:
{"expr": "p = pmt(0.06/12, 360, 250000); [round(p, 2), round(-p * 360 - 250000, 2)]"}
Result: [-1498.88, 289595.47].
The payment comes back negative on purpose. The finance functions follow the spreadsheet convention: money paid out is negative, and rate is the rate per period, so a yearly rate on monthly payments is divided by 12. pmt, fv, pv and npv stay exact when every input is exact. irr and rate search for an answer, and when there is none they refuse instead of guessing: irr([1000, 2000, 3000]) has no sign change, so it has no rate of return, and Calc says domain.
Then statistics, where the classic slip is sample versus population. For the list [2, 4, 4, 4, 5, 5, 7, 9], stdev gives 2.1381 (rounded to four places), because Calc uses the sample formula by default. stdev(v, "population") gives 2. A model may pick either without saying which; Calc makes the choice visible in the call.
6. Bigger math, and tables
The first release (v1.165) covered arithmetic, units, dates, statistics and finance. The next one (v1.166) grew Calc into a numerical toolkit:
- Matrices and linear algebra. A matrix is a list of rows.
*is the matrix product,A'the transpose, andsolve,inv,det,eig,svdand the rest are there.solve([[2, 1], [1, 3]], [3, 5])gives[0.8, 1.4]. - Probability distributions and numeric calculus. Normal, t, chi-squared, F, exponential, uniform, binomial and Poisson, plus
integrate,deriv,rootandminimize.round(norminv(0.975), 4)gives1.96. - Complex numbers.
3 + 2i,polyroots, and complex eigenvalues of any square matrix. The nameion its own stays an ordinary variable, so no older expression changed meaning. - Tables. Named columns from
varsor from CSV text, withwhere,groupby,pivot,joinanddescribe, and aformatfield that writes a table as JSON, Markdown or CSV.
A small table example:
{"expr": "groupby(csv(\"region,amount\\nEU,120\\nUS,300\\nEU,80\"), \"region\", \"total\", g -> sum(g.amount))"}
gives {"columns": ["region", "total"], "rows": [["EU", 200], ["US", 300]]}. Number cells stay exact, so a column of prices sums to the cent.
v1.168 added matrix powers ([[1, 1], [1, 0]]^10 gives [[89, 55], [55, 34]]), exact matrix products when every element is exact (up to 50 rows or columns a side), and products of complex matrices.
7. Big data without the round trip
A table of thousands of rows should not pass through the model twice: once when the model reads it, and again when it copies it into a tool call. So inside a run, Calc can read and write data by reference:
{"expr": "groupby(t, \"region\", \"total\", g -> sum(g.amount))", "from": {"t": "volume:sales.csv"}, "to": "volume:by-region.csv"}
from binds a stored file, a Memory value or a Document chunk to a name. to writes the result there instead of returning it. The model gets back where the result went and how big it is, never the content: "wrote a table of 2 rows and 2 columns, 27 bytes of csv, to volume:by-region.csv".
The rule we held to: Calc reaches nothing the agent could not reach itself. Reading a volume file needs the agent's Read tool, writing needs Write, and Memory and Document references need those tools and their scope grants. The tool's hooks are asked first. The destination is checked before any source is read, so a call that could not store its result reads nothing.
8. What Calc refuses
A calculator that guesses is no better than a model that guesses, so a lot of the design is about saying no clearly. A refused call returns an error code, what went wrong, a hint about what to write instead, and the position in the expression.
- A numeric method that does not settle, such as an integral over a singularity, is refused with
no_convergence, never answered with a guess. sqrt(-1)on a real number is still refused. Pass a complex argument,sqrt(-1 + 0i), and you get1i.- A singular matrix to
invorsolveis refused, with a pointer topinvorlstsq. - A matrix plus a vector is refused rather than guessed at.
- Since v1.167,
datefmtrefuses a pattern token it does not know. Before, an unknown token like%Awas copied into the output as text, which looked like a working format until someone read it. Now the hint names the right one: the weekday name isdddd, the week number isisoweek(d). - Names that other tools use, like
pnorm,lnornetworkdays, are refused with a hint naming the Calc function.
Every call is also bounded: 2 seconds, a million steps, exact numbers up to about 4,900 digits, matrices up to 500 by 500. No setting raises the bounds. The size check for heavy matrix work comes before any work starts, because those routines cannot be interrupted once they begin.
Calc evaluates numbers. It does not do symbolic algebra; root and solve find numeric answers.
9. Outside a run, and what it costs
The same evaluator is available without an agent. The MCP server offers a calc tool (handy for checking a figure from Claude Code), there is POST /v1/_calc over HTTP and a Calc RPC over gRPC, and both client libraries have calc():
curl -s -X POST "$LOOMCYCLE/v1/_calc" -H "Authorization: Bearer $TOKEN" \
-H 'Content-Type: application/json' -d '{"expr": "0.1 + 0.2"}'
Outside a run there is no agent whose storage from and to could reach, so those two fields are refused there.
Calc is added to every agent when a run resolves its tools. It is never written into a stored definition, so no content hash changed. Keeping it costs its description and schema in each model call, roughly 320 to 330 input tokens, cached where the provider caches tool definitions. A single-purpose agent that never calculates, like a classifier or a judge, can drop it with -Calc in its tools list. For any agent that converses or answers questions, we leave it on.
We tested the help the way a model reads it. Every example in the help articles runs through the tool in a test and is compared to the digit, so the documentation cannot drift from what Calc does. A measurement on 62 tasks found Haiku 5.5 answered all 62 correctly.
The point of Calc is not that models are bad at math. It is that a number you cannot check is a number you should not trust, and a formula is something you can check. If your agents quote figures, try them on v1.168 and look at the exact field. The full guide is in docs/CALC.md.