Skip to content

OpenAI cost calculation ignores cached input tokens (over-costs cache hits) #13104

Description

@eeshsaxena

Summary

calculateOpenAICost in core/llm/utils/calculateRequestCost.ts bills the full promptTokens at the standard input rate and never accounts for cached input tokens, so requests that hit OpenAI's prompt cache are over-costed. The Anthropic branch in the same file already handles cache tokens; the OpenAI branch does not.

Detail

OpenAI reports cached input as usage.prompt_tokens_details.cached_tokens, and prompt_tokens includes those cached tokens. Cached input is billed at a discount (for example gpt-4o cached input is half the standard input rate). The cost function charges every prompt token at full rate:

const inputCost = (usage.promptTokens / 1_000_000) * modelPricing.input;
// no use of usage.promptTokensDetails.cachedTokens

Compare calculateAnthropicCost, which reads usage.promptTokensDetails and prices cachedTokens / cacheWriteTokens at their own rates.

Effect

For an OpenAI request with cached input (common with long, stable system prompts), the reported cost is higher than the actual OpenAI charge — the cached portion is billed at full price instead of the cache-read discount.

Suggested direction

Give the OpenAI pricing table a cachedInput rate and subtract the cached tokens from the full-rate input, pricing them separately, the way the Anthropic branch does:

const cachedTokens = usage.promptTokensDetails?.cachedTokens ?? 0;
const uncachedInput = Math.max(0, usage.promptTokens - cachedTokens);
const inputCost = (uncachedInput / 1_000_000) * modelPricing.input
                + (cachedTokens / 1_000_000) * modelPricing.cachedInput;

Happy to open a PR with the cached-input rates for the models already listed if that direction sounds right.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions