keepcalculator

06 Numbers and Powers

Monitor AI Inference Costs & Catch Waste

Card No. 06·55355 · Updated · Standard formula, shown below

This guide shows you how to track the real cost of running AI models in production, so you can spot inefficient workflows before they blow your budget. You’ll set up a simple monitoring loop, define cost per inference, and learn to identify the usual culprits—oversized models, needless retries, and idle GPU time.

What You’ll Need

  • Access to your inference logs (API gateway logs, cloud provider logs, or your own server logs).
  • A spreadsheet or a basic dashboard tool (Excel, Google Sheets, Grafana, or even a text file).
  • Knowledge of your cloud provider’s pricing per token, per hour, or per request.
  • Permission to add a small piece of code or a logging middleware (if you need to capture custom metrics).

Step-by-Step

  1. Define your cost unit. Decide what you’re measuring: cost per 1,000 tokens (for LLMs), cost per request (for image or audio models), or cost per hour of GPU usage. Write it down and stick to it.
  2. Collect raw usage data. Export your inference logs for a representative period—at least one full business week. Include timestamps, model name, input/output token counts, latency, and any error codes.
  3. Calculate cost per inference. For token-based pricing: (input tokens × input price) + (output tokens × output price). For per-request pricing: just multiply the request count by the price. For GPU-based: multiply the GPU hours by the hourly rate.
  4. Add a unique request ID. If you don’t already have one, add a UUID or a counter to each request. This lets you tie a specific cost to a specific user action or workflow step.
  5. Tag your workflows. In your logs, add a field like workflow_name or feature. Common tags: “chat”, “summarization”, “embedding”, “batch”, “retry”. This is the single most important step for catching waste.
  6. Build a daily cost report. Sum the cost per tag per day. You can do this in a pivot table or a simple SQL query. Look for any tag that is growing faster than your user base.
  7. Set a baseline and alert threshold. After two weeks, calculate your average daily cost per tag. Set an alert at 1.5× that average. If a tag crosses it, investigate.
  8. Monitor latency and token usage per request. High latency often means the model is too big for the task. High token counts (especially output) mean your prompts are forcing long responses.
  9. Review retry and fallback logic. Count how many requests are retried due to timeouts or errors. Each retry doubles the cost for that user action. If the retry rate is above 2%, your infrastructure is inefficient.
  10. Check for idle GPU time. If you run your own models, look at GPU utilization. If it’s below 50%, you’re paying for idle capacity. Consider batching or using a serverless provider.
  11. Automate the report. Once the manual process works, write a script that pulls logs daily and sends a summary to your team chat or email. This ensures you catch issues the day they happen.
  12. Act on the findings. For each expensive tag, ask: Is this model too large? Can we cache repeated requests? Can we reduce output tokens? Can we use a cheaper model for simple tasks?

Common Mistakes

  • Ignoring output tokens. Output tokens usually cost 2–3× more than input tokens. A model that generates 500 tokens per request is often the real cost driver, not the prompt.
  • Not tagging anything. Without workflow tags, you can only see total cost. You can’t tell if the spike is from a new feature or a bug.
  • Only looking at average cost. Averages hide outliers. One user running a 10,000-token batch every minute will be invisible in the mean but can double your bill.
  • Forgetting retries. A timeout that triggers 3 retries multiplies cost by 4. Always count retries as part of the cost of the original action.
  • Not separating batch from real-time. Batch jobs can run at off-peak hours with lower pricing. If you’re mixing them, you’re overpaying for batch work.
  • Setting alerts too high. If your alert is at 5× the baseline, you’ll only notice when the bill is already catastrophic. Set it at 1.5× and accept a few false alarms.

Tips

  • Start with the top 3 workflows by cost. Fix those first; they usually account for 80% of the waste.
  • Use a simple cost-per-user metric. If cost per active user per day rises, something changed in your app, not in your user count.
  • Cache exact duplicate requests. If the same prompt is sent twice within an hour, return the cached result. This is the cheapest win.
  • For LLMs, set a maximum output token limit in your API call. Many providers default to a high number, and the model will happily fill it.
  • Use model distillation or a smaller model for simple classification tasks. You don’t need a 70B model to detect sentiment.
  • Schedule heavy batch jobs during off-peak hours if your provider offers discounted rates.
  • Log the model version. If you upgrade a model and costs jump, you can quickly roll back.

Troubleshooting

My logs don’t have token counts. If you’re using a managed API, check the response headers—most providers include usage data there. If not, you can estimate by counting characters in the response and dividing by 4 (roughly 4 characters per token). For your own model, add a small logging wrapper that records input and output lengths.

The cost report shows a spike but I can’t find the cause. Look at the time of the spike. Was a new model version deployed? Did you change a prompt template? Did you enable a new feature? Roll back the most recent change and see if the spike disappears.

My retry rate is high but I don’t know why. Check your timeout settings. If the timeout is too short (e.g., 2 seconds for a model that takes 5), you’ll get false failures. Increase the timeout and implement exponential backoff.

I don’t have access to cloud billing. You can still monitor relative cost by tracking token counts and request volumes. Even without dollar amounts, a 3× increase in tokens per request is a clear red flag.

The alert fires too often. Your baseline may be too low. Recalculate after a month of data. Also, consider separate alerts for batch (which can legitimately spike) and real-time traffic.

I can’t change the model because the team is used to it. Run a side-by-side test with a smaller model on a subset of traffic. Measure quality (e.g., accuracy, user satisfaction) and cost. Often the smaller model is good enough for 80% of requests.

Frequently Asked Questions

What is the single most effective way to reduce inference cost? Caching repeated requests. If you can eliminate 30% of calls by caching, you cut costs by 30% immediately.

How often should I check my cost metrics? At least once a day for real-time workloads. Weekly is too slow—a runaway loop can burn a month’s budget in a day.

Is it better to use a serverless inference provider or my own GPU? For low and variable traffic, serverless is cheaper. For steady, high-volume traffic, dedicated GPUs are more cost-effective. Calculate your break-even point.

What does a healthy cost per inference look like? It varies wildly by model and task. As a rule of thumb, if your cost per request is more than 10% of your revenue per user action, it’s too high.

Should I track cost per token or per request? Track both. Per token tells you if your prompts are efficient. Per request tells you if your app is making too many calls.

Can I use open-source models to cut costs? Yes, but factor in hosting and maintenance. A self-hosted smaller model can be 10× cheaper than a large API, but you need the engineering time to run it.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →