Skip to content

refactor(cost-map): drop the legacy max_tokens field in favor of max_input_tokens and max_output_tokens - #48471

Merged
kerry-berri merged 3 commits into
mainfrom
litellm_drop_legacy_max_tokens
Oct 27, 2026
Merged

kerry-berri merged 3 commits into
mainfrom
litellm_drop_legacy_max_tokens

Conversation

@kerry-berri

@kerry-berri kerry-berri commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

The cost map carried a legacy max_tokens field on 3,325 rows that duplicated either max_output_tokens or max_input_tokens, and five code paths read it as a fallback when the specific field was missing. This PR copies the legacy value into the missing specific field on the 231 rows that only had the legacy one, deletes the field from both bundled JSON files, and drops the fallback reads. /model/info still returns max_tokens, now derived from max_output_tokens (or max_input_tokens for rows with no output cap), so API clients see the same numbers as before

Details

TLDR

Problem this solves:

  • max_tokens in the cost map meant output cap on some rows and context window on others
  • Five reads fell back to it, in two different directions
  • New rows kept getting the field from the sync scripts

How it solves it:

  • 225 chat rows without max_output_tokens get it from the legacy value
  • 6 rows without max_input_tokens get it from the legacy value
  • max_tokens removed from every row and from sample_spec in both JSON files
  • get_max_tokens, trim_messages, OTPM reservation, MCP sampling and the Router azure fallback read only the specific field
  • get_model_info derives ModelInfoBase.max_tokens as max_output_tokens or max_input_tokens
  • The Together AI, Friendli, OpenRouter and Vercel sync scripts that wrote the field were removed on main in ci: remove the Together AI and weekly cost map sync workflows #48473, so nothing generates it anymore
  • Schema test asserts no bundled row carries max_tokens
  • Two tests that pinned the legacy field go, the OpenInfer row["max_tokens"] == row["max_output_tokens"] assertion and test_together_chat_entries_never_carry_context_length_as_output_ceiling, which forbade the shape the port creates on 71 together_ai rows

User Flow

Before: a proxy admin reading /model/info for a hosted model sees max_output_tokens: null even though the row has a cap under the legacy name

  1. They send GET http://localhost:4000/model/info with the master key, with mistral/mistral-large-4 in the config
  2. The row comes back with max_tokens: 1048576, max_input_tokens: 1048576, max_output_tokens: null
  3. They have to know that max_tokens is the output cap here, while on gpt-5.5 it is also the output cap but on an embedding row it is the context window

After: the same request returns the cap under its real name and max_tokens unchanged

  1. They send GET http://localhost:4000/model/info with the master key, with mistral/mistral-large-4 in the config
  2. The row comes back with max_tokens: 1048576, max_input_tokens: 1048576, max_output_tokens: 1048576
  3. Every row now answers max_input_tokens and max_output_tokens directly, and max_tokens keeps its old value for existing clients

Pre-Submission checklist

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

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/unit/<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
  • 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
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

Screenshots / Proof of Fix

Shared setup: proxy started with LITELLM_LOCAL_MODEL_COST_MAP=True and this config, no database

model_list:
  - model_name: mistral-large-4
    litellm_params: {model: mistral/mistral-large-4, api_key: fake}
  - model_name: gemma-2-27b
    litellm_params: {model: gemini/gemini-gemma-2-27b-it, api_key: fake}
  - model_name: gpt-5.5
    litellm_params: {model: openai/gpt-5.5, api_key: fake}
general_settings:
  master_key: sk-proof

Command on both sides, printing the three limit fields of each model_info

curl -s http://localhost:4000/model/info -H "Authorization: Bearer sk-proof" | python3 -c "
import json,sys
for d in json.load(sys.stdin)['data']:
    i=d['model_info']; print(d['model_name'], {k:i.get(k) for k in ('max_tokens','max_input_tokens','max_output_tokens')})"

Before (ccd783d)

  1. Output
    mistral-large-4 {'max_tokens': 1048576, 'max_input_tokens': 1048576, 'max_output_tokens': None}
    gemma-2-27b {'max_tokens': 8192, 'max_input_tokens': None, 'max_output_tokens': 8192}
    gpt-5.5 {'max_tokens': 128000, 'max_input_tokens': 1050000, 'max_output_tokens': 128000}
    

After (eb80d51)

  1. Output
    mistral-large-4 {'max_tokens': 1048576, 'max_input_tokens': 1048576, 'max_output_tokens': 1048576}
    gemma-2-27b {'max_tokens': 8192, 'max_input_tokens': 8192, 'max_output_tokens': 8192}
    gpt-5.5 {'max_tokens': 128000, 'max_input_tokens': 1050000, 'max_output_tokens': 128000}
    

Type

🧹 Refactoring

Caveats (if any)

Medium

  • On the 225 ported hosted rows (together_ai, deepinfra, inferenceapis, wandb, fireworks_ai and a few others) the legacy value was really the context window, so the new max_output_tokens overstates the real output cap. It is the same number get_max_tokens returned for them before, now under a truthful key, chosen over leaving the field unset so no caller sees a different value. The provider sync bots can tighten them, and the together_ai sync script still writes no max_output_tokens on new rows
  • Anyone reading max_tokens straight out of the raw JSON on GitHub loses the field. /model/info and get_model_info keep returning it

Low

  • trim_messages now indexes max_input_tokens directly. Rows without it raised KeyError before too, because they had no max_tokens either
  • A model_info: {max_tokens: X} set on a deployment is still stored by register_model but no longer feeds get_model_info's max_tokens. The proxy's /model/info merges deployment model_info on top, so the value still shows there

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

Link to Devin session: https://app.devin.ai/sessions/fbc55eed538941be81e05f1a5ff56d22
Open in Devin Desktop: https://app.devin.ai/desktop/session/fbc55eed538941be81e05f1a5ff56d22?variant=devin
Requested by: @kerry-berri

…input_tokens and max_output_tokens

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@codspeed

codspeed Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ 1 benchmark is dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 35 untouched benchmarks


Comparing litellm_drop_legacy_max_tokens (d1b7329) with main (a6fdc7c)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (8b1910c) during the generation of this report, so a6fdc7c was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@codecov

codecov Bot commented Oct 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…ax_tokens

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

# Conflicts:
#	.github/scripts/auto_update_price_and_context_window_file.py
#	scripts/sync_together_ai_models.py
#	tests/unit/test_auto_update_price_and_context_window_file.py
#	tests/unit/test_sync_together_ai_models.py
@kerry-berri

Copy link
Copy Markdown
Contributor Author

@BugBot run

@cursor

cursor Bot commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

This PR is too large for Bugbot to review. It changes 7,062 lines and 4,036,159 characters. Split the change into smaller pull requests to get a review.

@kerry-berri
kerry-berri enabled auto-merge (squash) October 26, 2026 23:00

@joshua-berri joshua-berri left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@kerry-berri
kerry-berri merged commit db1c871 into main Oct 27, 2026
104 of 105 checks passed
@kerry-berri
kerry-berri deleted the litellm_drop_legacy_max_tokens branch October 27, 2026 00:03
StudioPlatforms added a commit to StudioPlatforms/litellm that referenced this pull request Oct 27, 2026
…ap rows

Upstream BerriAI#48471 retired max_tokens in favor of max_input_tokens and
max_output_tokens; our routing-mode rows already carry both.
mateo-berri added a commit to vansh-one/litellm that referenced this pull request Oct 28, 2026
Keeps both main's new providers.json entries (refuseless, openinfer) and vispark, and drops max_tokens from the three vispark cost-map rows since main retired that field in BerriAI#48471 (max_output_tokens already carries the same value)
amaan-ai20 added a commit to zerogpu/litellm that referenced this pull request Oct 28, 2026
Upstream BerriAI#48471 retired max_tokens from the cost map in favor of max_input_tokens and max_output_tokens, and test_no_row_carries_the_retired_max_tokens_field failed on all 13 zerogpu rows. Every row already carries the same value in max_output_tokens, so nothing is lost. The zerogpu limits test now checks that each row's output limit fits its context window
mateo-berri pushed a commit that referenced this pull request Oct 28, 2026
* feat(providers): add ZeroGPU as an OpenAI-compatible provider

Registers zerogpu through the JSON provider registry so zerogpu/<model> routes to
https://api.zerogpu.ai/v1/chat/completions with ZEROGPU_API_KEY and an optional ZEROGPU_API_BASE.
Adds LlmProviders.ZEROGPU, the constants.py provider and endpoint lists, cost map entries for the
10 ZeroGPU text generation models, the endpoint matrix, the Add Model form fields, the README row
and the dashboard provider entry

* feat(zerogpu): add capability flags verified against the live API

Live calls to https://api.zerogpu.ai/v1/chat/completions confirmed tools, tool_choice and json_schema
response_format on 8 of the 10 catalog text models, cached token reporting on deepseek-v4.1-flash,
gpt-4.1-mini, gpt-5.4-nano and gpt-5.6-luna, and reasoning_effort on gpt-5.6-luna. LFM2.5-1.2B-Instruct
answers stream requests with plain JSON instead of SSE and LFM2.5-1.2B-Thinking returns 500 on every
request, so both stay out of the cost map until the API serves them correctly

* feat(zerogpu): price LFM2.5-1.2B-Instruct

Adds zerogpu/LFM2.5-1.2B-Instruct to the cost map with the catalog prices and context window. It
gets no capability flags because the live API ignores tools, response_format and
max_completion_tokens for this model, so LiteLLM drops tool params for it instead of sending them.
LFM2.5-1.2B-Thinking stays out of the cost map

* fix(zerogpu): reject transcription requests before calling the upstream API

ZeroGPU only serves chat completions, so drop it from the OpenAI transcription transport set. A zerogpu transcription request now fails locally instead of hitting an /audio/transcriptions endpoint that does not exist

* test(zerogpu): cover the responses and messages bridges

Both endpoints reach ZeroGPU through chat completions, so assert the request each one sends upstream and the reply it returns

* refactor(zerogpu): move zerogpu out of the provider list tails

New upstream providers append to the end of openai_compatible_endpoints, openai_compatible_providers and providers.json, so an entry at the tail conflicts with every one of them. Placing zerogpu next to long-stable entries keeps those merges clean. No behavior change

* fix(zerogpu): price cached input for qwen3-30b-a3b-fp8

The ZeroGPU model catalog (https://docs.zerogpu.ai/docs/model-catalog, checked 2026-10-09) lists cached input for qwen3-30b-a3b-fp8 at $0.05 per 1M tokens. Without cache_read_input_token_cost, cached tokens were billed at the full $0.10 input rate

* revert(zerogpu): leave the audio transcription provider set unchanged

Per review, keep OPENAI_AUDIO_TRANSCRIPTION_PROVIDERS as upstream defines it. This PR only covers chat completions, and ZeroGPU now serves /audio/transcriptions anyway, so the local refusal and its test are removed

* fix(zerogpu): add max_tokens so trim_messages can size ZeroGPU history

trim_messages reads max_tokens as the fallback for max_input_tokens and evaluates it eagerly, so without it the lookup raised, got swallowed, and the full history came back untrimmed. max_tokens matches each model's maxTokens in ZeroGPU's models API (https://api-dashboard.zerogpu.ai/api/models, checked 2026-10-10)

* test(zerogpu): record mocked requests on a recorder object

The mock handler appended to a list the caller passed in, which AGENTS.md
forbids. A recorder now owns the captured requests as a tuple

* fix(zerogpu): cap max_tokens at each model's output limit

Each cap is the max_output_tokens ZeroGPU stores for the model in its production models table (read 2026-10-20), the same column its /v1/models serves as max_length. That gives 65536 for deepseek-v4.1-flash, 32768 for gpt-4.1-mini and gpt-oss-120b, 128000 for gpt-5.4-nano and gpt-5.6-luna, 8192 for LFM2.5-1.2B-Instruct and llama-3.1-8b-instruct-fast, and 16384 for qwen3-30b-a3b-fp8. glm-5.3-flash is stored as 32786, which reads as a typo for 32768, so that row uses 32768

trim_messages sizes history from max_input_tokens, so lowering max_tokens leaves it unaffected

* fix(zerogpu): flag prompt caching on glm-5.3-flash

Repeating the same ~4.3K-token prompt against ZeroGPU on 2026-10-20 returned cached_tokens of 4320 on some glm-5.3-flash calls. gpt-oss-120b and llama-3.1-8b-instruct-fast returned 0 on all nine calls, and qwen3-30b-a3b-fp8 never returned cached_tokens, so they stay unflagged. Their cached prices stay as ZeroGPU publishes them, so any cache hit is still billed at the right rate

* fix(zerogpu): set gpt-5.4-nano context window to 272000

ZeroGPU accepts a 271001-token gpt-5.4-nano prompt and rejects 273000 tokens as over the context window (checked 2026-10-21), so max_input_tokens is 272000 rather than the 400000 its catalog lists

* fix(zerogpu): list zerogpu models for zerogpu/* wildcards

zerogpu had no models_by_provider entry, so get_provider_models("zerogpu") returned None and a zerogpu/* wildcard listed no models. zerogpu_models is now filled from the cost map rows like openinfer_models and registered in models_by_provider

* fix(zerogpu): flag gpt-5.4-nano as a reasoning model

ZeroGPU honors reasoning_effort on gpt-5.4-nano (checked 2026-10-21). Without it the model spends 0 reasoning tokens, and with low or high it spends 32 or more. LiteLLM only treated it as a reasoning model by falling back to OpenAI's bare gpt-5.4-nano row, so get_model_info and /model/info reported supports_reasoning as null. The row now carries the flag itself

* fix(zerogpu): flag deepseek, glm, gpt-oss and qwen3 as reasoning models

ZeroGPU's /v1/models marks these four as reasoning models and accepts reasoning_effort on all of them (checked 2026-10-21), but LiteLLM rejected the param with UnsupportedParamsError because their rows had no supports_reasoning flag. glm reports reasoning_tokens under reasoning_effort, and gpt-oss and qwen3 spend hidden reasoning tokens on every reply. The gpt-oss test case now expects reasoning_effort to reach ZeroGPU

* fix(zerogpu): set gpt-oss-120b context window to 128000

ZeroGPU routes gpt-oss-120b across several upstream backends, and at least one of them returns 413 past a 128000-token window, so max_input_tokens uses that as the safe floor. The backends reached on 2026-10-21 accepted up to about 131K prompt tokens, so this only gives up a little headroom on the larger ones

* feat(zerogpu): call ZeroGPU's Responses API natively

ZeroGPU serves /v1/responses for all nine catalog models, so LiteLLM no longer bridges Responses requests through chat completions. Native calls return ZeroGPU's reasoning output items for glm, gpt-oss and qwen3, which the bridge dropped. Checked live on 2026-10-22 through litellm.responses for every model, non-streaming and streaming, plus a gpt-oss tool call. LFM2.5-1.2B-Instruct streams correctly now that ZeroGPU's text_generation lane emits Responses SSE

* fix(zerogpu): stop inferring the zerogpu provider from api_base alone

Listing https://api.zerogpu.ai/v1 in openai_compatible_endpoints rerouted bare model names on that api_base from openai to zerogpu. A bare gpt-5.6-luna call with verbosity used to return 200 and started raising UnsupportedParamsError. Drop the entry and its api_base-alone test, so zerogpu is only selected through the zerogpu/ prefix

* fix(zerogpu): price gpt-6-luna, llama-3.2-3b, LFM2.5-1.2B-Thinking and qwen3.6

A zerogpu/* wildcard served these four chat models with no cost-map row, so their calls logged $0 spend. Add rows at ZeroGPU's catalog prices from https://api-dashboard.zerogpu.ai/api/models, with context windows and output caps from ZeroGPU's models table. LFM2.5-1.2B-Thinking has no cached price, like LFM2.5-1.2B-Instruct. gpt-6-luna is flagged supports_reasoning because LiteLLM already routes gpt-6 names as reasoning models. Checked live on 2026-10-28, all four now report nonzero response_cost

* fix(zerogpu): drop the retired max_tokens field from the zerogpu rows

Upstream #48471 retired max_tokens from the cost map in favor of max_input_tokens and max_output_tokens, and test_no_row_carries_the_retired_max_tokens_field failed on all 13 zerogpu rows. Every row already carries the same value in max_output_tokens, so nothing is lost. The zerogpu limits test now checks that each row's output limit fits its context window

* chore(zerogpu): regenerate provider endpoint support for zerogpu

Upstream #48619 generates provider_endpoints_support.json from the cost map and checks it in CI, and the hand-written zerogpu entry no longer matched. Regenerated with scripts/generate_provider_endpoints_support.py. Only the zerogpu entry changed, and it now matches what the generator emits for other JSON-registry providers such as openinfer

* fix(zerogpu): flag tools, json_schema and prompt caching on gpt-6-luna

ZeroGPU serves function calling, tool_choice, json_schema response_format and prompt caching on gpt-6-luna, so set supports_function_calling, supports_tool_choice, supports_response_schema and supports_prompt_caching on its row in both cost maps. Checked live on 2026-10-28, a weather tool call came back with the right arguments, json_schema output validated, and repeated prompts reported 3260 of 3263 tokens cached

* fix(zerogpu): set qwen3.6-35b-a3b context to 262144 and flag its tools and reasoning

ZeroGPU accepts up to 262,144 input tokens on qwen3.6-35b-a3b and serves tools, tool_choice, json_schema response_format and reasoning_effort on it. Raise max_input_tokens from 16384 and set supports_function_calling, supports_tool_choice, supports_response_schema and supports_reasoning in both cost maps. Before this, LiteLLM rejected tools and reasoning_effort for the model. Checked live on 2026-10-28, a 209,905-token prompt succeeded, an oversized one was refused with a 262144-token limit, and tool calls, json_schema output and reasoning_effort all worked

* fix(zerogpu): flag vision on gpt-6-luna and qwen3.6-35b-a3b

ZeroGPU reads image inputs on gpt-6-luna and qwen3.6-35b-a3b, so set supports_vision on both rows in both cost maps. Checked live on 2026-10-28, both models named the animal in an image_url photo, and the image showed up in their prompt token counts
sundar-flex added a commit to sundar-flex/litellm that referenced this pull request Nov 17, 2026
- Remove DeepSeek-V4.1-Flash and Qwen3-8B-FP8, which FlexAI no longer
  serves (absent from GET /v1/models on 2026-10-29).
- GLM-5.2 prices follow the catalog: $0.70 input, $2.20 output, $0.105
  cached input per 1M tokens.
- Drop the legacy max_tokens field from every flexai/* row, following
  BerriAI#48471; max_input_tokens and max_output_tokens already carry the limits.
- Main and backup cost maps stay identical for flexai/* rows.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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.

3 participants