Don't let the LLM do the math

By

Tagged: Essays

Keep pricing and numbers deterministic in AI products. Let the LLM write prose only. A hybrid architecture from a real deployment advisor.

When I built an app that recommends hosting stacks and estimates what they cost, I set one rule from the start: the LLM never picks a price.

It never ranks a stack or calculates monthly costs at 10k DAU either. Its only job is writing the explanation.

LLMs are bad at arithmetic

If you ask a model what Railway charges for 50GB of egress, you’ll get a confident answer that is often wrong or six months out of date. Ask it to multiply overage rates across three services and add them up, and it will drift, round things poorly, or miss a tier boundary altogether.

No amount of prompting fixes this, because models generate plausible text rather than exact ledger math. On top of that, pricing pages change and free tiers shrink, so you can’t build a product on whatever numbers the model happened to memorize during training.

The hybrid architecture

I split the recommendation engine into three layers:

  1. Curated data in SQLite. Providers, services, and plan tiers live here with effective_from dates, so I can update pricing without redeploying code.

  2. Deterministic scoring in TypeScript. This layer filters by capability fit, assembles candidate stacks, and runs the cost model in integer cents from start to finish to avoid floating-point bugs.

  3. LLM as presentation. Given the stacks and numbers the code already computed, the model writes the strengths, tradeoffs, and gotchas.

When a branch calls for judgment, like handling an angry support message or picking a route from messy text, a decision model like Jev makes sense where an if statement falls short. Pricing is not one of those cases, so I keep it in plain code.

Here is how a request moves through the app:

Questionnaire → rules engine → cost model → ranked stacks → LLM rationale → report page

The prompt hands the exact numbers to the model and tells it not to touch them:

return `You are writing rationale prose for a stack recommendation tool.
The rules engine already computed candidate stacks with exact costs.
Your job is ONLY to explain — never invent or alter any numbers.`

Even if the model invents a number anyway, the UI ignores it and renders every cost directly from the engine’s JSON output.

The cost model stays pure

costModel.ts has no network calls, no fetch, and no environment variables. You pass it tier definitions and a usage profile, and it returns the monthly cost in cents, or null if a hard limit is exceeded:

function computeTierCostCents(tier, usage) {
  let total = tier.basePriceCents

  for (const unit of ALL_USAGE_UNITS) {
    total += overageCostCents(tier, unit, usage)
  }

  return total
}

The same function runs on the server when generating a report and in the browser when you drag a tier slider on the canvas. Having one pure function means I can unit test every edge case without worrying that “the AI said $47” on one run and $52 on the next.

Graceful degradation

I wanted the engine to work on its own, with the LLM acting as an optional layer on top. When no API key is configured, getAdapter() returns null, and reports still generate, stacks still rank, and costs still compute. You only lose the rationale paragraph, and if free-text prefill isn’t available, the form works manually.

Locally I run Cursor’s SDK through a dev bridge, while production uses a plain fetch adapter against an OpenAI-compatible endpoint. Both implement the same LlmAdapter interface, so swapping providers doesn’t touch the engine.

Testability is the real win

If an LLM owned pricing, every run could return a slightly different figure and your test suite would be flaky. With the math in TypeScript, I test the engine against fixtures: given a specific questionnaire and knowledge-base snapshot, I expect the exact same three candidates in the same order with the same cent values.

For the LLM output itself, I validate the JSON with Zod. If the response comes back malformed, I retry once and then fail silently, since the report is still useful without the prose block. You can also cache the rationale in KV by input hash, so identical questionnaires and candidates return the same explanation without paying for another model call.

What this means for your AI product

Whenever your product outputs numbers people will act on, like prices, scores, rankings, or compliance flags, compute them in code.

Use the LLM for:

Keep it out of:

Maintaining a data layer and writing a real cost function takes more work up front, but you get numbers users can rely on and an app that keeps working even when the model provider goes down.

Want me to talk about your product? You can sponsor this site.

Related posts about essay: