Skip to content
AI engineering6 min read

Modelling what an AI feature will cost before you build it

AI features have a cost profile most software does not: marginal cost per request that scales linearly with usage. A feature that costs a fraction of a rupee per call is fine at a thousand calls a day and a real line item at a million — and the difference is knowable before you build it.

Part ofAI & Machine Learning Engineering

Conventional software cost is dominated by fixed infrastructure: the same server serves a hundred or ten thousand requests. Hosted language models invert that. Cost tracks usage almost exactly, which makes a cost model a design input rather than a finance exercise afterwards.

Measure tokens, do not estimate them

Every projection starts with tokens per request, measured on your actual evaluation set rather than guessed from a sample prompt. Retrieved context is usually the largest and most underestimated component.

ComponentTypical shareOften forgotten
System promptFixed per callMultiplied by every request — length matters more than it looks
Retrieved contextFrequently the largest inputScales with chunks retrieved × chunk size
Conversation historyGrows within a sessionUnbounded unless you truncate deliberately
User inputUsually smallExcept for document upload or paste
OutputPriced higher than inputCapped by max tokens, but verbose output costs real money
ts
// Instrument from the first prototype. Recording usage per call is the
// only way a projection is grounded rather than assumed.
const response = await model.generate({ messages, maxTokens: 800 });

await recordUsage({
  feature: "document_qa",
  tenantId,
  model: response.model,
  promptTokens: response.usage.promptTokens,
  outputTokens: response.usage.outputTokens,
  retrievedChunks: context.length,
  cacheHit: response.cacheHit ?? false,
  latencyMs: response.latencyMs,
});

// Then the projection is a query, not a spreadsheet guess:
// median and p95 tokens per request, by feature, over real traffic.

The levers, in order of effect

Cost reduction options are not equal. These are ordered by how much they typically save relative to the effort and the quality risk involved.

  1. Retrieve less context. Passing ten chunks where four suffice inflates every single request. Measure quality against chunk count on your evaluation set — the curve usually flattens earlier than expected.
  2. Use a smaller model where evaluation permits. Capability differences are task-specific; many extraction and classification tasks show no measurable quality loss on a cheaper model.
  3. Cache aggressively. Identical or near-identical requests are more common than teams assume, particularly for document questions and summaries.
  4. Route by difficulty. Send straightforward requests to a cheap model and escalate only the hard ones, using a cheap classifier to decide.
  5. Cap output length. Verbose answers cost more and are frequently worse; a max-token limit is both a cost and a quality control.
  6. Trim the system prompt. Lower impact per token, but it is paid on every request forever.

Caching, and what is actually cacheable

There are three distinct caching strategies here and they are frequently conflated. Each addresses a different repetition pattern.

StrategyCachesBest where
Exact-matchIdentical input → stored outputRepeated identical questions; simple and safe
SemanticSimilar input → stored outputParaphrased repeats; needs a similarity threshold you have tested
Provider prompt cachingRepeated prompt prefixesLong fixed system prompts or shared retrieved context

Cache keys must include the tenant, the retrieved context version and the prompt version. A cache that survives a prompt change serves output from a system you no longer run.

Building the projection

With measured tokens, the arithmetic is straightforward and worth writing down explicitly so assumptions are visible and challengeable.

text
Per request (measured on the evaluation set):
  input tokens  (median / p95)
  output tokens (median / p95)

Per request cost = (input  × input rate)
                 + (output × output rate)

Monthly cost = per-request cost
             × requests per active user per month
             × active users
             × (1 − cache hit rate)

Then state the assumptions separately, because they are what will
turn out to be wrong:
  - requests per user per month
  - cache hit rate
  - whether p95 or median is the realistic operating point
  - provider rates, which change

Run it at three volumes: current, ten times current, and a hundred times. A feature that is comfortable at all three is safe to ship. One that breaks at ten times needs a cost design decision now, not after growth arrives.

Guardrails, because projections are wrong

No model survives contact with real usage patterns. What protects the budget is enforcement at runtime rather than accuracy in the spreadsheet.

  • Per-tenant rate limits on AI features specifically, separate from general API limits
  • A monthly token budget per tenant, with defined behaviour on exhaustion — degrade or queue rather than fail opaquely
  • Alerting on cost per active user, which catches a change in usage pattern that a total-spend alert misses
  • A hard provider-side spend cap, so a runaway loop cannot produce an unbounded bill
  • Per-feature attribution, so you can tell which feature moved the number

Where this reaches the commercial model

A feature with genuine marginal cost per request sits awkwardly inside unlimited-usage pricing. That is a commercial decision rather than an engineering one, but engineering owns the number the decision depends on.

Producing a defensible cost-per-active-user figure early lets the pricing conversation happen while the product is still flexible — rather than after launch, when the options are raising prices or absorbing the cost.

Questions

What reduces AI feature cost most?

Retrieving less context, usually by a wide margin. Passing ten chunks where four would do inflates the input tokens of every single request. Measure quality against chunk count on your evaluation set — the curve typically flattens earlier than expected, which means the extra context was costing money without improving answers.

Should I use a cheaper model?

Test it rather than assume either way. Capability differences are task-specific: many extraction, classification and routing tasks show no measurable quality loss on a smaller model, while open-ended reasoning often does. The evaluation set answers this in an afternoon, and the saving compounds on every request thereafter.

Is semantic caching safe?

Only with a conservative threshold and per-tenant scoping. Two questions can be close in embedding space with genuinely different correct answers, particularly where one contains a temporal or conditional qualifier. Exact-match caching carries no such risk and frequently captures more repetition than teams expect, so start there.

How do I avoid a surprise bill?

Enforce limits at runtime rather than relying on the projection: per-tenant rate limits and token budgets, capped retries, per-feature cost attribution, and a hard spend cap at the provider. Alert on cost per active user rather than total spend alone — that is what catches a changed usage pattern before it becomes a large invoice.

Want this applied to your systems?

Most of what is described here begins as an assessment: a fixed-scope review producing a prioritised written report you can act on with any supplier.

Prefer email? hello@kornspire.com