AI Cost Control in Production: What Nobody Tells You Before You Ship

Photo by Jametlene Reskp on Unsplash
The bill arrives before the product does
When I was building the first AI features for CVChatly, I did what most product teams do: I picked a model, wired up the API, got impressive results in testing, and felt good about the direction. Then I looked at the projected cost at scale. The number was not catastrophic, but it was high enough to make me stop and redesign the architecture before a single real user had touched it.
This is the moment most teams miss. You build the demo, the demo works, and the demo is cheap because nobody is actually using it. Then you ship, usage grows, and the cost curve does something you did not plan for. I have seen this pattern in early-stage startups and in large public-sector programmes, and the shape of the problem is the same in both. AI cost control is not a DevOps afterthought. It is a product and architecture decision you need to make before you go live.
What follows is what I have learned from shipping two AI-powered MVPs in under four months each, and from rebuilding parts of both when the economics did not work as expected.
Where the money actually goes
The first thing to understand is that LLM costs are not a single line item. They come from at least four places, and most teams only watch one of them.
Token volume
Every call to an LLM costs tokens: input tokens for the prompt and context you send, output tokens for the response you get back. Input is usually cheaper than output, but both add up fast. The hidden driver is context length. If you are building an agent that accumulates a conversation history, or a retrieval-augmented system that stuffs documents into the prompt, your input token count grows with every turn. A conversation that starts at 500 tokens can be at 8,000 tokens by turn ten. Multiply that by thousands of users and you have a cost problem that scales with engagement, which is exactly the opposite of what you want.
Model selection
The most capable model is rarely the right model for every task. I use different models for different jobs in CVChatly: a smaller, faster model for classification and routing decisions, a mid-tier model for most generation tasks, and the most capable model only for the cases where quality genuinely requires it. The cost difference between tiers can be an order of magnitude. Using a frontier model to decide whether a user's input is a question or a command is like hiring a senior architect to sort your post. It is technically fine and economically absurd.
Call frequency and retry logic
Agents make multiple calls. A task that looks like one user action can trigger a chain of LLM calls: one to understand intent, one to plan, one to execute each step, one to summarise the result. If your orchestration logic is not tight, you will make redundant calls. If your retry logic is not careful, you will make expensive calls on transient errors that did not need an LLM at all. I have seen agent loops that called the model six times for what should have been two calls, simply because the prompt did not give the model enough structure to complete the task in fewer steps.
Caching and duplication
Many LLM API providers offer prompt caching for repeated prefixes. If you have a long system prompt that is the same across thousands of calls, caching it can cut your input costs significantly. Most teams do not enable this by default, and many do not know it exists. Similarly, if users ask similar questions repeatedly, a lightweight semantic cache in front of your LLM layer can serve a large fraction of requests without hitting the model at all. This is not a workaround; it is standard practice in production systems.
Governance before it becomes a crisis
Cost control is not just an engineering problem. It is a governance problem, and the governance needs to be in place before you have a cost crisis, not after.
Set limits at the architecture level, not the budget level
Telling your team to be careful with API calls is not a control. A control is a hard limit on tokens per user per day, enforced in the application layer. A control is an alert that fires when your hourly spend crosses a threshold. A control is a circuit breaker that degrades gracefully to a cheaper model or a cached response when you are approaching your budget. These are engineering decisions, and they need to be on your backlog from day one, not added reactively when the invoice arrives.
Instrument everything
You cannot optimise what you cannot see. Every LLM call in production should log the model used, the token counts, the latency, and the outcome. Not in aggregate, but at the call level, so you can trace which features, which user flows, and which prompt patterns are driving cost. In my experience, cost concentration is high: a small number of flows account for a disproportionate share of spend. You will not find them without call-level telemetry.
Treat AI spend as a product metric
Cost per user, cost per completed task, cost per unit of value delivered: these are product metrics, not just financial ones. If your cost per user is rising while your retention is flat, something in your product logic is wrong, not just your budget. I track these numbers the same way I track activation and engagement, because they tell me whether the AI layer is adding value efficiently or just burning tokens.
Public sector and regulated environments add a layer
If you are running AI systems in a public-sector or regulated context, cost governance intersects with data governance in ways that complicate your options. You may not be able to use a shared semantic cache if it risks exposing one user's query to another. You may not be able to route to a cheaper third-party model if your data processing agreement only covers specific providers. GDPR, the EU AI Act, and sector-specific rules all constrain the optimisation moves available to you. This does not mean you cannot control costs; it means you need to design your cost controls with the same rigour you apply to your compliance controls, and ideally at the same time.
What I would do differently at the start
If I were starting a new AI product today, these are the decisions I would make in the first sprint, not the fifth.
- Define a cost envelope before you define features. What is the maximum acceptable cost per user per month at your target scale? Work backwards from that number to constrain your architecture choices.
- Build a model routing layer from day one. Even if you only have one model at launch, the abstraction makes it trivial to introduce cheaper models for simpler tasks later. Retrofitting routing into a tightly coupled codebase is painful.
- Write prompts as if tokens cost money. They do. Shorter, more structured prompts that give the model clear constraints produce better results at lower cost than long, discursive prompts that leave the model to figure out what you want.
- Instrument before you optimise. Do not guess where your cost is coming from. Log everything, then look at the data.
- Plan for caching on day one, even if you do not implement it immediately. Design your prompt structure so that the stable prefix is separated from the dynamic content. This makes caching straightforward when you need it.
The fundamental point
AI does not change the economics of building software; it amplifies them. A well-governed project with clear scope and real instrumentation will control its AI costs the same way it controls any other technical cost: by measuring, deciding, and enforcing. A poorly governed project will discover its AI costs the same way it discovers every other problem, which is too late and too expensively.
The teams I have seen struggle most with AI cost are not the ones who chose the wrong model. They are the ones who treated cost as someone else's problem until it became everyone's problem. Build the governance into the architecture. Set the limits before you need them. And check the numbers every week, not every quarter.