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.