Skip to content

fix(router): propagate custom cost_per_token from db model_info in fallback path - #25888

Merged
krrish-berri-2 merged 2 commits into
BerriAI:litellm_oss_branchfrom
Zerohertz:fix/cost-per-token
Apr 25, 2026
Merged

fix(router): propagate custom cost_per_token from db model_info in fallback path#25888
krrish-berri-2 merged 2 commits into
BerriAI:litellm_oss_branchfrom
Zerohertz:fix/cost-per-token

Conversation

@Zerohertz

@Zerohertz Zerohertz commented Apr 16, 2026

Copy link
Copy Markdown
Contributor

Relevant issues

Fixes #25874

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

Before After
Before fix - cost_per_token is None After fix - cost_per_token propagated from db model_info

Unit tests — 2 new tests added and passing:

  • test_model_group_info_cost_from_db_model_info — verifies input_cost_per_token / output_cost_per_token are read from db_model_info when get_deployment_model_info fails
  • test_model_group_info_cost_none_when_db_model_info_has_no_cost — verifies values remain None when db_model_info has no cost fields
tests/test_litellm/test_router.py::test_model_group_info_cost_from_db_model_info PASSED
tests/test_litellm/test_router.py::test_model_group_info_cost_none_when_db_model_info_has_no_cost PASSED

Type

🐛 Bug Fix

Changes

When a model is not found in LiteLLM's built-in model cost map (model_info is None in get_model_group_info()), the fallback ModelMapInfo was hardcoding input_cost_per_token and output_cost_per_token to None, ignoring any custom cost values the user had configured in the database's model_info.

This fix reads input_cost_per_token and output_cost_per_token from db_model_info (the user's database/config model_info), consistent with how mode is already read from the same source. If the keys are not present, .get() returns None, preserving backward compatibility.

Before

model_info = ModelMapInfo(
    ...
    input_cost_per_token=None,
    output_cost_per_token=None,
    ...
)

After

input_cost_per_token = db_model_info.get("input_cost_per_token")
output_cost_per_token = db_model_info.get("output_cost_per_token")

model_info = ModelMapInfo(
    ...
    input_cost_per_token=input_cost_per_token,
    output_cost_per_token=output_cost_per_token,
    ...
)

…llback path

Signed-off-by: Zerohertz <ohg3417@gmail.com>
…allback path

Signed-off-by: Zerohertz <ohg3417@gmail.com>
@vercel

vercel Bot commented Apr 16, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
litellm Ready Ready Preview, Comment Apr 16, 2026 6:57pm

Request Review

@CLAassistant

CLAassistant commented Apr 16, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@greptile-apps

greptile-apps Bot commented Apr 16, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Fixes a bug in Router.get_model_group_info() where custom input_cost_per_token / output_cost_per_token values stored in the DB/config model_info were silently ignored when a model was not found in LiteLLM's built-in cost map. The fix mirrors the pre-existing mode extraction from db_model_info and preserves backward compatibility via .get() returning None when the keys are absent.

Confidence Score: 5/5

Safe to merge — the fix is minimal, backward-compatible, and well-tested.

The production change is two lines that mirror an existing pattern (mode extraction) and are guarded by .get() for backward compatibility. Both new tests are mock-only and pass. The only finding is a P2 style issue (redundant inline import) that does not affect correctness.

No files require special attention.

Important Files Changed

Filename Overview
litellm/router.py Reads input_cost_per_token and output_cost_per_token from db_model_info in the model_info is None fallback branch — consistent with the existing mode extraction pattern; no logic concerns.
tests/test_litellm/test_router.py Adds two unit tests covering the new fallback cost propagation; tests are mock-only (no real network calls). Minor: from unittest.mock import patch is imported inline inside both new test functions, but patch is already at module level on line 5.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[get_model_group_info] --> B{model_id present?}
    B -- yes --> C[get_deployment_model_info]
    C -- success --> D[model_info from LiteLLM map]
    C -- exception --> E[model_info = None fallback]
    B -- no --> E
    E --> F[db_model_info = model.get model_info]
    F --> G[Extract mode]
    F --> H[Extract input cost per token - NEW]
    F --> I[Extract output cost per token - NEW]
    G --> J[Build ModelMapInfo fallback]
    H --> J
    I --> J
    D --> K[Use model_info directly]
    J --> L[Build ModelGroupInfo]
    K --> L
Loading

Reviews (1): Last reviewed commit: "test(router): propagate custom cost_per_..." | Re-trigger Greptile

Comment thread tests/test_litellm/test_router.py
@codecov

codecov Bot commented Apr 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@Zerohertz Zerohertz changed the title fix(router): propagate custom cost_per_token from db model_info in fallback path fix(router): propagate custom cost_per_token from db model_info in fallback path Apr 17, 2026
@Zerohertz

Copy link
Copy Markdown
Contributor Author

There are over 300 Linting CI issues.
I haven't taken any action because this isn't related to this PR, but please reply if necessary!

334 files would be reformatted, 1411 files would be left unchanged.

@krrish-berri-2
krrish-berri-2 changed the base branch from litellm_internal_staging to litellm_oss_branch April 25, 2026 15:20
@krrish-berri-2
krrish-berri-2 merged commit 5b3762f into BerriAI:litellm_oss_branch Apr 25, 2026
43 of 44 checks passed
@Zerohertz
Zerohertz deleted the fix/cost-per-token branch April 26, 2026 02:33
Sameerlite pushed a commit that referenced this pull request Apr 27, 2026
fzowl pushed a commit to fzowl/litellm that referenced this pull request Jun 24, 2026
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.

[Bug]: _set_model_group_info fallback ignores DB model_info cost

3 participants