Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/ucode/agents/claude.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
external_provider_selected,
)
from ucode.os_compatibility import subprocess_cross_os
from ucode.smart_routing import pricing
from ucode.smart_routing import v2 as smart_routing_v2
from ucode.smart_routing.claude_hooks import (
FIRST_PROMPT_SOCKET_ENV,
Expand Down Expand Up @@ -2108,6 +2109,17 @@ def launch(
}
settings_override = _merge_claude_settings(settings_override or {}, {"env": fallback_env})
os.environ.update(fallback_env)
if smart_routing_v2.savings_statusline_enabled() and workspace:
# Show "Smart routing off" in the status row when nothing is routed: a plain launch, or a
# routed one whose setup failed (`launch_claude` otherwise exits without reaching here).
settings_override = settings_override or {}
smart_routing_v2.install_savings_statusline(
settings_override,
CLAUDE_USER_SETTINGS_PATH,
price_cache=pricing.price_cache_path(APP_DIR, workspace),
routing_enabled=False,
baseline_session_start=False,
)
exec_or_spawn(_build_claude_argv(binary, launch_args, settings_override=settings_override))


Expand Down
32 changes: 32 additions & 0 deletions src/ucode/databricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2092,6 +2092,38 @@ def fetch_external_model_prices(workspace: str, token: str) -> tuple[list[dict],
return models, None


# Per-token rates for Databricks-hosted (`system.ai`) model services from the gateway's
# pay-per-token endpoint-rates API, which the smart-routing savings statusline prices usage with.
# The model names travel as repeated `databricks_hosted_model_services` query parameters.
_ENDPOINT_RATES_API_PATH = "/api/ai-gateway/v2/endpoint-rates:batchGet"
_SYSTEM_AI_MODEL_PREFIX = "system.ai."


def fetch_endpoint_rates(
workspace: str, token: str, model_services: list[str]
) -> tuple[list[dict], str | None]:
"""Batch-fetch per-token rates for ``system.ai`` model services.

Returns ``(rates, reason)`` with each rate the raw ``EndpointRate`` entry; models the caller
can't see are absent rather than errors. Names outside ``system.ai`` are dropped because the
API rejects them. ``reason`` is non-None on failure so callers omit the estimate, not fail.
"""
names = sorted({name for name in model_services if name.startswith(_SYSTEM_AI_MODEL_PREFIX)})
if not names:
return [], "no system.ai model services to price"
query = urlencode([("databricks_hosted_model_services", name) for name in names])
url = f"https://{workspace_hostname(workspace)}{_ENDPOINT_RATES_API_PATH}?{query}"
payload, reason = _http_get_json(url, token, timeout=30)
if reason is not None:
return [], reason
if not isinstance(payload, dict):
return [], "endpoint-rates returned an unexpected response shape"
rates = payload.get("model_service_rates")
if not isinstance(rates, list):
return [], None
return [rate for rate in rates if isinstance(rate, dict)], None


# The `update_mask` paths a config PATCH sends. The server rejects paths outside its mutable set,
# so this omits `spec_version` (an estore-internal format marker, still sent in the body; naming it
# in the mask is the 400 this fixes) and the reserved/deprecated fields (`display_name`, `tracing`,
Expand Down
5 changes: 3 additions & 2 deletions src/ucode/mods.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ def write_mod(plugin_dir: Path, mod: ClaudeMod) -> None:


# ug's smart-routing UI mod: a register.ts entry that composes the concern
# modules it imports (today the status band).
# modules it imports (the status band, the subagent routing line, and the
# savings estimate the band appends).
SMART_ROUTING_UI = ClaudeMod(
source="register.ts",
extra=("smart-routing-status.ts", "subagent-routing.ts"),
extra=("smart-routing-status.ts", "subagent-routing.ts", "smart-routing-savings.ts"),
)
Loading
Loading