Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(pricing): DeepSeek V4.1-Flash peak/off-peak rate card (#3575)
Zenjibadb0dfa33478e7
diff --git a/crates/headroom-proxy/data/model_prices_and_context_window.json b/crates/headroom-proxy/data/model_prices_and_context_window.jsonindex 7bd746e32..f74cf66fb 100644--- a/crates/headroom-proxy/data/model_prices_and_context_window.json+++ b/crates/headroom-proxy/data/model_prices_and_context_window.json@@ -12514,28 +12514,46 @@"supports_reasoning": true,"supports_tool_choice": true},- "deepseek-v4-flash": {- "input_cost_per_token": 1.4e-07,- "output_cost_per_token": 2.8e-07,- "cache_read_input_token_cost": 2.8e-09,+ "deepseek-flash": {+ "input_cost_per_token": 3e-07,+ "output_cost_per_token": 1.2e-06,+ "cache_read_input_token_cost": 6e-09,+ "input_cost_per_token_cache_hit": 6e-09,"litellm_provider": "deepseek","max_input_tokens": 1000000,- "max_output_tokens": 384000,- "max_tokens": 384000,+ "max_output_tokens": 393216,+ "max_tokens": 393216,"mode": "chat","supports_function_calling": true,"supports_native_streaming": true,"supports_prompt_caching": true,"source": "https://api-docs.deepseek.com/quick_start/pricing"},- "deepseek-v4-pro": {- "input_cost_per_token": 4.35e-07,- "output_cost_per_token": 8.7e-07,- "cache_read_input_token_cost": 3.625e-09,+ "deepseek/deepseek-flash": {+ "input_cost_per_token": 3e-07,+ "output_cost_per_token": 1.2e-06,+ "cache_read_input_token_cost": 6e-09,+ "input_cost_per_token_cache_hit": 6e-09,+ "litellm_provider": "deepseek",+ "max_input_tokens": 1000000,+ "max_output_tokens": 393216,+ "max_tokens": 393216,+ "mode": "chat",+ "supports_function_calling": true,+ "supports_native_streaming": true,+ "supports_prompt_caching": true,+ "supports_assistant_prefill": true,+ "source": "https://api-docs.deepseek.com/quick_start/pricing"+ },+ "deepseek-v4-flash": {+ "input_cost_per_token": 3e-07,+ "output_cost_per_token": 1.2e-06,+ "cache_read_input_token_cost": 6e-09,+ "input_cost_per_token_cache_hit": 6e-09,"litellm_provider": "deepseek","max_input_tokens": 1000000,- "max_output_tokens": 384000,- "max_tokens": 384000,+ "max_output_tokens": 393216,+ "max_tokens": 393216,"mode": "chat","supports_function_calling": true,"supports_native_streaming": true,@@ -12543,14 +12561,45 @@"source": "https://api-docs.deepseek.com/quick_start/pricing"},"deepseek/deepseek-v4-flash": {- "input_cost_per_token": 1.4e-07,- "output_cost_per_token": 2.8e-07,- "cache_read_input_token_cost": 2.8e-09,- "input_cost_per_token_cache_hit": 2.8e-09,+ "input_cost_per_token": 3e-07,+ "output_cost_per_token": 1.2e-06,+ "cache_read_input_token_cost": 6e-09,+ "input_cost_per_token_cache_hit": 6e-09,+ "litellm_provider": "deepseek",+ "max_input_tokens": 1000000,+ "max_output_tokens": 393216,+ "max_tokens": 393216,+ "mode": "chat",+ "supports_function_calling": true,+ "supports_native_streaming": true,+ "supports_prompt_caching": true,+ "supports_assistant_prefill": true,+ "source": "https://api-docs.deepseek.com/quick_start/pricing"+ },+ "deepseek-v4-flash-vision-exp": {+ "input_cost_per_token": 3e-07,+ "output_cost_per_token": 1.2e-06,+ "cache_read_input_token_cost": 6e-09,+ "input_cost_per_token_cache_hit": 6e-09,+ "litellm_provider": "deepseek",+ "max_input_tokens": 1000000,+ "max_output_tokens": 393216,+ "max_tokens": 393216,+ "mode": "chat",+ "supports_function_calling": true,+ "supports_native_streaming": true,+ "supports_prompt_caching": true,+ "source": "https://api-docs.deepseek.com/quick_start/pricing"+ },+ "deepseek/deepseek-v4-flash-vision-exp": {+ "input_cost_per_token": 3e-07,+ "output_cost_per_token": 1.2e-06,+ "cache_read_input_token_cost": 6e-09,+ "input_cost_per_token_cache_hit": 6e-09,"litellm_provider": "deepseek","max_input_tokens": 1000000,- "max_output_tokens": 384000,- "max_tokens": 384000,+ "max_output_tokens": 393216,+ "max_tokens": 393216,"mode": "chat","supports_function_calling": true,"supports_native_streaming": true,@@ -12558,15 +12607,30 @@"supports_assistant_prefill": true,"source": "https://api-docs.deepseek.com/quick_start/pricing"},+ "deepseek-v4-pro": {+ "input_cost_per_token": 1.32e-06,+ "output_cost_per_token": 3.96e-06,+ "cache_read_input_token_cost": 4.4e-08,+ "input_cost_per_token_cache_hit": 4.4e-08,+ "litellm_provider": "deepseek",+ "max_input_tokens": 1000000,+ "max_output_tokens": 393216,+ "max_tokens": 393216,+ "mode": "chat",+ "supports_function_calling": true,+ "supports_native_streaming": true,+ "supports_prompt_caching": true,+ "source": "https://api-docs.deepseek.com/quick_start/pricing"+ },"deepseek/deepseek-v4-pro": {- "input_cost_per_token": 4.35e-07,- "output_cost_per_token": 8.7e-07,- "cache_read_input_token_cost": 3.625e-09,- "input_cost_per_token_cache_hit": 3.625e-09,+ "input_cost_per_token": 1.32e-06,+ "output_cost_per_token": 3.96e-06,+ "cache_read_input_token_cost": 4.4e-08,+ "input_cost_per_token_cache_hit": 4.4e-08,"litellm_provider": "deepseek","max_input_tokens": 1000000,- "max_output_tokens": 384000,- "max_tokens": 384000,+ "max_output_tokens": 393216,+ "max_tokens": 393216,"mode": "chat","supports_function_calling": true,"supports_native_streaming": true,diff --git a/docs/content/docs/opencode-deepseek.mdx b/docs/content/docs/opencode-deepseek.mdxindex 9d2bf94df..b649222e3 100644--- a/docs/content/docs/opencode-deepseek.mdx+++ b/docs/content/docs/opencode-deepseek.mdx@@ -132,15 +132,21 @@ Edit `~/.config/opencode/opencode.json`:**Important:** Only include model IDs that appear in the proxy's `/v1/models`response. OpenCode validates config models against the proxy's model list.-The current DeepSeek model names are `deepseek-v4-pro` and `deepseek-v4-flash`.-`deepseek-chat` and `deepseek-reasoner` are deprecated compatibility aliases.+The current DeepSeek model names are `deepseek-flash` and `deepseek-v4-pro`.+The retired `deepseek-v4-flash` and `deepseek-v4-flash-vision-exp` are still+accepted and billed at the Flash price; `deepseek-chat` and `deepseek-reasoner`+are deprecated compatibility aliases.### Model comparison-| Model | Input / Output (per 1M) | Context | Max Output |-|---|---|---|---|-| `deepseek-v4-pro` | $0.435 / $0.87 | 1M | 384K |-| `deepseek-v4-flash` | $0.14 / $0.28 | 1M | 384K |+All prices are USD per 1M tokens, at DeepSeek's **off-peak** tier. Peak hours —+Beijing 09:00-12:00 and 14:00-18:00, i.e. 01:00-04:00 and 06:00-10:00 UTC — cost+exactly twice these rates.++| Model | Input / Output (per 1M, off-peak) | Context | Max Output | Vision |+|---|---|---|---|---|+| `deepseek-flash` (V4.1-Flash) | $0.15 / $0.60 | 1M | 384K | yes |+| `deepseek-v4-pro` | $0.66 / $1.98 | 1M | 384K | no |Both models support **thinking mode** for step-by-step reasoning (see below).@@ -155,7 +161,7 @@ opencode```Run `/models` to confirm both DeepSeek models appear under "Headroom Proxy".-Select one with `/model deepseek-v4-flash` or `/model deepseek-v4-pro`.+Select one with `/model deepseek-flash` or `/model deepseek-v4-pro`.---diff --git a/headroom/models/registry.py b/headroom/models/registry.pyindex 6526c9c2a..d7cf0e5ac 100644--- a/headroom/models/registry.py+++ b/headroom/models/registry.py@@ -422,16 +422,17 @@ def _register_builtin_models() -> None:notes="DeepSeek Coder - Specialized for code",)- _MODELS["deepseek-v4-flash"] = ModelInfo(- name="deepseek-v4-flash",+ _MODELS["deepseek-flash"] = ModelInfo(+ name="deepseek-flash",provider="deepseek",context_window=1_000_000,max_output_tokens=384_000,supports_tools=True,- supports_vision=False,+ supports_vision=True,supports_streaming=True,tokenizer_backend="huggingface",- notes="DeepSeek V4 Flash - 13B active params; non-thinking + thinking modes",+ aliases=("deepseek-v4-flash", "deepseek-v4-flash-vision-exp"),+ notes="DeepSeek V4.1-Flash - vision; non-thinking + thinking modes",)_MODELS["deepseek-v4-pro"] = ModelInfo(diff --git a/headroom/pricing/__init__.py b/headroom/pricing/__init__.pyindex b96b01522..75dfc4164 100644--- a/headroom/pricing/__init__.py+++ b/headroom/pricing/__init__.py@@ -29,6 +29,16 @@ from .deepseek_prices import (from .deepseek_prices import (LAST_UPDATED as DEEPSEEK_LAST_UPDATED,)+from .deepseek_tiers import (+ OFF_PEAK_RATES_PER_1M,+ PEAK_MULTIPLIER,+ WEEKEND_OFF_PEAK_FROM,+ DeepSeekRates,+ bare_model,+ is_peak,+ off_peak_rates,+ rates_for,+)from .litellm_pricing import (LiteLLMModelPricing,estimate_cost,@@ -76,4 +86,13 @@ __all__ = ["DEEPSEEK_LAST_UPDATED","DEEPSEEK_PRICES","get_deepseek_registry",+ # DeepSeek peak/off-peak structure (Beijing windows; peak = 2x off-peak)+ "OFF_PEAK_RATES_PER_1M",+ "PEAK_MULTIPLIER",+ "WEEKEND_OFF_PEAK_FROM",+ "DeepSeekRates",+ "bare_model",+ "is_peak",+ "off_peak_rates",+ "rates_for",]diff --git a/headroom/pricing/deepseek_prices.py b/headroom/pricing/deepseek_prices.pyindex 89ed1a7e1..d880a924f 100644--- a/headroom/pricing/deepseek_prices.py+++ b/headroom/pricing/deepseek_prices.py@@ -1,35 +1,55 @@-"""DeepSeek model pricing information."""+"""DeepSeek model pricing information.-from datetime import date+Every row here is the vendor's OFF-PEAK tier: these flat consumers have no+request instant, so they take the cheaper published tier, and per-request+costing applies the Beijing peak window through+:func:`headroom.pricing.deepseek_tiers.rates_for`.+"""+from .deepseek_tiers import (+ LAST_UPDATED as LAST_UPDATED,+)+from .deepseek_tiers import OFF_PEAK_RATES_PER_1M+from .deepseek_tiers import (+ SOURCE_URL as SOURCE_URL,+)from .registry import ModelPricing, PricingRegistry-# Last verified date for pricing information-LAST_UPDATED = date(2026, 6, 19)+# ``LAST_UPDATED`` and ``SOURCE_URL`` live with the rates they describe, in+# ``deepseek_tiers``: this table is derived from that one, so the provenance date+# must move with the numbers rather than tracking a copy. They are re-exported+# here (the ``X as X`` form) so the existing+# ``from headroom.pricing.deepseek_prices import LAST_UPDATED`` path keeps working.-# Official pricing page-SOURCE_URL = "https://api-docs.deepseek.com/quick_start/pricing"+# All prices are in USD per 1 million tokens, off-peak.+_OFF_PEAK_NOTE = "off-peak; peak is 2x (Beijing 09:00-12:00, 14:00-18:00)"-# All prices are in USD per 1 million tokens-DEEPSEEK_PRICES: dict[str, ModelPricing] = {- "deepseek-v4-flash": ModelPricing(- model="deepseek-v4-flash",++def _flat_row(model_id: str, canonical: str, model_version: str) -> ModelPricing:+ """Build one off-peak row for ``model_id`` from the tier table."""+ cache_hit, miss, out = OFF_PEAK_RATES_PER_1M[canonical]+ return ModelPricing(+ model=model_id,provider="deepseek",- input_per_1m=0.14,- output_per_1m=0.28,- cached_input_per_1m=0.0028,+ input_per_1m=miss,+ output_per_1m=out,+ cached_input_per_1m=cache_hit,context_window=1_000_000,- notes="DeepSeek V4 Flash - 13B active params; non-thinking + thinking modes",+ notes=f"{model_version} - {_OFF_PEAK_NOTE}",+ )+++DEEPSEEK_PRICES: dict[str, ModelPricing] = {+ "deepseek-flash": _flat_row("deepseek-flash", "deepseek-flash", "DeepSeek V4.1-Flash"),+ "deepseek-v4-flash": _flat_row(+ "deepseek-v4-flash", "deepseek-flash", "Retired id, served as DeepSeek V4.1-Flash"),- "deepseek-v4-pro": ModelPricing(- model="deepseek-v4-pro",- provider="deepseek",- input_per_1m=0.435,- output_per_1m=0.87,- cached_input_per_1m=0.003625,- context_window=1_000_000,- notes="DeepSeek V4 Pro - 49B active params",+ "deepseek-v4-flash-vision-exp": _flat_row(+ "deepseek-v4-flash-vision-exp",+ "deepseek-flash",+ "Retired id, served as DeepSeek V4.1-Flash",),+ "deepseek-v4-pro": _flat_row("deepseek-v4-pro", "deepseek-v4-pro", "DeepSeek V4-Pro-0813"),}diff --git a/headroom/pricing/deepseek_tiers.py b/headroom/pricing/deepseek_tiers.pynew file mode 100644index 000000000..1e10a1220--- /dev/null+++ b/headroom/pricing/deepseek_tiers.py@@ -0,0 +1,219 @@+"""DeepSeek peak/off-peak pricing structure.++DeepSeek prices DeepSeek-V4.1-Flash (``deepseek-flash``) and DeepSeek-V4-Pro+(``deepseek-v4-pro``) on Beijing-time peak windows, and peak is exactly twice+off-peak. Because the ratio is structural, it is applied here instead of being+transcribed into every row of :mod:`headroom.pricing.deepseek_prices` - a+per-tier table would double the columns, would drift, and would let one tier be+mis-typed without anything noticing.++Windows are Beijing wall time (a fixed UTC+8; the PRC has no daylight saving):+09:00-12:00 and 14:00-18:00, which are the published 01:00-04:00 and+06:00-10:00 UTC windows. Weekends are all-day off-peak only from+:data:`WEEKEND_OFF_PEAK_FROM`; before that instant they still followed the+weekday windows, so a back-dated or replayed request is not silently repriced.++Prices are the vendor's own USD list. The zh-cn page lists the same tiers in+CNY, but the two lists are not one FX apart, so CNY billing needs a switch over+two published lists rather than a baked rate - not modelled here.++Consumers that need one flat number for a model take :func:`off_peak_rates`+(the cheaper published tier). Consumers pricing a real request take+:func:`rates_for` with that request's instant.+"""++from __future__ import annotations++from dataclasses import dataclass+from datetime import date, datetime, time, timedelta, timezone+from typing import Literal++#: Peak is exactly this multiple of every off-peak rate.+PEAK_MULTIPLIER: float = 2.0++#: Last date the vendor's USD rate card below was verified.+LAST_UPDATED = date(2026, 9, 14)++#: Official pricing page these rates come from.+SOURCE_URL = "https://api-docs.deepseek.com/quick_start/pricing"++#: The vendor's published card, as fetched, so the numbers below can be checked+#: without trusting prose. Values are quoted from the page, not re-derived.+#:+#: Source: :data:`SOURCE_URL`, fetched 2026-09-14 and re-verified 2026-09-15 in+#: both locales with a cache-busting query. Footnote (1) verbatim: "Use+#: ``deepseek-flash`` as the model name. The legacy names ``deepseek-v4-flash``+#: and ``deepseek-v4-flash-vision-exp`` are still accepted, but the corresponding+#: models have been retired, their requests are served by the DeepSeek-V4.1-Flash+#: model and billed at the Flash price."+#:+#: Footnote (2) verbatim: "In response to user demand, we have decided to continue+#: providing API services for DeepSeek V4 Pro after September 14, 2026, with the+#: billing method remaining unchanged. We will provide further notice should there+#: be any changes." (ZH: 为响应广大用户的需求,我们决定在 2026 年 9 月 14 日之后继续提供+#: DeepSeek V4 Pro 的 API 调用服务,计费方式保持不变;如有变动,我们将另行通知。)+#:+#: No V4 Pro cutover instant is documented for this fetch, so the Pro row above is+#: the operative rule for every current request. Should the vendor publish one, the+#: shape already exists in this module: :data:`WEEKEND_OFF_PEAK_FROM` gates a pricing+#: rule on an effective instant, and a Pro->Flash transition would use the same+#: pattern plus boundary tests either side of its instant.+VENDOR_CARD: dict[str, dict[str, object]] = {+ "deepseek-flash": {+ "model_version": "DeepSeek-V4.1-Flash",+ "off_peak": (0.003, 0.15, 0.60),+ "peak": (0.006, 0.30, 1.20),+ "max_output": "384K; upstream LiteLLM encodes it as 393216 = 384 x 1024",+ "vision": True,+ "legacy_ids": ("deepseek-v4-flash", "deepseek-v4-flash-vision-exp"),+ },+ "deepseek-v4-pro": {+ "model_version": "DeepSeek-V4-Pro-0813",+ "off_peak": (0.022, 0.66, 1.98),+ "peak": (0.044, 1.32, 3.96),+ "max_output": "384K",+ "vision": False,+ "legacy_ids": (),+ },+}++#: Off-peak ``(cache-hit input, cache-miss input, output)`` USD per 1M tokens.+#: These are the ``off_peak`` tuples of :data:`VENDOR_CARD`; the assertions in+#: ``tests/test_pricing_deepseek_tiers.py`` keep the two in step.+#:+#: ``deepseek-flash`` is the current id for DeepSeek-V4.1-Flash. The vendor still+#: accepts the retired ``deepseek-v4-flash`` and ``deepseek-v4-flash-vision-exp``+#: ids and serves them from the same model at the Flash price, so they resolve+#: through :data:`LEGACY_MODEL_IDS`.+OFF_PEAK_RATES_PER_1M: dict[str, tuple[float, float, float]] = {+ "deepseek-flash": (0.003, 0.15, 0.60),+ "deepseek-v4-pro": (0.022, 0.66, 1.98),+}++#: Retired ids the vendor still accepts, mapped to the id that now serves them.+LEGACY_MODEL_IDS: dict[str, str] = {+ "deepseek-v4-flash": "deepseek-flash",+ "deepseek-v4-flash-vision-exp": "deepseek-flash",+}++#: Peak windows in Beijing wall time, start-inclusive and end-exclusive.+PEAK_WINDOWS_BEIJING: tuple[tuple[time, time], ...] = (+ (time(9), time(12)),+ (time(14), time(18)),+)++#: Beijing is a fixed UTC+8 offset.+BEIJING_TZ = timezone(timedelta(hours=8))++#: Instant weekends became all-day off-peak: 2026-08-23 00:00 Beijing.+WEEKEND_OFF_PEAK_FROM = datetime(2026, 8, 22, 16, 0, tzinfo=timezone.utc)+++@dataclass(frozen=True)+class DeepSeekRates:+ """One tier of a DeepSeek model's rate card, in USD per 1M tokens."""++ cache_hit_per_1m: float+ input_per_1m: float+ output_per_1m: float+ #: DeepSeek bills no cache-write surcharge; write tokens bill as cache-miss input.+ cache_write_per_1m: float+ tier: Literal["peak", "off_peak"]+++def bare_model(model: str) -> str:+ """Return ``model`` without a ``provider/`` prefix, lowercased.++ A tag suffix after ``:`` (``deepseek/deepseek-v4-pro:free``) is not stripped: such an id+ falls out of tier scope, and it is priced only if a flat table happens to carry that exact+ key - otherwise the caller's cost is unknown rather than approximated.++ Args:+ model: A model id, optionally prefixed the way a gateway writes it+ (``deepseek/deepseek-v4-pro``).++ Returns:+ The bare, lowercased id used to key the tier table.+ """+ return model.rsplit("/", 1)[-1].strip().lower()+++def _canonical(model: str) -> str:+ """Return the id whose tier prices ``model``, resolving retired aliases."""+ bare = bare_model(model)+ return LEGACY_MODEL_IDS.get(bare, bare)+++def _tier_rates(canonical: str, tier: Literal["peak", "off_peak"]) -> DeepSeekRates | None:+ """Build the ``tier`` rate row for a canonical id, or ``None`` if unknown."""+ rates = OFF_PEAK_RATES_PER_1M.get(canonical)+ if rates is None:+ return None+ hit, miss, out = rates+ if tier == "peak":+ hit = hit * PEAK_MULTIPLIER+ miss = miss * PEAK_MULTIPLIER+ out = out * PEAK_MULTIPLIER+ return DeepSeekRates(+ cache_hit_per_1m=hit,+ input_per_1m=miss,+ output_per_1m=out,+ cache_write_per_1m=0.0,+ tier=tier,+ )+++def off_peak_rates(model: str) -> DeepSeekRates | None:+ """Return the off-peak tier for ``model``, or ``None`` when out of scope.++ Args:+ model: Model id, with or without a ``provider/`` prefix, and either a+ current id or a retired alias.++ Returns:+ The off-peak :class:`DeepSeekRates`, or ``None`` for any model outside+ the flash/pro rate card.+ """+ return _tier_rates(_canonical(model), "off_peak")+++def is_peak(now: datetime) -> bool:+ """Return whether ``now`` falls in a Beijing peak window.++ Args:+ now: The instant to test. A naive value is read as UTC.++ Returns:+ ``True`` during Beijing 09:00-12:00 or 14:00-18:00 on a day the published+ windows apply; weekends are all-day off-peak once+ :data:`WEEKEND_OFF_PEAK_FROM` is in force.+ """+ if now.tzinfo is None:+ now = now.replace(tzinfo=timezone.utc)+ beijing = now.astimezone(BEIJING_TZ)+ if now >= WEEKEND_OFF_PEAK_FROM and beijing.weekday() >= 5: # 5 = Saturday+ return False+ wall = beijing.time()+ return any(start <= wall < end for start, end in PEAK_WINDOWS_BEIJING)+++def rates_for(model: str, now: datetime | None = None) -> DeepSeekRates | None:+ """Return the tier that prices ``model`` at ``now``.++ This is the seam every cost path uses. A caller with a request instant passes+ it, so tests and replays stay deterministic; a caller without one gets the+ wall clock.++ Args:+ model: Model id, with or without a ``provider/`` prefix, current or retired.+ now: The request instant, or ``None`` to read the clock.++ Returns:+ The applicable :class:`DeepSeekRates`, or ``None`` for any model outside+ the flash/pro rate card - the caller's cue to use its generic path.+ """+ canonical = _canonical(model)+ if canonical not in OFF_PEAK_RATES_PER_1M:+ return None+ instant = now if now is not None else datetime.now(timezone.utc)+ return _tier_rates(canonical, "peak" if is_peak(instant) else "off_peak")diff --git a/headroom/pricing/litellm_pricing.py b/headroom/pricing/litellm_pricing.pyindex a9dce1ff6..22d83984b 100644--- a/headroom/pricing/litellm_pricing.py+++ b/headroom/pricing/litellm_pricing.py@@ -12,8 +12,16 @@ import jsonimport loggingimport osfrom dataclasses import dataclass+from datetime import datetimefrom typing import Any+from headroom.pricing.deepseek_tiers import (+ LEGACY_MODEL_IDS,+ OFF_PEAK_RATES_PER_1M,+)+from headroom.pricing.deepseek_tiers import (+ rates_for as _deepseek_rates_for,+)from headroom.pricing.litellm_model_resolution import (pricing_lookup_candidates,resolve_litellm_model_name,@@ -286,6 +294,7 @@ def estimate_cost_from_tokens(input_tokens: int = 0,output_tokens: int = 0,cached_tokens: int = 0,+ now: datetime | None = None,) -> float | None:"""Cost for one request from token counts, using LiteLLM's own cost model.@@ -295,14 +304,34 @@ def estimate_cost_from_tokens(Sonnet 4 / 4.5 family a prompt over 200K re-prices the *whole* request --input, output and cache alike. ``litellm.cost_per_token`` applies both.+ DeepSeek flash/pro do not go through litellm at all: the vendor prices them+ on Beijing peak/off-peak tiers (see+ :mod:`headroom.pricing.deepseek_tiers`), and ``now`` selects the tier. A+ ``now`` of ``None`` reads the wall clock, which is what live traffic wants.+``input_tokens`` is the TOTAL prompt, ``cached_tokens`` included. LiteLLMsubtracts the cached portion itself and tests the long-context thresholdagainst the total, so passing a cache-exclusive count would bothdouble-discount the cached tokens and understate the threshold.Returns ``None`` when LiteLLM is unavailable or doesn't know the model --- the caller's cue to fall back to its own table.+ the caller's cue to fall back to its own table. The DeepSeek flash/pro ids+ priced by the tier branch above are the exception: they never reach+ LiteLLM, so they are priced the same way whether or not it is installed."""+ # DeepSeek flash/pro are priced from the vendor's peak/off-peak card, which+ # litellm cannot express: model_cost holds one flat rate per model. The tier+ # is selected from the request instant, so this runs before - and without -+ # litellm.+ tier = _deepseek_rates_for(model, now)+ if tier is not None:+ uncached = max(input_tokens - cached_tokens, 0)+ return (+ (uncached / 1_000_000) * tier.input_per_1m+ + (cached_tokens / 1_000_000) * tier.cache_hit_per_1m+ + (output_tokens / 1_000_000) * tier.output_per_1m+ )+if not LITELLM_AVAILABLE:return Nonecandidate = next((c for c in pricing_lookup_candidates(model) if c in litellm.model_cost), None)@@ -333,53 +362,77 @@ def list_available_models() -> list[str]:# ============================================================-# DeepSeek V4 pricing injection-# ============================================================-# Vendored LiteLLM JSON predates DeepSeek V4 models. Inject pricing at-# import time so the primary cost-per-token path resolves them. Once-# upstream litellm adds these entries, injection becomes a no-op.+# DeepSeek pricing injection# ============================================================--_DEEPSEEK_V4_PRICING: dict[str, dict[str, float | str | int]] = {- "deepseek-v4-flash": {- "input_cost_per_token": 0.14 / 1_000_000,- "output_cost_per_token": 0.28 / 1_000_000,- "cache_read_input_token_cost": 0.0028 / 1_000_000,- "input_cost_per_token_cache_hit": 0.0028 / 1_000_000,+#+# The vendor prices DeepSeek flash/pro on Beijing peak/off-peak tiers. litellm's+# model_cost holds one flat rate per model, so these keys carry the OFF-PEAK+# figure - the same choice the rest of Headroom's flat tables make - and+# estimate_cost_from_tokens() prices real requests from the tier module instead.+# The per-token figures are derived from OFF_PEAK_RATES_PER_1M, so a rate change+# in deepseek_tiers moves this table with it.+#+# These rows are assigned, not fill-if-absent: upstream ships deepseek-flash at+# the PEAK rate ($0.30/$1.20), so a fill-if-absent rule would make the flat+# figure depend on the installed litellm version.+++def _deepseek_flat_row(canonical: str) -> dict[str, float | str | int]:+ """Off-peak litellm row for ``canonical``, derived from the tier table."""+ cache_hit, miss, out = OFF_PEAK_RATES_PER_1M[canonical]+ per_token = 1_000_000+ return {+ "input_cost_per_token": miss / per_token,+ "output_cost_per_token": out / per_token,+ "cache_read_input_token_cost": cache_hit / per_token,+ "input_cost_per_token_cache_hit": cache_hit / per_token,"litellm_provider": "deepseek",- "max_tokens": 384_000,+ "max_tokens": 393_216,"max_input_tokens": 1_000_000,- "max_output_tokens": 384_000,- },- "deepseek-v4-pro": {- "input_cost_per_token": 0.435 / 1_000_000,- "output_cost_per_token": 0.87 / 1_000_000,- "cache_read_input_token_cost": 0.003625 / 1_000_000,- "input_cost_per_token_cache_hit": 0.003625 / 1_000_000,- "litellm_provider": "deepseek",- "max_tokens": 384_000,- "max_input_tokens": 1_000_000,- "max_output_tokens": 384_000,- },+ # 393_216 = 384 x 1024; the vendor page states the same cap as "384K".+ "max_output_tokens": 393_216,+ }+++#: litellm ids → the tier-table id whose off-peak row prices them: every current+#: id maps to itself, and the retired ids resolve through ``LEGACY_MODEL_IDS``,+#: so a new retired id reaches this table without a second edit.+_DEEPSEEK_LITELLM_IDS: dict[str, str] = {+ **{model_id: model_id for model_id in OFF_PEAK_RATES_PER_1M},+ **LEGACY_MODEL_IDS,}+_DEEPSEEK_V4_PRICING: dict[str, dict[str, float | str | int]] = {+ model_id: _deepseek_flat_row(canonical) for model_id, canonical in _DEEPSEEK_LITELLM_IDS.items()+}-def _inject_deepseek_pricing() -> None:- """Inject DeepSeek V4 pricing into litellm's model_cost dict.- Only injects entries not already present, so upstream litellm additions- (once available) take precedence. Both bare and provider-prefixed keys- are added so resolve_litellm_model() catches them via its deepseek/- prefix loop.+def _inject_deepseek_pricing() -> None:+ """Write DeepSeek off-peak flat rows into litellm's ``model_cost``.++ Upstream litellm may already define some of these ids - at the vendor's peak+ rate - so every key is assigned rather than skipped, which keeps the flat+ figure identical across litellm versions. Both bare and provider-prefixed+ keys are written because ``resolve_litellm_model()`` resolves DeepSeek ids+ through its ``deepseek/`` prefix rule.++ Each row is merged over whatever upstream holds for that key: our cost and+ cap fields win by construction, while unrelated upstream metadata (capability+ flags, fields we do not set) survives. The ``**`` merge builds a fresh dict,+ so the injected rows stay independent of ``_DEEPSEEK_V4_PRICING``."""if not LITELLM_AVAILABLE:returnfor model_name, pricing in _DEEPSEEK_V4_PRICING.items():- if model_name not in litellm.model_cost:- litellm.model_cost[model_name] = pricing+ litellm.model_cost[model_name] = {+ **litellm.model_cost.get(model_name, {}),+ **pricing,+ }prefixed = f"deepseek/{model_name}"- if prefixed not in litellm.model_cost:- litellm.model_cost[prefixed] = pricing+ litellm.model_cost[prefixed] = {+ **litellm.model_cost.get(prefixed, {}),+ **pricing,+ }_inject_deepseek_pricing()diff --git a/headroom/pricing/registry.py b/headroom/pricing/registry.pyindex 3f1a92319..b207d6dc5 100644--- a/headroom/pricing/registry.py+++ b/headroom/pricing/registry.py@@ -1,7 +1,9 @@"""Pricing registry for LLM model cost estimation."""from dataclasses import dataclass, field-from datetime import date, timedelta+from datetime import date, datetime, timedelta++from .deepseek_tiers import rates_for as _deepseek_rates_for@dataclass(frozen=True)@@ -99,6 +101,7 @@ class PricingRegistry:cached_input_tokens: int = 0,batch_input_tokens: int = 0,batch_output_tokens: int = 0,+ now: datetime | None = None,) -> CostEstimate:"""Estimate the cost for a given token usage.@@ -109,18 +112,40 @@ class PricingRegistry:cached_input_tokens: Number of cached input tokens.batch_input_tokens: Number of batch API input tokens.batch_output_tokens: Number of batch API output tokens.+ now: Request instant used to select a peak/off-peak tier. ``None``+ reads the wall clock.Returns:CostEstimate with calculated cost and breakdown.Raises:ValueError: If model is not found in registry.++ Note:+ DeepSeek flash/pro ids are priced from the peak/off-peak tier table+ even when absent from ``self.prices``, and a flat row for such an id+ is deliberately ignored in favour of the tier."""- pricing = self.get_price(model)+ tier = _deepseek_rates_for(model, now)+ # The synthesized row deliberately leaves the batch rates ``None``, so+ # the flat body's own guards raise the same messages DeepSeek's flat+ # rows would. Tier and flat pricing share one arithmetic body, so the+ # two cannot drift.+ pricing = (+ self.get_price(model)+ if tier is None+ else ModelPricing(+ model=model,+ provider="deepseek",+ input_per_1m=tier.input_per_1m,+ output_per_1m=tier.output_per_1m,+ cached_input_per_1m=tier.cache_hit_per_1m,+ )+ )if pricing is None:raise ValueError(f"Model '{model}' not found in registry")- breakdown = {}+ breakdown: dict = {"tier": tier.tier} if tier is not None else {}total_cost = 0.0# Regular input tokensdiff --git a/headroom/providers/anthropic.py b/headroom/providers/anthropic.pyindex badce7a24..1c0bb44e1 100644--- a/headroom/providers/anthropic.py+++ b/headroom/providers/anthropic.py@@ -21,9 +21,15 @@ import loggingimport osimport reimport warnings+from datetime import datetimefrom typing import Any, castfrom headroom import paths as _paths+from headroom.pricing.deepseek_tiers import (+ LEGACY_MODEL_IDS,+ OFF_PEAK_RATES_PER_1M,+ off_peak_rates,+)from headroom.pricing.litellm_pricing import estimate_cost_from_tokensfrom headroom.tokenizers.base import (TokenCountCache,@@ -222,10 +228,33 @@ _UNKNOWN_CLAUDE_DEFAULT = {}-# DeepSeek fallback pricing for --anthropic-api-url deepseek routing+def _deepseek_fallback_row(model_id: str) -> dict[str, float]:+ """Off-peak fallback rates for ``model_id``, from the shared tier table.++ This table has no request instant, so it carries the cheaper published tier;+ per-request costing applies the peak window in+ :func:`headroom.pricing.litellm_pricing.estimate_cost_from_tokens`.+ """+ rates = off_peak_rates(model_id)+ if rates is None: # pragma: no cover - the ids below are all in the tier table+ raise ValueError(f"no DeepSeek tier for {model_id!r}")+ return {+ "input": rates.input_per_1m,+ "output": rates.output_per_1m,+ "cached_input": rates.cache_hit_per_1m,+ }+++# DeepSeek fallback pricing for --anthropic-api-url deepseek routing.+#+# While the tier seam in ``headroom.pricing.litellm_pricing`` claims these four+# ids first, ``estimate_cost`` never reaches this table on a live request: it is a+# safety net for the flat-consumer invariant (anything without a request instant+# carries the off-peak figure) and the only pricing left if that seam is ever+# bypassed. ``_get_pricing`` therefore reaches it only when called directly._DEEPSEEK_FALLBACK_PRICING: dict[str, dict[str, float]] = {- "deepseek-v4-flash": {"input": 0.14, "output": 0.28, "cached_input": 0.0028},- "deepseek-v4-pro": {"input": 0.435, "output": 0.87, "cached_input": 0.003625},+ model_id: _deepseek_fallback_row(model_id)+ for model_id in (*OFF_PEAK_RATES_PER_1M, *LEGACY_MODEL_IDS)}@@ -775,6 +804,7 @@ class AnthropicProvider(Provider):output_tokens: int,model: str,cached_tokens: int = 0,+ now: datetime | None = None,) -> float | None:"""Estimate cost for a request.@@ -782,6 +812,9 @@ class AnthropicProvider(Provider):Both paths apply Anthropic's long-context premium: on the Sonnet 4 / 4.5family a prompt over 200K re-prices the whole request (see``_LONG_CONTEXT_PREMIUM``).++ ``now`` selects a DeepSeek peak/off-peak tier (see+ :mod:`headroom.pricing.deepseek_tiers`); ``None`` reads the wall clock."""model = sanitize_anthropic_model_id(model)# LiteLLM knows per-model cache and long-context rates, so let it price@@ -791,6 +824,7 @@ class AnthropicProvider(Provider):input_tokens=input_tokens,output_tokens=output_tokens,cached_tokens=cached_tokens,+ now=now,)if cost is not None:return costdiff --git a/headroom/providers/openai.py b/headroom/providers/openai.pyindex bd224dd58..5243ebfca 100644--- a/headroom/providers/openai.py+++ b/headroom/providers/openai.py@@ -109,9 +109,11 @@ _CONTEXT_LIMITS: dict[str, int] = {"o3-mini": 200000,"o4-mini": 200000,# DeepSeek (often accessed via OpenAI-compatible API). Values verified- # against api-docs.deepseek.com (V4) and LiteLLM model_cost (deprecated- # aliases). LiteLLM lookup is still attempted first in get_context_limit;- # these are the manual fallback when LiteLLM doesn't know the model.+ # against api-docs.deepseek.com (V4.1-Flash / V4-Pro-0813) and LiteLLM+ # model_cost (deprecated aliases). LiteLLM lookup is still attempted first in+ # get_context_limit; these are the manual fallback when LiteLLM doesn't know+ # the model.+ "deepseek-flash": 1_000_000,"deepseek-v4-flash": 1_000_000,"deepseek-v4-pro": 1_000_000,"deepseek-v3.2": 128_000,diff --git a/headroom/providers/openai_compatible.py b/headroom/providers/openai_compatible.pyindex 4cb6673ce..34d08cde1 100644--- a/headroom/providers/openai_compatible.py+++ b/headroom/providers/openai_compatible.py@@ -98,6 +98,7 @@ _DEFAULT_CONTEXT_LIMITS: dict[str, int] = {"deepseek-r1-0528": 131072,"deepseek-reasoner": 131072,"deepseek-v4": 1048576,+ "deepseek-flash": 1_000_000,"deepseek-v4-pro": 1_000_000,"deepseek-v4-flash": 1_000_000,"deepseek-chat": 131072,@@ -313,10 +314,13 @@ class OpenAICompatibleProvider(Provider):if model_lower in _DEFAULT_CONTEXT_LIMITS:return _DEFAULT_CONTEXT_LIMITS[model_lower]- # Prefix match- for prefix, limit in _DEFAULT_CONTEXT_LIMITS.items():+ # Prefix match, longest prefix first. Plain dict order let a shorter+ # family shadow a longer one: "deepseek-v4-flash-vision-exp" hit the+ # "deepseek-v4" entry and got 1_048_576 instead of the 1_000_000 the+ # model registry, the OpenAI provider and the pricing tables agree on.+ for prefix in sorted(_DEFAULT_CONTEXT_LIMITS, key=len, reverse=True):if model_lower.startswith(prefix):- return limit+ return _DEFAULT_CONTEXT_LIMITS[prefix]# Default to 128K for modern modelsreturn 128000diff --git a/headroom/tokenizers/huggingface.py b/headroom/tokenizers/huggingface.pyindex 255866736..0afa93d0f 100644--- a/headroom/tokenizers/huggingface.py+++ b/headroom/tokenizers/huggingface.py@@ -80,9 +80,12 @@ MODEL_TO_TOKENIZER: dict[str, str] = {"deepseek-r1": "deepseek-ai/DeepSeek-R1","deepseek-r1-0528": "deepseek-ai/DeepSeek-R1-0528","deepseek-reasoner": "deepseek-ai/DeepSeek-R1",- # DeepSeek V4 family (2025-2026)+ # DeepSeek V4 family (2025-2026). The retired v4-flash ids are still+ # accepted on the wire but served by V4.1-Flash, so they resolve there.+ "deepseek-flash": "deepseek-ai/DeepSeek-V4.1-Flash","deepseek-v4-pro": "deepseek-ai/DeepSeek-V4-Pro",- "deepseek-v4-flash": "deepseek-ai/DeepSeek-V4-Flash",+ "deepseek-v4-flash": "deepseek-ai/DeepSeek-V4.1-Flash",+ "deepseek-v4-flash-vision-exp": "deepseek-ai/DeepSeek-V4.1-Flash",# DeepSeek API aliases (routed through the proxy)"deepseek-chat": "deepseek-ai/DeepSeek-V3","deepseek-r1-distill-qwen": "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B",diff --git a/tests/test_huggingface_tokenizer_timeout.py b/tests/test_huggingface_tokenizer_timeout.pyindex 4a8a4fb97..a04ada7e4 100644--- a/tests/test_huggingface_tokenizer_timeout.py+++ b/tests/test_huggingface_tokenizer_timeout.py@@ -121,7 +121,9 @@ def test_count_messages_fails_open_to_estimation(monkeypatch: pytest.MonkeyPatchdef test_deepseek_model_aliases_resolve_to_expected_tokenizers() -> None:assert get_tokenizer_name("deepseek-v3.2") == "deepseek-ai/DeepSeek-V3.2"assert get_tokenizer_name("deepseek-v4-pro") == "deepseek-ai/DeepSeek-V4-Pro"- assert get_tokenizer_name("deepseek-v4-flash") == "deepseek-ai/DeepSeek-V4-Flash"+ assert get_tokenizer_name("deepseek-v4-flash") == "deepseek-ai/DeepSeek-V4.1-Flash"+ assert get_tokenizer_name("deepseek-flash") == "deepseek-ai/DeepSeek-V4.1-Flash"+ assert get_tokenizer_name("deepseek-v4-flash-vision-exp") == "deepseek-ai/DeepSeek-V4.1-Flash"assert get_tokenizer_name("deepseek-r1") == "deepseek-ai/DeepSeek-R1"assert get_tokenizer_name("deepseek-r1-0528") == "deepseek-ai/DeepSeek-R1-0528"diff --git a/tests/test_models.py b/tests/test_models.pyindex 117d7c031..d8c7e105c 100644--- a/tests/test_models.py+++ b/tests/test_models.py@@ -314,3 +314,27 @@ class TestBuiltInModels:info = get_model_info("mistral-large")assert info.provider == "mistral"assert info.supports_tools is True+++def test_deepseek_flash_is_registered_with_vision_and_legacy_aliases() -> None:+ """The current DeepSeek id carries V4.1-Flash capabilities; retired ids alias it."""+ from headroom.models.registry import ModelRegistry++ flash = ModelRegistry.get("deepseek-flash")+ assert flash is not None+ assert flash.provider == "deepseek"+ assert flash.context_window == 1_000_000+ assert flash.max_output_tokens == 384_000+ assert flash.supports_vision is True+ assert flash.supports_tools is True+ assert flash.tokenizer_backend == "huggingface"+ assert set(flash.aliases) == {"deepseek-v4-flash", "deepseek-v4-flash-vision-exp"}++ for alias in ("deepseek-v4-flash", "deepseek-v4-flash-vision-exp"):+ resolved = ModelRegistry.get(alias)+ assert resolved is not None+ assert resolved.name == "deepseek-flash"++ pro = ModelRegistry.get("deepseek-v4-pro")+ assert pro is not None+ assert pro.supports_vision is Falsediff --git a/tests/test_pricing.py b/tests/test_pricing.pyindex 6b81c665b..cf6b9371b 100644--- a/tests/test_pricing.py+++ b/tests/test_pricing.py@@ -30,12 +30,16 @@ def test_pricing_public_exports_and_provider_registries() -> None:assert "get_deepseek_registry" in pricing.__all__assert "DEEPSEEK_PRICES" in pricing.__all__+ assert "OFF_PEAK_RATES_PER_1M" in pricing.__all__+ assert "DeepSeekRates" in pricing.__all__+ assert "off_peak_rates" in pricing.__all__+ assert "rates_for" in pricing.__all__deepseek_registry = pricing.get_deepseek_registry()assert deepseek_registry.source_url == "https://api-docs.deepseek.com/quick_start/pricing"flash = deepseek_registry.get_price("deepseek-v4-flash")assert flash is not None- assert flash.input_per_1m == 0.14- assert flash.output_per_1m == 0.28+ assert flash.input_per_1m == 0.15+ assert flash.output_per_1m == 0.60openai_registry.prices.pop("gpt-4o")anthropic_registry.prices.pop("claude-3-5-sonnet-20241022")diff --git a/tests/test_pricing_deepseek_tiers.py b/tests/test_pricing_deepseek_tiers.pynew file mode 100644index 000000000..9768df491--- /dev/null+++ b/tests/test_pricing_deepseek_tiers.py@@ -0,0 +1,247 @@+"""Tests for DeepSeek peak/off-peak tier resolution."""++from __future__ import annotations++from datetime import datetime, timezone++import pytest++from headroom.pricing.deepseek_tiers import (+ LEGACY_MODEL_IDS,+ OFF_PEAK_RATES_PER_1M,+ PEAK_MULTIPLIER,+ VENDOR_CARD,+ WEEKEND_OFF_PEAK_FROM,+ bare_model,+ is_peak,+ off_peak_rates,+ rates_for,+)+++class TestBareModel:+ def test_provider_prefix_is_stripped(self):+ assert bare_model("deepseek/deepseek-v4-pro") == "deepseek-v4-pro"++ def test_case_is_normalised(self):+ assert bare_model("DeepSeek-Flash") == "deepseek-flash"++ def test_bare_id_is_returned_unchanged(self):+ assert bare_model("deepseek-flash") == "deepseek-flash"+++class TestOffPeakRates:+ def test_flash_matches_the_published_off_peak_usd_list(self):+ rates = off_peak_rates("deepseek-flash")+ assert rates is not None+ assert rates.cache_hit_per_1m == 0.003+ assert rates.input_per_1m == 0.15+ assert rates.output_per_1m == 0.60+ assert rates.tier == "off_peak"++ def test_pro_matches_the_published_off_peak_usd_list(self):+ rates = off_peak_rates("deepseek-v4-pro")+ assert rates is not None+ assert rates.cache_hit_per_1m == 0.022+ assert rates.input_per_1m == 0.66+ assert rates.output_per_1m == 1.98++ def test_deepseek_bills_no_cache_write_surcharge(self):+ rates = off_peak_rates("deepseek-flash")+ assert rates is not None+ assert rates.cache_write_per_1m == 0.0++ def test_peak_is_exactly_twice_off_peak(self):+ assert PEAK_MULTIPLIER == 2.0++ @pytest.mark.parametrize(+ "alias",+ ["deepseek-v4-flash", "deepseek-v4-flash-vision-exp"],+ )+ def test_retired_ids_resolve_to_the_flash_tier(self, alias):+ flash = off_peak_rates("deepseek-flash")+ legacy = off_peak_rates(alias)+ assert flash is not None+ assert legacy is not None+ assert (+ legacy.cache_hit_per_1m,+ legacy.input_per_1m,+ legacy.output_per_1m,+ ) == (+ flash.cache_hit_per_1m,+ flash.input_per_1m,+ flash.output_per_1m,+ )++ @pytest.mark.parametrize(+ "model",+ ["deepseek/deepseek-flash", "deepseek/deepseek-v4-pro"],+ )+ def test_provider_prefixed_ids_are_tiered(self, model):+ assert off_peak_rates(model) is not None++ @pytest.mark.parametrize(+ "model",+ ["deepseek-chat", "deepseek-reasoner", "deepseek-v3.2", "gpt-4o", ""],+ )+ def test_out_of_scope_models_have_no_tier(self, model):+ assert off_peak_rates(model) is None++ def test_rates_are_frozen(self):+ rates = off_peak_rates("deepseek-flash")+ assert rates is not None+ with pytest.raises(AttributeError):+ rates.input_per_1m = 1.0 # type: ignore[misc]+++def utc(day: int, hour: int, minute: int = 0) -> datetime:+ """UTC instant in August 2026; day 17 is a Monday, 22/29 Saturdays, 23 Sunday."""+ return datetime(2026, 8, day, hour, minute, tzinfo=timezone.utc)+++class TestBeijingPeakWindows:+ @pytest.mark.parametrize("hour,minute", [(1, 0), (3, 59), (6, 0), (9, 59)])+ def test_weekday_peak_windows(self, hour, minute):+ # 01:00-04:00 and 06:00-10:00 UTC are 09:00-12:00 and 14:00-18:00 Beijing.+ assert is_peak(utc(17, hour, minute)) is True++ @pytest.mark.parametrize("hour,minute", [(4, 0), (10, 0), (0, 0), (12, 0), (23, 59)])+ def test_weekday_off_peak(self, hour, minute):+ assert is_peak(utc(17, hour, minute)) is False++ def test_pre_rule_saturday_keeps_the_weekday_windows(self):+ # 2026-08-22 02:00 UTC is Saturday 10:00 Beijing, before the rule takes+ # effect, so it is still peak.+ assert WEEKEND_OFF_PEAK_FROM > utc(22, 2)+ assert is_peak(utc(22, 2)) is True++ @pytest.mark.parametrize("day,hour", [(23, 2), (23, 7), (29, 2), (29, 7)])+ def test_post_rule_weekends_are_all_day_off_peak(self, day, hour):+ assert is_peak(utc(day, hour)) is False++ # Every peak case above uses 2026-08-17, which is *before*+ # WEEKEND_OFF_PEAK_FROM, so none of them pins the weekday arm of the weekend+ # gate. Without the cases below, a mutant that made the gate unconditional+ # ("no peak pricing ever again" from 2026-08-23) would pass the whole file.+ @pytest.mark.parametrize("hour", [2, 7])+ def test_post_rule_weekday_peak_windows_still_apply(self, hour):+ # 2026-08-24 is a Monday: 02:00 and 07:00 UTC are 10:00 and 15:00 Beijing.+ assert is_peak(utc(24, hour)) is True++ def test_post_rule_weekday_evening_is_off_peak(self):+ # 12:00 UTC on that same Monday is 20:00 Beijing, outside both windows.+ assert is_peak(utc(24, 12)) is False++ def test_the_effective_instant_itself_is_off_peak(self):+ assert WEEKEND_OFF_PEAK_FROM == utc(22, 16)+ assert is_peak(WEEKEND_OFF_PEAK_FROM) is False+ # The instant above is Beijing Sunday 00:00, outside both windows, so it+ # is off-peak with or without the weekend rule. This boundary is the+ # informative one: Saturday 09:00 Beijing is inside a window but only+ # just before the gate, so peak here means the gate did not apply early.+ assert WEEKEND_OFF_PEAK_FROM > utc(22, 1)+ assert is_peak(utc(22, 1)) is True++ def test_naive_instants_are_read_as_utc(self):+ assert is_peak(datetime(2026, 8, 17, 2, 0)) is True+ assert is_peak(datetime(2026, 8, 17, 12, 0)) is False+++class TestRatesFor:+ def test_peak_instant_selects_the_peak_tier(self):+ off = off_peak_rates("deepseek-flash")+ rates = rates_for("deepseek-flash", utc(17, 2))+ assert off is not None+ assert rates is not None+ assert rates.tier == "peak"+ # Literal vendor peak rates, not just the product of the off-peak row and+ # PEAK_MULTIPLIER: re-deriving from the same constant cannot catch a wrong+ # multiplier or a field that should not have been multiplied.+ assert rates.cache_hit_per_1m == 0.006+ assert rates.input_per_1m == 0.30+ assert rates.output_per_1m == 1.20+ assert rates.cache_write_per_1m == 0.0+ assert rates.input_per_1m == off.input_per_1m * PEAK_MULTIPLIER++ def test_off_peak_instant_selects_the_off_peak_tier(self):+ off = off_peak_rates("deepseek-v4-pro")+ rates = rates_for("deepseek-v4-pro", utc(17, 12))+ assert off is not None+ assert rates is not None+ assert rates.tier == "off_peak"+ assert rates.input_per_1m == off.input_per_1m++ def test_post_rule_weekday_instant_selects_the_peak_tier(self):+ # The live regime: past WEEKEND_OFF_PEAK_FROM, a Monday mid-morning must+ # still bill peak, through the seam the cost paths actually call.+ rates = rates_for("deepseek-flash", utc(24, 2))+ assert rates is not None+ assert rates.tier == "peak"+ assert rates.input_per_1m == 0.30++ def test_default_instant_reads_the_wall_clock(self):+ before = datetime.now(timezone.utc)+ rates = rates_for("deepseek-flash")+ after = datetime.now(timezone.utc)+ assert rates is not None+ # The default read is its own clock read, so it can land on either side of+ # a window boundary. Bracket it and accept either bracket's tier.+ assert rates.tier in {+ "peak" if is_peak(before) else "off_peak",+ "peak" if is_peak(after) else "off_peak",+ }+++class TestVendoredCard:+ """The shipping tables must equal the vendored vendor card.++ The card exists so a reviewer can check the published numbers inside the repo+ instead of trusting the PR description, and so editing a rate without its+ source fails the suite.+ """++ def test_shipping_tables_match_the_vendored_card(self):+ assert set(VENDOR_CARD) == set(OFF_PEAK_RATES_PER_1M)+ for model, row in VENDOR_CARD.items():+ assert OFF_PEAK_RATES_PER_1M[model] == row["off_peak"]+ # Monday 2026-08-17 02:00 UTC = 10:00 Beijing = peak.+ peak = rates_for(model, utc(17, 2))+ assert peak is not None+ assert (+ peak.cache_hit_per_1m,+ peak.input_per_1m,+ peak.output_per_1m,+ ) == row["peak"]+ assert row["peak"] == tuple(rate * PEAK_MULTIPLIER for rate in row["off_peak"])++ def test_vendored_legacy_ids_match_the_alias_table(self):+ vendored = {legacy for row in VENDOR_CARD.values() for legacy in row["legacy_ids"]}+ assert vendored == set(LEGACY_MODEL_IDS)+ for legacy in vendored:+ assert off_peak_rates(legacy) == off_peak_rates("deepseek-flash")++ def test_naive_instant_selects_the_same_tier_as_the_aware_one(self):+ aware = rates_for("deepseek-flash", utc(17, 2))+ naive = rates_for("deepseek-flash", datetime(2026, 8, 17, 2))+ assert aware is not None+ assert naive is not None+ assert (naive.tier, naive.input_per_1m) == (aware.tier, aware.input_per_1m)++ @pytest.mark.parametrize(+ "model",+ [+ "deepseek-v4-flash",+ "deepseek-v4-flash-vision-exp",+ "deepseek/deepseek-v4-pro",+ ],+ )+ def test_aliases_and_prefixed_ids_are_tiered(self, model):+ # Compare against the bare id rather than asserting non-None: a prefix bug+ # that resolved the prefixed id to the wrong model's row would still be+ # non-None.+ bare = model.rsplit("/", 1)[-1]+ assert rates_for(model, utc(17, 2)) == rates_for(bare, utc(17, 2))++ @pytest.mark.parametrize("model", ["deepseek-chat", "gpt-4o", ""])+ def test_out_of_scope_models_return_none(self, model):+ assert rates_for(model, utc(17, 2)) is Nonediff --git a/tests/test_providers/test_deepseek.py b/tests/test_providers/test_deepseek.pyindex b8ba48d90..15a033194 100644--- a/tests/test_providers/test_deepseek.py+++ b/tests/test_providers/test_deepseek.py@@ -1,44 +1,69 @@"""Tests for DeepSeek model pricing and cost estimation."""+from datetime import datetime, timezone+import pytestfrom headroom.pricing.deepseek_prices import (DEEPSEEK_PRICES,get_deepseek_registry,)-from headroom.pricing.registry import PricingRegistry+from headroom.pricing.registry import ModelPricing, PricingRegistry++# Monday 2026-08-17: 02:00 UTC = 10:00 Beijing (peak), 12:00 UTC = 20:00 Beijing (off-peak).+PEAK = datetime(2026, 8, 17, 2, 0, tzinfo=timezone.utc)+OFF_PEAK = datetime(2026, 8, 17, 12, 0, tzinfo=timezone.utc)class TestDeepSeekPricingModule:"""Tests for the DeepSeek pricing data module."""- def test_deepseek_pricing_contains_v4_models(self):- assert "deepseek-v4-flash" in DEEPSEEK_PRICES+ def test_deepseek_pricing_contains_every_current_id(self):+ assert "deepseek-flash" in DEEPSEEK_PRICESassert "deepseek-v4-pro" in DEEPSEEK_PRICES- assert len(DEEPSEEK_PRICES) == 2-- def test_deepseek_v4_flash_pricing(self):- pricing = DEEPSEEK_PRICES["deepseek-v4-flash"]- assert pricing.input_per_1m == 0.14- assert pricing.output_per_1m == 0.28- assert pricing.cached_input_per_1m == 0.0028+ assert "deepseek-v4-flash" in DEEPSEEK_PRICES+ assert "deepseek-v4-flash-vision-exp" in DEEPSEEK_PRICES+ assert len(DEEPSEEK_PRICES) == 4++ def test_deepseek_flash_pricing_is_the_off_peak_tier(self):+ pricing = DEEPSEEK_PRICES["deepseek-flash"]+ assert pricing.input_per_1m == 0.15+ assert pricing.output_per_1m == 0.60+ assert pricing.cached_input_per_1m == 0.003assert pricing.context_window == 1_000_000assert pricing.provider == "deepseek"assert pricing.notes is not None- def test_deepseek_v4_pro_pricing(self):+ def test_deepseek_v4_pro_pricing_is_the_off_peak_tier(self):pricing = DEEPSEEK_PRICES["deepseek-v4-pro"]- assert pricing.input_per_1m == 0.435- assert pricing.output_per_1m == 0.87- assert pricing.cached_input_per_1m == 0.003625+ assert pricing.input_per_1m == 0.66+ assert pricing.output_per_1m == 1.98+ assert pricing.cached_input_per_1m == 0.022assert pricing.context_window == 1_000_000assert pricing.provider == "deepseek"assert pricing.notes is not None+ def test_legacy_flash_ids_carry_the_flash_rates(self):+ flash = DEEPSEEK_PRICES["deepseek-flash"]+ for alias in ("deepseek-v4-flash", "deepseek-v4-flash-vision-exp"):+ row = DEEPSEEK_PRICES[alias]+ assert row.model == alias+ assert (+ row.input_per_1m,+ row.output_per_1m,+ row.cached_input_per_1m,+ ) == (+ flash.input_per_1m,+ flash.output_per_1m,+ flash.cached_input_per_1m,+ )+def test_get_deepseek_registry(self):registry = get_deepseek_registry()assert isinstance(registry, PricingRegistry)+ assert registry.get_price("deepseek-flash") is not Noneassert registry.get_price("deepseek-v4-flash") is not None+ assert registry.get_price("deepseek-v4-flash-vision-exp") is not Noneassert registry.get_price("deepseek-v4-pro") is not Noneassert registry.get_price("nonexistent") is None@@ -55,61 +80,154 @@ class TestDeepSeekPricingModule:def test_deepseek_registry_estimate_cost(self):registry = get_deepseek_registry()- cost = registry.estimate_cost("deepseek-v4-flash", input_tokens=1_000_000)- assert cost.cost_usd == 0.14+ cost = registry.estimate_cost("deepseek-flash", input_tokens=1_000_000, now=OFF_PEAK)+ assert cost.cost_usd == pytest.approx(0.15)assert "input" in cost.breakdownassert cost.pricing_date is not Nonedef test_deepseek_registry_estimate_cost_with_cached(self):registry = get_deepseek_registry()cost = registry.estimate_cost(- "deepseek-v4-flash",+ "deepseek-flash",input_tokens=1_000_000,cached_input_tokens=1_000_000,+ now=OFF_PEAK,+ )+ assert cost.cost_usd == pytest.approx(0.15 + 0.003)++ def test_registry_prices_the_peak_tier_at_a_peak_instant(self):+ registry = get_deepseek_registry()+ peak = registry.estimate_cost("deepseek-flash", input_tokens=1_000_000, now=PEAK)+ off = registry.estimate_cost("deepseek-flash", input_tokens=1_000_000, now=OFF_PEAK)+ assert off.cost_usd == pytest.approx(0.15)+ assert peak.cost_usd == pytest.approx(0.30)+ assert peak.breakdown["tier"] == "peak"+ assert off.breakdown["tier"] == "off_peak"+ assert peak.breakdown["input"]["rate_per_1m"] == pytest.approx(0.30)+ assert off.breakdown["input"]["rate_per_1m"] == pytest.approx(0.15)++ def test_registry_cached_bucket_uses_the_tier_cache_hit_rate(self):+ registry = get_deepseek_registry()+ cost = registry.estimate_cost(+ "deepseek-flash",+ cached_input_tokens=1_000_000,+ now=OFF_PEAK,+ )+ assert cost.cost_usd == pytest.approx(0.003)+ assert cost.breakdown["cached_input"]["rate_per_1m"] == pytest.approx(0.003)++ @pytest.mark.parametrize(+ ("kwargs", "match"),+ [+ ({"batch_input_tokens": 1}, "batch input pricing"),+ ({"batch_output_tokens": 1}, "batch output pricing"),+ ],+ )+ def test_registry_rejects_batch_tokens_for_deepseek(self, kwargs, match):+ registry = get_deepseek_registry()+ with pytest.raises(ValueError, match=match):+ registry.estimate_cost("deepseek-flash", now=OFF_PEAK, **kwargs)++ def test_tiered_and_flat_paths_carry_the_same_estimate_metadata(self):+ # A tagged id is out of tier scope, so it is priced by the flat body; the+ # two paths must not drift on how they build the CostEstimate.+ registry = get_deepseek_registry()+ registry.prices["deepseek/deepseek-v4-pro:free"] = DEEPSEEK_PRICES["deepseek-v4-pro"]++ tiered = registry.estimate_cost("deepseek-flash", input_tokens=1_000_000, now=OFF_PEAK)+ flat = registry.estimate_cost(+ "deepseek/deepseek-v4-pro:free", input_tokens=1_000_000, now=OFF_PEAK+ )+ assert (tiered.pricing_date, tiered.is_stale, tiered.warning) == (+ flat.pricing_date,+ flat.is_stale,+ flat.warning,+ )++ def test_registry_ignores_a_flat_row_for_a_tiered_id(self):+ # The tier table is authoritative for flash/pro, so a stale or custom flat+ # row for one of those ids must not be able to reprice it (the documented+ # contract on estimate_cost).+ registry = get_deepseek_registry()+ registry.prices["deepseek-flash"] = ModelPricing(+ model="deepseek-flash",+ provider="deepseek",+ input_per_1m=999.0,+ output_per_1m=999.0,)- assert cost.cost_usd == 0.14 + 0.0028++ cost = registry.estimate_cost("deepseek-flash", input_tokens=1_000_000, now=OFF_PEAK)++ assert cost.cost_usd == pytest.approx(0.15)+ assert cost.breakdown["input"]["rate_per_1m"] == pytest.approx(0.15)+ assert cost.breakdown["tier"] == "off_peak"class TestDeepSeekLiteLLMInjection:- """Tests for DeepSeek V4 pricing injection into litellm."""+ """Tests for DeepSeek pricing injection into litellm."""- def test_deepseek_v4_models_in_litellm_model_cost(self):+ def test_deepseek_ids_in_litellm_model_cost(self):from headroom.pricing.litellm_pricing import LITELLM_AVAILABLE, litellmif not LITELLM_AVAILABLE:pytest.skip("litellm not available")- assert "deepseek-v4-flash" in litellm.model_cost- assert "deepseek-v4-pro" in litellm.model_cost+ for model in (+ "deepseek-flash",+ "deepseek-v4-flash",+ "deepseek-v4-flash-vision-exp",+ "deepseek-v4-pro",+ ):+ assert model in litellm.model_cost+ assert f"deepseek/{model}" in litellm.model_cost- def test_deepseek_v4_prefixed_models_in_litellm_model_cost(self):+ def test_deepseek_flash_litellm_rows_are_off_peak(self):from headroom.pricing.litellm_pricing import LITELLM_AVAILABLE, litellmif not LITELLM_AVAILABLE:pytest.skip("litellm not available")- assert "deepseek/deepseek-v4-flash" in litellm.model_cost- assert "deepseek/deepseek-v4-pro" in litellm.model_cost-- def test_deepseek_v4_flash_litellm_pricing(self):+ for key in ("deepseek-flash", "deepseek/deepseek-flash"):+ row = litellm.model_cost[key]+ assert row["input_cost_per_token"] == 0.15 / 1_000_000+ assert row["output_cost_per_token"] == 0.60 / 1_000_000+ assert row["cache_read_input_token_cost"] == 0.003 / 1_000_000+ assert row["litellm_provider"] == "deepseek"+ assert row["max_input_tokens"] == 1_000_000+ assert row["max_output_tokens"] == 393_216++ def test_deepseek_v4_pro_litellm_rows_are_off_peak(self):from headroom.pricing.litellm_pricing import LITELLM_AVAILABLE, litellmif not LITELLM_AVAILABLE:pytest.skip("litellm not available")- flash = litellm.model_cost["deepseek-v4-flash"]- assert flash["input_cost_per_token"] > 0- assert flash["output_cost_per_token"] > 0- assert flash["litellm_provider"] == "deepseek"-- def test_deepseek_v4_pro_litellm_pricing(self):- from headroom.pricing.litellm_pricing import LITELLM_AVAILABLE, litellm+ for key in ("deepseek-v4-pro", "deepseek/deepseek-v4-pro"):+ row = litellm.model_cost[key]+ assert row["input_cost_per_token"] == 0.66 / 1_000_000+ assert row["output_cost_per_token"] == 1.98 / 1_000_000+ assert row["cache_read_input_token_cost"] == 0.022 / 1_000_000+ assert row["litellm_provider"] == "deepseek"+ assert row["max_input_tokens"] == 1_000_000+ assert row["max_output_tokens"] == 393_216++ def test_injected_rows_track_the_tier_table(self):+ """Injected rows are the tier module's off-peak figures, not a copy."""+ from headroom.pricing.deepseek_tiers import OFF_PEAK_RATES_PER_1M+ from headroom.pricing.litellm_pricing import (+ _DEEPSEEK_LITELLM_IDS,+ LITELLM_AVAILABLE,+ litellm,+ )if not LITELLM_AVAILABLE:pytest.skip("litellm not available")- pro = litellm.model_cost["deepseek-v4-pro"]- assert pro["input_cost_per_token"] > 0- assert pro["output_cost_per_token"] > 0- assert pro["litellm_provider"] == "deepseek"-- def test_cost_per_token_resolves_deepseek_v4_flash(self):+ for model_id, canonical in _DEEPSEEK_LITELLM_IDS.items():+ cache_hit, miss, out = OFF_PEAK_RATES_PER_1M[canonical]+ for key in (model_id, f"deepseek/{model_id}"):+ row = litellm.model_cost[key]+ assert row["input_cost_per_token"] == miss / 1e6+ assert row["output_cost_per_token"] == out / 1e6+ assert row["cache_read_input_token_cost"] == cache_hit / 1e6++ def test_cost_per_token_resolves_deepseek_flash(self):from headroom.pricing.litellm_pricing import (LITELLM_AVAILABLE,litellm,@@ -122,7 +240,7 @@ class TestDeepSeekLiteLLMInjection:# litellm.cost_per_token can determine the provider via# get_llm_provider(). Bare model names without a provider prefix# would fail with BadRequestError.- resolved = resolve_litellm_model("deepseek-v4-flash")+ resolved = resolve_litellm_model("deepseek-flash")# The point of this test is RESOLUTION: an unprefixed name raises# BadRequestError out of get_llm_provider(), so reaching a price at all# is the assertion. Not raising is half of it.@@ -131,7 +249,7 @@ class TestDeepSeekLiteLLMInjection:prompt_tokens=1_000_000,completion_tokens=1_000_000,)- active_pricing = litellm.model_cost["deepseek-v4-flash"]+ active_pricing = litellm.model_cost["deepseek-flash"]list_input = active_pricing["input_cost_per_token"] * 1_000_000list_output = active_pricing["output_cost_per_token"] * 1_000_000@@ -153,66 +271,66 @@ class TestDeepSeekLiteLLMInjection:)def test_resolve_litellm_model_prefixes_deepseek(self):- from headroom.pricing.litellm_pricing import resolve_litellm_model+ from headroom.pricing.litellm_pricing import LITELLM_AVAILABLE, resolve_litellm_model- resolved = resolve_litellm_model("deepseek-v4-flash")- assert resolved == "deepseek/deepseek-v4-flash"+ if not LITELLM_AVAILABLE:+ pytest.skip("litellm not available")+ assert resolve_litellm_model("deepseek-flash") == "deepseek/deepseek-flash"+ assert resolve_litellm_model("deepseek-v4-pro") == "deepseek/deepseek-v4-pro"- def test_injection_does_not_overwrite_existing_upstream_entries(self):- """If litellm upstream already has these, our injection is a no-op."""+ def test_injection_overrides_upstream_peak_rows(self):+ """Upstream litellm ships these ids at peak; the flat figure stays off-peak."""from headroom.pricing.litellm_pricing import LITELLM_AVAILABLE, litellmif not LITELLM_AVAILABLE:pytest.skip("litellm not available")- # Force-inject with wrong value, then verify the injection guard- litellm.model_cost["deepseek-v4-flash"] = {"input_cost_per_token": 999}- # Reimport to trigger _inject_deepseek_pricing — but it should NOT overwrite- import importlibimport headroom.pricing.litellm_pricing as lp- importlib.reload(lp)- assert litellm.model_cost["deepseek-v4-flash"]["input_cost_per_token"] == 999- # Reset to correct value- litellm.model_cost["deepseek-v4-flash"] = {- "input_cost_per_token": 0.14 / 1_000_000,- "output_cost_per_token": 0.28 / 1_000_000,- "cache_read_input_token_cost": 0.0028 / 1_000_000,- "litellm_provider": "deepseek",- "max_tokens": 384_000,- "max_input_tokens": 1_000_000,- }+ saved = dict(litellm.model_cost["deepseek-flash"])+ try:+ litellm.model_cost["deepseek-flash"] = {"input_cost_per_token": 0.30 / 1_000_000}+ lp._inject_deepseek_pricing()+ assert litellm.model_cost["deepseek-flash"]["input_cost_per_token"] == 0.15 / 1_000_000+ finally:+ litellm.model_cost["deepseek-flash"] = savedclass TestDeepSeekAnthropicProviderFallback:"""Tests that Anthropic provider's _get_pricing handles DeepSeek models."""- def test_deepseek_v4_flash_fallback(self):+ def test_deepseek_flash_fallback_rates_are_off_peak(self):from headroom.providers.anthropic import AnthropicProvider- provider = AnthropicProvider()- pricing = provider._get_pricing("deepseek-v4-flash")+ pricing = AnthropicProvider()._get_pricing("deepseek-flash")assert pricing is not None- assert pricing["input"] == 0.14- assert pricing["output"] == 0.28- assert pricing["cached_input"] == 0.0028+ assert pricing["input"] == 0.15+ assert pricing["output"] == 0.60+ assert pricing["cached_input"] == 0.003- def test_deepseek_v4_pro_fallback(self):+ def test_deepseek_v4_pro_fallback_rates_are_off_peak(self):from headroom.providers.anthropic import AnthropicProvider- provider = AnthropicProvider()- pricing = provider._get_pricing("deepseek-v4-pro")+ pricing = AnthropicProvider()._get_pricing("deepseek-v4-pro")assert pricing is not None- assert pricing["input"] == 0.435- assert pricing["output"] == 0.87- assert pricing["cached_input"] == 0.003625+ assert pricing["input"] == 0.66+ assert pricing["output"] == 1.98+ assert pricing["cached_input"] == 0.022- def test_deepseek_unknown_model_returns_none(self):+ def test_retired_flash_id_shares_the_flash_rates(self):from headroom.providers.anthropic import AnthropicProviderprovider = AnthropicProvider()- pricing = provider._get_pricing("deepseek-unknown-model")- assert pricing is None+ legacy = provider._get_pricing("deepseek-v4-flash")+ current = provider._get_pricing("deepseek-flash")+ assert current is not None+ assert legacy == current+ assert provider._get_pricing("deepseek-v4-flash-vision-exp") == current++ def test_deepseek_unknown_model_returns_none(self):+ from headroom.providers.anthropic import AnthropicProvider++ assert AnthropicProvider()._get_pricing("deepseek-unknown-model") is Nonedef test_deepseek_partial_match_v4_flash_alias(self):from headroom.providers.anthropic import AnthropicProvider@@ -222,28 +340,138 @@ class TestDeepSeekAnthropicProviderFallback:pricing = provider._get_pricing("deepseek-v4-flash-v1")assert pricing is not None- def test_estimate_cost_deepseek_v4_flash(self):+ def test_estimate_cost_prices_the_off_peak_tier(self):from headroom.providers.anthropic import AnthropicProvider- provider = AnthropicProvider()- cost = provider.estimate_cost(+ cost = AnthropicProvider().estimate_cost(input_tokens=1_000_000,output_tokens=0,- model="deepseek-v4-flash",+ model="deepseek-flash",+ now=OFF_PEAK,)- assert cost is not None- assert cost == 0.14+ assert cost == pytest.approx(0.15)- def test_estimate_cost_deepseek_v4_flash_with_cache(self):+ def test_estimate_cost_prices_the_peak_tier(self):from headroom.providers.anthropic import AnthropicProvider- provider = AnthropicProvider()- cost = provider.estimate_cost(+ cost = AnthropicProvider().estimate_cost(+ input_tokens=1_000_000,+ output_tokens=0,+ model="deepseek-flash",+ now=PEAK,+ )+ assert cost == pytest.approx(0.30)++ def test_estimate_cost_cached_tokens_use_the_cache_hit_rate(self):+ from headroom.providers.anthropic import AnthropicProvider++ cost = AnthropicProvider().estimate_cost(+ input_tokens=1_000_000,+ output_tokens=0,+ model="deepseek-flash",+ cached_tokens=1_000_000,+ now=OFF_PEAK,+ )+ assert cost == pytest.approx(0.003)+++class TestDeepSeekTieredCost:+ """The per-request cost seam prices DeepSeek by the request's tier.++ The tier tests pass with litellm absent: the tier branch sits in front of+ the litellm availability check. The last two cover the litellm path itself.+ """++ def test_flash_off_peak_one_megatoken_each_way(self):+ from headroom.pricing.litellm_pricing import estimate_cost_from_tokens++ cost = estimate_cost_from_tokens(+ "deepseek-flash", input_tokens=1_000_000, output_tokens=1_000_000, now=OFF_PEAK+ )+ assert cost == pytest.approx(0.15 + 0.60)++ def test_peak_is_exactly_twice_off_peak(self):+ from headroom.pricing.litellm_pricing import estimate_cost_from_tokens++ peak = estimate_cost_from_tokens(+ "deepseek-flash", input_tokens=1_000_000, output_tokens=1_000_000, now=PEAK+ )+ off = estimate_cost_from_tokens(+ "deepseek-flash", input_tokens=1_000_000, output_tokens=1_000_000, now=OFF_PEAK+ )+ assert peak == pytest.approx(off * 2)++ def test_pro_off_peak_one_megatoken_each_way(self):+ from headroom.pricing.litellm_pricing import estimate_cost_from_tokens++ cost = estimate_cost_from_tokens(+ "deepseek-v4-pro", input_tokens=1_000_000, output_tokens=1_000_000, now=OFF_PEAK+ )+ assert cost == pytest.approx(0.66 + 1.98)++ def test_cached_tokens_bill_at_the_cache_hit_rate_exactly_once(self):+ from headroom.pricing.litellm_pricing import estimate_cost_from_tokens++ # input_tokens is the TOTAL prompt, cached included (litellm contract).+ cost = estimate_cost_from_tokens(+ "deepseek-flash",input_tokens=1_000_000,output_tokens=0,- model="deepseek-v4-flash",cached_tokens=1_000_000,+ now=OFF_PEAK,+ )+ assert cost == pytest.approx(0.003)++ def test_partially_cached_prompt_splits_the_two_input_rates(self):+ from headroom.pricing.litellm_pricing import estimate_cost_from_tokens++ cost = estimate_cost_from_tokens(+ "deepseek-flash",+ input_tokens=1_000_000,+ output_tokens=0,+ cached_tokens=400_000,+ now=OFF_PEAK,)+ assert cost == pytest.approx(0.6 * 0.15 + 0.4 * 0.003)++ @pytest.mark.parametrize(+ "model",+ ["deepseek-v4-flash", "deepseek-v4-flash-vision-exp", "deepseek/deepseek-v4-pro"],+ )+ def test_legacy_and_prefixed_ids_price_like_the_canonical_id(self, model):+ from headroom.pricing.litellm_pricing import estimate_cost_from_tokens++ canonical = "deepseek-v4-pro" if model.endswith("v4-pro") else "deepseek-flash"+ alias_cost = estimate_cost_from_tokens(+ model, input_tokens=1_000_000, output_tokens=0, now=OFF_PEAK+ )+ canonical_cost = estimate_cost_from_tokens(+ canonical, input_tokens=1_000_000, output_tokens=0, now=OFF_PEAK+ )+ # Both must be real numbers first: ``None == pytest.approx(None)`` is True,+ # so comparing unbound results would pass even if both fell out of scope.+ assert alias_cost is not None+ assert canonical_cost is not None+ assert alias_cost == pytest.approx(canonical_cost)++ def test_non_deepseek_models_still_take_the_litellm_path(self):+ from headroom.pricing.litellm_pricing import LITELLM_AVAILABLE, estimate_cost_from_tokens++ if not LITELLM_AVAILABLE:+ pytest.skip("litellm not available")+ cost = estimate_cost_from_tokens("gpt-4o", input_tokens=1_000_000, now=OFF_PEAK)assert cost is not None- # All 1M input tokens are cached, so cost = cached_input only- assert cost == 0.0028+ assert cost > 0.0++ def test_deepseek_models_outside_the_rate_card_take_the_litellm_path(self):+ from headroom.pricing.litellm_pricing import LITELLM_AVAILABLE, estimate_cost_from_tokens++ if not LITELLM_AVAILABLE:+ pytest.skip("litellm not available")+ # deepseek-chat is not on the flash/pro card, so the tier branch must not+ # claim it; litellm prices it (0.28/0.42 per 1M). The provider prefix is+ # required: litellm.cost_per_token refuses a bare "deepseek-chat".+ cost = estimate_cost_from_tokens(+ "deepseek/deepseek-chat", input_tokens=1_000_000, now=OFF_PEAK+ )+ assert cost == pytest.approx(0.28, rel=0.01)diff --git a/tests/test_providers/test_universal.py b/tests/test_providers/test_universal.pyindex aa3f337d4..3ac692926 100644--- a/tests/test_providers/test_universal.py+++ b/tests/test_providers/test_universal.py@@ -1,6 +1,7 @@"""Tests for universal provider support.-Tests OpenAICompatibleProvider, GoogleProvider, and LiteLLMProvider.+Tests OpenAICompatibleProvider, OpenAIProvider, GoogleProvider, and+LiteLLMProvider."""from __future__ import annotations@@ -12,6 +13,7 @@ from headroom.providers import (LiteLLMProvider,ModelCapabilities,OpenAICompatibleProvider,+ OpenAIProvider,create_anyscale_provider,create_fireworks_provider,create_groq_provider,@@ -90,7 +92,13 @@ class TestOpenAICompatibleProvider:assert provider.get_context_limit("deepseek-v2") == 128000assert provider.get_context_limit("deepseek-v3.2") == 128000assert provider.get_context_limit("deepseek-v4-pro") == 1_000_000- assert provider.get_context_limit("deepseek-v4-flash") == 1_000_000+ # All three flash-family ids — the current one plus the two retired+ # aliases DeepSeek still accepts — are served by V4.1-Flash at 1M on+ # both providers that carry a DeepSeek row.+ for p in (provider, OpenAIProvider()):+ assert p.get_context_limit("deepseek-flash") == 1_000_000+ assert p.get_context_limit("deepseek-v4-flash") == 1_000_000+ assert p.get_context_limit("deepseek-v4-flash-vision-exp") == 1_000_000assert provider.get_context_limit("deepseek-r1") == 131072assert provider.get_context_limit("deepseek-coder-v2") == 128000