A Practical Framework for Governing AI Costs on Azure — The Hidden Cost of Generative AI on Azure
From the publisher’s feed, collected in the ZeroLabs newsroom.
Why Your Cost Dashboard Doesn’t Explain Your AI SpendEight months into running AI in production on Azure, our finance team asked a question I should have been able to answer easily.Which use case is costing the most?I opened Azure Cost Management — the same tool we’d used for years to manage cloud spend across the organisation. I expected a breakdown. By application, by team, by use case.What I got was one line.Azure OpenAI Service — $38,400.The same number finance already had from the invoice. No detail underneath it. No explanation of why it had jumped 34% the previous month.That moment was the start of a three-month journey to build what Azure doesn’t give you — a framework for actually governing AI costs, not just receiving the bill at the end of the month.This is what that framework looks like.Why Your Azure Cost Dashboard Doesn’t Work for AIBefore the framework, it’s worth being honest about why Azure Cost Management falls short — because it’s not a criticism of the tool. It’s a tool that was designed for a different problem.Traditional cloud cost management works because resources consume predictably. A virtual machine running 24 hours costs roughly the same 24 hours every day. Storage costs per gigabyte. Costs attach to resources, resources attach to tags, tags attach to teams. It works.Generative AI breaks every one of these assumptions.Token consumption is non-linear. A simple FAQ query costs fractions of a penny. A complex multi-document reasoning task through the same endpoint costs pounds. Azure sees one resource. It doesn’t see those two completely different transactions.AI agents multiply the problem. When an AI agent handles a task, it doesn’t make one call to your model — it might make 10, 20, or 50 calls, each one carrying an ever-growing conversation history. One user interaction that feels instant could be 15 LLM calls under the hood. Azure Cost Management sees none of that structure — just aggregate token consumption.Cost doesn’t attach to tags. You can tag an Azure OpenAI resource with a team name. That tag tells you who owns the resource. It says nothing about which application called it at 3pm Tuesday, which agent run was responsible for the spike on the 15th, or which business unit’s users are driving the most expensive queries.This is not a solvable problem with better tagging. It requires a different architecture for cost visibility — one that sits at the inference layer, not the resource layer.Why Azure AI Cost Is Harder Than Normal Cloud CostWith regular cloud services — servers, storage, databases — the cost is fairly predictable. More users, more cost. It scales roughly in line with what you’d expect.AI is different. Here’s why:The same AI endpoint can cost very different amounts for different tasks. A simple “what are your opening hours?” question costs fractions of a penny. A complex query that asks the AI to read three documents, compare them, and write a summary could cost several pounds — through the same endpoint, in the same second. Azure sees one resource. It doesn’t see those two very different transactions.You can’t tag individual conversations. In normal cloud, you tag a server with a team name and Azure tells you that team’s cost. With AI, every call from every team goes through the same resource. The tags on the resource tell you who owns the resource — not who’s using it and for what.AI agents make things worse. When an AI agent is working on a task, it might make 10, 20, sometimes 50 calls to the AI service to complete one job. Each call costs money. If the agent runs more than you expected, the cost multiplies fast.The Two Pricing Models You Need to UnderstandBefore anything else, you need to understand how Azure OpenAI bills you.Pay-as-you-go (Consumption): You pay for every token you use. A token is roughly three-quarters of a word. You get billed per million tokens, at different rates for different models. No upfront cost. Good when your usage is variable or unpredictable.Provisioned Throughput Units (PTU): You pay upfront for a reserved block of AI capacity. Think of it like renting a dedicated lane on a motorway rather than paying per journey. Much cheaper per token if you’re using it consistently. Expensive if you’re not, because you pay whether you use it or not.The trap most teams fall into: They buy PTUs based on how much they used during the pilot. Pilots run at steady, predictable volumes. Production doesn’t — there are quiet nights, busy Monday mornings, seasonal spikes. Many teams end up with PTU reservations sitting at 40–50% utilisation, which means they’re actually paying more per token than if they’d stayed on pay-as-you-go.The fix: Use a mix. PTU for your steady baseline — the volume you’re confident you’ll always hit. Pay-as-you-go for anything above that. Azure OpenAI supports this natively. It’s called overflow routing. Set it up so when your PTU capacity is full, extra requests automatically go to consumption pricing.The Azure Agent Cost Traps We HitWe were using Azure AI Agent Service — Microsoft’s managed agent platform within Azure AI Foundry — which simplifies a lot of the orchestration. But it doesn’t solve the cost problem. It just wraps it differently.The runaway loop. An agent that hits an unexpected error can get stuck in a retry loop, calling Azure OpenAI repeatedly trying to resolve the problem. We had one agent run that made 94 LLM calls for a single interaction before timing out. Each call was carrying more context than the last. That one interaction cost more than a hundred normal interactions combined. We didn’t know it happened until we saw the token logs.Tool call inflation. Azure AI agents can call tools — search, database queries, API calls. Each tool call that returns a large result gets injected into the agent’s context. If an agent calls a search tool that returns a 10-page document, that document is now in the context for every subsequent LLM call in that run. We had one agent that was retrieving far more data than it needed for its decisions, inflating every downstream call.Context that never gets pruned. In a multi-turn conversation where an agent maintains state across interactions, the context grows with every turn. By conversation turn 15, the agent might be sending 20,000 tokens of history for a question that only needs the last three turns to answer. Azure AI Foundry’s thread management helps, but it doesn’t automatically prune context — that’s your responsibility to design.Seven Things We Built to Fix ItOnce we understood the problem, we stopped relying on what Azure gave us natively and built the visibility and controls ourselves.A gateway with proper logging. We deployed Azure API Management in front of our Azure OpenAI endpoints. Every AI call — whether from a simple chatbot or an agent mid-run — passes through APIM, which logs the application name, agent ID, run ID, step number, model, input tokens, and output tokens to Azure Monitor. The step number and run ID were the additions that made agents visible. We could now see not just that an agent ran, but how many steps it took and how much context it was carrying by the end.A cost dashboard that shows agent-level spend. With token data per agent run in Log Analytics, we built a Power BI report that shows average cost per agent interaction, not just total spend. When we first ran this, we found that our most-used agent was costing an average of £0.34 per interaction. Our second most-used agent was costing £2.17. Same user-facing experience. Very different underlying behaviour. The expensive one was running nearly twice as many steps and retrieving much more data than necessary.Real-time circuit breakers for agents. In APIM, we set a maximum token budget per agent run — not per call, but per entire session identified by run ID. If an agent run accumulates more than a threshold of tokens across its calls, the next call returns a “budget exceeded” response that terminates the run. This killed the runaway loop problem immediately. We also set a maximum step count per run — if an agent makes more than 25 LLM calls for a single task, something has gone wrong and the run stops.Context pruning at the agent layer. For agents maintaining state across multi-turn conversations, we implemented a summarisation step every five turns. Rather than passing the full conversation history forward, the agent calls a cheap model (GPT-4o mini) to summarise the last five turns into a compact summary. That summary replaces the raw history in subsequent calls. Context stayed manageable. The cost of the summarisation step was tiny compared to the cost it was preventing.Tool output filtering before context injection. Before any tool result gets injected into the agent’s context, we added a filtering step that extracts only the relevant portions. A search result that returns three pages of text becomes three bullet points by the time the agent sees it. This was the single highest-impact change we made for agent cost — it reduced the average context size per agent run by 61%.Step budgets per agent type. Different agent tasks have different expected step counts. A document classification agent should complete in 3–5 steps. A research and synthesis agent might legitimately take 15. Define expected step ranges per agent type, and flag — or terminate — runs that exceed them. This is different from the circuit breaker: the circuit breaker stops runaway loops, the step budget catches agents that are working harder than they should be.Model routing by complexity. Not every query needs GPT-4o. GPT-4o mini is roughly 15 times cheaper per token and handles the majority of routine tasks — standard questions, simple classifications, common responses — with no meaningful quality difference for those use cases. Sort your use cases: simple, standard, complex. Route through APIM to the appropriate model deployment. In our deployment, this reduced inference spend by 44% in the first month.Semantic caching for repeated queries. Customer-facing AI applications get asked the same questions repeatedly. APIM’s semantic caching capability (backed by Azure Cache for Redis) returns cached responses when a new query is sufficiently similar to something already answered. We hit a 31% cache rate on our customer service application — roughly a third of inference calls now cost nothing.Current State of Azure AI Cost GovernanceAzure is investing in this problem. Azure AI Foundry’s project-level tracking and AI Tracing are genuinely useful additions. The gap between what the platform provides and what enterprise finance teams need is narrowing.It has not closed. The chargeback attribution, the real-time guardrails, the agent-level controls, the model routing logic — these still need to be built. The build is maybe three to four weeks for a team that hasn’t done it before. The savings are immediate.The enterprises that are managing Azure AI spend well are not the ones with the most sophisticated optimisation architecture. They are the ones who built visibility first and made decisions from actual data.Start here: Deploy APIM. Define your logging schema. Get token data flowing. Build one dashboard.You will find something in that first dashboard that changes what you work on next.You can’t govern what you can’t see.A Practical Framework for Governing AI Costs on Azure — The Hidden Cost of Generative AI on Azure was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.