Skip to content

The agent that grows with you

fix(pricing): wire kilocode into the pricing dispatcher

Austin Pickett86df3224e2dd
diff --git a/hermes_cli/models_pricing.py b/hermes_cli/models_pricing.py
index 4b4dbcc0d04..753c690fce4 100644
--- a/hermes_cli/models_pricing.py
+++ b/hermes_cli/models_pricing.py
@@ -414,11 +414,78 @@ def _fetch_novita_pricing_for_provider(*, force_refresh: bool = False) -> dict[s
return _fetch_novita_pricing(force_refresh=force_refresh)
+def _kilo_pricing_scope() -> str:
+ """The kilocode provider profile's gateway base (``KILOCODE_BASE_URL`` overrides it) —
+ the endpoint the doctor connectivity probe and chat requests already use, so pricing reads
+ the same server the user is actually wired to."""
+ from providers import get_provider_profile
+
+ base_url = (
+ os.getenv("KILOCODE_BASE_URL", "").strip()
+ or getattr(get_provider_profile("kilocode"), "base_url", "")
+ or ""
+ )
+ return base_url.rstrip("/").removesuffix("/v1")
+
+
+def _fetch_kilocode_pricing(timeout: float = 8.0, *, force_refresh: bool = False) -> dict[str, dict[str, str]]:
+ """Kilo Gateway ``{base}/models`` pricing (public OpenRouter-shaped catalog — per-token
+ ``prompt``/``completion``/``input_cache_read`` strings, no key required, attached best-effort
+ so an authenticated read also carries the key), cached on the catalog URL.
+
+ ``isFree`` is the explicit free flag the picker badge needs — authoritative over the
+ rates, because a free router (``kilo-auto/free``) can carry non-zero underlying pricing.
+ The gateway's ``-1`` rate sentinel means "varies" (auto-router models); no price is shown
+ rather than a fabricated one, so such rows are skipped.
+ """
+ from hermes_cli.models import _HERMES_USER_AGENT
+
+ cache_key = _kilo_pricing_scope()
+ if not cache_key:
+ return {}
+ if not force_refresh:
+ cached = _cached_catalog(cache_key)
+ if cached is not None:
+ return cached
+
+ headers = {"Accept": "application/json", "User-Agent": _HERMES_USER_AGENT}
+ api_key = os.getenv("KILOCODE_API_KEY", "").strip()
+ if api_key:
+ headers["Authorization"] = f"Bearer {api_key}"
+ payload = _get_json(cache_key + "/models", headers, timeout)
+ if payload is None:
+ return _cache_catalog(cache_key, {})
+
+ result: dict[str, dict[str, str]] = {}
+ for item in _catalog_items(payload):
+ mid, pricing = item.get("id"), item.get("pricing")
+ if not mid or not isinstance(pricing, dict):
+ continue
+ entry = _pricing_entry(pricing)
+ if item.get("isFree"):
+ entry["prompt"] = entry["completion"] = "0"
+ entry.pop("input_cache_read", None)
+ # The gateway's ``-1`` sentinel means "rate varies" (auto-router models); no price is
+ # shown rather than a fabricated one. _price_float also drops any other unusable value
+ # (blank / non-numeric), so a malformed row cannot poison the rest of the catalog.
+ if _price_float(entry["prompt"], positive=False) is None or _price_float(entry["completion"], positive=False) is None:
+ continue
+ if entry:
+ result[str(mid)] = entry
+
+ return _cache_catalog(cache_key, result)
+
+
def _fetch_fireworks_pricing_for_provider(*, force_refresh: bool = False) -> dict[str, dict[str, Any]]:
_remember_provider_cache_key("fireworks", _FIREWORKS_PRICING_KEY)
return _fireworks_pricing_from_models_dev(force_refresh=force_refresh)
+def _fetch_kilocode_pricing_for_provider(*, force_refresh: bool = False) -> dict[str, dict[str, Any]]:
+ _remember_provider_cache_key("kilocode", _kilo_pricing_scope())
+ return _fetch_kilocode_pricing(force_refresh=force_refresh)
+
+
def _fetch_nous_pricing_for_provider(*, force_refresh: bool = False) -> dict[str, dict[str, Any]]:
api_key, base_url = _resolve_nous_pricing_credentials()
if not base_url:
@@ -466,6 +533,7 @@ _STATIC_PRICING_SCOPES = {
"openrouter": lambda: _OPENROUTER_PRICING_BASE,
"ai-gateway": _ai_gateway_pricing_scope,
"novita": _novita_pricing_scope,
+ "kilocode": _kilo_pricing_scope,
"fireworks": lambda: _FIREWORKS_PRICING_KEY,
}
@@ -549,9 +617,9 @@ def get_pricing_for_provider(
provider: str, *, base_url: str = "", force_refresh: bool = False, cached_only: bool = False
) -> dict[str, dict[str, str]]:
"""Return live pricing for providers that support it (openrouter, nous, ai-gateway, novita,
- deepinfra, fireworks); ``{}`` for everything else. ``cached_only`` never starts provider I/O:
- normal picker opens use it so cold endpoints cannot hold the response path, while a background
- prewarm fills the same caches for later opens."""
+ deepinfra, fireworks, kilocode); ``{}`` for everything else. ``cached_only`` never starts
+ provider I/O: normal picker opens use it so cold endpoints cannot hold the response path,
+ while a background prewarm fills the same caches for later opens."""
normalized = resolve_pricing_provider(provider, base_url=base_url)
if cached_only:
return _cached_only_pricing(normalized)
@@ -650,5 +718,6 @@ _PRICING_FETCHERS = {
"novita": _fetch_novita_pricing_for_provider,
"deepinfra": _fetch_deepinfra_pricing,
"fireworks": _fetch_fireworks_pricing_for_provider,
+ "kilocode": _fetch_kilocode_pricing_for_provider,
"nous": _fetch_nous_pricing_for_provider,
}
diff --git a/tests/hermes_cli/test_api_key_providers.py b/tests/hermes_cli/test_api_key_providers.py
index 7872ca4641d..139ff7225a2 100644
--- a/tests/hermes_cli/test_api_key_providers.py
+++ b/tests/hermes_cli/test_api_key_providers.py
@@ -967,6 +967,103 @@ class TestDeepInfraPricingFetcher:
assert "input_cache_read" not in result["vendor/model-b"]
+def _kilo_pricing_isolation(monkeypatch):
+ """Reset the shared pricing caches around each Kilo pricing test.
+
+ The Kilo fetcher keys the shared pricing cache by its catalog URL; without
+ a reset, one test's payload (or its negative cache entry) would answer
+ the next test's read.
+ """
+ import hermes_cli.models_pricing as _mp
+ monkeypatch.setattr(_mp, "_pricing_cache", {})
+ monkeypatch.setattr(_mp, "_pricing_cache_retry_after", {})
+ monkeypatch.setattr(_mp, "_pricing_provider_cache_keys", {})
+
+
[email protected]("_kilo_pricing_isolation")
+class TestKilocodePricingFetcher:
+ """_fetch_kilocode_pricing maps the Kilo Gateway's OpenRouter-shaped catalog
+ (per-token pricing + explicit isFree + ``-1`` rate sentinels) into picker
+ pricing, and is wired into the get_pricing_for_provider dispatch."""
+
+ def test_pricing_shape_and_dispatch(self, monkeypatch):
+ payload = {"data": [
+ {
+ "id": "vendor/paid",
+ "pricing": {
+ "prompt": "0.000004",
+ "completion": "0.00002",
+ "input_cache_read": "0.0000004",
+ },
+ },
+ # Free router with non-zero underlying rates — the explicit isFree
+ # flag is authoritative and must win (the kilo-auto/free case).
+ {"id": "kilo-auto/free", "isFree": True,
+ "pricing": {"prompt": "0.000002", "completion": "0.00001"}},
+ # Sentinel pricing ("rate varies per request") — no price is better
+ # than a fabricated one, so the row is skipped.
+ {"id": "kilo-auto/efficient",
+ "pricing": {"prompt": "-1", "completion": "-1"}},
+ # Malformed records must be skipped, not crash the parser.
+ {"id": "vendor/no-pricing"},
+ {"id": ""},
+ "not-a-dict",
+ ]}
+ import hermes_cli.models as models
+ monkeypatch.setattr(
+ models,
+ "_urlopen_model_catalog_request",
+ _make_urlopen_returning(payload),
+ )
+ from hermes_cli.models_pricing import get_pricing_for_provider
+
+ # get_pricing_for_provider → _fetch_kilocode_pricing dispatch path
+ result = get_pricing_for_provider("kilocode")
+ assert set(result) == {"vendor/paid", "kilo-auto/free"}
+ # Paid: per-token strings pass through, cache-read when the source had it.
+ assert result["vendor/paid"] == {
+ "prompt": "0.000004", "completion": "0.00002",
+ "input_cache_read": "0.0000004",
+ }
+ # Free: explicit zero mapping, ignoring the non-zero underlying rates.
+ assert result["kilo-auto/free"] == {"prompt": "0", "completion": "0"}
+
+ def test_endpoint_comes_from_profile_base_url_and_is_public(self, monkeypatch):
+ """Default endpoint is the kilocode provider profile's base_url + /models
+ (no second hardcode); KILOCODE_BASE_URL overrides it; the catalog is
+ fetched without a key; a failing endpoint yields {} without raising."""
+ import hermes_cli.models as models
+ from hermes_cli.models_pricing import get_pricing_for_provider
+
+ seen = []
+
+ def fake_urlopen(req, timeout=None):
+ seen.append((req.full_url, req.get_header("Authorization")))
+ return _make_urlopen_returning(
+ {"data": [{"id": "a/b", "pricing": {"prompt": "0.1", "completion": "0.2"}}]}
+ )(req, timeout=timeout)
+
+ monkeypatch.delenv("KILOCODE_API_KEY", raising=False)
+ monkeypatch.delenv("KILOCODE_BASE_URL", raising=False)
+ monkeypatch.setattr(models, "_urlopen_model_catalog_request", fake_urlopen)
+
+ expected = {"a/b": {"prompt": "0.1", "completion": "0.2"}}
+ assert get_pricing_for_provider("kilocode", force_refresh=True) == expected
+ assert seen == [("https://api.kilo.ai/api/gateway/models", None)]
+
+ monkeypatch.setenv("KILOCODE_BASE_URL", "https://proxy.example.com/kilo")
+ assert get_pricing_for_provider("kilocode", force_refresh=True) == expected
+ assert seen[-1][0] == "https://proxy.example.com/kilo/models"
+
+ monkeypatch.setattr(
+ models,
+ "_urlopen_model_catalog_request",
+ lambda *a, **kw: (_ for _ in ()).throw(OSError("endpoint down")),
+ )
+ assert get_pricing_for_provider("kilocode", force_refresh=True) == {}
+
+
class TestDeepInfraProviderProfile:
"""plugins/model-providers/deepinfra registration + aux resolution."""