Skip to content

test: route the cost-calculator tests through a local-cost-map fixture - #37836

Closed
yuneng-berri wants to merge 1 commit into
litellm_internal_stagingfrom
litellm_cost_calculator_test_env_isolation
Closed

test: route the cost-calculator tests through a local-cost-map fixture#37836
yuneng-berri wants to merge 1 commit into
litellm_internal_stagingfrom
litellm_cost_calculator_test_env_isolation

Conversation

@yuneng-berri

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • 28 cost-calculator tests leave LITELLM_LOCAL_MODEL_COST_MAP set session-wide
  • they also leave litellm.model_cost swapped to the local snapshot
  • two more assigned model_cost by hand despite taking monkeypatch

How it solves it:

  • those 28 tests request a new _local_model_cost_map fixture
  • the fixture uses monkeypatch, so a failing test still restores
  • drops a sys.path.insert that resolved against the working directory

User Flow

Before: a proxy admin reports a cost discrepancy, and the maintainer checking it locally reads a stale price

  1. The maintainer runs the unit tier in one process, cost-calculator tests included
  2. A later test in that process reads litellm.model_cost and gets the checked-in snapshot, not the live map
  3. Any price quoted from that run reflects the snapshot, so the answer sent back can disagree with what the proxy actually charged

After: the same run reads the live map, so the answer matches the proxy

  1. The maintainer runs the same unit tier in one process, cost-calculator tests included
  2. The cost-calculator tests put LITELLM_LOCAL_MODEL_COST_MAP and litellm.model_cost back when each one finishes
  3. A later test reads the live map, so a quoted price matches what the proxy charged

Relevant issues

Linear ticket

Pre-Submission checklist

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. 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
  • 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 (Greptile reviews automatically once the PR is opened; only comment @greptileai to 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

The leak is only visible from outside the pytest process, so the probe runs the file in-process and reads the environment back afterwards. LITELLM_LOCAL_MODEL_COST_MAP is not part of the conftest snapshot, so nothing else restores it.

# leakprobe.py
import os, pytest
os.environ.pop("LITELLM_LOCAL_MODEL_COST_MAP", None)
pytest.main(["tests/test_litellm/test_cost_calculator.py", "-q", "-p", "no:randomly"])
print("LITELLM_LOCAL_MODEL_COST_MAP after:", repr(os.environ.get("LITELLM_LOCAL_MODEL_COST_MAP")))

Before (ff02d5c)

  1. python leakprobe.py
  2. 89 passed, then LITELLM_LOCAL_MODEL_COST_MAP after: 'True'

After (cff2af5)

  1. python leakprobe.py
  2. 89 passed, then LITELLM_LOCAL_MODEL_COST_MAP after: None

Type

🧹 Refactoring
✅ Test

Caveats (if any)

  • litellm.model_cost restores via the conftest snapshot either way

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

28 tests in test_cost_calculator.py set LITELLM_LOCAL_MODEL_COST_MAP and
swapped litellm.model_cost inline and never put either back, so anything that
ran after them in the same process read the checked-in snapshot instead of the
live map. They now request a fixture that does both through monkeypatch, which
restores on teardown including when the test fails. Two tests that already took
monkeypatch for the environment variable but still assigned model_cost by hand
go through setattr for the same reason.

Dropping the sys.path.insert takes the last reader of os and sys with it. It
resolved "../.." against the working directory, so it only ever pointed at the
repo root when pytest happened to be invoked from this test's own folder.
@greptile-apps

greptile-apps Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR isolates cost-calculator tests by loading the local pricing snapshot through a monkeypatch-backed fixture, ensuring environment and litellm.model_cost changes are restored after each test.

  • Replaces repeated process-global environment and pricing-map assignments with _local_model_cost_map.
  • Uses monkeypatch.setattr in two remaining tests that previously assigned litellm.model_cost directly.
  • Removes a redundant working-directory-dependent import-path modification.
  • Ratchets the corresponding test-quality budgets downward.

Confidence Score: 5/5

The PR appears safe to merge, with no concrete blocking or independently actionable non-blocking issues identified.

The local-map environment and module attribute are now restored by pytest teardown, the local-map branch avoids network access, and supported test collection does not depend on the removed per-file path modification.

Important Files Changed

Filename Overview
tests/test_litellm/test_cost_calculator.py Consolidates repeated local cost-map setup into an automatically restored fixture without changing test assertions or introducing network access.
test-quality-budget.json Lowers test-quality limits to account for removed direct environment writes, global assignments, and path manipulation.

Reviews (1): Last reviewed commit: "test: route the cost-calculator tests th..." | Re-trigger Greptile

@yuneng-berri

Copy link
Copy Markdown
Contributor Author

Superseded by #37815, which already covers this file and more

@yuneng-berri
yuneng-berri deleted the litellm_cost_calculator_test_env_isolation branch August 21, 2026 12:57
@codecov

codecov Bot commented Aug 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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.

1 participant