mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(proxy): skip pricing lookup for passthrough:* models (#2585)
## Description Internal synthetic model names such as `passthrough:count_tokens` are used for passthrough endpoint accounting. They are not billable models, but cost lookup still sent them through LiteLLM and logged pricing failures, and they could appear as pseudo rows in per-model token savings stats. Closes #2578 ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) - [ ] New feature (non-breaking change that adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update - [ ] Performance improvement - [ ] Code refactoring (no functional changes) ## Changes Made - Short-circuit `CostTracker.estimate_cost` for models prefixed with `passthrough:` so LiteLLM is never called and no pricing WARNING is emitted. - Omit `passthrough:*` models from `CostTracker.stats()` per-model rows and related aggregate fields that feed dashboard cost/savings views. - Add regression tests for silent skip and stats omission. ## Testing - [x] Unit tests pass (`pytest`) - [x] Linting passes (`ruff check .`) - [ ] Type checking passes (`mypy headroom`) - [x] New tests added for new functionality - [ ] Manual testing performed ### Test Output ```text $ python -m pytest tests/test_cost_pricing_warning_dedup.py -q ============================= test session starts ============================== platform linux -- Python 3.11.15, pytest-9.1.0, pluggy-1.6.0 rootdir: /tmp/headroom-pr/headroom configfile: pyproject.toml collected 5 items tests/test_cost_pricing_warning_dedup.py ..... [100%] ========================= 5 passed, 1 warning in 0.26s ========================= $ python -m ruff check headroom/proxy/cost.py tests/test_cost_pricing_warning_dedup.py All checks passed! $ python -m ruff format --check headroom/proxy/cost.py tests/test_cost_pricing_warning_dedup.py 2 files already formatted ``` ## Real Behavior Proof - Environment: Linux x86_64, Python 3.11.15, shallow checkout of headroomlabs-ai/headroom main at904bc675plus this branch commit13526dc- Exact command / steps: `python -m pytest tests/test_cost_pricing_warning_dedup.py -q`; `python -m ruff check headroom/proxy/cost.py tests/test_cost_pricing_warning_dedup.py`; `python -m ruff format --check headroom/proxy/cost.py tests/test_cost_pricing_warning_dedup.py` - Observed result: 5 targeted tests passed, including passthrough skip without warning and stats omitting passthrough models; ruff check and format check clean on touched files - Not tested: live `headroom proxy` session with Claude Code count_tokens traffic; full monorepo `make ci-precheck`; mypy ## Review Readiness - [x] I have performed a self-review - [x] This PR is ready for human review ## Checklist - [x] My code follows the project's style guidelines - [x] I have performed a self-review of my code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [x] I did **not** edit `CHANGELOG.md` - it is generated by release-please from my Conventional Commit PR title (a CI guard enforces this) ## Screenshots (if applicable) N/A Co-authored-by: JD Davis <mxjerrett@gmail.com>
This commit is contained in:
+19
-2
@@ -965,6 +965,9 @@ class CostTracker:
|
||||
cache_read_tokens: Tokens served from cache (~10% of input rate)
|
||||
cache_write_tokens: Tokens written to cache (~125% of input rate)
|
||||
"""
|
||||
if model.startswith("passthrough:"):
|
||||
return None
|
||||
|
||||
litellm = _get_litellm_module()
|
||||
if litellm is None:
|
||||
_warn_pricing_once(
|
||||
@@ -1479,6 +1482,8 @@ class CostTracker:
|
||||
# ``_tokens_saved_by_model`` alone would drop such a model from the table
|
||||
# entirely rather than merely under-report it.
|
||||
for model in sorted(set(self._tokens_saved_by_model) | set(self._tool_saved_by_model)):
|
||||
if model.startswith("passthrough:"):
|
||||
continue
|
||||
compression_saved = self._tokens_saved_by_model.get(model, 0)
|
||||
tool_saved = self._tool_saved_by_model.get(model, 0)
|
||||
# What the "Tokens Saved" column means: everything Headroom kept off
|
||||
@@ -1513,6 +1518,8 @@ class CostTracker:
|
||||
total_billed_input_tokens = 0
|
||||
total_input_tokens = 0
|
||||
for model in self._tokens_saved_by_model:
|
||||
if model.startswith("passthrough:"):
|
||||
continue
|
||||
saved = self._tokens_saved_by_model[model]
|
||||
sent = self._tokens_sent_by_model.get(model, 0)
|
||||
cr = self._api_cache_read_by_model.get(model, 0)
|
||||
@@ -1546,6 +1553,8 @@ class CostTracker:
|
||||
# cache-aware valuation below is reported alongside it.
|
||||
savings_usd = 0.0
|
||||
for model in self._tokens_saved_by_model:
|
||||
if model.startswith("passthrough:"):
|
||||
continue
|
||||
saved = self._tokens_saved_by_model[model]
|
||||
if saved <= 0:
|
||||
continue
|
||||
@@ -1635,8 +1644,16 @@ class CostTracker:
|
||||
"total_tool_tokens_saved": total_tool_saved,
|
||||
"total_input_tokens": total_input_tokens,
|
||||
"total_input_cost_usd": round(cost_with_headroom, 4),
|
||||
"cache_write_5m_tokens": sum(self._api_cache_write_5m_by_model.values()),
|
||||
"cache_write_1h_tokens": sum(self._api_cache_write_1h_by_model.values()),
|
||||
"cache_write_5m_tokens": sum(
|
||||
tokens
|
||||
for model, tokens in self._api_cache_write_5m_by_model.items()
|
||||
if not model.startswith("passthrough:")
|
||||
),
|
||||
"cache_write_1h_tokens": sum(
|
||||
tokens
|
||||
for model, tokens in self._api_cache_write_1h_by_model.items()
|
||||
if not model.startswith("passthrough:")
|
||||
),
|
||||
"per_model": per_model,
|
||||
# Input-only, unchanged: budgets, the per-model table and the
|
||||
# persistent savings tracker all read it as input spend.
|
||||
|
||||
@@ -63,3 +63,31 @@ def test_litellm_unavailable_warns_once_per_model(monkeypatch, caplog):
|
||||
|
||||
unavailable = [r for r in caplog.records if "LiteLLM not available" in r.getMessage()]
|
||||
assert len(unavailable) == 1
|
||||
|
||||
|
||||
def test_passthrough_model_skips_pricing_without_warning(monkeypatch, caplog):
|
||||
import headroom.proxy.cost as cost_mod
|
||||
|
||||
class _FakeLiteLLM:
|
||||
@staticmethod
|
||||
def cost_per_token(**_kwargs):
|
||||
raise AssertionError("cost_per_token must not be called")
|
||||
|
||||
monkeypatch.setattr(cost_mod, "_get_litellm_module", lambda: _FakeLiteLLM())
|
||||
tracker = cost_mod.CostTracker()
|
||||
|
||||
with caplog.at_level(logging.WARNING, logger="headroom.proxy"):
|
||||
assert tracker.estimate_cost("passthrough:count_tokens", 100, 50) is None
|
||||
|
||||
assert not caplog.records
|
||||
|
||||
|
||||
def test_stats_omit_passthrough_models(cost_tracker):
|
||||
cost_tracker.record_tokens("passthrough:count_tokens", 10, 90)
|
||||
cost_tracker.record_tokens("glm-5.2", 20, 80)
|
||||
|
||||
stats = cost_tracker.stats()
|
||||
|
||||
assert set(stats["per_model"]) == {"glm-5.2"}
|
||||
assert stats["total_tokens_saved"] == 20
|
||||
assert stats["total_input_tokens"] == 80
|
||||
|
||||
Reference in New Issue
Block a user