Tracking Token Consumption and Cost
This lesson explains where tokens come from, how consumption is measured and attributed, how cost is seen in Azure, and which techniques reduce it.
Learning outcomes
After completing this lesson, you will be able to:
- Distinguish input, output, reasoning and cached tokens.
- Describe how traces attribute token use.
- Describe cost management with budgets and tags.
- Choose techniques to reduce token cost.
Where tokens come from
Every model call reports its usage: input (prompt) tokens, output (completion) tokens, which for reasoning models include reasoning tokens that the user never sees, and cached input tokens, prompt prefixes served from the prompt cache at a lower price. Output tokens usually cost several times as much as input tokens.
Input tokens are often larger than expected, because each request includes the system message, few-shot examples, retrieved passages, conversation history and tool definitions. An agent may make several model calls per user request, each with its own prompt. Cost per user request is therefore the sum of every call's tokens, priced per model.
Measuring consumption
Platform metrics show token totals per deployment, which is enough to see trends but not causes. Recording usage per call in traces, with attributes such as the feature, prompt version, user tier and model, answers the questions cost reviews ask: which feature uses the most tokens, whether a prompt change increased cost, and which customers drive spend.
response = openai.responses.create(model="chat", input=messages)
usage = response.usage
span.set_attribute("gen_ai.usage.input_tokens", usage.input_tokens)
span.set_attribute("gen_ai.usage.output_tokens", usage.output_tokens)
span.set_attribute("app.prompt_version", PROMPT_VERSION)
span.set_attribute("app.feature", "claims-summary")Instrumentation libraries for the OpenAI SDK and agent frameworks record token usage on model call spans automatically, using the generative AI semantic conventions, so explicit attributes are needed mainly for application context.
Seeing cost in Azure
Azure Cost Management shows billed cost by resource, meter and tag: token charges per model and deployment type, provisioned throughput hours and reservations, fine-tuning training and hourly hosting of fine-tuned deployments, managed compute hours, and the supporting services, such as Azure AI Search, storage and Application Insights. Budgets with alerts warn when spend approaches a limit, and tags on resources allocate cost to teams and applications.
Telemetry and billing complement each other. Billing gives the authoritative total; telemetry explains it and estimates cost per feature or per request, by multiplying recorded tokens by the published prices.
Optimization techniques
Cost falls when fewer tokens are processed, when tokens are processed at a lower price, or when idle capacity is removed. The following table lists the main techniques and their trade-offs.
| Technique | Effect | Trade-off |
|---|---|---|
| Shorter system messages and fewer examples | Fewer input tokens on every call | Must be re-evaluated for quality |
| Fewer or smaller retrieved chunks | Fewer input tokens | May reduce groundedness if too aggressive |
| Output limits and concise instructions | Fewer output tokens | Truncated answers if too tight |
| Prompt caching (stable prefix first) | Cached input tokens at a discount | Needs prompts structured with fixed content first |
| Smaller models or a model router for simple tasks | Lower price per token | Quality must be confirmed by evaluation |
| Lower reasoning effort | Fewer reasoning tokens | Less accuracy on hard problems |
| Batch deployments for asynchronous work | About half the price | Up to 24 hours turnaround |
| Response caching for repeated questions | No model call for cache hits | Stale answers if not invalidated |
| PTU reservations for steady load | Lower price for reserved capacity | Commitment for one month or one year |
| Removing idle hourly resources | No hosting or compute charges | Redeployment time when needed again |
Costs rising with flat traffic
Monthly token spend for a support assistant rose 45 percent while the number of conversations stayed flat. Traces with token attributes show that input tokens per request doubled after a release that increased retrieval from 5 to 12 chunks, and that 30 percent of requests are greetings and thank-you messages handled by the large model.
Evaluate a lower top-k, or a relevance threshold with semantic ranking, to return fewer, better chunks, and confirm that groundedness holds. Route trivial messages to a small model, or answer them without a model call. Structure the prompt with the fixed system message first to benefit from prompt caching. Track tokens per request by prompt version to confirm the effect.
Buying a PTU reservation would lower the price of the same wasted tokens, and suits steady load rather than fixing a regression. Capping output tokens would not address input growth. Moving to a cheaper region is not a pricing lever for the same deployment type.
Cost rises while request volume stays flat. Which token counts should be checked first?
Show answer
Input tokens per request, which grow with longer prompts, conversation history and retrieved context, and output tokens, including reasoning tokens, which cost more per token.
Exam Tips
Remember for the exam
- Usage reports input, output (including reasoning) and cached tokens; output tokens cost more.
- Record tokens per call in traces with feature and prompt version to explain cost.
- Azure Cost Management shows billed cost; budgets and tags control and allocate it.
- Cut tokens (prompts, retrieval, outputs), cut price (smaller models, caching, batch, reservations), remove idle capacity.