fix(fal_ai): price gpt-image-2 per size and quality from request params - #37751
Conversation
Greptile SummaryThe PR adds request-sensitive fal.ai GPT Image 2 cost calculation using normalized image size and quality, while retaining flat fallback prices.
Confidence Score: 5/5The PR appears safe to merge. No blocking failure remains.
|
| Filename | Overview |
|---|---|
| litellm/llms/fal_ai/cost_calculator.py | Adds keyed size-and-quality pricing selection, provider-prefix normalization, and flat-price fallback behavior. |
| litellm/litellm_core_utils/llm_cost_calc/utils.py | Forwards transformed image-generation parameters to the fal.ai calculator. |
| model_prices_and_context_window.json | Adds canonical generation and edit prices for supported GPT Image 2 size and quality combinations. |
| litellm/model_prices_and_context_window_backup.json | Keeps the backup cost map synchronized with the canonical pricing data. |
| tests/test_litellm/llms/fal_ai/test_cost_calculator.py | Covers keyed pricing, aliases, defaults, edit pricing, fallback behavior, and image-count multiplication. |
| tests/test_litellm/llms/fal_ai/image_generation/test_fal_ai_gpt_image_2_transformation.py | Updates registry-price expectations for generation and edit models. |
Reviews (3): Last reviewed commit: "fix(fal_ai): strip provider prefix befor..." | Re-trigger Greptile
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
|
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit d5e6a0c. Configure here.
tin-berri
left a comment
There was a problem hiding this comment.
Solid fix, and well-proven — the PR body includes live before/after curl proof against the real fal API for 2 of the 96 new keyed entries (hd 1024x1024: $0.211, low 1024x1024: $0.006), both matching exactly. Cost-calculator selection logic correctly mirrors the existing FalAIGPTImage2Config.map_openai_params key shapes (image_size dict → WIDTH-x-HEIGHT, quality normalization including the auto→high caveat), and every fallback path (unlisted size/quality, missing params, size="auto" on edit) degrades to the flat entry rather than $0 or a crash — confirmed by dedicated tests. No regression risk to other fal models or other providers' cost paths.
One thing worth a quick sanity check before/after merge: fal_ai/high/1920-x-1080/openai/gpt-image-2/edit is priced identically to its generation counterpart ($0.158), where every other size/quality combination shows a small nonzero edit increment (+$0.006 to +$0.013) for the extra input-image cost. Could be a genuine copy-paste slip in transcribing fal's price table — worth a 30-second look, but even if wrong it undercharges by a few cents on that one row, same direction as the bug this PR fixes and far smaller magnitude. Not a blocker. CI green (70/70, including validate-model-prices-json). Approved.
TLDR
Problem this solves:
How it solves it:
User Flow
Before: a developer generating a high-quality 1024x1024 image with fal gpt-image-2 sees $0.145 in spend logs while fal bills them $0.211
{"model": "fal_ai/openai/gpt-image-2", "prompt": "a red bicycle", "size": "1024x1024", "quality": "hd"}x-litellm-response-costheader reads0.145After: the same request is logged at fal's actual $0.211 price
{"model": "fal_ai/openai/gpt-image-2", "prompt": "a red bicycle", "size": "1024x1024", "quality": "hd"}x-litellm-response-costheader reads0.211Relevant issues
Follow-up to #37729, which added fal gpt-image-2 support with a single flat price
Linear ticket
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/test_litellm/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more@greptileaito re-request a review after pushing changes)Delays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
Both legs boot a real DB-backed proxy from the exact commit under test (own Postgres, random free ports,
LITELLM_LOCAL_MODEL_COST_MAP=True) and hit the real fal API with the same two requests, differing only in the commit the proxy was booted fromBefore (987478a)
Tree proof
Image (verified live, image/png): https://v3b.fal.media/files/b/0aa72a6b/eQv-lG5O96zx7i7Qn6qhW_SHEPbKvK.png
{"request_id": "46c6dc33-d1ab-4cb7-bc81-b905ab44f0b1", "call_type": "aimage_generation", "spend": 0.145, "model": "fal_ai/openai/gpt-image-2", "custom_llm_provider": "fal_ai", "api_base": "https://fal.run/openai/gpt-image-2", "status": "success"}Image (verified live, image/png): https://v3b.fal.media/files/b/0aa72a7b/OyUU7fZeWk4EtaIydEEVB_bq4RstLp.png
{"request_id": "aa6ba115-5b75-48d0-b487-46d8327e39d2", "call_type": "aimage_generation", "spend": 0.145, "model": "fal_ai/openai/gpt-image-2", "custom_llm_provider": "fal_ai", "api_base": "https://fal.run/openai/gpt-image-2", "status": "success"}Both qualities billed the flat $0.145: the low image is overcharged 24x and the hd image undercharged 31%, even though fal really rendered different qualities upstream
After (d5e6a0c)
Tree proof
Image (verified live, image/png): https://v3b.fal.media/files/b/0aa72b03/5BWQE6p0ULdTRT1BcrvwP_8zGtRjC9.png
{"request_id": "7daa6ad4-76b1-46bc-8474-638091e70a1c", "call_type": "aimage_generation", "spend": 0.211, "model": "fal_ai/openai/gpt-image-2", "custom_llm_provider": "fal_ai", "status": "success"}Image (verified live, image/png): https://v3b.fal.media/files/b/0aa72b13/W5u_yHg0GF1VIFxdEDCSY_2PLUZKpz.png
{"request_id": "476058c0-b355-41ba-8de3-41f99114a9fc", "call_type": "aimage_generation", "spend": 0.006, "model": "fal_ai/openai/gpt-image-2", "custom_llm_provider": "fal_ai", "status": "success"}The hd request is billed $0.211 and the low request $0.006, matching fal's price table, and the totals agree end to end:
Notes from the runs, all pre-existing behavior this PR leaves alone:
Type
🐛 Bug Fix
Caveats (if any)
autois priced as high, fal's documented defaultFinal 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
d5e6a0c passes /live-pr-risk
Note
Medium Risk
Changes spend/cost calculation for fal gpt-image-2 (including edit), so logged spend and budgets can shift vs the previous flat $0.145 rate. Unlisted sizes still fall back to the flat entry.
Overview
Prices fal.ai
gpt-image-2(and/edit) from the request's quality and size instead of always charging the default flat per-image rate.The fal cost calculator now looks up keyed cost-map entries
fal_ai/{quality}/{width}-x-{height}/…fromoptional_params(named fal sizes, width/height maps,quality=autoas high). Missing params or unlisted sizes still use the flat model entry. Edit fallback is corrected to $0.151. Generation and alias models keep $0.145 as the unmatched default.Reviewed by Cursor Bugbot for commit d5e6a0c. Bugbot is set up for automated code reviews on this repo. Configure here.