Skip to content

Show the smart-routing savings estimate in the Claude mod - #850

Open
JJ27 wants to merge 7 commits into
mainfrom
josh-joseph/smart-routing-savings-statusline
Open

JJ27 wants to merge 7 commits into
mainfrom
josh-joseph/smart-routing-savings-statusline

Conversation

@JJ27

@JJ27 JJ27 commented Sep 25, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Adds a smart-routing savings estimate to ug's Claude Code mod. The estimate appears in a dim row above the prompt, alongside the Smart Router Orchestrator skill version. It's on by default; ENABLE_SMART_ROUTING_SAVINGS=0 opts a launch out.

💰 Est. saved with smart routing: $0.18 (48%) · Smart Router Orchestrator v1.2.0
Smart routing cost $0.04 more (10%) · Smart Router Orchestrator v1.2.0
Smart Router Orchestrator v1.2.0
  • The estimate appears once a request has run on a model other than its baseline. If routing cost more, the row says so.
  • The version comes from skills/smart-router-orchestrator/SKILL.claude.md (metadata.version), and is shown only when ENABLE_SMART_ROUTER_ORCHESTRATOR=1.
  • With neither an estimate nor a version, there is no row.

This replaces the earlier transcript-statusline approach. The PR is rebuilt on main, on top of the native orchestrator skill and the mods scaffolding from #1062, and the statusline is gone.

Screenshots

Pending. A laptop run will add them. Prod endpoint-rates currently returns 404, so the screenshots will use seeded list-price rates, not live endpoint rates.

How it works

  • Accounting (mod, savings.ts): on each turn.step, for the main agent and subagents, the mod adds the request's token usage to totals grouped by agent, baseline, served model, and whether the request ran before a user model switch.
    • Served is the model service the request named.
    • The main agent's baseline is its own served model. A subagent's baseline is the main model in effect at the time.
    • Claude Code's side queries (titles, compaction) aren't turn steps, so they never count.
  • Pricing (Python, ucode.smart_routing.savings): a hooks module has no Node APIs, and the price table lives in Python. So after each turn the mod writes its totals to a per-launch usage file and runs the pricer command on it. The pricer returns the row text as one JSON line. It computes:
    • saved = Σ tokens × price(baseline) − Σ tokens × price(served), per token class: input, output, cache read, and 5-minute and 1-hour cache writes.
    • Cache writes without the 5m/1h split are priced at the 1-hour rate, since ug launches Claude with 1-hour caching.
  • First-prompt routing: requests before a user switch are priced against the session's start model. After a /model, requests are priced against their own baseline.
  • Plugin options: ug passes the pricer argv, the usage file path, and the orchestrator version to the mod as pluginConfigs options. These are declared with defaults in the generated plugin's userConfig. The pricer is a multiple: true list, so paths with spaces and unicode reach the mod intact.
  • Hidden rather than undercounted: if any entry is unpriced or malformed, or there are no prices yet, the row omits the estimate.

Price source

Each routed launch refreshes prices on a background thread from GET /api/ai-gateway/v2/endpoint-rates:batchGet, with repeated databricks_hosted_model_services params covering the router's and main agent's model services. Results are cached per workspace at ~/.ucode/model-prices-<host hash>.json.

  • The client reads the USD cost group: per-million-token rates for input, output, cache reads, and 5m/1h cache writes.
  • A failed refresh keeps the existing cache.
  • Served ids are matched back to system.ai names. This covers Bedrock-style ids, -colo-on-* deployments, and dotted versions.
  • This was validated end-to-end against the staging endpoint earlier. Prod returned 404 on 2026-10-07; once the route is live, the estimate populates with no client change.

Testing

  • Unit tests:
    • tests/test_smart_routing_savings.py covers baselines, user switches, net cost increases, cache-write splits, first-prompt start-model pricing, voiding on unpriced or malformed entries, the row wording, and the pricer subprocess (one UTF-8 JSON line under an ASCII locale; never fails).
    • tests/test_smart_routing_pricing.py and tests/test_databricks.py cover rate mapping and the endpoint request.
    • tests/test_skills.py covers the skill version.
    • tests/test_claude_mod.py and tests/test_claude_smart_routing_v2.py cover the plugin contents, option defaults, launch options for both routing modes, the opt-out, and the background price refresh.
  • Native Claude Code 2.1.290 (tests/native_claude/test_mod.py):
    • the mod validates;
    • the savings options round-trip through pluginConfigs and stay launch-local;
    • a real session writes the usage file with its start model.
  • Not covered automatically: token accumulation across real turn.step events, and rendering of the row above the prompt. Those will be checked in the laptop run above.
  • Results: full suite green apart from the two test_e2e_user_agent tests that also fail on clean main; ruff and ty clean.

Also in this PR: tracing CI fix

The Claude and Codex tracing journeys were timing out (SQL statement did not finish within 180 seconds). tests/integration/utils/sql.py picked the first warehouse when none was running, then counted warehouse startup against the query's 180s budget. The fix:

  • prefer a serverless warehouse;
  • allow up to 10 minutes in PENDING before the 180s execution limit starts;
  • report which phase timed out.

This can move to its own PR if reviewers prefer.

This pull request and its description were written by Isaac.

@JJ27
JJ27 marked this pull request as ready for review September 29, 2026 23:28
Copilot AI balanced review requested due to automatic review settings September 29, 2026 23:28

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI balanced review requested due to automatic review settings September 29, 2026 23:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Comment thread src/ucode/smart_routing/claude_statusline.py Outdated
JJ27 added a commit that referenced this pull request Oct 6, 2026
Carries PR #850's statusline, pricing, and endpoint-rates client onto the
Claude mods stack. Conflicts with the mods branch are resolved by keeping
both sides; ENABLE_SMART_ROUTING_SAVINGS stays in v2 since the routing
flags moved to ucode.constants.

Co-authored-by: Isaac <no-reply@databricks.com>
@JJ27
JJ27 force-pushed the josh-joseph/smart-routing-savings-statusline branch from e547da8 to 6f8dc9c Compare October 6, 2026 05:20
Copilot AI balanced review requested due to automatic review settings October 6, 2026 05:20
@JJ27
JJ27 changed the base branch from main to claude-mods-subagent-routing October 6, 2026 05:20

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

The launch-scoped ug-smart-router mod now draws a dim row above the
prompt with the estimated savings from subagent smart routing and,
when Smart Router Orchestrator is active, the skill's version from
SKILL.claude.md. The mod sums each turn request's tokens by served and
baseline model and runs a stdlib-only pricer against cached endpoint
rates, which each routed launch refreshes in the background. The
pricer, usage file and version reach the mod as plugin options.
ENABLE_SMART_ROUTING_SAVINGS=0 opts a launch out.

Co-authored-by: Isaac <no-reply@databricks.com>
@JJ27
JJ27 force-pushed the josh-joseph/smart-routing-savings-statusline branch from 63aa3f9 to da5c0b9 Compare October 12, 2026 20:22
@JJ27 JJ27 changed the title Add Claude Code statusline estimating smart-routing savings (client side) Show the smart-routing savings estimate in the Claude mod Oct 12, 2026
@JJ27
JJ27 changed the base branch from claude-mods-subagent-routing to main October 12, 2026 20:23
@lilly-luo lilly-luo added the ug-review Run the automated UG review label Oct 13, 2026

@lilly-luo lilly-luo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add ENABLE_SMART_ROUTING_SAVINGS to config.py

, make it 0 for all the configurations, then add 1 more configuration that makes this 1?

# Conflicts:
#	src/ucode/agents/claude_mods/register.ts
#	src/ucode/smart_routing/v2.py
#	tests/README.md
#	tests/test_claude_mod.py
Copilot AI balanced review requested due to automatic review settings October 13, 2026 22:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

JJ27 and others added 2 commits October 13, 2026 22:24
Co-authored-by: Isaac <no-reply@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>
Copilot AI balanced review requested due to automatic review settings October 14, 2026 00:08

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

JJ27 and others added 2 commits October 14, 2026 17:14
Co-authored-by: Isaac <no-reply@databricks.com>
Label the version as Unity Gateway Smart Router, show the amount in a
green (saved) or yellow (cost more) pill, and render the row after the
other AbovePrompt handlers so it sits under the band in either order.
The pricer now prints the estimate's parts instead of preformatted text.

Co-authored-by: Isaac <no-reply@databricks.com>
Copilot AI balanced review requested due to automatic review settings October 14, 2026 17:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ug-review Run the automated UG review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants