Skip to content

fix(azure_ai): bill FLUX.2-flex edit reference images by megapixel - #42822

Draft
shreshth-berri wants to merge 6 commits into
mainfrom
litellm_lit8140_azure_flux2_reference_billing
Draft

shreshth-berri wants to merge 6 commits into
mainfrom
litellm_lit8140_azure_flux2_reference_billing

Conversation

@shreshth-berri

@shreshth-berri shreshth-berri commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • Azure bills FLUX.2 edits for every reference image's megapixels too
  • LiteLLM logged only the generated image, so edits were under-billed
  • Pro edits, proxy deployment prices, and router retries still had gaps

How it solves it:

  • Measure each reference's pixels from its PNG, JPEG, WebP or GIF header
  • Bill them at a new input_cost_per_reference_pixel rate on Flex and Pro
  • Price Flex per 1024x1024 megapixel, which is what Azure meters
  • Rewind reference streams so retries resend the real image and rebill it
  • Keep a proxy deployment's pixel prices on /v1/images/edits

Intentional product change: azure_ai/FLUX.2-flex generations now log $0.05 per 1024x1024 megapixel instead of $0.0524288, matching Azure's meter

User Flow

Before: an admin sending FLUX.2 edits through the proxy sees spend far below the Azure invoice

  1. They send POST http://localhost:4000/v1/images/edits with model=azure_ai/FLUX.2-flex, one 1024x1024 reference, size=1024x1024
  2. The response carries x-litellm-response-cost: 0.0524288 whatever the reference size is
  3. A 4032x3024 phone photo as the reference still logs 0.0524288, while Azure bills about $0.63
  4. A Data Zone deployment with its own input_cost_per_pixel still logs the global rate

After: the logged cost matches what Azure meters for the same edit

  1. They send the same POST http://localhost:4000/v1/images/edits
  2. The response carries x-litellm-response-cost: 0.1 (one generated plus one reference megapixel)
  3. The 4032x3024 reference logs 0.6314, and a flux.2-pro edit adds $0.015 per reference megapixel
  4. The Data Zone deployment logs its own rate, 0.11

Linear ticket

Resolves LIT-8140

Pre-Submission checklist

  • I have added meaningful tests
  • The handful of test files covering my change pass locally
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review

Screenshots / Proof of Fix

Shared setup: a proxy started with the config below, pointed at a local stand-in for the Azure BFL endpoint that answers with the same JSON shape and measures megapixels with PIL (independent of LiteLLM's parser). Live Azure runs are still to do, see Caveats

model_list:
  - model_name: flux2-flex
    litellm_params: {model: azure_ai/FLUX.2-flex, api_base: http://127.0.0.1:8765, api_key: fake}
  - model_name: flux2-pro
    litellm_params: {model: azure_ai/flux.2-pro, api_base: http://127.0.0.1:8765, api_key: fake}
  - model_name: flux2-flex-dzone
    litellm_params: {model: azure_ai/FLUX.2-flex, api_base: http://127.0.0.1:8765, api_key: fake,
                     input_cost_per_pixel: 5.245208740234375e-08, input_cost_per_reference_pixel: 5.245208740234375e-08}
  - model_name: flux2-flex-retry   # upstream returns 500 on every first attempt
    litellm_params: {model: azure_ai/FLUX.2-flex, api_base: http://127.0.0.1:8766, api_key: fake}
litellm_settings: {num_retries: 1}

Each case runs curl -s -D - http://localhost:4000/v1/images/edits -H "Authorization: Bearer $KEY" -F model=<group> -F prompt="make it blue" -F size=<size> -F "image[]=@<file>" and reads x-litellm-response-cost. The spend log's response_cost matched the header on every request. Azure's bill uses the retail price API (productName eq 'Azure BFL Flux Models', Global Standard): Flex $0.05/MP, Flex Ref $0.05/MP, Flux 2 Pro Initial MP $0.03, Flux 2 Pro MP $0.015, Flux 2 Ref MP $0.015, with 1 MP = 1024x1024

Before (d33d36c)

group size references x-litellm-response-cost Azure bill
flux2-flex 1024x1024 1024x1024 png 0.0524288 0.1000
flux2-flex 1024x1024 1024x1024 png + 640x640 webp 0.0524288 0.1195
flux2-flex 1024x1024 4032x3024 jpeg 0.0524288 0.6314
flux2-flex 640x640 4032x3024 progressive jpeg + 640x640 lossless webp + 512x512 gif 0.02048 0.6330
flux2-pro 1024x1024 1024x1024 png 0.04 0.045
flux2-pro 1024x1024 1024x1024 png + 4032x3024 jpeg 0.04 0.2194
flux2-flex-dzone 1024x1024 1024x1024 png 0.0524288 0.11
flux2-flex-retry 1024x1024 1024x1024 png 0.0524288, and the retry sent an empty input_image 0.1

After (2fec04a)

group size references x-litellm-response-cost Azure bill
flux2-flex 1024x1024 1024x1024 png 0.1 0.1000
flux2-flex 1024x1024 1024x1024 png + 640x640 webp 0.11953125 0.1195
flux2-flex 1024x1024 4032x3024 jpeg 0.6313965 0.6314
flux2-flex 640x640 4032x3024 progressive jpeg + 640x640 lossless webp + 512x512 gif 0.6329590 0.6330
flux2-pro 1024x1024 1024x1024 png 0.055 0.045 (see Caveats)
flux2-pro 1024x1024 1024x1024 png + 4032x3024 jpeg 0.2294189 0.2194 (see Caveats)
flux2-flex-dzone 1024x1024 1024x1024 png 0.11 0.11
flux2-flex-retry 1024x1024 1024x1024 png 0.1, and the retry resent the full image 0.1

Generations with no references: Flex 1024x1024 goes from 0.0524288 to 0.05 and 640x640 from 0.02048 to 0.01953125. Pro stays at 0.04 and the Data Zone deployment stays at 0.055. The 4032x3024 reference measuring 11.628 MP here agrees with the request_meta.input_mp of 11.63 that Azure returned for the same size in the live run on #43094

Type

🐛 Bug Fix

Caveats (if any)

Medium

  • Pro's generated image still uses the flat $0.04 per image price
    • Azure meters $0.03 for the first MP plus $0.015 per extra MP
    • So a Pro edit logs $0.01 above Azure per generated image, a follow-up
  • No live Azure run yet, the proof above uses a local stand-in
  • Azure's megapixel metering is assumed proportional, not rounded up per image
    • If Azure rounds up, sub-megapixel and non-square references under-bill by the gap
    • Settle it with a live 1024x1280 edit against the cost-analysis row

Low

  • A reference whose header can't be parsed (bmp, tiff, heic, truncated) is billed as 1 MP
  • The deployment-price fix applies to every provider's image edits on the proxy
    • They now bill a deployment's own prices, the way generations already did
  • The cost map change reaches older LiteLLM versions through the hosted cost map
    • Older versions ignore the new key and only pick up the Flex rate fix
  • azure_ai/FLUX.1-Kontext-pro has no reference meter on Azure, so it stays flat
  • Terraform drops an explicit 0 for input_cost_per_reference_pixel, same > 0 guard as input_cost_per_pixel

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

Shreshth Kharbanda added 4 commits September 23, 2026 13:30
The Flex row stored 0.05 / 1,000,000 per pixel, so a 1024x1024 image billed
0.0524288 instead of 0.05. Azure and Black Forest Labs define one megapixel as
1,048,576 pixels, so the rate is 0.05 / 1048576, exact in float64. Tests that
pinned the old literal now derive the rate from the catalog and assert the
invariant our code owns: one 1024x1024 image prices at exactly one megapixel
…_image_dimensions

get_image_dimensions fetched or decoded its input, parsed the header, and
substituted a 300x300 default when the header was unreadable, all in one
function. Billing code needs the middle step on its own: given bytes, return
the dimensions or None, never a default. The parsers are unchanged and the
string entry point still applies the default, so its callers see no
difference. Unreadable headers used to raise TypeError or struct.error out of
the parser; the bytes entry point turns those into None
Azure meters FLUX.2 image edits twice: once for the generated pixels and once
for every pixel of the reference images sent with the edit. input_cost_per_pixel
already means the generated image's pixels in default_image_cost_calculator, so
the reference meter gets its own key. The Flex row carries it at the same
0.05 per 1024x1024 megapixel as its output rate, per the Azure retail prices
API rows for 'Azure BFL Flux Models' (Flex Megapixel and Flex Ref Megapixel,
checked 2026-09-23). Declared everywhere a deployment can set a price: the
cost map and its regenerated schema, CustomPricingLiteLLMParams, the Rust
catalog, the Terraform provider, and the dashboard's OpenAPI types
Azure meters a FLUX.2 edit for the pixels it generates and again for every
pixel of the reference images sent with it. The proxy priced the generated
image only, so an edit cost the same with one reference or three

The Flux2 edit config already reads each reference's bytes to base64 them into
the request. It now measures those same bytes with image_dimensions_from_bytes,
sums the areas, and hands the total to the response as the
reference_image_pixels hidden param. The azure_ai image cost calculator adds
that count times input_cost_per_reference_pixel, resolving the rate the same
way it resolves the generated-pixel rate: deployment pricing first, shared
catalog second. A reference whose header cannot be read contributes zero
pixels and the request goes ahead, so the fallback only ever under-bills.
Provider-reported token usage still short-circuits everything. Flat-priced
rows carry no reference rate, so their edits are unchanged
@CLAassistant

CLAassistant commented Sep 23, 2026 •

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ shreshth-berri
❌ Shreshth Kharbanda


Shreshth Kharbanda seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@codspeed

codspeed Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing litellm_lit8140_azure_flux2_reference_billing (2fec04a) with main (081f73f)

Open in CodSpeed

@codecov

codecov Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 64.10256% with 28 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...m/llms/azure_ai/image_edit/flux2_transformation.py 33.33% 26 Missing ⚠️
litellm/litellm_core_utils/token_counter.py 93.75% 1 Missing ⚠️
.../llms/azure_ai/image_generation/cost_calculator.py 95.45% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

…_flux2_reference_billing

# Conflicts:
#	litellm-rust/crates/model-catalog/src/model_info.rs
Bill azure_ai/flux.2-pro edit references on Azure's Flux 2 Ref meter
($0.015 per 1024x1024 megapixel) so Pro edits stop logging the flat
per-image price only

Read seekable reference streams from offset 0 and rewind them after, so
a router retry or fallback resends the full image instead of an empty
input_image and bills its pixels again

Bill a reference whose header cannot be parsed (bmp, tiff, heic, a
truncated file) as one megapixel instead of zero, and log a warning

Carry the deployment's custom pricing params into image edit logging so
a proxy deployment's input_cost_per_pixel and input_cost_per_reference_pixel
apply to /v1/images/edits the way they already do for generations

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants