1 min read 2 wks ago

Cost Usage Calculation

Most services return the number of tokens used, and some also return the price. When a service returns nothing, AI Engine estimates the tokens and calculates the price from its own model list.

Services

Hosting ServiceStreamingTokens UsagePrice UsageNotes
OpenAI (+ Azure)No❤️ Returned by OpenAI❤️ Calculated by AI Engine
OpenAI (+ Azure)Yes❤️ Returned by OpenAI❤️ Calculated by AI Engine
OpenRouterNo❤️ Returned by OpenRouter❤️ Returned by OpenRouter
OpenRouterYes❤️ Returned by OpenRouter❤️ Returned by OpenRouterIncluded in the stream, no extra request.
Anthropic (Claude)Any❤️ Returned by Anthropic❤️ Calculated by AI Engine
GoogleAny❤️ Returned by Google❤️ Calculated by AI Enginehttps://ai.google.dev/pricing

⚠️ When the tokens are not returned: Guessed by AI Engine

This is done through the estimate_tokens function in the core. In fact, the implementation of each model could do it differently, but there is currently no way to know how each service actually calculate those tokens (since they depend on how the messages are actually formatted, how the images are counted, etc). This is highly inaccurate. If you depend on this, use something else. You can also plug in your own calculation with the mwai_estimate_tokens filter (see PHP Filters).