Calculating LLM costs

Last updated:

|Edit this page|

How are LLM costs calculated?

PostHog calculates cost based on the number of input (prompt) and output (completion) tokens generated by specific AI models matching on the model name, $ai_model, sent in the event. We use OpenRouter's API to fetch the prices for each model whenever possible and fall back to manually setting prices for models that OpenRouter doesn't support.

For cached LLM response, we apply discounts to reflect the reduced costs. For example, cached tokens are charged at 50% of the normal cost for OpenAI models.

We also take into account the reasoning / thinking tokens for models that support it.

You can find the code for this on GitHub.

Questions? Ask Max AI.

It's easier than reading through 728 pages of documentation

Community questions

Was this page useful?

Next article

LLM analytics dashboard (beta)

The LLM analytics dashboard provides an overview of your LLM usage and performance. It includes insights on: Users Traces Costs Generations Latency It can be filtered like any dashboard in PostHog, including by event, person, and group properties. Our SDKs autocapture especially useful properties like provider, tokens, cost, model, and more. This dashboard is a great starting point for understanding your LLM usage and performance. You can use it to answer questions like: Are users using our…

Read next article