litellm - ✅(Solved) Fix [Feature]: Token-based cost tracking for OpenAI gpt-image models at arbitrary resolutions (gpt-image-2) [1 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
BerriAI/litellm#26876Fetched 2026-05-01 05:34:38
View on GitHub
Comments
0
Participants
1
Timeline
4
Reactions
0
Participants
Timeline (top)
labeled ×3cross-referenced ×1

Error Message

But for any other resolution this lookup returns no match, the function raises Exception("Model not found in cost map"), the cost is swallowed downstream, and the request lands in LiteLLM_SpendLogs with spend = 0.

Root Cause

gpt-image-1 works only because BerriAI/litellm's cost map happens to include the 9 size-aliased entries; gpt-image-2 (and any non-standard size on gpt-image-1) does not. Adding the 9 cells for gpt-image-2 would close the standard-size case but still leaves every non-standard resolution at spend 0.

Fix Action

Fixed

PR fix notes

PR #26880: [Feature] image cost: token-based fallback when (quality, size) lookup misses

Description (problem / solution / changelog)

Relevant issues

Fixes #26876

Linear ticket

<!-- if you are an internal contributor, add the Linear ticket e.g. "Resolves LIT-1234" to magically link the Linear ticket to the GitHub PR -->

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have Added testing in the tests/test_litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem
  • I have requested a Greptile review by commenting @greptileai and received a Confidence Score of at least 4/5 before requesting a maintainer review

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

CI (LiteLLM team)

CI status guideline:

  • 50-55 passing tests: main is stable with minor issues.
  • 45-49 passing tests: acceptable but needs attention
  • <= 40 passing tests: unstable; be careful with your merges and assess the risk.
  • Branch creation CI run
    Link:

  • CI run for the last commit
    Link:

  • Merge / cherry-pick CI run
    Links:

Screenshots / Proof of Fix

I've set the gpt-image-2 token-cost entry in my local environment like below.

    "gpt-image-2": {
        "cache_read_input_image_token_cost": 2e-06,
        "cache_read_input_token_cost": 1.25e-06,
        "input_cost_per_image_token": 8e-06,
        "input_cost_per_token": 5e-06,
        "litellm_provider": "openai",
        "mode": "image_generation",
        "output_cost_per_image_token": 3e-05,
        "supported_endpoints": [
            "/v1/images/generations",
            "/v1/images/edits"
        ]
    },

AS-IS

<img width="766" height="1189" alt="스크린샷 2026-04-30 오후 7 15 06" src="https://github.com/user-attachments/assets/7c7792a9-79dc-4a39-8661-1f5f31271f55" />

TO-BE

<img width="766" height="1189" alt="스크린샷 2026-04-30 오후 7 12 18" src="https://github.com/user-attachments/assets/e2dbf22d-807e-4f8a-b7a7-8ccd6d102403" />

Verified end-to-end against an internal LiteLLM proxy with the patch applied, calling gpt-image-2 at a non-standard size (2048x768).

Type

🆕 New Feature

Changes

Add a token-based fallback to default_image_cost_calculator for image-gen models that publish only per-token pricing (e.g. gpt-image-2 accepting "thousands of valid resolutions").

  • New helper _image_cost_from_token_usage computes cost from usage block + per-token rates (input_cost_per_token, input_cost_per_image_token, output_cost_per_image_token, cache_read_input_token_cost).
  • default_image_cost_calculator accepts an optional image_response. When the per-image / per-pixel paths do not resolve, it falls back to the helper using the response's usage.
  • Three call sites in route_image_generation_cost_calculator forward completion_response so the fallback has access to usage.
  • Backward compatible: per-image / per-pixel paths unchanged, fallback only fires when the original code would have raised.

Changed files

  • litellm/cost_calculator.py (modified, +66/-15)
  • litellm/litellm_core_utils/llm_cost_calc/utils.py (modified, +3/-0)
  • tests/test_litellm/test_default_image_cost_calculator.py (added, +311/-0)

Code Example

"usage": {
    "prompt_tokens_details":     {"text_tokens": 22,  "image_tokens": 0},
    "completion_tokens_details": {"text_tokens": 0,   "image_tokens": 1372}
  }

---

{quality}/{provider}/{size}/{model}
  {provider}/{size}/{model}
  high/{provider}/{size}/{model_no_prov}
  {quality}/{size}/{model_no_prov}     ← e.g. high/1024-x-1024/gpt-image-1
  {quality}/{size}/{model_no_prov}     ← e.g. high/1024-x-1024/gpt-image-1
  {size}/{model_no_prov}
  {quality}/{size}/{model_no_prov}     ← e.g. high/1024-x-1024/gpt-image-1
  {size}/{model_no_prov}
  {model}
  {model_no_prov}

  {quality}/{size}/{model_no_prov}     ← e.g. high/1024-x-1024/gpt-image-1
  {size}/{model_no_prov}
  {quality}/{size}/{model_no_prov}     ← e.g. high/1024-x-1024/gpt-image-1
  {size}/{model_no_prov}
  {model}
  {model_no_prov}

---

model              | call_type         | spend   | prompt_tokens | completion_tokens
  openai/gpt-image-1 | aimage_generation | 0.24967 | 22            | 1372
  openai/gpt-image-2 | aimage_generation | 0       | 22            | 1372     ← same usage shape, lost cost
  openai/gpt-image-2 | aimage_edit       | 0       | 1962          | 5488
RAW_BUFFERClick to expand / collapse

Check for existing issues

  • I have searched the existing issues and checked that my issue is not a duplicate.

The Feature

For OpenAI gpt-image family models (gpt-image-1, gpt-image-1-mini, gpt-image-1.5, gpt-image-2, ...), prefer token-based cost calculation using the model's input_cost_per_token / input_cost_per_image_token / output_cost_per_image_token and the usage returned by the API, instead of (or as a fallback to) the (quality, size)-aliased per-image lookup in default_image_cost_calculator.

Concretely:

  • When the cost map entry resolved for a gpt-image call has token-based cost keys, compute cost as prompt_tokens × input_cost_per_token + image_input_tokens × input_cost_per_image_token + image_output_tokens × output_cost_per_image_token (mirroring openai_gpt_image_cost_calculator / generic_cost_per_token).

Motivation, pitch

OpenAI's image API responses already carry the right data for token-based costing (verified against a prod /v1/images/generations call to gpt-image-2):

  "usage": {
    "prompt_tokens_details":     {"text_tokens": 22,  "image_tokens": 0},
    "completion_tokens_details": {"text_tokens": 0,   "image_tokens": 1372}
  }

image_tokens scales with the rendered resolution, so token-based pricing naturally handles any size OpenAI accepts.

The current path in litellm/cost_calculator.py::default_image_cost_calculator resolves cost via a 7-step model-name lookup chain:

  {quality}/{provider}/{size}/{model}
  {provider}/{size}/{model}
  high/{provider}/{size}/{model_no_prov}
  {quality}/{size}/{model_no_prov}     ← e.g. high/1024-x-1024/gpt-image-1
  {quality}/{size}/{model_no_prov}     ← e.g. high/1024-x-1024/gpt-image-1
  {size}/{model_no_prov}
  {quality}/{size}/{model_no_prov}     ← e.g. high/1024-x-1024/gpt-image-1
  {size}/{model_no_prov}
  {model}
  {model_no_prov}

  {quality}/{size}/{model_no_prov}     ← e.g. high/1024-x-1024/gpt-image-1
  {size}/{model_no_prov}
  {quality}/{size}/{model_no_prov}     ← e.g. high/1024-x-1024/gpt-image-1
  {size}/{model_no_prov}
  {model}
  {model_no_prov}

For models registered in model_prices_and_context_window.json with the standard 9 (low|medium|high) × (1024-x-1024 | 1024-x-1536 | 1536-x-1024) entries (e.g. gpt-image-1, gpt-image-1.5), the standard sizes hit and per-image cost is recorded correctly.

But for any other resolution this lookup returns no match, the function raises Exception("Model not found in cost map"), the cost is swallowed downstream, and the request lands in LiteLLM_SpendLogs with spend = 0.

This is particularly visible with gpt-image-2. Per the OpenAI pricing page:

"gpt-image-2 supports thousands of valid resolutions; the table below lists the same sizes used for previous GPT Image models for comparison." ref : https://developers.openai.com/api/docs/guides/image-generation#calculating-costs

OpenAI publishes the per-image table only for the 9 reference cells; the actual billing model is token-based.

So once a caller picks any non-standard size (e.g. 768x768, 2048x2048, or any of the "thousands of valid resolutions"), the size-aliased path can never match and cost tracking silently drops to 0 — even though the response carries everything needed to compute the correct token-based cost.

Real example from our LiteLLM_SpendLogs (after registering only gpt-image-2 token-cost entry, no size aliases):

  model              | call_type         | spend   | prompt_tokens | completion_tokens
  openai/gpt-image-1 | aimage_generation | 0.24967 | 22            | 1372
  openai/gpt-image-2 | aimage_generation | 0       | 22            | 1372     ← same usage shape, lost cost
  openai/gpt-image-2 | aimage_edit       | 0       | 1962          | 5488

gpt-image-1 works only because BerriAI/litellm's cost map happens to include the 9 size-aliased entries; gpt-image-2 (and any non-standard size on gpt-image-1) does not. Adding the 9 cells for gpt-image-2 would close the standard-size case but still leaves every non-standard resolution at spend 0.

A token-based calculation path would make this provider-correct and resolution-independent.

Proposed direction

  1. In default_image_cost_calculator (or route_image_generation_cost_calculator's OpenAI branch), if the resolved cost entry has token-based keys (input_cost_per_token / input_cost_per_image_token / output_cost_per_image_token), compute via generic_cost_per_token using the response's usage block — same logic that already exists in openai_gpt_image_cost_calculator.
  2. Keep the existing per-image lookup as a fast path / explicit override when a (quality, size) entry matches, but no longer treat its absence as a hard failure — fall through to (1).
  3. As a side benefit this would simplify maintenance: instead of registering an O(qualities × sizes) cross-product per new gpt-image model, a single token-cost entry would suffice.

What part of LiteLLM is this about?

Proxy

LiteLLM is hiring a founding backend engineer, are you interested in joining us and shipping to all our users?

No

Twitter / LinkedIn details

https://www.linkedin.com/in/세환-김-a31543202/

extent analysis

TL;DR

Implement a token-based cost calculation for OpenAI gpt-image models to accurately track costs for non-standard image sizes.

Guidance

  • Update the default_image_cost_calculator function to check for token-based cost keys in the resolved cost entry and compute the cost using the generic_cost_per_token logic if available.
  • Keep the existing per-image lookup as a fast path, but allow it to fall through to the token-based calculation if no match is found.
  • Register a single token-cost entry for each new gpt-image model instead of maintaining an O(qualities × sizes) cross-product of entries.

Example

No explicit code example is provided, but the proposed direction suggests modifying the default_image_cost_calculator function to include the token-based calculation logic.

Notes

This solution assumes that the OpenAI API responses will always contain the necessary usage data for token-based costing. If this is not the case, additional error handling may be necessary.

Recommendation

Apply the proposed workaround by implementing the token-based cost calculation for OpenAI gpt-image models. This will provide a more accurate and resolution-independent cost tracking solution.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

litellm - ✅(Solved) Fix [Feature]: Token-based cost tracking for OpenAI gpt-image models at arbitrary resolutions (gpt-image-2) [1 pull requests, 1 participants]