Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(cost): price models the static table never listed, and meter the Live socket's real model

Harald2219cdf48814
diff --git a/docs/BUGS.md b/docs/BUGS.md
index 7334a2cd96..4f702f5fdd 100644
--- a/docs/BUGS.md
+++ b/docs/BUGS.md
@@ -9767,3 +9767,72 @@ and `float()` raised — logged as a WARNING on every probe. A provider that was
not instantiated has no handshake this session could wait for, so its
declaration cannot stretch the budget; the probe now skips a descriptor
quietly. **Tests.** `tests/unit/realtime/test_factory_handshake_budget.py`.
+
+## BUG-142: the deck's "API this session" card shows 272k tokens as $0 — every model id of the live install was missing from the hand-maintained price table, and the table was the only source (HIGH, FIXED 2026-08-18)
+
+**Symptom.** After 14 turns the card read `272.2k` tokens, `$0` cost, and a
+per-model list of `vertex-live 10× $0` and `gemini-3.7-flash 4× $0`. The log
+carried `No pricing entry for model 'gemini-3.7-flash' — 18117 in / 167 out
+tokens recorded as $0.00` on every delegated turn.
+
+**Root.** Three things, all shipping "$0" honestly and uselessly.
+(1) `jarvis/brain/cost.py::calculate_cost_usd` priced ONLY from
+`PRICING_USD_PER_MTOK`, a static dict last extended in the 2026-07-28 audit —
+`gemini-3.7-flash` (the tool/CU model on the Vertex card) was not in it, and
+neither was `gemini-live-2.5-flash-native-audio` (`VertexLiveProvider.
+default_model`). Same class as the 1.87M deepseek tokens that went unbilled
+in July: the table can never know the next generation, and every unknown
+model reads as free. (2) `RealtimeVoiceSession` set `_active_model` to the
+card's pin — an EMPTY string when the card pins nothing — so `_publish_live_
+usage` priced `""` and `BrainTurnCompleted` carried no model; the deck keyed
+the row on the provider name, hence "vertex-live". (3) The Realtime session
+objects never told the orchestrator which id they had actually connected.
+Along the way the table's own rows for `gemini-2.5-flash` ($0.075/$0.30, a
+2025 preview rate) and `gemini-3.6-flash` ($1.50/$7.50) had drifted from the
+vendor's price page ($0.30/$2.50 and $0.75/$3.75).
+
+**Fix.** `resolve_rates` in `jarvis/brain/cost.py` now has three sources:
+the static table; the provider feed that `jarvis/brain/model_catalog.py`
+already downloads (OpenRouter publishes `pricing.prompt/completion` for every
+model it routes — the catalog now carries it as `ModelInfo.pricing` and
+round-trips it through `model_catalog_cache.json`); and $0 with a warning. A
+`vendor/model` id (an aggregator id) is priced from the feed FIRST, because
+that is what the aggregator bills; an origin id trusts the hand-verified table
+and falls back to the feed's entry of the same model name
+(`gemini-3.7-flash` → `google/gemini-3.7-flash`, `.`/`-` insensitive,
+`:batch`/`:free` variants never shadow the plain model). `ensure_pricing_for`
+— awaited by the manager's post-call hook and by `_publish_live_usage` —
+refreshes the feed ONCE per unknown model per process (3 s cap, never under
+the airgapped profile, never for a feed younger than 10 min), so a new
+generation is priced from its first turn. The catalog is a process-wide
+singleton now (`shared_catalog()`), because `_save_cache` writes the whole
+file from memory and a second instance would have overwritten the first
+one's refresh. The Realtime session objects expose `model` (the id the
+socket really opened with), and `_active_model` is pin → session.model →
+`provider.default_model`. Table rows added/corrected against
+ai.google.dev/gemini-api/docs/pricing and the OpenRouter feed on 2026-08-18.
+
+**Class.** A hand-maintained lookup table as the ONLY source for a value the
+world keeps adding to. It is right on the day it is written and wrong on the
+day the next model ships — and "unknown → 0" is the worst possible default
+for money, because $0 looks like a fact, not like a gap. The fix is not a
+bigger table; it is a live source with the table as offline fallback, plus a
+turn that pauses once to learn a price rather than shipping a wrong number.
+Second shape, again (BUG-139): a value derived in one place (the model the
+socket opened) that another place re-derived from a weaker signal (the
+card's pin) — and the weaker one was empty.
+
+**Verification.** `resolve_rates("gemini-3.7-flash") == (0.75, 3.75)`;
+`resolve_rates("gemini-live-2.5-flash-native-audio") == (0.5, 2.0)` plus
+audio rates; a live OpenRouter feed pull confirms `google/gemini-3.7-flash`
+at 0.375/1.875. Needs an app restart to take effect on the deck.
+
+**Tests.** `tests/unit/brain/test_cost_feed_pricing.py` (feed resolution,
+vendor-first precedence, variant suffixes, dot/dash spellings, mtime reload,
+one-refresh-per-model, airgapped, timeout, never raises);
+`tests/unit/brain/test_model_catalog.py` (pricing parse + cache round-trip);
+`tests/unit/realtime/test_session.py::test_open_reports_the_model_the_socket_
+really_opened_with`; `test_live_usage_metering.py` (Vertex Live default
+priced); `test_gemini_live.py` / `test_openai_realtime.py` (`session.model`).
+The root `tests/conftest.py` redirects the feed to `tmp_path` per test so no
+suite reaches the network or the developer's real cache file.
diff --git a/jarvis/brain/cost.py b/jarvis/brain/cost.py
index c951454857..ac7ab2c0f1 100644
--- a/jarvis/brain/cost.py
+++ b/jarvis/brain/cost.py
@@ -1,11 +1,31 @@
-"""Cost pricing table for brain providers (USD per 1M tokens).
+"""Cost pricing for brain providers (USD per 1M tokens).
-As of 2026-04. Sources:
+Three sources, in this order (see :func:`resolve_rates`):
+
+1. ``PRICING_USD_PER_MTOK`` — the static table below, hand-verified against
+ the vendors' own price pages. Authoritative for models called through
+ their ORIGIN API (Anthropic, Google/Vertex, OpenAI, xAI, DeepSeek …).
+2. The provider feed cached by ``jarvis/brain/model_catalog.py``
+ (``data/model_catalog_cache.json``). OpenRouter's ``/api/v1/models``
+ publishes a price for every model it routes, so a ``vendor/model`` id is
+ priced from the feed FIRST (that is what OpenRouter actually bills), and
+ an origin id the table has never heard of falls back to the feed entry
+ with the same model name (``gemini-3.7-flash`` → ``google/gemini-3.7-flash``)
+ — an honest approximation instead of $0.00.
+3. Nothing → 0.0, and the caller logs it.
+
+The static table alone shipped every new model generation as "free" until
+someone noticed (1.87M deepseek tokens in 2026-07, gemini-3.7-flash in
+2026-08). :func:`ensure_pricing_for` closes that gap at runtime: an unknown
+model triggers ONE feed refresh per process before it is priced.
+
+Static table as of 2026-08-18. Sources:
- Anthropic — https://www.anthropic.com/pricing
(Claude Opus 4.x: $15 in / $75 out, Sonnet 4.x: $3 / $15, Haiku 4.5: $0.80 / $4)
- Google — https://ai.google.dev/pricing
- (Gemini 2.5 Pro: $1.25 / $10, Gemini 2.5 Flash: $0.075 / $0.30)
+ (Gemini 2.5 Pro: $1.25 / $10, Gemini 2.5 Flash: $0.30 / $2.50,
+ Gemini 3.6/3.7 Flash: $0.75 / $3.75)
- OpenAI — https://openai.com/api/pricing
(GPT-4o: $2.50 / $10, GPT-4o-mini: $0.15 / $0.60)
- xAI / Grok — https://console.x.ai/pricing
@@ -14,18 +34,23 @@ As of 2026-04. Sources:
- DeepSeek — https://platform.deepseek.com/api-docs/pricing
(deepseek-chat: $0.27 / $1.10, deepseek-reasoner: $0.55 / $2.19)
-If a model is missing here, ``calculate_cost_usd`` returns 0.0 — no crash,
-but also no cost tracking. Logging at the call site is mandatory so that
-missing entries become visible rather than silently turning into a
-"free" banner.
+If a model is missing here AND in the feed, ``calculate_cost_usd`` returns
+0.0 — no crash, but also no cost tracking. Logging at the call site is
+mandatory so that missing entries become visible rather than silently
+turning into a "free" banner.
Aliases are NOT mapped here — callers must pass the canonical model name
(e.g. ``claude-opus-4-7-20251022`` instead of ``opus``). PROVIDER_ALIASES
in ``manager.py`` is for provider names, not model IDs.
"""
+
from __future__ import annotations
+import asyncio
+import json
import logging
+import time
+from pathlib import Path
log = logging.getLogger(__name__)
@@ -52,8 +77,10 @@ PRICING_USD_PER_MTOK: dict[str, tuple[float, float]] = {
"gemini-3-flash-preview": (0.10, 0.40),
"gemini-3.1-flash-lite": (0.05, 0.20),
"gemini-2.5-pro": (1.25, 10.0),
- "gemini-2.5-flash": (0.075, 0.30),
- "gemini-2.5-flash-lite": (0.075, 0.30),
+ # 2.5 Flash GA rates (raised from the preview's $0.075/$0.30 in 2025);
+ # re-verified against the OpenRouter feed 2026-08-18.
+ "gemini-2.5-flash": (0.30, 2.50),
+ "gemini-2.5-flash-lite": (0.10, 0.40),
"gemini-3.1-flash-tts-preview": (0.075, 0.30), # TTS, same rate
# 2026-07-28 cost audit: the 3.5/3.6 flash generation is ~20x pricier
# than 2.5-flash; these entries were missing, so the live install
@@ -61,10 +88,20 @@ PRICING_USD_PER_MTOK: dict[str, tuple[float, float]] = {
# /api/v1/models pricing feed on 2026-07-28).
"gemini-3.5-flash": (1.50, 9.0),
"gemini-3.5-flash-lite": (0.30, 2.50),
- "gemini-3.6-flash": (1.50, 7.50),
- # Live API model — TEXT rates; audio rates live in
+ # 3.6 / 3.7 Flash: $0.75 / $3.75 per ai.google.dev/gemini-api/docs/pricing
+ # (2026-08-18; promotional rate "through Dec 31, 2026"). 3.7 was missing
+ # here on 2026-08-18 and the live install showed a whole session as $0.
+ "gemini-3.6-flash": (0.75, 3.75),
+ "gemini-3.7-flash": (0.75, 3.75),
+ # Live API models — TEXT rates; audio rates live in
# REALTIME_AUDIO_PRICING_USD_PER_MTOK below.
"gemini-3.1-flash-live-preview": (0.75, 4.50),
+ # 2.5 Flash native-audio Live: $0.50 / $2.00 text (ai.google.dev pricing,
+ # 2026-08-18). ``gemini-live-2.5-flash-native-audio`` is the Vertex id of
+ # the same model — VertexLiveProvider.default_model — and Vertex bills
+ # the same per-token rates.
+ "gemini-2.5-flash-native-audio-preview-12-2025": (0.50, 2.0),
+ "gemini-live-2.5-flash-native-audio": (0.50, 2.0),
# ── OpenAI (Frontier: GPT-5.5 + 5.5-pro, released 2026-04-23) ──
"gpt-5.5": (5.0, 30.0),
"gpt-5.5-pro": (30.0, 180.0), # corrected 2026-07-28 (OpenRouter feed)
@@ -98,7 +135,11 @@ PRICING_USD_PER_MTOK: dict[str, tuple[float, float]] = {
"anthropic/claude-sonnet-4.6": (3.0, 15.0),
"google/gemini-3.5-flash": (1.50, 9.0),
"google/gemini-3.5-flash-lite": (0.30, 2.50),
- "google/gemini-3.6-flash": (1.50, 7.50),
+ # Feed values 2026-08-18 (OpenRouter lists 3.7 at half of Google's own
+ # list price). These rows are the OFFLINE fallback only — a vendor/model
+ # id is priced from the live feed first (resolve_rates).
+ "google/gemini-3.6-flash": (0.75, 3.75),
+ "google/gemini-3.7-flash": (0.375, 1.875),
# 2026-07-28 audit: 1.87M tokens in 30 days ran on deepseek-v4-flash
# while it was absent here — the single biggest remaining $0 hole.
"deepseek/deepseek-v4-flash": (0.14, 0.28),
@@ -128,7 +169,7 @@ def calculate_cost_usd(
"""
if not model or tokens_in <= 0 and tokens_out <= 0:
return 0.0
- rates = PRICING_USD_PER_MTOK.get(model)
+ rates = resolve_rates(model)
if rates is None:
log.debug("Cost pricing missing for model %r — returning 0.0", model)
return 0.0
@@ -136,6 +177,164 @@ def calculate_cost_usd(
return (max(0, tokens_in) * in_rate + max(0, tokens_out) * out_rate) / 1_000_000
+# ----------------------------------------------------------------------
+# Rate resolution: static table + the cached provider feed
+# ----------------------------------------------------------------------
+
+# Feed cache: model id -> (in, out) USD per 1M tokens, plus a "same model
+# name, any vendor" index for origin ids. Reloaded whenever the catalog cache
+# file changes on disk (mtime), so a picker refresh or ensure_pricing_for()
+# is picked up by the next turn without a restart.
+_feed_rates: dict[str, tuple[float, float]] = {}
+_feed_by_name: dict[str, tuple[float, float]] = {}
+_feed_loaded: tuple[Path, float] | None = None # (path, mtime) of what is loaded
+_feed_fetched_at: float = 0.0
+# Test hook: where the feed cache lives (default: <DATA_DIR>/model_catalog_cache.json).
+_feed_path_override: Path | None = None
+# Models that already triggered a feed refresh this process — one network
+# round-trip per unknown model, never one per turn.
+_refresh_attempted: set[str] = set()
+# A feed younger than this is not re-fetched for a model it does not list —
+# the model is simply not on OpenRouter (local weights, a Live-API id …).
+_FEED_MIN_REFRESH_AGE_S = 600.0
+
+
+def _catalog_cache_path() -> Path:
+ if _feed_path_override is not None:
+ return _feed_path_override
+ from jarvis.core import config as cfg # lazy — keeps import light
+
+ return Path(cfg.DATA_DIR) / "model_catalog_cache.json"
+
+
+def _norm(model_id: str) -> str:
+ """``claude-sonnet-4-6`` and ``claude-sonnet-4.6`` name the same model."""
+ return model_id.strip().casefold().replace(".", "-")
+
+
+def _load_feed() -> None:
+ """(Re)load feed prices from the catalog cache when the file changed."""
+ global _feed_loaded, _feed_fetched_at
+ path = _catalog_cache_path()
+ try:
+ mtime = path.stat().st_mtime
+ except OSError:
+ if _feed_loaded is not None:
+ _feed_rates.clear()
+ _feed_by_name.clear()
+ _feed_loaded = None
+ _feed_fetched_at = 0.0
+ return
+ if _feed_loaded == (path, mtime):
+ return
+ rates: dict[str, tuple[float, float]] = {}
+ fetched_at = 0.0
+ try:
+ data = json.loads(path.read_text(encoding="utf-8"))
+ for entry in data.values():
+ if not isinstance(entry, dict):
+ continue
+ fetched_at = max(fetched_at, float(entry.get("fetched_at", 0.0) or 0.0))
+ for m in entry.get("models", []) or []:
+ if not isinstance(m, dict):
+ continue
+ pricing = m.get("pricing")
+ model_id = str(m.get("id", "") or "")
+ if not model_id or not isinstance(pricing, list) or len(pricing) != 2:
+ continue
+ rates[model_id] = (float(pricing[0]), float(pricing[1]))
+ except (OSError, ValueError, TypeError) as exc:
+ log.debug("Feed pricing unavailable from %s: %s", path, exc)
+ rates = {}
+ by_name: dict[str, tuple[float, float]] = {}
+ # Sorted → deterministic when two vendors publish the same model name;
+ # ``:free`` / ``:batch`` / ``:nitro`` variants are NOT the plain model.
+ for model_id in sorted(rates):
+ _vendor, sep, name = model_id.partition("/")
+ if not sep or ":" in name:
+ continue
+ by_name.setdefault(_norm(name), rates[model_id])
+ _feed_rates.clear()
+ _feed_rates.update(rates)
+ _feed_by_name.clear()
+ _feed_by_name.update(by_name)
+ _feed_loaded = (path, mtime)
+ _feed_fetched_at = fetched_at
+
+
+def feed_rates(model: str) -> tuple[float, float] | None:
+ """(in, out) USD per 1M tokens from the cached provider feed, or ``None``.
+
+ Exact id first; an id without a vendor prefix matches the feed entry of
+ the same model name under any vendor (``.``/``-`` insensitive).
+ """
+ if not model:
+ return None
+ _load_feed()
+ exact = _feed_rates.get(model)
+ if exact is not None:
+ return exact
+ if "/" in model:
+ return None
+ return _feed_by_name.get(_norm(model))
+
+
+def resolve_rates(model: str | None) -> tuple[float, float] | None:
+ """The (in, out) USD-per-1M-token rates for ``model``, or ``None``.
+
+ A ``vendor/model`` id is an aggregator id → the aggregator's feed is what
+ it bills, so the feed wins over the static row (the row is the offline
+ fallback). An origin id trusts the hand-verified table first and falls
+ back to the feed's entry for the same model name.
+ """
+ if not model:
+ return None
+ if "/" in model:
+ return feed_rates(model) or PRICING_USD_PER_MTOK.get(model)
+ return PRICING_USD_PER_MTOK.get(model) or feed_rates(model)
+
+
+async def ensure_pricing_for(model: str | None, *, timeout_s: float = 3.0) -> bool:
+ """Make ``model`` priceable if the provider feed can do it; True when priced.
+
+ Cheap when the model is already known. Otherwise ONE catalog refresh per
+ unknown model per process, capped at ``timeout_s`` — a turn is delayed by
+ at most that once, never per turn. Never raises. Skipped for a feed
+ fetched within the last 10 minutes (the model is not on the feed at all)
+ and under the airgapped privacy profile (no outbound call).
+ """
+ if not model:
+ return False
+ if resolve_rates(model) is not None:
+ return True
+ if model in _refresh_attempted:
+ return False
+ _refresh_attempted.add(model)
+ try:
+ from jarvis.core import config as cfg # lazy
+
+ profile = getattr(getattr(cfg, "profile", None), "name", "default")
+ if profile == "airgapped":
+ return False
+ _load_feed()
+ if _feed_fetched_at and time.time() - _feed_fetched_at < _FEED_MIN_REFRESH_AGE_S:
+ return False
+ from jarvis.brain import model_catalog as mc # lazy (httpx)
+
+ # The process-wide instance shares its memory copy with the picker
+ # routes; a redirected feed (tests) gets its own instance on that path.
+ catalog = (
+ mc.shared_catalog()
+ if _feed_path_override is None
+ else mc.ModelCatalog(cache_path=_feed_path_override)
+ )
+ await asyncio.wait_for(catalog.list_models("openrouter", force_refresh=True), timeout_s)
+ except Exception as exc: # noqa: BLE001 — pricing never breaks a turn
+ log.debug("Feed pricing refresh for %r skipped: %s", model, exc)
+ return False
+ return resolve_rates(model) is not None
+
+
# Realtime/Live API audio token rates (USD per 1M AUDIO tokens).
# Audio tokens are billed 4-40x above the same model's text tokens, so
# realtime cost accounting that prices everything at text rates
@@ -145,6 +344,10 @@ def calculate_cost_usd(
# gpt-realtime-2.1-mini $10 in / $20 out
REALTIME_AUDIO_PRICING_USD_PER_MTOK: dict[str, tuple[float, float]] = {
"gemini-3.1-flash-live-preview": (3.0, 12.0),
+ # 2.5 Flash native audio (AI Studio id + Vertex id): $3.00 in / $12.00
+ # out per ai.google.dev/gemini-api/docs/pricing, 2026-08-18.
+ "gemini-2.5-flash-native-audio-preview-12-2025": (3.0, 12.0),
+ "gemini-live-2.5-flash-native-audio": (3.0, 12.0),
"gpt-realtime-2.1": (32.0, 64.0),
"gpt-realtime-2.1-mini": (10.0, 20.0),
}
@@ -182,4 +385,7 @@ __all__ = [
"REALTIME_AUDIO_PRICING_USD_PER_MTOK",
"calculate_cost_usd",
"calculate_realtime_cost_usd",
+ "ensure_pricing_for",
+ "feed_rates",
+ "resolve_rates",
]
diff --git a/jarvis/brain/manager.py b/jarvis/brain/manager.py
index c9c6f4b83c..474f50b470 100644
--- a/jarvis/brain/manager.py
+++ b/jarvis/brain/manager.py
@@ -11409,7 +11409,16 @@ class BrainManager:
tokens_out_total = int(agg.usage.get("output_tokens", 0)) if agg.usage else 0
cost_usd_total = 0.0
try:
- from jarvis.brain.cost import calculate_cost_usd
+ from jarvis.brain.cost import (
+ calculate_cost_usd,
+ ensure_pricing_for,
+ )
+ # A model the static table has never heard of is priced
+ # from the provider feed — ONE refresh per model per
+ # process (capped at 3 s), so a new generation stops
+ # shipping as "$0.00" until someone edits the table.
+ if tokens_in_total > 0 or tokens_out_total > 0:
+ await ensure_pricing_for(model)
cost_usd_total = calculate_cost_usd(model, tokens_in_total, tokens_out_total)
if cost_usd_total == 0.0 and tokens_in_total > 0:
# An unknown model prices as $0.00 and every surface
@@ -11417,9 +11426,10 @@ class BrainManager:
# 1.87M deepseek tokens went unbilled for a month
# (2026-07-28 cost audit). Say it once per turn.
log.warning(
- "No pricing entry for model %r — %d in / %d out "
- "tokens recorded as $0.00; add it to "
- "jarvis/brain/cost.py PRICING_USD_PER_MTOK",
+ "No pricing for model %r (static table and "
+ "provider feed) — %d in / %d out tokens recorded "
+ "as $0.00; add it to jarvis/brain/cost.py "
+ "PRICING_USD_PER_MTOK",
model, tokens_in_total, tokens_out_total,
)
except Exception: # noqa: BLE001
diff --git a/jarvis/brain/model_catalog.py b/jarvis/brain/model_catalog.py
index 4c00712282..575b336895 100644
--- a/jarvis/brain/model_catalog.py
+++ b/jarvis/brain/model_catalog.py
@@ -164,6 +164,14 @@ class ModelInfo:
# supported_parameters`` ⇒ tool-calling.
input_modalities: tuple[str, ...] | None = None
supported_parameters: tuple[str, ...] | None = None
+ # (input, output) in USD per 1M tokens, straight from the provider's own
+ # feed (OpenRouter publishes ``pricing.prompt`` / ``pricing.completion``
+ # per token). ``None`` when the endpoint has no price data. This is what
+ # lets cost tracking price a model the static table in
+ # ``jarvis/brain/cost.py`` has never heard of — the table used to be the
+ # only source, and every new model generation shipped as "$0.00" until
+ # someone noticed (2026-07-28 and 2026-08-18 audits).
+ pricing: tuple[float, float] | None = None
def _curated(pairs: list[tuple[str, str]]) -> list[ModelInfo]:
@@ -835,11 +843,33 @@ def parse_models_response(provider: str, payload: dict) -> list[ModelInfo]:
output_modalities=_output_modalities(m),
input_modalities=_input_modalities(m),
supported_parameters=_supported_parameters(m),
+ pricing=_pricing(m),
)
)
return out
+def _pricing(entry: dict) -> tuple[float, float] | None:
+ """Pull ``pricing.prompt`` / ``pricing.completion`` as USD per 1M tokens.
+
+ OpenRouter quotes both as decimal strings per TOKEN (``"0.000000375"``);
+ direct provider endpoints carry no ``pricing`` block at all → ``None``.
+ A negative value is OpenRouter's marker for "variable" (``openrouter/auto``
+ routes to whatever answers) — no honest single price exists, so ``None``.
+ """
+ pricing = entry.get("pricing")
+ if not isinstance(pricing, dict):
+ return None
+ try:
+ prompt = float(pricing.get("prompt"))
+ completion = float(pricing.get("completion"))
+ except (TypeError, ValueError):
+ return None
+ if prompt < 0 or completion < 0:
+ return None
+ return (prompt * 1_000_000, completion * 1_000_000)
+
+
def gemini_entry_serves_generate_content(entry: dict) -> bool:
"""True when a Gemini ListModels entry can serve ``generateContent``.
@@ -1416,6 +1446,25 @@ def sort_models(provider: str, models: list[ModelInfo]) -> list[ModelInfo]:
# ----------------------------------------------------------------------
+_shared_catalog: ModelCatalog | None = None
+
+
+def shared_catalog() -> ModelCatalog:
+ """The ONE process-wide :class:`ModelCatalog`.
+
+ Every reader and writer of ``model_catalog_cache.json`` must go through
+ the same instance: ``_save_cache`` writes the whole file from memory, so
+ a second instance that refreshed one provider would be overwritten by the
+ first one's older in-memory copy on its next save. The web layer stashes
+ this same object on ``app.state`` (provider_routes) and cost tracking's
+ feed refresh (``jarvis.brain.cost.ensure_pricing_for``) uses it too.
+ """
+ global _shared_catalog
+ if _shared_catalog is None:
+ _shared_catalog = ModelCatalog()
+ return _shared_catalog
+
+
class ModelCatalog:
"""Live model lists per provider with a TTL cache and honest fallbacks."""
@@ -1464,6 +1513,11 @@ class ModelCatalog:
if isinstance(m.get("supported_parameters"), list)
else None
),
+ pricing=(
+ (float(m["pricing"][0]), float(m["pricing"][1]))
+ if isinstance(m.get("pricing"), list) and len(m["pricing"]) == 2
+ else None
+ ),
)
for m in entry.get("models", [])
]
@@ -1496,6 +1550,7 @@ class ModelCatalog:
if m.supported_parameters is not None
else {}
),
+ **({"pricing": list(m.pricing)} if m.pricing is not None else {}),
}
for m in models
],
diff --git a/jarvis/plugins/realtime/gemini_live.py b/jarvis/plugins/realtime/gemini_live.py
index ba463194e4..6aeeac34b2 100644
--- a/jarvis/plugins/realtime/gemini_live.py
+++ b/jarvis/plugins/realtime/gemini_live.py
@@ -220,11 +220,16 @@ class _GeminiLiveSession:
session_id: str,
instructions: str = "",
language: str = "",
+ model: str = "",
) -> None:
self._session = session
self._connection_cm = connection_cm
self._client = client
self.session_id = session_id
+ # The id the Live socket was opened with — the orchestrator meters
+ # (and prices) usage against it, so it must be the REAL one, not the
+ # card's possibly-empty pin.
+ self.model = str(model or "")
self._closed = False
# Latest usage_metadata snapshot of the CURRENT generation. Emitted
# once per generation boundary (tool call, turn end, barge-in) so the
@@ -895,8 +900,9 @@ class GeminiLiveProvider:
),
**_compression_kwargs(types),
)
+ connect_model = str(getattr(cfg, "model", "") or self.default_model)
connection_cm = client.aio.live.connect(
- model=str(getattr(cfg, "model", "") or self.default_model),
+ model=connect_model,
config=live_config,
)
try:
@@ -920,6 +926,7 @@ class GeminiLiveProvider:
# re-delivered (see update_session).
instructions=str(getattr(cfg, "instructions", "") or ""),
language=str(getattr(cfg, "language", "") or ""),
+ model=connect_model,
)
diff --git a/jarvis/plugins/realtime/openai_realtime.py b/jarvis/plugins/realtime/openai_realtime.py
index c0681e8152..d518748d4f 100644
--- a/jarvis/plugins/realtime/openai_realtime.py
+++ b/jarvis/plugins/realtime/openai_realtime.py
@@ -385,6 +385,9 @@ class _OpenAIRealtimeSession:
# Model the transport was opened with — required to rebuild the
# connection in place when the server goes deaf (BUG-064 escalation).
self._connect_model = str(connect_model or "")
+ # The same id, exposed for the orchestrator's usage metering: what
+ # this socket really bills, even when the card pinned nothing.
+ self.model = self._connect_model
# Bounded call transcript for context restoration (BUG-088). Seeded
# from the open-time config and kept current by the orchestrator via
# set_history_snapshot after every completed turn, so a BUG-064
diff --git a/jarvis/realtime/session.py b/jarvis/realtime/session.py
index db0ce381ba..d7dc15038f 100644
--- a/jarvis/realtime/session.py
+++ b/jarvis/realtime/session.py
@@ -2828,7 +2828,15 @@ class RealtimeVoiceSession:
self._active_requires_public_fact_grounding = bool(
getattr(provider, "requires_public_fact_grounding", False)
)
- self._active_model = model
+ # The id the socket was REALLY opened with: the card's pin, else
+ # what the provider connected (its default). An empty string here
+ # made every Live turn unpriceable and the deck's API card list the
+ # provider name in the model column (2026-08-18).
+ self._active_model = (
+ model
+ or str(getattr(session, "model", "") or "")
+ or str(getattr(provider, "default_model", "") or "")
+ )
# Captured at accept so every per-turn instruction rebuild keeps
# the profile the accepted provider asked for.
self._compact_instructions = bool(
@@ -6636,11 +6644,13 @@ class RealtimeVoiceSession:
return
try:
from jarvis.brain.cost import (
- PRICING_USD_PER_MTOK,
calculate_realtime_cost_usd,
+ ensure_pricing_for,
+ resolve_rates,
)
from jarvis.core.events import BrainTurnCompleted
+ await ensure_pricing_for(self._active_model)
text_in = usage.get("input_text", 0)
audio_in = usage.get("input_audio", 0)
text_out = usage.get("output_text", 0)
@@ -6658,7 +6668,7 @@ class RealtimeVoiceSession:
audio_in,
audio_out,
)
- rates = PRICING_USD_PER_MTOK.get(self._active_model)
+ rates = resolve_rates(self._active_model)
if cached_in > 0 and rates is not None:
cost += cached_in * rates[0] * 0.1 / 1_000_000
await self._bus.publish(
diff --git a/jarvis/ui/web/provider_routes.py b/jarvis/ui/web/provider_routes.py
index d5beea807e..54ab28e918 100644
--- a/jarvis/ui/web/provider_routes.py
+++ b/jarvis/ui/web/provider_routes.py
@@ -1951,9 +1951,11 @@ def _get_model_catalog(request: Request):
"""
cat = getattr(request.app.state, "model_catalog", None)
if cat is None:
- from jarvis.brain.model_catalog import ModelCatalog
+ from jarvis.brain.model_catalog import shared_catalog
- cat = ModelCatalog()
+ # The process-wide instance — cost tracking's feed refresh writes the
+ # same cache file through it, so both must share one memory copy.
+ cat = shared_catalog()
try:
request.app.state.model_catalog = cat
except Exception as exc: # noqa: BLE001 — detached app.state is not an error
diff --git a/tests/conftest.py b/tests/conftest.py
index 9f745c99ba..f52a83e23b 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -115,6 +115,40 @@ def _agentic_ide_history_in_tmp(tmp_path_factory, monkeypatch): # noqa: ANN001
yield root
[email protected](autouse=True)
+def _pricing_feed_in_tmp(tmp_path_factory, monkeypatch): # noqa: ANN001
+ """Keep cost tracking's provider-feed refresh off the network and off the
+ developer's real ``data/model_catalog_cache.json``.
+
+ ``jarvis.brain.cost.ensure_pricing_for`` prices a model the static table
+ does not know by refreshing the OpenRouter feed ONCE per process — a real
+ HTTP round-trip that would also rewrite the real cache file. Every suite
+ that runs a brain turn with a made-up model id ("fake-model") would trigger
+ it. So each test gets its own feed file, stamped as fetched just now: the
+ refresh sees a fresh feed that simply does not list the model and stays
+ home. Suites that exercise the refresh itself (test_cost_feed_pricing)
+ override the path and the catalog again on top of this.
+ """
+ import json
+ import time
+
+ from jarvis.brain import cost
+
+ root = tmp_path_factory.mktemp("pricing-feed")
+ path = root / "model_catalog_cache.json"
+ path.write_text(
+ json.dumps({"openrouter": {"fetched_at": time.time(), "models": []}}),
+ encoding="utf-8",
+ )
+ monkeypatch.setattr(cost, "_feed_path_override", path)
+ monkeypatch.setattr(cost, "_feed_loaded", None)
+ monkeypatch.setattr(cost, "_feed_fetched_at", 0.0)
+ monkeypatch.setattr(cost, "_feed_rates", {})
+ monkeypatch.setattr(cost, "_feed_by_name", {})
+ monkeypatch.setattr(cost, "_refresh_attempted", set())
+ yield
+
+
@pytest.fixture(autouse=True)
def _reset_bus():
"""Reset the global default bus before and after each test."""
diff --git a/tests/unit/brain/test_cost_feed_pricing.py b/tests/unit/brain/test_cost_feed_pricing.py
new file mode 100644
index 0000000000..3360ebdadb
--- /dev/null
+++ b/tests/unit/brain/test_cost_feed_pricing.py
@@ -0,0 +1,231 @@
+"""Cost tracking prices models the static table never listed.
+
+Background (2026-08-18): the deck's "API this session" card showed 272k
+tokens on ``gemini-3.7-flash`` and ``vertex-live`` as ``$0`` — both ids were
+absent from ``PRICING_USD_PER_MTOK`` and the table was the ONLY source. Now
+``resolve_rates`` also reads the provider feed the model catalog caches on
+disk (OpenRouter publishes a price for every model it routes), and
+``ensure_pricing_for`` refreshes that feed once for an unknown model.
+
+Fakes, not mocks: the feed is a real JSON file in ``tmp_path``; the catalog
+refresh is a fake ``ModelCatalog`` that writes that file the way the real
+one does.
+"""
+
+from __future__ import annotations
+
+import asyncio
+import json
+import os
+import time
+from pathlib import Path
+
+import pytest
+
+from jarvis.brain import cost
+
+
+def _write_feed(
+ path: Path, models: list[dict], *, fetched_at: float | None = None, bump: int = 1
+) -> None:
+ payload = {
+ "openrouter": {
+ "fetched_at": time.time() if fetched_at is None else fetched_at,
+ "models": models,
+ }
+ }
+ path.write_text(json.dumps(payload), encoding="utf-8")
+ # A rewrite inside the same clock tick must still count as a change;
+ # push the mtime forward explicitly instead of trusting the filesystem.
+ stamp = 1_700_000_000 + bump
+ os.utime(path, (stamp, stamp))
+
+
+def feed(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
+ path = tmp_path / "model_catalog_cache.json"
+ monkeypatch.setattr(cost, "_feed_path_override", path)
+ monkeypatch.setattr(cost, "_feed_loaded", None)
+ monkeypatch.setattr(cost, "_feed_fetched_at", 0.0)
+ monkeypatch.setattr(cost, "_feed_rates", {})
+ monkeypatch.setattr(cost, "_feed_by_name", {})
+ monkeypatch.setattr(cost, "_refresh_attempted", set())
+ return path
+
+
+class TestResolveRates:
+ def test_static_table_still_wins_for_origin_ids(self, feed: Path) -> None:
+ # An origin id (no vendor prefix) trusts the hand-verified table even
+ # when the feed lists the same model at another price.
+ _write_feed(feed, [{"id": "google/gemini-3.7-flash", "pricing": [0.375, 1.875]}])
+ assert (
+ cost.resolve_rates("gemini-3.7-flash") == cost.PRICING_USD_PER_MTOK["gemini-3.7-flash"]
+ )
+
+ def test_unknown_origin_id_falls_back_to_the_feed_by_model_name(self, feed: Path) -> None:
+ _write_feed(feed, [{"id": "google/gemini-9.9-flash", "pricing": [0.5, 2.0]}])
+ assert "gemini-9.9-flash" not in cost.PRICING_USD_PER_MTOK
+ assert cost.resolve_rates("gemini-9.9-flash") == (0.5, 2.0)
+ assert cost.calculate_cost_usd("gemini-9.9-flash", 1_000_000, 1_000_000) == pytest.approx(
+ 2.5
+ )
+
+ def test_vendor_prefixed_id_prefers_the_feed_over_the_table(self, feed: Path) -> None:
+ # ``vendor/model`` is the aggregator's own id: what its feed says is
+ # what it bills; the static row is only the offline fallback.
+ assert "google/gemini-3.6-flash" in cost.PRICING_USD_PER_MTOK
+ _write_feed(feed, [{"id": "google/gemini-3.6-flash", "pricing": [0.11, 0.22]}])
+ assert cost.resolve_rates("google/gemini-3.6-flash") == (0.11, 0.22)
+
+ def test_vendor_prefixed_id_falls_back_to_the_table_when_the_feed_lacks_it(
+ self, feed: Path
+ ) -> None:
+ _write_feed(feed, [{"id": "vendor/other", "pricing": [1.0, 1.0]}])
+ assert (
+ cost.resolve_rates("google/gemini-3.6-flash")
+ == cost.PRICING_USD_PER_MTOK["google/gemini-3.6-flash"]
+ )
+
+ def test_vendor_prefixed_id_never_matches_by_bare_name(self, feed: Path) -> None:
+ # ``other/gemini-9.9-flash`` is a DIFFERENT listing than ``google/…``.
+ _write_feed(feed, [{"id": "google/gemini-9.9-flash", "pricing": [0.5, 2.0]}])
+ assert cost.resolve_rates("other/gemini-9.9-flash") is None
+
+ def test_variant_suffixes_do_not_shadow_the_plain_model(self, feed: Path) -> None:
+ # ``:batch`` / ``:free`` are other SKUs — the plain name must resolve
+ # to the plain listing whatever the sort order puts first.
+ _write_feed(
+ feed,
+ [
+ {"id": "google/gemini-9.9-flash:batch", "pricing": [0.1, 0.1]},
+ {"id": "google/gemini-9.9-flash", "pricing": [0.5, 2.0]},
+ {"id": "google/gemini-9.9-flash:free", "pricing": [0.0, 0.0]},
+ ],
+ )
+ assert cost.resolve_rates("gemini-9.9-flash") == (0.5, 2.0)
+
+ def test_dot_and_dash_version_spellings_match(self, feed: Path) -> None:
+ # Anthropic's API says claude-sonnet-9-9, OpenRouter anthropic/claude-sonnet-9.9.
+ _write_feed(feed, [{"id": "anthropic/claude-sonnet-9.9", "pricing": [3.0, 15.0]}])
+ assert cost.resolve_rates("claude-sonnet-9-9") == (3.0, 15.0)
+
+ def test_missing_feed_file_means_no_feed_rates(self, feed: Path) -> None:
+ assert not feed.exists()
+ assert cost.feed_rates("gemini-9.9-flash") is None
+ assert cost.calculate_cost_usd("gemini-9.9-flash", 1000, 10) == 0.0
+
+ def test_corrupt_feed_file_is_ignored(self, feed: Path) -> None:
+ feed.write_text("{ not json", encoding="utf-8")
+ assert cost.feed_rates("gemini-9.9-flash") is None
+
+ def test_feed_reloads_when_the_file_changes(self, feed: Path) -> None:
+ _write_feed(feed, [{"id": "google/gemini-9.9-flash", "pricing": [0.5, 2.0]}], bump=1)
+ assert cost.resolve_rates("gemini-9.9-flash") == (0.5, 2.0)
+ _write_feed(feed, [{"id": "google/gemini-9.9-flash", "pricing": [0.7, 3.0]}], bump=2)
+ assert cost.resolve_rates("gemini-9.9-flash") == (0.7, 3.0)
+
+ def test_empty_model_resolves_to_nothing(self, feed: Path) -> None:
+ assert cost.resolve_rates("") is None
+ assert cost.resolve_rates(None) is None
+
+
+class _FakeCatalog:
+ """Stands in for ModelCatalog: a refresh writes the feed file."""
+
+ calls: list[tuple[str, bool]] = []
+ write: list[dict] = []
+ delay_s: float = 0.0
+ fail: bool = False
+
+ def __init__(self, cache_path: Path | None = None) -> None:
+ self._path = cache_path
+
+ async def list_models(self, provider: str, *, force_refresh: bool = False):
+ _FakeCatalog.calls.append((provider, force_refresh))
+ if _FakeCatalog.fail:
+ raise RuntimeError("network down")
+ if _FakeCatalog.delay_s:
+ await asyncio.sleep(_FakeCatalog.delay_s)
+ assert self._path is not None
+ _write_feed(self._path, _FakeCatalog.write, bump=7)
+ return None
+
+
+def fake_catalog(monkeypatch: pytest.MonkeyPatch):
+ import jarvis.brain.model_catalog as mc
+
+ _FakeCatalog.calls = []
+ _FakeCatalog.write = []
+ _FakeCatalog.delay_s = 0.0
+ _FakeCatalog.fail = False
+ monkeypatch.setattr(mc, "ModelCatalog", _FakeCatalog)
+ return _FakeCatalog
+
+
+class TestEnsurePricingFor:
+ def test_known_model_needs_no_refresh(self, feed: Path, fake_catalog) -> None:
+ assert asyncio.run(cost.ensure_pricing_for("gemini-3.7-flash")) is True
+ assert fake_catalog.calls == []
+
+ def test_unknown_model_refreshes_the_feed_once_and_is_then_priced(
+ self, feed: Path, fake_catalog
+ ) -> None:
+ fake_catalog.write = [{"id": "google/gemini-9.9-flash", "pricing": [0.5, 2.0]}]
+ assert asyncio.run(cost.ensure_pricing_for("gemini-9.9-flash")) is True
+ assert fake_catalog.calls == [("openrouter", True)]
+ assert cost.calculate_cost_usd("gemini-9.9-flash", 1_000_000, 0) == pytest.approx(0.5)
+ # Priced now → the next turn is answered from the table/feed, no fetch.
+ assert asyncio.run(cost.ensure_pricing_for("gemini-9.9-flash")) is True
+ assert len(fake_catalog.calls) == 1
+
+ def test_a_model_the_feed_does_not_list_is_fetched_only_once_per_process(
+ self, feed: Path, fake_catalog
+ ) -> None:
+ fake_catalog.write = [{"id": "vendor/other", "pricing": [1.0, 1.0]}]
+ assert asyncio.run(cost.ensure_pricing_for("qwen-local:9b")) is False
+ assert asyncio.run(cost.ensure_pricing_for("qwen-local:9b")) is False
+ assert len(fake_catalog.calls) == 1
+
+ def test_a_fresh_feed_is_not_refetched(self, feed: Path, fake_catalog) -> None:
+ # The feed was pulled a minute ago and does not list the model — it is
+ # simply not on the feed (local weights, a Live-API id); no round-trip.
+ _write_feed(
+ feed, [{"id": "vendor/other", "pricing": [1.0, 1.0]}], fetched_at=time.time() - 60
+ )
+ assert asyncio.run(cost.ensure_pricing_for("gemini-9.9-flash")) is False
+ assert fake_catalog.calls == []
+
+ def test_a_stale_feed_is_refetched(self, feed: Path, fake_catalog) -> None:
+ _write_feed(
+ feed, [{"id": "vendor/other", "pricing": [1.0, 1.0]}], fetched_at=time.time() - 3600
+ )
+ fake_catalog.write = [{"id": "google/gemini-9.9-flash", "pricing": [0.5, 2.0]}]
+ assert asyncio.run(cost.ensure_pricing_for("gemini-9.9-flash")) is True
+ assert fake_catalog.calls == [("openrouter", True)]
+
+ def test_airgapped_profile_makes_no_outbound_call(
+ self, feed: Path, fake_catalog, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ from types import SimpleNamespace
+
+ from jarvis.core import config as cfg
+
+ monkeypatch.setattr(cfg, "profile", SimpleNamespace(name="airgapped"), raising=False)
+ assert asyncio.run(cost.ensure_pricing_for("gemini-9.9-flash")) is False
+ assert fake_catalog.calls == []
+
+ def test_refresh_failure_never_raises(self, feed: Path, fake_catalog) -> None:
+ fake_catalog.fail = True
+ assert asyncio.run(cost.ensure_pricing_for("gemini-9.9-flash")) is False
+
+ def test_refresh_is_capped_by_the_timeout(self, feed: Path, fake_catalog) -> None:
+ fake_catalog.delay_s = 5.0
+ started = time.monotonic()
+ assert asyncio.run(cost.ensure_pricing_for("gemini-9.9-flash", timeout_s=0.05)) is False
+ assert time.monotonic() - started < 2.0
+
+ def test_empty_model_is_never_fetched(self, feed: Path, fake_catalog) -> None:
+ assert asyncio.run(cost.ensure_pricing_for("")) is False
+ assert asyncio.run(cost.ensure_pricing_for(None)) is False
+ assert fake_catalog.calls == []
diff --git a/tests/unit/brain/test_model_catalog.py b/tests/unit/brain/test_model_catalog.py
index 3f603312c0..ec8010c966 100644
--- a/tests/unit/brain/test_model_catalog.py
+++ b/tests/unit/brain/test_model_catalog.py
@@ -234,6 +234,28 @@ class TestParseModelsResponse:
payload = {"data": [{"id": "gpt-5.5"}]}
assert parse_models_response("openai", payload)[0].output_modalities is None
+ def test_openrouter_pricing_is_carried_as_usd_per_mtok(self) -> None:
+ # OpenRouter quotes USD per TOKEN as decimal strings; the catalog keeps
+ # USD per 1M tokens so cost tracking can price a model the static
+ # table never listed (2026-08-18: gemini-3.7-flash shipped as $0.00).
+ payload = {
+ "data": [
+ {
+ "id": "google/gemini-3.7-flash",
+ "pricing": {"prompt": "0.000000375", "completion": "0.000001875"},
+ },
+ {"id": "openrouter/auto", "pricing": {"prompt": "-1", "completion": "-1"}},
+ {"id": "vendor/no-price"},
+ {"id": "vendor/garbage", "pricing": {"prompt": "n/a", "completion": None}},
+ ]
+ }
+ by_id = {m.id: m for m in parse_models_response("openrouter", payload)}
+ assert by_id["google/gemini-3.7-flash"].pricing == pytest.approx((0.375, 1.875))
+ # "-1" is OpenRouter's "variable" marker — no honest single price.
+ assert by_id["openrouter/auto"].pricing is None
+ assert by_id["vendor/no-price"].pricing is None
+ assert by_id["vendor/garbage"].pricing is None
+
def test_empty_payload_yields_empty_list(self) -> None:
assert parse_models_response("openai", {}) == []
assert parse_models_response("gemini", {}) == []
@@ -495,6 +517,26 @@ class TestCache:
assert models[0].id == "gemini-3-flash"
assert models[0].label == "Gemini 3 Flash"
+ def test_pricing_survives_the_cache_round_trip(self, tmp_path: Path) -> None:
+ # The cost layer reads prices back from THIS file — a cache hit must
+ # not silently drop them (else every restart is a "$0.00" restart).
+ cache_path = tmp_path / "model_catalog_cache.json"
+ cat = ModelCatalog(cache_path=cache_path)
+ cat._cache["openrouter"] = (
+ time.time(),
+ [
+ ModelInfo(id="google/gemini-3.7-flash", label="G", pricing=(0.375, 1.875)),
+ ModelInfo(id="vendor/free", label="F"),
+ ],
+ )
+ cat._save_cache()
+ raw = json.loads(cache_path.read_text(encoding="utf-8"))
+ assert raw["openrouter"]["models"][0]["pricing"] == [0.375, 1.875]
+ assert "pricing" not in raw["openrouter"]["models"][1]
+ _ts, models = ModelCatalog(cache_path=cache_path)._cache["openrouter"]
+ assert models[0].pricing == (0.375, 1.875)
+ assert models[1].pricing is None
+
def test_corrupt_cache_is_discarded(self, tmp_path: Path) -> None:
cache_path = tmp_path / "model_catalog_cache.json"
cache_path.write_text("{ not json", encoding="utf-8")
diff --git a/tests/unit/realtime/test_gemini_live.py b/tests/unit/realtime/test_gemini_live.py
index 193c88df1e..4ab82380d2 100644
--- a/tests/unit/realtime/test_gemini_live.py
+++ b/tests/unit/realtime/test_gemini_live.py
@@ -680,6 +680,9 @@ async def test_open_session_uses_current_default_model(
assert holder["client"].aio.live.connect_calls[0][0] == (
"gemini-3.1-flash-live-preview"
)
+ # The session tells the orchestrator which id it REALLY opened — usage is
+ # metered and priced against it, so an empty pin must not leak through.
+ assert session.model == "gemini-3.1-flash-live-preview"
await session.close()
diff --git a/tests/unit/realtime/test_live_usage_metering.py b/tests/unit/realtime/test_live_usage_metering.py
index 9769a8a031..73027b193c 100644
--- a/tests/unit/realtime/test_live_usage_metering.py
+++ b/tests/unit/realtime/test_live_usage_metering.py
@@ -238,6 +238,29 @@ async def test_publish_live_usage_prices_audio_and_resets() -> None:
assert session._turn_usage == {}
+async def test_publish_live_usage_prices_the_vertex_live_default_model() -> None:
+ # VertexLiveProvider.default_model was unpriced on 2026-08-18: ten Live
+ # turns of the maintainer's session showed as "$0". Same audio rates as
+ # the AI Studio id of the model.
+ from jarvis.plugins.realtime.gemini_live import VertexLiveProvider
+
+ session = _bare_session(
+ {
+ "input_total": 1_000_000,
+ "output_total": 1_000_000,
+ "input_audio": 1_000_000,
+ "output_audio": 1_000_000,
+ },
+ VertexLiveProvider.default_model,
+ )
+ session._provider = SimpleNamespace(name="vertex-live")
+ await session._publish_live_usage()
+ (event,) = session._bus.published
+ assert event.model == "gemini-live-2.5-flash-native-audio"
+ assert event.cost_usd == pytest.approx(15.0)
+
+
@pytest.mark.asyncio
async def test_publish_live_usage_without_usage_is_silent() -> None:
session = _bare_session({}, "gemini-3.1-flash-live-preview")
diff --git a/tests/unit/realtime/test_openai_realtime.py b/tests/unit/realtime/test_openai_realtime.py
index e5f095c236..d194a1b96f 100644
--- a/tests/unit/realtime/test_openai_realtime.py
+++ b/tests/unit/realtime/test_openai_realtime.py
@@ -560,6 +560,8 @@ async def test_open_session_falls_back_to_adapter_default(
session = await provider.open_session(RealtimeSessionConfig(model=""))
assert holder["client"].realtime.connect_calls == ["gpt-realtime"]
+ # The id the socket really opened with is what usage is priced against.
+ assert session.model == "gpt-realtime"
await session.close()
diff --git a/tests/unit/realtime/test_session.py b/tests/unit/realtime/test_session.py
index 0d1815a52c..f896a256ff 100644
--- a/tests/unit/realtime/test_session.py
+++ b/tests/unit/realtime/test_session.py
@@ -330,6 +330,59 @@ async def test_open_injects_active_providers_model_and_voice():
assert ready["model"] == "gpt-realtime-2.1"
+async def test_open_reports_the_model_the_socket_really_opened_with():
+ """No pin on the card → the id the provider connected (its default), not
+ an empty string. An empty _active_model made every Live turn unpriceable
+ and the deck's API card listed the provider name as the model
+ (2026-08-18)."""
+ from types import SimpleNamespace
+
+ class DefaultModelSession(FakeSession):
+ model = "gemini-live-9.9-flash-native-audio"
+
+ class DefaultModelProvider(FakeProvider):
+ default_model = "gemini-live-9.9-flash-native-audio"
+
+ async def open_session(self, cfg):
+ self.opened_with = cfg
+ self.session = DefaultModelSession(self._events)
+ return self.session
+
+ class ProviderOnlyDefault(FakeProvider):
+ # Adapter whose session object carries no ``model`` attribute.
+ default_model = "provider-default-live"
+
+ async def _open(provider, pinned: str) -> tuple[str, str]:
+ messages = []
+ sess = RealtimeVoiceSession(
+ session_id="s-default-model",
+ send_binary=lambda b: asyncio.sleep(0),
+ send_json=lambda message: messages.append(message) or asyncio.sleep(0),
+ provider=provider,
+ config=_cfg(providers={"fake": SimpleNamespace(model=pinned, voice="")}),
+ bus=None,
+ )
+ await sess.handle_control({"type": "audio_start", "sample_rate": 16000})
+ await asyncio.sleep(0.02)
+ await sess.end(reason="test")
+ ready = next(message for message in messages if message["type"] == "audio_ready")
+ return sess._active_model, ready["model"]
+
+ # The session object knows best; a provider whose session carries no
+ # ``model`` still contributes its default.
+ assert await _open(DefaultModelProvider([]), "") == (
+ "gemini-live-9.9-flash-native-audio",
+ "gemini-live-9.9-flash-native-audio",
+ )
+ assert await _open(ProviderOnlyDefault([]), "") == (
+ "provider-default-live",
+ "provider-default-live",
+ )
+ # A pinned model on the card still wins over the provider's default.
+ assert await _open(DefaultModelProvider([]), "pinned-live") == ("pinned-live", "pinned-live")
+
+
@pytest.mark.asyncio
async def test_browser_webrtc_offer_reaches_provider_and_answer_returns_in_ready():
class AnswerProvider(FakeProvider):