From 97f657250e63f68db482791db5dab69ba654bc22 Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 13:35:10 -0500 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9C=A8=20Price=20gateway=20models=20from?= =?UTF-8?q?=20the=20gateway=20and=20show=20unknown=20prices=20explicitly?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - tokens: gateway price source hook (ResolvePrice/EstimateCostWithSource); gateway-path models are never tier-guessed; unknown prices are excluded from totals and listed - dashboard: OpenRouter catalog prices, LiteLLM /model/info, and a new per-gateway prices: override, cached in the background - proxy: record cached prompt tokens (prompt_tokens_details.cached_tokens) so the cache-read rate applies; new gateway_cache callback kind - usage gauge: one spend row per gateway; cost card says price unknown Signed-off-by: Andy Anderson --- changelog.d/added-gateway-model-pricing.md | 1 + docs/inference-backends.md | 29 ++ src/crux/api/callback/callback.go | 5 +- src/crux/api/callback/server.go | 4 +- src/crux/config/agent_config.go | 25 ++ src/crux/hivecap/hivecap.go | 6 + src/crux/proxy/anthropic_translate.go | 52 ++- src/crux/proxy/gateway_cached_usage_test.go | 76 ++++ src/crux/proxy/github_proxy_inference.go | 20 +- src/crux/proxy/host_seams.go | 24 ++ src/docs/design/crux.md | 2 +- src/pkg/config/crux_aliases.go | 2 + src/pkg/cruxhost/cruxhost.go | 9 + src/pkg/cruxhost/gateway_cache_usage_test.go | 41 ++ src/pkg/dashboard/api_providers_headroom.go | 12 +- src/pkg/dashboard/cost.go | 48 ++- src/pkg/dashboard/gateway_prices.go | 384 ++++++++++++++++++ src/pkg/dashboard/gateway_prices_test.go | 178 ++++++++ src/pkg/dashboard/gateway_spend_gauge.test.js | 58 +++ src/pkg/dashboard/gateways.go | 2 + src/pkg/dashboard/openrouter.go | 1 + src/pkg/dashboard/server.go | 5 + src/pkg/dashboard/static-src/css/cost.css | 1 + src/pkg/dashboard/static-src/css/overview.css | 1 + src/pkg/dashboard/static-src/js/cost.js | 17 +- .../static-src/js/subscription-gauge.js | 59 ++- src/pkg/dashboard/static/index.html | 78 +++- src/pkg/tokens/gateway_pricing.go | 126 ++++++ src/pkg/tokens/gateway_pricing_test.go | 127 ++++++ src/pkg/tokens/inference_sink.go | 7 + src/pkg/tokens/pricing.go | 98 +++-- 31 files changed, 1409 insertions(+), 89 deletions(-) create mode 100644 changelog.d/added-gateway-model-pricing.md create mode 100644 src/crux/proxy/gateway_cached_usage_test.go create mode 100644 src/pkg/cruxhost/gateway_cache_usage_test.go create mode 100644 src/pkg/dashboard/gateway_prices.go create mode 100644 src/pkg/dashboard/gateway_prices_test.go create mode 100644 src/pkg/dashboard/gateway_spend_gauge.test.js create mode 100644 src/pkg/tokens/gateway_pricing.go create mode 100644 src/pkg/tokens/gateway_pricing_test.go diff --git a/changelog.d/added-gateway-model-pricing.md b/changelog.d/added-gateway-model-pricing.md new file mode 100644 index 0000000000..1814f4b040 --- /dev/null +++ b/changelog.d/added-gateway-model-pricing.md @@ -0,0 +1 @@ +- Gateway spend is priced from the gateway: OpenRouter's catalog prices, LiteLLM `/model/info`, or a new per-gateway `prices:` block (input, cached input, output per 1M tokens). Cached prompt tokens are billed at the cache-read rate. Gateway models with no price show as "price unknown" instead of $0 or a tier guess, and the usage gauge gets a spend row per gateway. diff --git a/docs/inference-backends.md b/docs/inference-backends.md index 48a5e7a85f..655d6fcc5f 100644 --- a/docs/inference-backends.md +++ b/docs/inference-backends.md @@ -169,6 +169,35 @@ A spoke with an OpenRouter key proxies OpenRouter's `/api/v1/key` credit endpoin Hive probes `/v1/models` on OpenAI-compatible gateways. LiteLLM discovery includes bearer auth when a key is configured. If discovery fails, the UI falls back to static or configured model lists and marks entries as unverified; fallback data should not be treated as proof that the endpoint is healthy. +## Gateway spend and prices + +The cost card and the Overview usage gauge price gateway models from the gateway itself, in this order: + +1. A `prices:` block on the gateway in `hive.yaml` (USD per 1M tokens). It wins over everything else and is the only source for `vllm`, `llm-d`, `watsonx` and `custom` gateways, which publish no prices. +2. For `openrouter`, the `pricing` block of the public `/models` catalog that model discovery already reads (input, output and cache-read prices per token). +3. For `litellm`, the proxy's `/model/info` (`input_cost_per_token`, `output_cost_per_token`, `cache_read_input_token_cost`), read with the gateway key. +4. The static list-price table, for gateway ids it knows. + +If a gateway model has none of these, it shows as **price unknown**. Its tokens are left out of the total (which is then a lower bound) and listed by name. It is never counted as $0 or as a tier guess. When the gateway reports cached prompt tokens (`usage.prompt_tokens_details.cached_tokens`), they are billed at the cache-read price. A cached-input price you leave unset defaults to the input price. + +```yaml +governor: + gateways: + - name: local-vllm + kind: vllm + endpoint: http://vllm:8000 + prices: # USD per 1M tokens + input: 0.20 + cached_input: 0.02 + output: 0.60 + models: # optional per-model overrides + Qwen/Qwen3-235B: + input: 0.90 + output: 2.70 +``` + +The gauge shows gateways as dollar rows, not dials, because a gateway has no plan to measure. Each row covers the enabled agents whose backend names that gateway, over the same token window as the cost card. + ## Guided example: Watsonx via gateway The watsonx path uses the same gateway machinery: configure the gateway endpoint and credentials, verify `/v1/models`, then assign an agent to that backend/gateway. Use it as the guided flow for any enterprise gateway: diff --git a/src/crux/api/callback/callback.go b/src/crux/api/callback/callback.go index 70f7c63946..2402733ede 100644 --- a/src/crux/api/callback/callback.go +++ b/src/crux/api/callback/callback.go @@ -268,11 +268,14 @@ type RateLimitSnapshot struct { OK bool `json:"ok"` } -// Usage kinds, the three UsageSink record paths. +// Usage kinds, the UsageSink record paths. const ( UsageKindGateway = "gateway" UsageKindCopilot = "copilot" UsageKindCopilotCache = "copilot_cache" + // UsageKindGatewayCache is a gateway response whose prompt was partly + // served from cache: InputTokens excludes CacheRead. + UsageKindGatewayCache = "gateway_cache" ) // UsageRecord is the body of POST /v1/usage. diff --git a/src/crux/api/callback/server.go b/src/crux/api/callback/server.go index f6dfb14e6f..3f526951c5 100644 --- a/src/crux/api/callback/server.go +++ b/src/crux/api/callback/server.go @@ -241,9 +241,9 @@ func (s *Server) handleUsage(w http.ResponseWriter, r *http.Request) { return } switch rec.Kind { - case UsageKindGateway, UsageKindCopilot, UsageKindCopilotCache: + case UsageKindGateway, UsageKindGatewayCache, UsageKindCopilot, UsageKindCopilotCache: default: - s.writeError(w, r, http.StatusBadRequest, CodeBadRequest, "kind must be gateway, copilot or copilot_cache") + s.writeError(w, r, http.StatusBadRequest, CodeBadRequest, "kind must be gateway, gateway_cache, copilot or copilot_cache") return } if err := s.Caps.RecordUsage(r.Context(), rec); err != nil { diff --git a/src/crux/config/agent_config.go b/src/crux/config/agent_config.go index c3b9d573e8..3f54e513cb 100644 --- a/src/crux/config/agent_config.go +++ b/src/crux/config/agent_config.go @@ -647,6 +647,31 @@ type GatewayConfig struct { // round-trip; an absent name is a normal, backwards-compatible state the // dashboard renders as "(unnamed)" rather than an error. KeyName string `yaml:"key_name,omitempty" json:"key_name,omitempty"` + // Prices optionally sets what this gateway charges, in USD per 1M tokens, + // so spend reports can cost its models. It is the only price source for + // vllm, llm-d, watsonx and custom gateways (which publish none) and wins + // over the OpenRouter catalog and LiteLLM /model/info when set. Without a + // price the dashboard reports the gateway's spend as "price unknown" + // rather than $0. omitempty keeps existing hive.yaml files byte-identical. + Prices *GatewayPrices `yaml:"prices,omitempty" json:"prices,omitempty"` +} + +// GatewayPrices is a gateway's configured price, USD per 1M tokens. The +// top-level rates apply to every model the gateway serves; Models overrides +// them for specific model ids. CachedInput defaults to Input when unset (cached +// prompt tokens are never assumed free). +type GatewayPrices struct { + Input *float64 `yaml:"input,omitempty" json:"input,omitempty"` + CachedInput *float64 `yaml:"cached_input,omitempty" json:"cached_input,omitempty"` + Output *float64 `yaml:"output,omitempty" json:"output,omitempty"` + Models map[string]GatewayModelPrice `yaml:"models,omitempty" json:"models,omitempty"` +} + +// GatewayModelPrice is one model's configured price, USD per 1M tokens. +type GatewayModelPrice struct { + Input *float64 `yaml:"input,omitempty" json:"input,omitempty"` + CachedInput *float64 `yaml:"cached_input,omitempty" json:"cached_input,omitempty"` + Output *float64 `yaml:"output,omitempty" json:"output,omitempty"` } // TrajectoryConfig governs the trajectory-review lane: a periodic, diff --git a/src/crux/hivecap/hivecap.go b/src/crux/hivecap/hivecap.go index 1520730332..c3b7fb0e64 100644 --- a/src/crux/hivecap/hivecap.go +++ b/src/crux/hivecap/hivecap.go @@ -204,6 +204,12 @@ func (u UsageSink) Record(agent, model string, in, out int64) { u.send(callback.UsageRecord{Agent: agent, Model: model, Kind: callback.UsageKindGateway, InputTokens: in, OutputTokens: out}) } +// RecordWithCache records a gateway response with its cached prompt tokens +// split out (in excludes cacheRead). +func (u UsageSink) RecordWithCache(agent, model string, in, out, cacheRead, cacheCreation int64) { + u.send(callback.UsageRecord{Agent: agent, Model: model, Kind: callback.UsageKindGatewayCache, InputTokens: in, OutputTokens: out, CacheRead: cacheRead, CacheCreation: cacheCreation}) +} + func (u UsageSink) RecordCopilot(agent, model string, in, out int64) { u.send(callback.UsageRecord{Agent: agent, Model: model, Kind: callback.UsageKindCopilot, InputTokens: in, OutputTokens: out}) } diff --git a/src/crux/proxy/anthropic_translate.go b/src/crux/proxy/anthropic_translate.go index 649451c89d..c764a21262 100644 --- a/src/crux/proxy/anthropic_translate.go +++ b/src/crux/proxy/anthropic_translate.go @@ -254,14 +254,22 @@ func translateAnthropicToolChoice(raw json.RawMessage) json.RawMessage { // usage block is present. Used to attribute inference-agent token consumption // into the token store. func extractOpenAIUsage(body []byte) (inputTokens, outputTokens int64) { + u := extractOpenAIUsageDetailed(body) + return u.Input, u.Output +} + +// extractOpenAIUsageDetailed is extractOpenAIUsage plus the cached part of +// the prompt (usage.prompt_tokens_details.cached_tokens). +func extractOpenAIUsageDetailed(body []byte) gatewayUsage { var resp openaiResponse - if err := json.Unmarshal(body, &resp); err != nil { - return 0, 0 + if err := json.Unmarshal(body, &resp); err != nil || resp.Usage == nil { + return gatewayUsage{} } - if resp.Usage == nil { - return 0, 0 + return gatewayUsage{ + Input: int64(resp.Usage.PromptTokens), + Output: int64(resp.Usage.CompletionTokens), + Cached: int64(resp.Usage.cachedTokens()), } - return int64(resp.Usage.PromptTokens), int64(resp.Usage.CompletionTokens) } // translateOpenAIResponseToAnthropic converts a non-streaming OpenAI Chat @@ -332,6 +340,13 @@ func translateOpenAIResponseToAnthropic(body []byte, model string) ([]byte, erro // stream_options.include_usage). Returns (0, 0) when the gateway did not emit // usage. func translateOpenAISSEToAnthropic(r io.Reader, w io.Writer, model string) (inputTokens, outputTokens int64, err error) { + u, err := translateOpenAISSEToAnthropicUsage(r, w, model) + return u.Input, u.Output, err +} + +// translateOpenAISSEToAnthropicUsage is translateOpenAISSEToAnthropic that +// also reports the cached part of the prompt from the final usage chunk. +func translateOpenAISSEToAnthropicUsage(r io.Reader, w io.Writer, model string) (gatewayUsage, error) { msgID := fmt.Sprintf("msg_%d", time.Now().UnixNano()) writeSSE(w, "message_start", anthropicSSEMessageStart{ @@ -352,6 +367,7 @@ func translateOpenAISSEToAnthropic(r io.Reader, w io.Writer, model string) (inpu var totalInputTokens int var totalOutputTokens int + var totalCachedTokens int var contentBlockIndex int textBlockStarted := false toolCallState := make(map[int]*sseToolCallAccumulator) @@ -376,6 +392,9 @@ func translateOpenAISSEToAnthropic(r io.Reader, w io.Writer, model string) (inpu if chunk.Usage.PromptTokens > 0 { totalInputTokens = chunk.Usage.PromptTokens } + if c := chunk.Usage.cachedTokens(); c > 0 { + totalCachedTokens = c + } } if len(chunk.Choices) > 0 { @@ -473,7 +492,7 @@ func translateOpenAISSEToAnthropic(r io.Reader, w io.Writer, model string) (inpu } writeSSE(w, "message_stop", map[string]string{"type": "message_stop"}) - return int64(totalInputTokens), int64(totalOutputTokens), scanner.Err() + return gatewayUsage{Input: int64(totalInputTokens), Output: int64(totalOutputTokens), Cached: int64(totalCachedTokens)}, scanner.Err() } type sseToolCallAccumulator struct { @@ -697,6 +716,27 @@ type openaiUsage struct { PromptTokens int `json:"prompt_tokens"` CompletionTokens int `json:"completion_tokens"` TotalTokens int `json:"total_tokens"` + // PromptTokensDetails.CachedTokens is the part of PromptTokens served + // from the provider's prompt cache (OpenAI, OpenRouter, LiteLLM, vLLM + // with prefix caching). It bills at the cache-read rate. + PromptTokensDetails *struct { + CachedTokens int `json:"cached_tokens"` + } `json:"prompt_tokens_details,omitempty"` +} + +// cachedTokens is the cached part of the prompt, 0 when not reported. +func (u *openaiUsage) cachedTokens() int { + if u == nil || u.PromptTokensDetails == nil { + return 0 + } + return u.PromptTokensDetails.CachedTokens +} + +// gatewayUsage is one upstream response's token usage on the gateway path. +// Input is the FULL prompt as the gateway reports it (prompt_tokens); Cached +// is the part of it read from the prompt cache. +type gatewayUsage struct { + Input, Output, Cached int64 } type openaiSSEChunk struct { diff --git a/src/crux/proxy/gateway_cached_usage_test.go b/src/crux/proxy/gateway_cached_usage_test.go new file mode 100644 index 0000000000..1a7466b4b6 --- /dev/null +++ b/src/crux/proxy/gateway_cached_usage_test.go @@ -0,0 +1,76 @@ +package proxy + +import ( + "bytes" + "log/slog" + "strings" + "testing" +) + +// cacheSink records gateway usage with the cached split, like +// *tokens.InferenceSink. +type cacheSink struct { + memUsageSink + in, out, cacheRead int64 + calls int +} + +func (c *cacheSink) RecordWithCache(agent, model string, in, out, cacheRead, cacheCreation int64) { + c.calls++ + c.in, c.out, c.cacheRead = in, out, cacheRead +} + +func TestExtractOpenAIUsageDetailedReadsCachedTokens(t *testing.T) { + body := []byte(`{"choices":[],"usage":{"prompt_tokens":1000,"completion_tokens":50,"prompt_tokens_details":{"cached_tokens":800}}}`) + u := extractOpenAIUsageDetailed(body) + if u.Input != 1000 || u.Output != 50 || u.Cached != 800 { + t.Fatalf("usage = %+v", u) + } + if in, out := extractOpenAIUsage(body); in != 1000 || out != 50 { + t.Fatalf("legacy extract = %d %d", in, out) + } + if u := extractOpenAIUsageDetailed([]byte(`{"usage":{"prompt_tokens":5,"completion_tokens":1}}`)); u.Cached != 0 { + t.Fatalf("no details must mean no cached tokens: %+v", u) + } +} + +func TestSSEUsageReportsCachedTokens(t *testing.T) { + stream := strings.Join([]string{ + `data: {"choices":[{"delta":{"content":"hi"}}]}`, + `data: {"choices":[],"usage":{"prompt_tokens":1200,"completion_tokens":30,"prompt_tokens_details":{"cached_tokens":1000}}}`, + `data: [DONE]`, + }, "\n") + "\n" + var out bytes.Buffer + u, err := translateOpenAISSEToAnthropicUsage(strings.NewReader(stream), &out, "deepseek/deepseek-v4.1-flash") + if err != nil { + t.Fatal(err) + } + if u.Input != 1200 || u.Output != 30 || u.Cached != 1000 { + t.Fatalf("usage = %+v", u) + } +} + +// OpenAI-shaped usage counts cached tokens INSIDE prompt_tokens: the sink gets +// the uncached remainder as input and the cached part as cache reads. +func TestRecordGatewayUsageSplitsCachedPrompt(t *testing.T) { + p := &GitHubProxy{logger: slog.Default()} + sink := &cacheSink{} + p.SetTokenSink(sink) + p.recordGatewayUsage("a", "m", gatewayUsage{Input: 1000, Output: 50, Cached: 800}) + if sink.calls != 1 || sink.in != 200 || sink.out != 50 || sink.cacheRead != 800 { + t.Fatalf("split = %+v", sink) + } + // Cached larger than the prompt is clamped, never a negative input. + p.recordGatewayUsage("a", "m", gatewayUsage{Input: 10, Output: 1, Cached: 99}) + if sink.in != 0 || sink.cacheRead != 10 { + t.Fatalf("clamp = %+v", sink) + } + // No cached tokens: the plain Record path, as before. + p.recordGatewayUsage("b", "m", gatewayUsage{Input: 7, Output: 3}) + if recs := sink.forAgent("b"); len(recs) != 1 { + t.Fatalf("plain record = %+v", recs) + } + // Nothing to record, and a nil sink, are no-ops. + p.recordGatewayUsage("c", "m", gatewayUsage{}) + (&GitHubProxy{logger: slog.Default()}).recordGatewayUsage("c", "m", gatewayUsage{Input: 1, Cached: 1}) +} diff --git a/src/crux/proxy/github_proxy_inference.go b/src/crux/proxy/github_proxy_inference.go index fba6ad30a6..45fd160ea6 100644 --- a/src/crux/proxy/github_proxy_inference.go +++ b/src/crux/proxy/github_proxy_inference.go @@ -559,13 +559,11 @@ func (p *GitHubProxy) inferenceTranslatorHandler() http.Handler { if f, ok := w.(http.Flusher); ok { f.Flush() } - in, out, err := translateOpenAISSEToAnthropic(resp.Body, w, route.Model) + usage, err := translateOpenAISSEToAnthropicUsage(resp.Body, w, route.Model) if err != nil { p.logger.Error("inference SSE translation failed", "agent", agentName, "error", err) } - if in > 0 || out > 0 { - p.recordUsage(agentName, route.Model, in, out) - } + p.recordGatewayUsage(agentName, route.Model, usage) return } @@ -581,9 +579,7 @@ func (p *GitHubProxy) inferenceTranslatorHandler() http.Handler { "preview", truncateBytes(respBody, 500), ) - if in, out := extractOpenAIUsage(respBody); in > 0 || out > 0 { - p.recordUsage(agentName, route.Model, in, out) - } + p.recordGatewayUsage(agentName, route.Model, extractOpenAIUsageDetailed(respBody)) translated, err := translateOpenAIResponseToAnthropic(respBody, route.Model) if err != nil { @@ -761,13 +757,11 @@ func (p *GitHubProxy) handleInferenceRequest(conn net.Conn, req *http.Request, a if _, err := conn.Write([]byte(hdr)); err != nil { return } - in, out, err := translateOpenAISSEToAnthropic(resp.Body, conn, route.Model) + usage, err := translateOpenAISSEToAnthropicUsage(resp.Body, conn, route.Model) if err != nil { p.logger.Error("inference SSE translation failed", "agent", agentName, "error", err) } - if in > 0 || out > 0 { - p.recordUsage(agentName, route.Model, in, out) - } + p.recordGatewayUsage(agentName, route.Model, usage) return } @@ -777,9 +771,7 @@ func (p *GitHubProxy) handleInferenceRequest(conn net.Conn, req *http.Request, a return } - if in, out := extractOpenAIUsage(respBody); in > 0 || out > 0 { - p.recordUsage(agentName, route.Model, in, out) - } + p.recordGatewayUsage(agentName, route.Model, extractOpenAIUsageDetailed(respBody)) translated, err := translateOpenAIResponseToAnthropic(respBody, route.Model) if err != nil { diff --git a/src/crux/proxy/host_seams.go b/src/crux/proxy/host_seams.go index d4383c9b37..8fda5f3578 100644 --- a/src/crux/proxy/host_seams.go +++ b/src/crux/proxy/host_seams.go @@ -78,6 +78,30 @@ func (p *GitHubProxy) recordUsage(agent, model string, in, out int64) { } } +// cacheUsageSink is the optional UsageSink extension that records the +// cached part of a gateway prompt separately, so it is priced at the +// cache-read rate. *tokens.InferenceSink and the crux callback sink implement +// it; a sink without it gets the full prompt as input, as before. +type cacheUsageSink interface { + RecordWithCache(agent, model string, inputTokens, outputTokens, cacheRead, cacheCreation int64) +} + +// recordGatewayUsage records one gateway response. OpenAI-shaped usage counts +// cached tokens INSIDE prompt_tokens, so the uncached input is the +// difference; recording the full prompt as input would bill cached tokens at +// the input rate. +func (p *GitHubProxy) recordGatewayUsage(agent, model string, u gatewayUsage) { + if u.Input <= 0 && u.Output <= 0 { + return + } + cached := min(max(u.Cached, 0), max(u.Input, 0)) + if cs, ok := p.tokenSink.(cacheUsageSink); ok && cached > 0 { + cs.RecordWithCache(agent, model, u.Input-cached, u.Output, cached, 0) + return + } + p.recordUsage(agent, model, u.Input, u.Output) +} + func (p *GitHubProxy) recordCopilotUsage(agent, model string, in, out int64) { if p.tokenSink != nil { p.tokenSink.RecordCopilot(agent, model, in, out) diff --git a/src/docs/design/crux.md b/src/docs/design/crux.md index 836b2cb8dc..f49da7f593 100644 --- a/src/docs/design/crux.md +++ b/src/docs/design/crux.md @@ -881,7 +881,7 @@ it links nothing of hive. | Mint scoped token | `POST /v1/tokens` | agent, repo (`owner/name`) or repos (names), tier, requested TTL | token, `expires_at` | | GitHub REST accounting | `POST /v1/github/rest` | caller, method, path, status, injected (bool), the `X-RateLimit-*` response headers | `204` | | Core rate-limit snapshot | `GET /v1/github/rate-limit` | — | remaining, reset, ok | - | Inference usage | `POST /v1/usage` | agent, model, kind (`gateway` / `copilot` / `copilot_cache`), input, output, cache read, cache creation | `204` | + | Inference usage | `POST /v1/usage` | agent, model, kind (`gateway` / `gateway_cache` / `copilot` / `copilot_cache`), input, output, cache read, cache creation | `204` | | Canary scan | `POST /v1/scan/canary` | agent, text, source | detected, agent, source (**never the canary token**) | | Text scan | `POST /v1/scan/text` | direction (`input` / `output`), text | blocked, critical_injection, findings (kind, masked snippet, rule, high) | | Condition | `POST /v1/conditions` | action (`observe` / `clear`), class, key, message, diagnostics | `204` | diff --git a/src/pkg/config/crux_aliases.go b/src/pkg/config/crux_aliases.go index 6e8998cc56..c735259c6f 100644 --- a/src/pkg/config/crux_aliases.go +++ b/src/pkg/config/crux_aliases.go @@ -18,6 +18,8 @@ type ( ConnectionConfig = cruxconfig.ConnectionConfig DefinitionSourceConfig = cruxconfig.DefinitionSourceConfig GatewayConfig = cruxconfig.GatewayConfig + GatewayModelPrice = cruxconfig.GatewayModelPrice + GatewayPrices = cruxconfig.GatewayPrices InferenceAuthConfig = cruxconfig.InferenceAuthConfig KickLimitsConfig = cruxconfig.KickLimitsConfig LiteLLMConfig = cruxconfig.LiteLLMConfig diff --git a/src/pkg/cruxhost/cruxhost.go b/src/pkg/cruxhost/cruxhost.go index b040bea3dc..c06c16a179 100644 --- a/src/pkg/cruxhost/cruxhost.go +++ b/src/pkg/cruxhost/cruxhost.go @@ -281,6 +281,15 @@ func (c *Capabilities) RecordUsage(_ context.Context, rec callback.UsageRecord) switch rec.Kind { case callback.UsageKindGateway: c.deps.Usage.Record(rec.Agent, rec.Model, rec.InputTokens, rec.OutputTokens) + case callback.UsageKindGatewayCache: + // Sinks that cannot split cached tokens get the whole prompt as input. + if cs, ok := c.deps.Usage.(interface { + RecordWithCache(agent, model string, inputTokens, outputTokens, cacheRead, cacheCreation int64) + }); ok { + cs.RecordWithCache(rec.Agent, rec.Model, rec.InputTokens, rec.OutputTokens, rec.CacheRead, rec.CacheCreation) + } else { + c.deps.Usage.Record(rec.Agent, rec.Model, rec.InputTokens+rec.CacheRead, rec.OutputTokens) + } case callback.UsageKindCopilot: c.deps.Usage.RecordCopilot(rec.Agent, rec.Model, rec.InputTokens, rec.OutputTokens) case callback.UsageKindCopilotCache: diff --git a/src/pkg/cruxhost/gateway_cache_usage_test.go b/src/pkg/cruxhost/gateway_cache_usage_test.go new file mode 100644 index 0000000000..314c3e3d99 --- /dev/null +++ b/src/pkg/cruxhost/gateway_cache_usage_test.go @@ -0,0 +1,41 @@ +package cruxhost + +import ( + "context" + "testing" + + "github.com/hivecommons/hive/crux/api/callback" +) + +// splitSink records the cached split the way *tokens.InferenceSink does. +type splitSink struct { + fakeDeps + in, cacheRead int64 +} + +func (s *splitSink) RecordWithCache(agent, model string, in, out, cacheRead, cacheCreation int64) { + s.in, s.cacheRead = in, cacheRead +} + +// gateway_cache records reach the sink's cached split; a sink without one +// gets the whole prompt (input + cached) as input rather than losing tokens. +func TestRecordUsageGatewayCache(t *testing.T) { + ctx := context.Background() + rec := callback.UsageRecord{Agent: "a", Model: "m", Kind: callback.UsageKindGatewayCache, InputTokens: 200, OutputTokens: 5, CacheRead: 800} + + split := &splitSink{} + if err := New(Deps{Usage: split}).RecordUsage(ctx, rec); err != nil { + t.Fatal(err) + } + if split.in != 200 || split.cacheRead != 800 { + t.Fatalf("split = in %d cache %d", split.in, split.cacheRead) + } + + plain := &fakeDeps{} + if err := New(Deps{Usage: plain}).RecordUsage(ctx, rec); err != nil { + t.Fatal(err) + } + if len(plain.usage) != 1 || plain.usage[0] != "gateway" { + t.Fatalf("fallback usage = %v", plain.usage) + } +} diff --git a/src/pkg/dashboard/api_providers_headroom.go b/src/pkg/dashboard/api_providers_headroom.go index 942466e2ad..a877635316 100644 --- a/src/pkg/dashboard/api_providers_headroom.go +++ b/src/pkg/dashboard/api_providers_headroom.go @@ -22,21 +22,22 @@ func (s *Server) handleProvidersHeadroom(w http.ResponseWriter, r *http.Request) return } usedBy := s.usageProvidersUsedBy() + spend := s.gatewaySpend() if !headroomReporterDisabled(s.deps.RotationMgr) { resp := s.deps.RotationMgr.HeadroomResponse() resp.Enabled = true resp.Readings = true - jsonResponse(w, headroomWithUsers{resp, usedBy}) + jsonResponse(w, headroomWithUsers{resp, usedBy, spend}) return } if !headroomReporterDisabled(s.deps.HeadroomPublisher) { resp := s.deps.HeadroomPublisher.HeadroomResponse() resp.Enabled = false resp.Readings = len(resp.Providers) > 0 - jsonResponse(w, headroomWithUsers{resp, usedBy}) + jsonResponse(w, headroomWithUsers{resp, usedBy, spend}) return } - jsonResponse(w, headroomWithUsers{rotation.HeadroomResponse{Providers: []rotation.Headroom{}, Enabled: false, Readings: false}, usedBy}) + jsonResponse(w, headroomWithUsers{rotation.HeadroomResponse{Providers: []rotation.Headroom{}, Enabled: false, Readings: false}, usedBy, spend}) } // headroomWithUsers adds used_by to the headroom response: for each provider, @@ -45,6 +46,11 @@ func (s *Server) handleProvidersHeadroom(w http.ResponseWriter, r *http.Request) type headroomWithUsers struct { rotation.HeadroomResponse UsedBy map[string][]string `json:"used_by"` + // GatewaySpend is, per configured gateway with enabled agents, their + // estimated spend at the gateway's prices, with the models whose price is + // unknown listed instead of counted. Gateways have no plan to measure, so + // the gauge shows dollars for them, not a dial. + GatewaySpend []gatewaySpendRow `json:"gateway_spend,omitempty"` } // usageProvidersUsedBy groups the enabled agents by the provider whose plan diff --git a/src/pkg/dashboard/cost.go b/src/pkg/dashboard/cost.go index 4396358d29..0d02308ff3 100644 --- a/src/pkg/dashboard/cost.go +++ b/src/pkg/dashboard/cost.go @@ -120,7 +120,7 @@ type costModelEntry struct { Name string `json:"name"` USD float64 `json:"usd"` Coins float64 `json:"coins,omitempty"` - Source string `json:"source"` // "estimated" | "unpriced" + Source string `json:"source"` // "estimated" | "unpriced" | "unknown" (price unknown: not in USD) Input int64 `json:"input"` Output int64 `json:"output"` CacheRead int64 `json:"cache_read"` @@ -141,6 +141,9 @@ type costEstimated struct { ByModel []costModelEntry `json:"by_model"` ByAgent []costModelEntry `json:"by_agent"` UnpricedModels []string `json:"unpriced_models"` + // UnknownPriceModels are gateway models with no published or configured + // price: their tokens are not in TotalUSD, which is therefore a floor. + UnknownPriceModels []string `json:"unknown_price_models"` // WindowStart / WindowEnd bracket the token sessions represented by the // aggregate rows. They let the UI label "usage by sandbox" with the actual // observed span instead of leaving readers to guess. @@ -394,6 +397,9 @@ func (s *Server) estimatedCost() costEstimated { if est.UnpricedModels == nil { est.UnpricedModels = []string{} } + if est.UnknownPriceModels == nil { + est.UnknownPriceModels = []string{} + } if est.BySession == nil { est.BySession = []costSessionEntry{} } @@ -488,13 +494,13 @@ func estimatedSessions(summary *tokens.AggregateSummary) []costSessionEntry { sessions := summary.Sessions out := make([]costSessionEntry, 0, len(sessions)) for _, sess := range sessions { - usd, exact := tokens.EstimateCostUSD(sess.Model, sess.InputTokens, sess.OutputTokens, sess.CacheRead, sess.CacheCreate) + usd, src := tokens.EstimateCostWithSource(sess.Model, sess.Backend == tokens.BackendInference, sess.InputTokens, sess.OutputTokens, sess.CacheRead, sess.CacheCreate) out = append(out, costSessionEntry{ SessionID: sess.SessionID, Agent: sess.Agent, Model: sess.Model, USD: usd, - Source: sourceForPriced(exact), + Source: sourceForPriceSource(src), Input: sess.InputTokens, Output: sess.OutputTokens, CacheRead: sess.CacheRead, @@ -523,16 +529,17 @@ func estimatedSessions(summary *tokens.AggregateSummary) []costSessionEntry { // re-deriving "priced vs unpriced". func flattenEstimated(est tokens.EstimatedCost) costEstimated { out := costEstimated{ - TotalUSD: est.TotalUSD, - ByModel: make([]costModelEntry, 0, len(est.ByModel)), - ByAgent: make([]costModelEntry, 0, len(est.ByAgent)), - UnpricedModels: est.UnpricedModels, + TotalUSD: est.TotalUSD, + ByModel: make([]costModelEntry, 0, len(est.ByModel)), + ByAgent: make([]costModelEntry, 0, len(est.ByAgent)), + UnpricedModels: est.UnpricedModels, + UnknownPriceModels: est.UnknownPriceModels, } for name, mc := range est.ByModel { out.ByModel = append(out.ByModel, costModelEntry{ Name: name, USD: mc.USD, - Source: sourceForPriced(mc.Priced), + Source: sourceForCost(mc), Input: mc.Input, Output: mc.Output, CacheRead: mc.CacheRead, @@ -543,7 +550,7 @@ func flattenEstimated(est tokens.EstimatedCost) costEstimated { out.ByAgent = append(out.ByAgent, costModelEntry{ Name: name, USD: ac.USD, - Source: sourceForPriced(ac.Priced), + Source: sourceForCost(ac), Input: ac.Input, Output: ac.Output, CacheRead: ac.CacheRead, @@ -560,6 +567,29 @@ func sourceForPriced(priced bool) string { return "unpriced" } +// costSourceUnknown tags a row whose price is unknown: a gateway model with no +// published or configured price. Its USD excludes those tokens. +const costSourceUnknown = "unknown" + +// sourceForPriceSource maps a tokens.PriceSource onto the row tag the cost UI +// reads: exact (gateway or table) → estimated, tier → unpriced, unknown → +// unknown. +func sourceForPriceSource(src tokens.PriceSource) string { + if src == tokens.PriceSourceUnknown { + return costSourceUnknown + } + return sourceForPriced(src.Exact()) +} + +// sourceForCost tags a model/agent row. An agent with any unknown-priced +// model is "unknown" so the UI never shows its partial USD as complete. +func sourceForCost(mc tokens.ModelCost) string { + if mc.PriceSource == tokens.PriceSourceUnknown || len(mc.UnknownPriceModels) > 0 { + return costSourceUnknown + } + return sourceForPriced(mc.Priced) +} + // costHTTPClient builds an HTTP client that honors a gateway's optional private // CA bundle (never disabling verification), mirroring how the LiteLLM key store // builds its TLS config. A missing/unreadable bundle falls back to the system diff --git a/src/pkg/dashboard/gateway_prices.go b/src/pkg/dashboard/gateway_prices.go new file mode 100644 index 0000000000..4829cf04fb --- /dev/null +++ b/src/pkg/dashboard/gateway_prices.go @@ -0,0 +1,384 @@ +package dashboard + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "sort" + "strings" + "sync" + "time" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/tokens" +) + +// Gateway pricing: where a gateway model's dollars come from. +// +// - Any kind: an optional `prices:` block on the gateway (USD per 1M +// tokens, flat or per model). It wins over everything below and is the +// only source for vllm, llm-d, watsonx and custom gateways. +// - openrouter: the public catalog's per-token `pricing` block (the same +// cached catalog model discovery reads). +// - litellm: the proxy's /model/info (input_cost_per_token, +// output_cost_per_token, cache_read_input_token_cost). +// +// A model a gateway serves with none of these is priced UNKNOWN: spend +// reports say "price unknown" instead of counting it as $0 or a tier guess. +// +// The remote sources are read in the background and cached, never on the +// request path: an offline hive must not stall /api/cost on a 10-second +// catalog fetch. + +const ( + // gatewayPriceTTL is how long a fetched price table is reused, and how + // long a failed fetch waits before it is retried. + gatewayPriceTTL = 15 * time.Minute + // liteLLMModelInfoTimeout bounds one /model/info request. + liteLLMModelInfoTimeout = 10 * time.Second + // maxModelInfoBody caps the /model/info body read. + maxModelInfoBody = 8 << 20 + // usdPerTokenToPerMillion converts per-token prices to per-1M. + usdPerTokenToPerMillion = 1_000_000.0 + // liteLLMModelInfoPath is LiteLLM's model metadata route (prices included). + liteLLMModelInfoPath = "/model/info" +) + +// gatewayPriceTable is one gateway's remote price list: model id → price. +type gatewayPriceTable map[string]tokens.ModelPrice + +type gatewayPriceEntry struct { + table gatewayPriceTable + attemptedAt time.Time + inFlight bool +} + +// gatewayPriceCache holds the remote price tables, keyed by gateway +// kind+name+endpoint. Package-level because tokens.SetGatewayPricer is +// process-wide too. +var gatewayPriceCache = struct { + sync.Mutex + entries map[string]*gatewayPriceEntry +}{entries: map[string]*gatewayPriceEntry{}} + +// gatewayPriceRefreshAsync is a test seam: tests refresh synchronously. +var gatewayPriceRefreshAsync = true + +// gatewayPriceNow is the cache clock; tests may swap it. +var gatewayPriceNow = time.Now + +// cachedGatewayPrices returns the last good table for key, starting a +// refresh when it is older than gatewayPriceTTL. A failed refresh keeps the +// previous table. +func cachedGatewayPrices(key string, fetch func() (gatewayPriceTable, error)) gatewayPriceTable { + gatewayPriceCache.Lock() + e := gatewayPriceCache.entries[key] + if e == nil { + e = &gatewayPriceEntry{} + gatewayPriceCache.entries[key] = e + } + stale := !e.inFlight && (e.attemptedAt.IsZero() || gatewayPriceNow().Sub(e.attemptedAt) >= gatewayPriceTTL) + if stale { + e.inFlight = true + e.attemptedAt = gatewayPriceNow() + } + table := e.table + gatewayPriceCache.Unlock() + if !stale { + return table + } + refresh := func() { + t, err := fetch() + gatewayPriceCache.Lock() + defer gatewayPriceCache.Unlock() + e.inFlight = false + if err == nil && len(t) > 0 { + e.table = t + } + } + if gatewayPriceRefreshAsync { + go refresh() + return table + } + refresh() + gatewayPriceCache.Lock() + defer gatewayPriceCache.Unlock() + return e.table +} + +// perMillion converts a per-token USD price to per-1M. +func perMillion(perToken float64) float64 { return perToken * usdPerTokenToPerMillion } + +// openRouterPriceTable converts the catalog into a price table. Models with +// unknown input or output price are left out (unknown, not free). Without a +// published cache-read rate, cached input bills at the input rate. +func openRouterPriceTable(catalog []OpenRouterModel) gatewayPriceTable { + t := gatewayPriceTable{} + for _, m := range catalog { + p := m.Pricing + if p.Prompt == nil || p.Completion == nil { + continue + } + price := tokens.ModelPrice{ + InputPerMTok: perMillion(*p.Prompt), + OutputPerMTok: perMillion(*p.Completion), + CacheReadPerMTok: perMillion(*p.Prompt), + CacheWritePerMTok: perMillion(*p.Prompt), + } + if p.InputCacheRead != nil { + price.CacheReadPerMTok = perMillion(*p.InputCacheRead) + } + t[m.ID] = price + } + return t +} + +// parseLiteLLMModelInfo extracts per-model prices from a LiteLLM /model/info +// body: {data:[{model_name, model_info:{input_cost_per_token, +// output_cost_per_token, cache_read_input_token_cost, +// cache_creation_input_token_cost}}]}. Entries without both an input and an +// output cost are skipped (unknown, not free). +func parseLiteLLMModelInfo(r io.Reader) (gatewayPriceTable, error) { + var body struct { + Data []struct { + ModelName string `json:"model_name"` + ModelInfo struct { + Input *float64 `json:"input_cost_per_token"` + Output *float64 `json:"output_cost_per_token"` + CacheRead *float64 `json:"cache_read_input_token_cost"` + CacheWrite *float64 `json:"cache_creation_input_token_cost"` + } `json:"model_info"` + } `json:"data"` + } + if err := json.NewDecoder(io.LimitReader(r, maxModelInfoBody)).Decode(&body); err != nil { + return nil, fmt.Errorf("decode model info: %w", err) + } + t := gatewayPriceTable{} + for _, d := range body.Data { + mi := d.ModelInfo + name := strings.TrimSpace(d.ModelName) + if name == "" || mi.Input == nil || mi.Output == nil { + continue + } + p := tokens.ModelPrice{ + InputPerMTok: perMillion(*mi.Input), + OutputPerMTok: perMillion(*mi.Output), + CacheReadPerMTok: perMillion(*mi.Input), + CacheWritePerMTok: perMillion(*mi.Input), + } + if mi.CacheRead != nil { + p.CacheReadPerMTok = perMillion(*mi.CacheRead) + } + if mi.CacheWrite != nil { + p.CacheWritePerMTok = perMillion(*mi.CacheWrite) + } + t[name] = p + } + return t, nil +} + +// liteLLMModelInfoURL is the gateway's /model/info URL. LiteLLM serves it at +// the proxy root, so a trailing /v1 on the endpoint is dropped. +func liteLLMModelInfoURL(endpoint string) string { + base := strings.TrimRight(strings.TrimSpace(endpoint), "/") + base = strings.TrimSuffix(base, "/v1") + return base + liteLLMModelInfoPath +} + +// fetchLiteLLMPrices reads a LiteLLM gateway's /model/info with its key. +func fetchLiteLLMPrices(gw config.GatewayConfig) (gatewayPriceTable, error) { + req, err := http.NewRequest(http.MethodGet, liteLLMModelInfoURL(gw.Endpoint), nil) + if err != nil { + return nil, fmt.Errorf("build model info request: %w", err) + } + if key := gw.ResolveAPIKey(); key != "" { + req.Header.Set("Authorization", "Bearer "+key) + } + client := costHTTPClient(gw.CABundle) + client.Timeout = liteLLMModelInfoTimeout + resp, err := client.Do(req) + if err != nil { + return nil, fmt.Errorf("model info request failed: %w", err) + } + defer closeHTTPBody(resp.Body) + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("model info returned HTTP %d", resp.StatusCode) + } + return parseLiteLLMModelInfo(resp.Body) +} + +// configuredGatewayPrice returns the gateway's prices: override for model: +// the per-model entry first, then the flat rate. Both input and output must +// be set; cached input defaults to input. +func configuredGatewayPrice(p *config.GatewayPrices, model string) (tokens.ModelPrice, bool) { + if p == nil { + return tokens.ModelPrice{}, false + } + in, cached, out := p.Input, p.CachedInput, p.Output + if m, ok := p.Models[model]; ok { + if m.Input != nil { + in = m.Input + } + if m.CachedInput != nil { + cached = m.CachedInput + } + if m.Output != nil { + out = m.Output + } + } + if in == nil || out == nil { + return tokens.ModelPrice{}, false + } + price := tokens.ModelPrice{InputPerMTok: *in, OutputPerMTok: *out, CacheReadPerMTok: *in, CacheWritePerMTok: *in} + if cached != nil { + price.CacheReadPerMTok = *cached + } + return price, true +} + +// remoteGatewayPrices is the cached remote table for a gateway (OpenRouter +// catalog or LiteLLM /model/info); nil for kinds that publish no prices. +func (s *Server) remoteGatewayPrices(gw config.GatewayConfig) gatewayPriceTable { + key := strings.ToLower(gw.Kind) + "|" + gw.Name + "|" + gw.Endpoint + switch { + case isOpenRouterGateway(gw.Kind, gw.Endpoint): + or := s.openRouter() + if or == nil { + return nil + } + return cachedGatewayPrices(key, func() (gatewayPriceTable, error) { + return openRouterPriceTable(or.PublicModels()), nil + }) + case strings.EqualFold(gw.Kind, config.GatewayKindLiteLLM): + return cachedGatewayPrices(key, func() (gatewayPriceTable, error) { + return fetchLiteLLMPrices(gw) + }) + } + return nil +} + +// gatewayServesModel reports whether model is routed through gw: it is the +// gateway's default, an enabled agent on the gateway selects it, or the +// gateway's price list or prices: block names it. +func (s *Server) gatewayServesModel(gw config.GatewayConfig, model string, remote gatewayPriceTable) bool { + if model == strings.TrimSpace(gw.DefaultModel) { + return true + } + if _, ok := remote[model]; ok { + return true + } + if gw.Prices != nil { + if _, ok := gw.Prices.Models[model]; ok { + return true + } + } + for _, agent := range s.deps.Config.EnabledAgents() { + if strings.EqualFold(agent.Backend, gw.Name) && agent.Model == model { + return true + } + } + return false +} + +// gatewayModelPrice is the tokens.GatewayPriceFunc the dashboard installs: +// the price of model on the first configured gateway that serves it. +// known=false with served=true means that gateway publishes no price. +func (s *Server) gatewayModelPrice(model string) (tokens.ModelPrice, bool, bool) { + if s == nil || s.deps == nil || s.deps.Config == nil { + return tokens.ModelPrice{}, false, false + } + model = strings.TrimSpace(model) + if model == "" { + return tokens.ModelPrice{}, false, false + } + for _, gw := range s.deps.Config.Governor.ResolvedGateways() { + remote := s.remoteGatewayPrices(gw) + if !s.gatewayServesModel(gw, model, remote) { + continue + } + if p, ok := configuredGatewayPrice(gw.Prices, model); ok { + return p, true, true + } + if p, ok := remote[model]; ok { + return p, true, true + } + return tokens.ModelPrice{}, false, true + } + return tokens.ModelPrice{}, false, false +} + +// installGatewayPricer makes every cost estimate in this process consult the +// configured gateways' prices. +func (s *Server) installGatewayPricer() { + tokens.SetGatewayPricer(s.gatewayModelPrice) +} + +// gatewaySpendRow is one gateway's estimated spend for the usage gauge: the +// enabled agents routed through it and what their tokens cost at the +// gateway's prices. UnknownPriceModels are models whose tokens are NOT in USD. +type gatewaySpendRow struct { + Gateway string `json:"gateway"` + Kind string `json:"kind,omitempty"` + Agents []string `json:"agents"` + USD float64 `json:"usd"` + Tokens int64 `json:"tokens"` + UnknownPriceModels []string `json:"unknown_price_models"` +} + +// gatewaySpend groups enabled agents by the gateway their backend names and +// sums their estimated cost over the token window the cost card uses. +// Gateways with no enabled agent are left out; nil when nothing applies. +func (s *Server) gatewaySpend() []gatewaySpendRow { + if s.deps == nil || s.deps.Config == nil || s.deps.Tokens == nil { + return nil + } + gws := s.deps.Config.Governor.ResolvedGateways() + if len(gws) == 0 { + return nil + } + agentsBy := map[string][]string{} + for name, agent := range s.deps.Config.EnabledAgents() { + for _, gw := range gws { + if strings.EqualFold(agent.Backend, gw.Name) { + agentsBy[gw.Name] = append(agentsBy[gw.Name], name) + break + } + } + } + if len(agentsBy) == 0 { + return nil + } + summary := s.deps.Tokens.Summary() + if summary == nil { + return nil + } + est := tokens.EstimateFromSummary(summary) + var rows []gatewaySpendRow + for _, gw := range gws { + agents := agentsBy[gw.Name] + if len(agents) == 0 { + continue + } + sort.Strings(agents) + row := gatewaySpendRow{Gateway: gw.Name, Kind: gw.Kind, Agents: agents, UnknownPriceModels: []string{}} + seen := map[string]bool{} + for _, a := range agents { + mc, ok := est.ByAgent[a] + if !ok { + continue + } + row.USD += mc.USD + row.Tokens += mc.Input + mc.Output + mc.CacheRead + mc.CacheCreate + for _, m := range mc.UnknownPriceModels { + if !seen[m] { + seen[m] = true + row.UnknownPriceModels = append(row.UnknownPriceModels, m) + } + } + } + sort.Strings(row.UnknownPriceModels) + rows = append(rows, row) + } + return rows +} diff --git a/src/pkg/dashboard/gateway_prices_test.go b/src/pkg/dashboard/gateway_prices_test.go new file mode 100644 index 0000000000..695ddf8757 --- /dev/null +++ b/src/pkg/dashboard/gateway_prices_test.go @@ -0,0 +1,178 @@ +package dashboard + +import ( + "math" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/tokens" +) + +const gwPriceEps = 1e-9 + +func f64(v float64) *float64 { return &v } + +func approx(a, b float64) bool { return math.Abs(a-b) < gwPriceEps } + +// syncGatewayPrices makes the background price refresh synchronous and +// clears the cache for the test. +func syncGatewayPrices(t *testing.T) { + t.Helper() + prev := gatewayPriceRefreshAsync + gatewayPriceRefreshAsync = false + gatewayPriceCache.Lock() + gatewayPriceCache.entries = map[string]*gatewayPriceEntry{} + gatewayPriceCache.Unlock() + t.Cleanup(func() { + gatewayPriceRefreshAsync = prev + gatewayPriceCache.Lock() + gatewayPriceCache.entries = map[string]*gatewayPriceEntry{} + gatewayPriceCache.Unlock() + }) +} + +// The OpenRouter catalog's per-token prices (recorded shape) become per-1M +// prices, with the published cache-read rate; unknown prices are skipped. +func TestOpenRouterPriceTableFromCatalog(t *testing.T) { + table := openRouterPriceTable([]OpenRouterModel{ + {ID: "deepseek/deepseek-v4.1-flash", Pricing: OpenRouterPricing{Prompt: f64(0.00000014), Completion: f64(0.00000042), InputCacheRead: f64(0.0000000042)}}, + {ID: "z-ai/glm-5.3-flash", Pricing: OpenRouterPricing{Prompt: f64(0.0000000399), Completion: f64(0.0000000784)}}, + {ID: "openrouter/auto"}, + }) + p, ok := table["deepseek/deepseek-v4.1-flash"] + if !ok || !approx(p.InputPerMTok, 0.14) || !approx(p.OutputPerMTok, 0.42) || !approx(p.CacheReadPerMTok, 0.0042) { + t.Fatalf("flash = %+v %v", p, ok) + } + // No published cache-read rate: cached input bills at the input rate. + if g := table["z-ai/glm-5.3-flash"]; !approx(g.CacheReadPerMTok, g.InputPerMTok) { + t.Fatalf("glm flash cache read = %+v", g) + } + if _, ok := table["openrouter/auto"]; ok { + t.Fatal("an unpriced model must be left out, not priced at $0") + } +} + +func TestParseLiteLLMModelInfo(t *testing.T) { + body := `{"data":[ + {"model_name":"claude_sonnet_5.5","model_info":{"input_cost_per_token":0.000002,"output_cost_per_token":0.00001,"cache_read_input_token_cost":0.0000002}}, + {"model_name":"granite","model_info":{"input_cost_per_token":0.0000001,"output_cost_per_token":0.0000003}}, + {"model_name":"no-price","model_info":{}} + ]}` + table, err := parseLiteLLMModelInfo(strings.NewReader(body)) + if err != nil { + t.Fatal(err) + } + if p := table["claude_sonnet_5.5"]; !approx(p.InputPerMTok, 2) || !approx(p.OutputPerMTok, 10) || !approx(p.CacheReadPerMTok, 0.2) { + t.Fatalf("sonnet = %+v", p) + } + if p := table["granite"]; !approx(p.CacheReadPerMTok, p.InputPerMTok) { + t.Fatalf("granite cache default = %+v", p) + } + if _, ok := table["no-price"]; ok { + t.Fatal("no-price must be skipped") + } + if _, err := parseLiteLLMModelInfo(strings.NewReader("nope")); err == nil { + t.Fatal("bad json must error") + } +} + +func TestLiteLLMModelInfoURL(t *testing.T) { + for in, want := range map[string]string{ + "https://litellm.example.com": "https://litellm.example.com/model/info", + "https://litellm.example.com/v1/": "https://litellm.example.com/model/info", + } { + if got := liteLLMModelInfoURL(in); got != want { + t.Errorf("liteLLMModelInfoURL(%q) = %q, want %q", in, got, want) + } + } +} + +func TestConfiguredGatewayPriceOverride(t *testing.T) { + prices := &config.GatewayPrices{ + Input: f64(0.5), Output: f64(1.5), + Models: map[string]config.GatewayModelPrice{"big": {Input: f64(3), CachedInput: f64(0.3), Output: f64(9)}}, + } + if p, ok := configuredGatewayPrice(prices, "small"); !ok || p.InputPerMTok != 0.5 || p.OutputPerMTok != 1.5 || p.CacheReadPerMTok != 0.5 { + t.Fatalf("flat = %+v %v (cached input defaults to input)", p, ok) + } + if p, ok := configuredGatewayPrice(prices, "big"); !ok || p.InputPerMTok != 3 || p.CacheReadPerMTok != 0.3 || p.OutputPerMTok != 9 { + t.Fatalf("per-model = %+v %v", p, ok) + } + if _, ok := configuredGatewayPrice(&config.GatewayPrices{Input: f64(1)}, "x"); ok { + t.Fatal("input without output must not count as a price") + } + if _, ok := configuredGatewayPrice(nil, "x"); ok { + t.Fatal("nil prices") + } +} + +func gatewayPriceServer(t *testing.T, gws []config.GatewayConfig, agents map[string]config.AgentConfig) *Server { + t.Helper() + cfg := &config.Config{Agents: agents} + cfg.Governor.Gateways = gws + return &Server{deps: &Dependencies{Config: cfg}} +} + +// vllm publishes no price: its served models are UNKNOWN unless prices: is set. +func TestGatewayModelPriceUnknownAndOverride(t *testing.T) { + syncGatewayPrices(t) + agents := map[string]config.AgentConfig{ + "worker": {Enabled: true, Backend: "local-vllm", Model: "Qwen/Qwen3-32B"}, + } + s := gatewayPriceServer(t, []config.GatewayConfig{{Name: "local-vllm", Kind: "vllm", Endpoint: "http://vllm:8000"}}, agents) + if _, known, served := s.gatewayModelPrice("Qwen/Qwen3-32B"); known || !served { + t.Fatalf("vllm model: known=%v served=%v, want unknown but served", known, served) + } + if _, _, served := s.gatewayModelPrice("claude-opus-5"); served { + t.Fatal("a model no gateway serves must not be claimed") + } + + s = gatewayPriceServer(t, []config.GatewayConfig{{Name: "local-vllm", Kind: "vllm", Endpoint: "http://vllm:8000", + Prices: &config.GatewayPrices{Input: f64(0.2), CachedInput: f64(0.02), Output: f64(0.6)}}}, agents) + p, known, served := s.gatewayModelPrice("Qwen/Qwen3-32B") + if !known || !served || p.InputPerMTok != 0.2 || p.CacheReadPerMTok != 0.02 || p.OutputPerMTok != 0.6 { + t.Fatalf("override = %+v known=%v served=%v", p, known, served) + } + + // Installed as the process pricer, the cost estimate uses it. + tokens.SetGatewayPricer(s.gatewayModelPrice) + t.Cleanup(func() { tokens.SetGatewayPricer(nil) }) + usd, src := tokens.EstimateCostWithSource("Qwen/Qwen3-32B", true, 1_000_000, 1_000_000, 1_000_000, 0) + if src != tokens.PriceSourceGateway || !approx(usd, 0.2+0.6+0.02) { + t.Fatalf("estimate = %v %q", usd, src) + } +} + +// LiteLLM prices come from /model/info, read with the gateway key. +func TestGatewayModelPriceFromLiteLLMModelInfo(t *testing.T) { + syncGatewayPrices(t) + var gotAuth string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/model/info" { + http.NotFound(w, r) + return + } + gotAuth = r.Header.Get("Authorization") + _, _ = w.Write([]byte(`{"data":[{"model_name":"granite","model_info":{"input_cost_per_token":0.0000001,"output_cost_per_token":0.0000003}}]}`)) + })) + defer srv.Close() + t.Setenv("TEST_LITELLM_PRICE_KEY", "sk-test") + s := gatewayPriceServer(t, []config.GatewayConfig{{Name: "corp", Kind: "litellm", Endpoint: srv.URL, APIKeyEnv: "TEST_LITELLM_PRICE_KEY"}}, nil) + p, known, served := s.gatewayModelPrice("granite") + if !known || !served || !approx(p.InputPerMTok, 0.1) || !approx(p.OutputPerMTok, 0.3) { + t.Fatalf("litellm price = %+v known=%v served=%v", p, known, served) + } + if gotAuth != "Bearer sk-test" { + t.Fatalf("auth = %q", gotAuth) + } +} + +func TestGatewaySpendNilWithoutTokens(t *testing.T) { + s := gatewayPriceServer(t, []config.GatewayConfig{{Name: "openrouter", Kind: "openrouter"}}, nil) + if rows := s.gatewaySpend(); rows != nil { + t.Fatalf("rows = %+v, want nil without a token collector", rows) + } +} diff --git a/src/pkg/dashboard/gateway_spend_gauge.test.js b/src/pkg/dashboard/gateway_spend_gauge.test.js new file mode 100644 index 0000000000..5ca3d72819 --- /dev/null +++ b/src/pkg/dashboard/gateway_spend_gauge.test.js @@ -0,0 +1,58 @@ +// Gateway spend rows on the usage gauge: /api/providers/headroom's +// gateway_spend becomes model.spend, and a gateway whose models have no price +// says "price unknown" instead of $0. +const test = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const plain = value => JSON.parse(JSON.stringify(value)); + +function load() { + const src = fs.readFileSync(path.join(__dirname, 'static-src/js/subscription-gauge.js'), 'utf8'); + const model = src.match(/function subscriptionGaugeModel\([^\n]*\) \{[\s\S]*?\n \}/); + const text = src.match(/function subscriptionGaugeSpendText\([^\n]*\) \{[\s\S]*?\n \}/); + assert.ok(model && text, 'gauge helpers found'); + const ctx = vm.createContext({}); + vm.runInContext(model[0] + '\n' + text[0] + '\nthis.model = subscriptionGaugeModel; this.text = subscriptionGaugeSpendText;', ctx); + return ctx; +} +const G = load(); +const NOW = Date.parse('2026-11-16T18:00:00Z'); + +test('gateway_spend rows become model.spend even with no plan reading', () => { + const m = G.model({ providers: [], used_by: {}, gateway_spend: [ + { gateway: 'openrouter', kind: 'openrouter', agents: ['scout'], usd: 1.234, tokens: 900, unknown_price_models: [] }, + { gateway: 'local-vllm', kind: 'vllm', agents: ['worker'], usd: 0, tokens: 5000, unknown_price_models: ['Qwen/Qwen3-32B'] }, + { gateway: 'corp', kind: 'litellm', agents: ['a', 'b'], usd: 2, unknown_price_models: ['granite'] }, + { gateway: 'idle', kind: 'custom', agents: [], usd: 0 }, + ] }, NOW); + assert.equal(m.shown, false, 'no plan row'); + assert.deepEqual(plain(m.spend.map(r => [r.gateway, r.state])), [['openrouter', 'priced'], ['local-vllm', 'unknown'], ['corp', 'partial']]); + assert.deepEqual(plain(m.spend[2].usedBy), ['a', 'b']); +}); + +test('spend text: priced, partial (unknown not counted), unknown (never $0)', () => { + const m = G.model({ providers: [], gateway_spend: [ + { gateway: 'openrouter', agents: ['scout'], usd: 1.234, unknown_price_models: [] }, + { gateway: 'corp', agents: ['a'], usd: 2, unknown_price_models: ['granite'] }, + { gateway: 'local-vllm', agents: ['w'], usd: 0, unknown_price_models: ['Qwen/Qwen3-32B'] }, + ] }, NOW); + assert.equal(G.text(m.spend[0]), '$1.23 at gateway prices (estimate from token counts).'); + assert.equal(G.text(m.spend[1]), '$2.00 at gateway prices, plus price unknown for granite (not counted).'); + const unknown = G.text(m.spend[2]); + assert.match(unknown, /^Price unknown \(Qwen\/Qwen3-32B\)/); + assert.ok(!unknown.includes('$0'), 'an unpriced gateway never reads as $0'); +}); + +test('plan rows keep working and carry spend alongside', () => { + const m = G.model({ providers: [{ provider: 'anthropic', available: true, captured_at: '2026-11-16T17:59:00Z', + limits: [{ id: 'weekly_all', kind: 'weekly', pct_remaining: 60, duration_mins: 10080, resets_at: '2026-11-20T00:00:00Z' }] }], + used_by: { anthropic: ['claude-agent'] }, + gateway_spend: [{ gateway: 'openrouter', agents: ['scout'], usd: 0.5, unknown_price_models: [] }] }, NOW); + assert.equal(m.state, 'reading'); + assert.equal(m.rows.length, 1); + assert.equal(m.spend.length, 1); + const none = G.model({ providers: [] }, NOW); + assert.deepEqual(plain(none.spend), []); +}); diff --git a/src/pkg/dashboard/gateways.go b/src/pkg/dashboard/gateways.go index d50b6e5f52..55285fc32c 100644 --- a/src/pkg/dashboard/gateways.go +++ b/src/pkg/dashboard/gateways.go @@ -258,6 +258,8 @@ func (s *Server) handleGovernorGatewaysUpsert(w http.ResponseWriter, r *http.Req gw.ProjectID = gws[idx].ProjectID gw.Region = gws[idx].Region gw.KeyName = gws[idx].KeyName + // prices: is YAML-only (the form does not edit it); keep it. + gw.Prices = gws[idx].Prices } if body.APIKeyEnv != nil { gw.APIKeyEnv = strings.TrimSpace(*body.APIKeyEnv) diff --git a/src/pkg/dashboard/openrouter.go b/src/pkg/dashboard/openrouter.go index 359389e070..c4684efb6f 100644 --- a/src/pkg/dashboard/openrouter.go +++ b/src/pkg/dashboard/openrouter.go @@ -306,6 +306,7 @@ func (s *Server) upsertOpenRouterGateway(key, model string) error { // and default model are (re)written by the funding flow. gw.APIKeyEnv = gws[idx].APIKeyEnv gw.CABundle = gws[idx].CABundle + gw.Prices = gws[idx].Prices gws[idx] = gw } else { gws = append(gws, gw) diff --git a/src/pkg/dashboard/server.go b/src/pkg/dashboard/server.go index d2cad90cf9..7457134bc9 100644 --- a/src/pkg/dashboard/server.go +++ b/src/pkg/dashboard/server.go @@ -1801,6 +1801,11 @@ func (s *Server) registerCoreRoutes() { } func (s *Server) Start() error { + // Price gateway models from their gateways (OpenRouter catalog, LiteLLM + // /model/info, prices: overrides) in every cost estimate this process + // makes. Installed here, not in RegisterAPI, so unit tests that only wire + // deps keep the static price table. + s.installGatewayPricer() staticContent, err := fs.Sub(staticFS, "static") if err != nil { return fmt.Errorf("loading embedded static files: %w", err) diff --git a/src/pkg/dashboard/static-src/css/cost.css b/src/pkg/dashboard/static-src/css/cost.css index 596a960396..f24ab15633 100644 --- a/src/pkg/dashboard/static-src/css/cost.css +++ b/src/pkg/dashboard/static-src/css/cost.css @@ -90,6 +90,7 @@ .cost-session-active { color: var(--green); font-weight: 600; } .cost-subtable-title { font-size: 0.72rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; margin: var(--sp-6) var(--sp-0) var(--sp-2); } .cost-disclaimer { font-size: 0.65rem; color: var(--muted); margin-top: 10px; line-height: 1.4; } + .cost-price-unknown { color: var(--muted); font-style: italic; white-space: nowrap; } .cost-recording-notice { border: var(--line-width) solid var(--status-warn); border-radius: var(--r-lg); color: var(--status-warn); font-size: var(--fs-sm); font-weight: var(--fw-bold); line-height: 1.4; margin-bottom: var(--sp-5); padding: var(--sp-4) var(--sp-5); } .cost-recording-notice a { color: inherit; text-decoration: underline; } .cost-recording-incomplete { color: var(--status-warn); font-weight: var(--fw-bold); } diff --git a/src/pkg/dashboard/static-src/css/overview.css b/src/pkg/dashboard/static-src/css/overview.css index daf9051d4d..4c6721350a 100644 --- a/src/pkg/dashboard/static-src/css/overview.css +++ b/src/pkg/dashboard/static-src/css/overview.css @@ -182,6 +182,7 @@ .overview-usage-gauge__row + .overview-usage-gauge__row { padding-top: var(--sp-3); border-top: 1px solid var(--border); } .overview-usage-gauge__provider { font-weight: var(--fw-semibold); } .overview-usage-gauge__users { font-size: var(--fs-sm); color: var(--text-muted); overflow-wrap: anywhere; } + .overview-usage-gauge__spend { font-size: var(--fs-base); font-weight: 600; } .overview-usage-gauge__dials { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: var(--sp-4); } .overview-usage-gauge__dial { display: flex; flex-direction: column; align-items: center; gap: var(--sp-1); text-align: center; } .overview-usage-gauge__label { font-weight: var(--fw-semibold); } diff --git a/src/pkg/dashboard/static-src/js/cost.js b/src/pkg/dashboard/static-src/js/cost.js index c8e33733f3..72e523bfc1 100644 --- a/src/pkg/dashboard/static-src/js/cost.js +++ b/src/pkg/dashboard/static-src/js/cost.js @@ -337,6 +337,11 @@ const est = cost.estimated || { total_usd: 0, by_model: [], by_agent: [], unpriced_models: [] }; const gateways = cost.gateways || []; const isTierEstimated = (row) => row && (row.source === 'unpriced' || row.source === 'estimated_tier'); + // source "unknown": a gateway model with no published or configured + // price. Its tokens are not in the total, so the cell says so instead + // of showing $0. + const isPriceUnknown = (row) => row && row.source === 'unknown'; + const priceUnknownCell = (row) => `price unknown${row && row.usd > 0 ? ' (+' + fmtUSD(row.usd) + ' priced)' : ''}`; const usageWindow = estimatedUsageWindowLabel(est); // Native gateway cards (OpenRouter / LiteLLM real spend). @@ -409,7 +414,7 @@ }), 'model', (a, b) => String(a.name || '').localeCompare(String(b.name || ''), undefined, { sensitivity: 'base', numeric: true })); const modelRows = models.map(m => { const approx = isTierEstimated(m); - const usdCell = approx + const usdCell = isPriceUnknown(m) ? priceUnknownCell(m) : approx ? `~${fmtUSD(m.usd)}` : fmtUSD(m.usd); const users = m._users || []; @@ -425,9 +430,11 @@ `; }).join(''); - const unpricedNote = (est.unpriced_models && est.unpriced_models.length) + const unpricedNote = ((est.unpriced_models && est.unpriced_models.length) ? `
Tier-estimated (no exact list price, marked ~ and included in the total): ${est.unpriced_models.map(esc).join(', ')}.
` - : ''; + : '') + ((est.unknown_price_models && est.unknown_price_models.length) + ? `
Price unknown (gateway models with no published or configured price; NOT in the total, which is therefore a floor): ${est.unknown_price_models.map(esc).join(', ')}. Add prices: to the gateway in hive.yaml to cost them.
` + : ''); // ── Per-sandbox (per-agent) estimated cost ── // In hive each agent runs in its own sandbox, so "usage by sandbox" is the @@ -435,7 +442,7 @@ const bySandbox = sortCostRows(est.by_agent || [], 'sandbox', (a, b) => String(a.name || '').localeCompare(String(b.name || ''), undefined, { sensitivity: 'base', numeric: true })); const sandboxRows = bySandbox.map(a => { const approx = isTierEstimated(a); - const usdCell = approx + const usdCell = isPriceUnknown(a) ? priceUnknownCell(a) : approx ? `~${fmtUSD(a.usd)}` : fmtUSD(a.usd); return ` @@ -468,7 +475,7 @@ }; const sessionRows = bySession.map(sn => { const approx = isTierEstimated(sn); - const usdCell = approx + const usdCell = isPriceUnknown(sn) ? priceUnknownCell(sn) : approx ? `~${fmtUSD(sn.usd)}` : fmtUSD(sn.usd); // "started → ended" in local time; a still-active session (latest event diff --git a/src/pkg/dashboard/static-src/js/subscription-gauge.js b/src/pkg/dashboard/static-src/js/subscription-gauge.js index 4ff6c167e8..6edc7cd26b 100644 --- a/src/pkg/dashboard/static-src/js/subscription-gauge.js +++ b/src/pkg/dashboard/static-src/js/subscription-gauge.js @@ -138,19 +138,45 @@ ? Object.keys(usedBy).filter((id) => Array.isArray(usedBy[id]) && usedBy[id].length > 0) .sort((a, b) => rank(a) - rank(b) || a.localeCompare(b)) : ['anthropic']; + // Gateways (OpenRouter, LiteLLM, vLLM, …) have no plan to measure, so + // they get a spend row in dollars at the gateway's prices instead of a + // dial. Models with no published or configured price are listed as + // "price unknown", never counted as $0. + const spend = (resp && Array.isArray(resp.gateway_spend) ? resp.gateway_spend : []) + .filter((g) => g && g.gateway && Array.isArray(g.agents) && g.agents.length) + .map((g) => { + const unknown = Array.isArray(g.unknown_price_models) ? g.unknown_price_models.map(String) : []; + const usd = Number(g.usd) || 0; + return { + gateway: String(g.gateway), kind: String(g.kind || ''), usedBy: g.agents.map(String), + usd: usd, tokens: Number(g.tokens) || 0, unknownModels: unknown, + state: unknown.length && usd === 0 ? 'unknown' : (unknown.length ? 'partial' : 'priced'), + }; + }); const rows = ids.map((id) => providerRow(id, readingFor(id), usedBy ? usedBy[id].map(String) : null, !!usedBy)) .filter((row) => row.shown); if (!rows.length) { const legacy = !usedBy ? providerRow('anthropic', readingFor('anthropic'), null, false) : null; return legacy && legacy.state === 'no-reading' && legacy.cause !== 'absent' - ? Object.assign(legacy, { rows: [] }) - : { state: 'no-reading', shown: false, rows: [], cause: 'absent', reason: 'Usage measurement is not running on this hive.' }; + ? Object.assign(legacy, { rows: [], spend: spend }) + : { state: 'no-reading', shown: false, rows: [], spend: spend, cause: 'absent', reason: 'Usage measurement is not running on this hive.' }; } // The needle row is the one with the least left; rows without a // reading only lead when no row has one. let lead = rows.find((row) => row.state === 'reading') || rows[0]; rows.forEach((row) => { if (row.state === 'reading' && row.needle.pctLeft < lead.needle.pctLeft) lead = row; }); - return Object.assign({}, lead, { shown: true, rows: rows }); + return Object.assign({}, lead, { shown: true, rows: rows, spend: spend }); + } + + // subscriptionGaugeSpendText is a gateway spend row's one-line summary: + // "$1.23 at gateway prices", "$1.23 + price unknown for qwen", or + // "price unknown (qwen)" when nothing could be costed. + function subscriptionGaugeSpendText(row) { + const usd = '$' + (Number(row.usd) || 0).toFixed(2); + const unknown = (row.unknownModels || []).join(', '); + if (row.state === 'unknown') return 'Price unknown (' + unknown + '): this gateway publishes no price. Set prices: on the gateway to cost it.'; + if (row.state === 'partial') return usd + ' at gateway prices, plus price unknown for ' + unknown + ' (not counted).'; + return usd + ' at gateway prices (estimate from token counts).'; } // Shared loader. The headroom endpoint is owner-only and the fetch @@ -261,7 +287,8 @@ const host = document.getElementById('overview-usage-gauge'); if (!host) return; const m = subscriptionGaugeCurrentModel(); - if (!subscriptionGaugeOwner() || !subscriptionGaugeOverviewEnabled() || !m || !m.shown) { + const spend = (m && m.spend) || []; + if (!subscriptionGaugeOwner() || !subscriptionGaugeOverviewEnabled() || !m || (!m.shown && !spend.length)) { host.hidden = true; host.replaceChildren(); return; @@ -272,8 +299,8 @@ if (text !== undefined) n.textContent = text; return n; }; - const rows = m.rows && m.rows.length ? m.rows : [m]; - const single = rows.length === 1; + const rows = !m.shown ? [] : (m.rows && m.rows.length ? m.rows : [m]); + const single = rows.length === 1 && !spend.length; const metaFor = (row) => { const meta = []; if (row.state === 'reading') { @@ -283,7 +310,7 @@ return meta.join(' · '); }; const head = el('div', 'overview-usage-gauge__head'); - head.appendChild(el('div', 'overview-usage-gauge__title', single ? rows[0].name + ' usage left' : 'Usage left')); + head.appendChild(el('div', 'overview-usage-gauge__title', single ? rows[0].name + ' usage left' : (rows.length ? 'Usage left' : 'Gateway spend'))); head.appendChild(el('div', 'overview-usage-gauge__meta', single ? metaFor(rows[0]) : '')); const parts = [head]; rows.forEach((row) => { @@ -315,6 +342,24 @@ } parts.push(box); }); + spend.forEach((row) => { + const box = el('div', 'overview-usage-gauge__row'); + box.dataset.gateway = row.gateway; + const rowHead = el('div', 'overview-usage-gauge__head'); + rowHead.appendChild(el('div', 'overview-usage-gauge__provider', row.gateway + ' (gateway)')); + box.appendChild(rowHead); + if (row.usedBy.length) box.appendChild(el('div', 'overview-usage-gauge__users', 'Used by ' + subscriptionGaugeUsers(row.usedBy))); + const line = el('div', row.state === 'priced' ? 'overview-usage-gauge__spend' : 'overview-usage-gauge__note', subscriptionGaugeSpendText(row)); + line.dataset.priceState = row.state; + box.appendChild(line); + parts.push(box); + }); + if (!rows.length) { + parts.push(el('div', 'overview-usage-gauge__note', 'Gateway spend is estimated from token counts at each gateway\'s published or configured prices.')); + host.replaceChildren.apply(host, parts); + host.hidden = false; + return; + } parts.push(el('div', 'overview-usage-gauge__note', single ? (rows[0].accountNote || 'Reflects the ' + rows[0].name + ' account this hive is signed in with.') + ' This is the ' + rows[0].name + ' plan, not the hive token budget.' : 'Each row reflects the account this hive is signed in with for that provider. These are the providers\' plans, not the hive token budget.')); diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index 8c4e40c271..4844e744cd 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -3790,6 +3790,7 @@ .overview-usage-gauge__row + .overview-usage-gauge__row { padding-top: var(--sp-3); border-top: 1px solid var(--border); } .overview-usage-gauge__provider { font-weight: var(--fw-semibold); } .overview-usage-gauge__users { font-size: var(--fs-sm); color: var(--text-muted); overflow-wrap: anywhere; } + .overview-usage-gauge__spend { font-size: var(--fs-base); font-weight: 600; } .overview-usage-gauge__dials { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: var(--sp-4); } .overview-usage-gauge__dial { display: flex; flex-direction: column; align-items: center; gap: var(--sp-1); text-align: center; } .overview-usage-gauge__label { font-weight: var(--fw-semibold); } @@ -4168,6 +4169,7 @@ .cost-session-active { color: var(--green); font-weight: 600; } .cost-subtable-title { font-size: 0.72rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; margin: var(--sp-6) var(--sp-0) var(--sp-2); } .cost-disclaimer { font-size: 0.65rem; color: var(--muted); margin-top: 10px; line-height: 1.4; } + .cost-price-unknown { color: var(--muted); font-style: italic; white-space: nowrap; } .cost-recording-notice { border: var(--line-width) solid var(--status-warn); border-radius: var(--r-lg); color: var(--status-warn); font-size: var(--fs-sm); font-weight: var(--fw-bold); line-height: 1.4; margin-bottom: var(--sp-5); padding: var(--sp-4) var(--sp-5); } .cost-recording-notice a { color: inherit; text-decoration: underline; } .cost-recording-incomplete { color: var(--status-warn); font-weight: var(--fw-bold); } @@ -40133,6 +40135,11 @@

All labels

const est = cost.estimated || { total_usd: 0, by_model: [], by_agent: [], unpriced_models: [] }; const gateways = cost.gateways || []; const isTierEstimated = (row) => row && (row.source === 'unpriced' || row.source === 'estimated_tier'); + // source "unknown": a gateway model with no published or configured + // price. Its tokens are not in the total, so the cell says so instead + // of showing $0. + const isPriceUnknown = (row) => row && row.source === 'unknown'; + const priceUnknownCell = (row) => `price unknown${row && row.usd > 0 ? ' (+' + fmtUSD(row.usd) + ' priced)' : ''}`; const usageWindow = estimatedUsageWindowLabel(est); // Native gateway cards (OpenRouter / LiteLLM real spend). @@ -40205,7 +40212,7 @@

All labels

}), 'model', (a, b) => String(a.name || '').localeCompare(String(b.name || ''), undefined, { sensitivity: 'base', numeric: true })); const modelRows = models.map(m => { const approx = isTierEstimated(m); - const usdCell = approx + const usdCell = isPriceUnknown(m) ? priceUnknownCell(m) : approx ? `~${fmtUSD(m.usd)}` : fmtUSD(m.usd); const users = m._users || []; @@ -40221,9 +40228,11 @@

All labels

`; }).join(''); - const unpricedNote = (est.unpriced_models && est.unpriced_models.length) + const unpricedNote = ((est.unpriced_models && est.unpriced_models.length) ? `
Tier-estimated (no exact list price, marked ~ and included in the total): ${est.unpriced_models.map(esc).join(', ')}.
` - : ''; + : '') + ((est.unknown_price_models && est.unknown_price_models.length) + ? `
Price unknown (gateway models with no published or configured price; NOT in the total, which is therefore a floor): ${est.unknown_price_models.map(esc).join(', ')}. Add prices: to the gateway in hive.yaml to cost them.
` + : ''); // ── Per-sandbox (per-agent) estimated cost ── // In hive each agent runs in its own sandbox, so "usage by sandbox" is the @@ -40231,7 +40240,7 @@

All labels

const bySandbox = sortCostRows(est.by_agent || [], 'sandbox', (a, b) => String(a.name || '').localeCompare(String(b.name || ''), undefined, { sensitivity: 'base', numeric: true })); const sandboxRows = bySandbox.map(a => { const approx = isTierEstimated(a); - const usdCell = approx + const usdCell = isPriceUnknown(a) ? priceUnknownCell(a) : approx ? `~${fmtUSD(a.usd)}` : fmtUSD(a.usd); return ` @@ -40264,7 +40273,7 @@

All labels

}; const sessionRows = bySession.map(sn => { const approx = isTierEstimated(sn); - const usdCell = approx + const usdCell = isPriceUnknown(sn) ? priceUnknownCell(sn) : approx ? `~${fmtUSD(sn.usd)}` : fmtUSD(sn.usd); // "started → ended" in local time; a still-active session (latest event @@ -53323,19 +53332,45 @@ ? Object.keys(usedBy).filter((id) => Array.isArray(usedBy[id]) && usedBy[id].length > 0) .sort((a, b) => rank(a) - rank(b) || a.localeCompare(b)) : ['anthropic']; + // Gateways (OpenRouter, LiteLLM, vLLM, …) have no plan to measure, so + // they get a spend row in dollars at the gateway's prices instead of a + // dial. Models with no published or configured price are listed as + // "price unknown", never counted as $0. + const spend = (resp && Array.isArray(resp.gateway_spend) ? resp.gateway_spend : []) + .filter((g) => g && g.gateway && Array.isArray(g.agents) && g.agents.length) + .map((g) => { + const unknown = Array.isArray(g.unknown_price_models) ? g.unknown_price_models.map(String) : []; + const usd = Number(g.usd) || 0; + return { + gateway: String(g.gateway), kind: String(g.kind || ''), usedBy: g.agents.map(String), + usd: usd, tokens: Number(g.tokens) || 0, unknownModels: unknown, + state: unknown.length && usd === 0 ? 'unknown' : (unknown.length ? 'partial' : 'priced'), + }; + }); const rows = ids.map((id) => providerRow(id, readingFor(id), usedBy ? usedBy[id].map(String) : null, !!usedBy)) .filter((row) => row.shown); if (!rows.length) { const legacy = !usedBy ? providerRow('anthropic', readingFor('anthropic'), null, false) : null; return legacy && legacy.state === 'no-reading' && legacy.cause !== 'absent' - ? Object.assign(legacy, { rows: [] }) - : { state: 'no-reading', shown: false, rows: [], cause: 'absent', reason: 'Usage measurement is not running on this hive.' }; + ? Object.assign(legacy, { rows: [], spend: spend }) + : { state: 'no-reading', shown: false, rows: [], spend: spend, cause: 'absent', reason: 'Usage measurement is not running on this hive.' }; } // The needle row is the one with the least left; rows without a // reading only lead when no row has one. let lead = rows.find((row) => row.state === 'reading') || rows[0]; rows.forEach((row) => { if (row.state === 'reading' && row.needle.pctLeft < lead.needle.pctLeft) lead = row; }); - return Object.assign({}, lead, { shown: true, rows: rows }); + return Object.assign({}, lead, { shown: true, rows: rows, spend: spend }); + } + + // subscriptionGaugeSpendText is a gateway spend row's one-line summary: + // "$1.23 at gateway prices", "$1.23 + price unknown for qwen", or + // "price unknown (qwen)" when nothing could be costed. + function subscriptionGaugeSpendText(row) { + const usd = '$' + (Number(row.usd) || 0).toFixed(2); + const unknown = (row.unknownModels || []).join(', '); + if (row.state === 'unknown') return 'Price unknown (' + unknown + '): this gateway publishes no price. Set prices: on the gateway to cost it.'; + if (row.state === 'partial') return usd + ' at gateway prices, plus price unknown for ' + unknown + ' (not counted).'; + return usd + ' at gateway prices (estimate from token counts).'; } // Shared loader. The headroom endpoint is owner-only and the fetch @@ -53446,7 +53481,8 @@ const host = document.getElementById('overview-usage-gauge'); if (!host) return; const m = subscriptionGaugeCurrentModel(); - if (!subscriptionGaugeOwner() || !subscriptionGaugeOverviewEnabled() || !m || !m.shown) { + const spend = (m && m.spend) || []; + if (!subscriptionGaugeOwner() || !subscriptionGaugeOverviewEnabled() || !m || (!m.shown && !spend.length)) { host.hidden = true; host.replaceChildren(); return; @@ -53457,8 +53493,8 @@ if (text !== undefined) n.textContent = text; return n; }; - const rows = m.rows && m.rows.length ? m.rows : [m]; - const single = rows.length === 1; + const rows = !m.shown ? [] : (m.rows && m.rows.length ? m.rows : [m]); + const single = rows.length === 1 && !spend.length; const metaFor = (row) => { const meta = []; if (row.state === 'reading') { @@ -53468,7 +53504,7 @@ return meta.join(' · '); }; const head = el('div', 'overview-usage-gauge__head'); - head.appendChild(el('div', 'overview-usage-gauge__title', single ? rows[0].name + ' usage left' : 'Usage left')); + head.appendChild(el('div', 'overview-usage-gauge__title', single ? rows[0].name + ' usage left' : (rows.length ? 'Usage left' : 'Gateway spend'))); head.appendChild(el('div', 'overview-usage-gauge__meta', single ? metaFor(rows[0]) : '')); const parts = [head]; rows.forEach((row) => { @@ -53500,6 +53536,24 @@ } parts.push(box); }); + spend.forEach((row) => { + const box = el('div', 'overview-usage-gauge__row'); + box.dataset.gateway = row.gateway; + const rowHead = el('div', 'overview-usage-gauge__head'); + rowHead.appendChild(el('div', 'overview-usage-gauge__provider', row.gateway + ' (gateway)')); + box.appendChild(rowHead); + if (row.usedBy.length) box.appendChild(el('div', 'overview-usage-gauge__users', 'Used by ' + subscriptionGaugeUsers(row.usedBy))); + const line = el('div', row.state === 'priced' ? 'overview-usage-gauge__spend' : 'overview-usage-gauge__note', subscriptionGaugeSpendText(row)); + line.dataset.priceState = row.state; + box.appendChild(line); + parts.push(box); + }); + if (!rows.length) { + parts.push(el('div', 'overview-usage-gauge__note', 'Gateway spend is estimated from token counts at each gateway\'s published or configured prices.')); + host.replaceChildren.apply(host, parts); + host.hidden = false; + return; + } parts.push(el('div', 'overview-usage-gauge__note', single ? (rows[0].accountNote || 'Reflects the ' + rows[0].name + ' account this hive is signed in with.') + ' This is the ' + rows[0].name + ' plan, not the hive token budget.' : 'Each row reflects the account this hive is signed in with for that provider. These are the providers\' plans, not the hive token budget.')); diff --git a/src/pkg/tokens/gateway_pricing.go b/src/pkg/tokens/gateway_pricing.go new file mode 100644 index 0000000000..0548acfc14 --- /dev/null +++ b/src/pkg/tokens/gateway_pricing.go @@ -0,0 +1,126 @@ +package tokens + +import "sync/atomic" + +// PriceSource says where a model's price came from, so a spend figure can be +// labelled honestly. +type PriceSource string + +const ( + // PriceSourceGateway: the price the model's gateway publishes — the + // OpenRouter catalog, LiteLLM /model/info, or a gateway's configured + // prices: override. Preferred over the static table because it is the + // rate the gateway actually bills for that exact id. + PriceSourceGateway PriceSource = "gateway" + // PriceSourceTable: an exact list price from modelPrices. + PriceSourceTable PriceSource = "table" + // PriceSourceTier: a coarse tier estimate (FallbackPrice) for a CLI model + // absent from the table. Counted, but marked approximate. + PriceSourceTier PriceSource = "tier" + // PriceSourceUnknown: the model runs through a gateway that publishes no + // price and is not in the table. Its cost is NOT counted (and not shown as + // $0): a tier guess for a self-hosted or custom model is meaningless. + PriceSourceUnknown PriceSource = "unknown" +) + +// Counted reports whether a cost from this source is included in totals. +func (s PriceSource) Counted() bool { return s != PriceSourceUnknown } + +// Exact reports whether the price is a published figure (gateway or table) +// rather than an estimate or unknown. +func (s PriceSource) Exact() bool { return s == PriceSourceGateway || s == PriceSourceTable } + +// GatewayPriceFunc resolves a model's price from the configured gateways. +// served reports whether any configured gateway serves model at all; known +// reports whether that gateway publishes (or is configured with) a price. +type GatewayPriceFunc func(model string) (price ModelPrice, known, served bool) + +var gatewayPricer atomic.Pointer[GatewayPriceFunc] + +// SetGatewayPricer installs (or, with nil, removes) the host's gateway price +// source. pkg/tokens stays free of config and network: the dashboard wires +// a function that reads the OpenRouter catalog, LiteLLM /model/info and +// gateway prices: overrides. +func SetGatewayPricer(fn GatewayPriceFunc) { + if fn == nil { + gatewayPricer.Store(nil) + return + } + gatewayPricer.Store(&fn) +} + +func lookupGatewayPrice(model string) (ModelPrice, bool, bool) { + p := gatewayPricer.Load() + if p == nil || model == "" { + return ModelPrice{}, false, false + } + return (*p)(model) +} + +// ResolvePrice returns the price for model and its source. viaGateway marks a +// model observed on the inference (gateway) path, which is never tier-guessed: +// gateway price, then the static table, else unknown. Other models resolve +// gateway price (when a configured gateway serves that exact id), then table, +// then tier. +func ResolvePrice(model string, viaGateway bool) (ModelPrice, PriceSource) { + gp, known, served := lookupGatewayPrice(model) + if known { + return gp, PriceSourceGateway + } + if p, ok := LookupPrice(model); ok { + return p, PriceSourceTable + } + if viaGateway || served { + return ModelPrice{}, PriceSourceUnknown + } + p, _ := FallbackPrice(model) + return p, PriceSourceTier +} + +// costAt prices token counts at p. +func costAt(p ModelPrice, inputTok, outputTok, cacheReadTok, cacheWriteTok int64) float64 { + return float64(inputTok)/tokensPerMillion*p.InputPerMTok + + float64(outputTok)/tokensPerMillion*p.OutputPerMTok + + float64(cacheReadTok)/tokensPerMillion*p.CacheReadPerMTok + + float64(cacheWriteTok)/tokensPerMillion*p.CacheWritePerMTok +} + +// EstimateCostWithSource is EstimateCostUSD plus the price source. An unknown +// price returns 0 with PriceSourceUnknown; callers must show "price unknown", +// never $0. +func EstimateCostWithSource(model string, viaGateway bool, inputTok, outputTok, cacheReadTok, cacheWriteTok int64) (float64, PriceSource) { + p, src := ResolvePrice(model, viaGateway) + if src == PriceSourceUnknown { + return 0, src + } + return costAt(p, inputTok, outputTok, cacheReadTok, cacheWriteTok), src +} + +// priceSourceRank orders sources from most to least certain. +var priceSourceRank = map[PriceSource]int{ + PriceSourceGateway: 1, + PriceSourceTable: 1, + PriceSourceTier: 2, + PriceSourceUnknown: 3, +} + +// leastCertainSource returns whichever of a and b is less certain ("" is +// treated as no source yet). +func leastCertainSource(a, b PriceSource) PriceSource { + if a == "" { + return b + } + if priceSourceRank[b] > priceSourceRank[a] { + return b + } + return a +} + +func containsString(list []string, s string) bool { + for _, v := range list { + if v == s { + return true + } + } + return false +} diff --git a/src/pkg/tokens/gateway_pricing_test.go b/src/pkg/tokens/gateway_pricing_test.go new file mode 100644 index 0000000000..2bbd500296 --- /dev/null +++ b/src/pkg/tokens/gateway_pricing_test.go @@ -0,0 +1,127 @@ +package tokens + +import ( + "math" + "testing" +) + +const priceEps = 1e-9 + +func withGatewayPricer(t *testing.T, fn GatewayPriceFunc) { + t.Helper() + SetGatewayPricer(fn) + t.Cleanup(func() { SetGatewayPricer(nil) }) +} + +func near(a, b float64) bool { return math.Abs(a-b) < priceEps } + +// DeepSeek V4.1 Flash on OpenRouter, per 1M: $0.14 in, $0.0042 cached, $0.42 out. +var flashPrice = ModelPrice{InputPerMTok: 0.14, OutputPerMTok: 0.42, CacheReadPerMTok: 0.0042, CacheWritePerMTok: 0.14} + +func gatewayStub(prices map[string]ModelPrice, servedUnpriced ...string) GatewayPriceFunc { + unpriced := map[string]bool{} + for _, m := range servedUnpriced { + unpriced[m] = true + } + return func(model string) (ModelPrice, bool, bool) { + if p, ok := prices[model]; ok { + return p, true, true + } + return ModelPrice{}, false, unpriced[model] + } +} + +func TestGatewayPriceWinsAndCachedInputUsesCacheRate(t *testing.T) { + withGatewayPricer(t, gatewayStub(map[string]ModelPrice{"deepseek/deepseek-v4.1-flash": flashPrice})) + const in, cached, out = 1_000_000, 4_000_000, 1_000_000 + usd, src := EstimateCostWithSource("deepseek/deepseek-v4.1-flash", true, in, out, cached, 0) + if src != PriceSourceGateway { + t.Fatalf("source = %q, want gateway", src) + } + // 1M × 0.14 + 1M × 0.42 + 4M × 0.0042 — cached tokens at the cache rate, + // not the input rate. + if want := 0.14 + 0.42 + 4*0.0042; !near(usd, want) { + t.Fatalf("usd = %v, want %v", usd, want) + } + if usd2, exact := EstimateCostUSD("deepseek/deepseek-v4.1-flash", in, out, cached, 0); !exact || !near(usd2, usd) { + t.Fatalf("EstimateCostUSD = %v, %v", usd2, exact) + } +} + +func TestGatewayModelWithoutPriceIsUnknownNotCounted(t *testing.T) { + withGatewayPricer(t, gatewayStub(nil, "Qwen/Qwen3-32B")) + usd, src := EstimateCostWithSource("Qwen/Qwen3-32B", false, 1000, 1000, 0, 0) + if src != PriceSourceUnknown || usd != 0 { + t.Fatalf("served-but-unpriced = %v %q, want 0 unknown", usd, src) + } + // Seen on the inference path with no pricer entry at all: still unknown, + // never a tier guess. + SetGatewayPricer(nil) + if _, src := EstimateCostWithSource("my-finetune-7b", true, 1000, 1000, 0, 0); src != PriceSourceUnknown { + t.Fatalf("inference-path model source = %q, want unknown", src) + } + // A CLI model absent from the table keeps the tier estimate. + if usd, src := EstimateCostWithSource("some-cli-model", false, 1_000_000, 0, 0, 0); src != PriceSourceTier || usd <= 0 { + t.Fatalf("CLI fallback = %v %q, want tier > 0", usd, src) + } + // A gateway model that is in the static table is still priced from it. + if _, src := EstimateCostWithSource("claude-sonnet-4-6", true, 1, 1, 0, 0); src != PriceSourceTable { + t.Fatalf("table model via gateway source = %q", src) + } +} + +func TestEstimateFromSummaryListsUnknownAndExcludesFromTotal(t *testing.T) { + withGatewayPricer(t, gatewayStub(map[string]ModelPrice{"deepseek/deepseek-v4.1-flash": flashPrice})) + agg := &AggregateSummary{ + ByModelDetail: map[string]*AgentModelBucket{ + "deepseek/deepseek-v4.1-flash": {Input: 1_000_000, Output: 1_000_000}, + "local/qwen": {Input: 5_000_000, Output: 5_000_000}, + }, + ByAgentDetail: map[string]*AgentModelBucket{ + "scout": {Input: 1_000_000, Output: 1_000_000}, + "worker": {Input: 5_000_000, Output: 5_000_000}, + }, + Sessions: []SessionSummary{ + {Agent: "scout", Model: "deepseek/deepseek-v4.1-flash", Backend: BackendInference, InputTokens: 1_000_000, OutputTokens: 1_000_000}, + {Agent: "worker", Model: "local/qwen", Backend: BackendInference, InputTokens: 5_000_000, OutputTokens: 5_000_000}, + }, + } + est := EstimateFromSummary(agg) + if want := 0.14 + 0.42; !near(est.TotalUSD, want) { + t.Fatalf("total = %v, want %v (unknown-priced tokens excluded)", est.TotalUSD, want) + } + if len(est.UnknownPriceModels) != 1 || est.UnknownPriceModels[0] != "local/qwen" { + t.Fatalf("UnknownPriceModels = %v", est.UnknownPriceModels) + } + if len(est.UnpricedModels) != 0 { + t.Fatalf("an unknown gateway model must not be tier-estimated: %v", est.UnpricedModels) + } + if mc := est.ByModel["local/qwen"]; mc.PriceSource != PriceSourceUnknown || mc.USD != 0 || mc.Priced { + t.Fatalf("local/qwen row = %+v", mc) + } + if mc := est.ByModel["deepseek/deepseek-v4.1-flash"]; mc.PriceSource != PriceSourceGateway || !mc.Priced { + t.Fatalf("flash row = %+v", mc) + } + w := est.ByAgent["worker"] + if w.PriceSource != PriceSourceUnknown || len(w.UnknownPriceModels) != 1 || w.UnknownPriceModels[0] != "local/qwen" { + t.Fatalf("worker agent row = %+v", w) + } + if s := est.ByAgent["scout"]; s.PriceSource != PriceSourceGateway || len(s.UnknownPriceModels) != 0 { + t.Fatalf("scout agent row = %+v", s) + } +} + +func TestLeastCertainSource(t *testing.T) { + if got := leastCertainSource("", PriceSourceTable); got != PriceSourceTable { + t.Errorf("empty start = %q", got) + } + if got := leastCertainSource(PriceSourceGateway, PriceSourceTier); got != PriceSourceTier { + t.Errorf("gateway+tier = %q", got) + } + if got := leastCertainSource(PriceSourceUnknown, PriceSourceTable); got != PriceSourceUnknown { + t.Errorf("unknown+table = %q", got) + } + if !PriceSourceTier.Counted() || PriceSourceUnknown.Counted() { + t.Error("Counted") + } +} diff --git a/src/pkg/tokens/inference_sink.go b/src/pkg/tokens/inference_sink.go index f9d16696c8..47a21e50a8 100644 --- a/src/pkg/tokens/inference_sink.go +++ b/src/pkg/tokens/inference_sink.go @@ -206,6 +206,13 @@ func (s *InferenceSink) Record(agent, model string, inputTokens, outputTokens in s.record(agent, model, BackendInference, inputTokens, outputTokens, 0, 0) } +// RecordWithCache is Record with the prompt split into uncached input and +// cached reads (OpenAI usage.prompt_tokens_details.cached_tokens), so a +// gateway's cache-read price applies. inputTokens EXCLUDES cacheRead. +func (s *InferenceSink) RecordWithCache(agent, model string, inputTokens, outputTokens, cacheRead, cacheCreation int64) { + s.record(agent, model, BackendInference, inputTokens, outputTokens, cacheRead, cacheCreation) +} + // RecordCopilot adds one Copilot completion captured live by the MITM proxy. // Unlike Copilot session.shutdown metrics, each call has a real wall-clock // timestamp, so the collector persists a Usage timeline that /api/repo-cost can diff --git a/src/pkg/tokens/pricing.go b/src/pkg/tokens/pricing.go index 78ae08158d..df359390ed 100644 --- a/src/pkg/tokens/pricing.go +++ b/src/pkg/tokens/pricing.go @@ -2,6 +2,7 @@ package tokens import ( "regexp" + "sort" "strings" ) @@ -318,16 +319,14 @@ func FallbackPrice(model string) (ModelPrice, bool) { // true = exact list price, false = tier estimate — so the UI can mark a row as // approximate, but the number is real either way. (This whole card is an // estimate; we do not depend on unavailable "actual" gateway spend.) +// +// A price a configured gateway publishes for the exact id (see +// SetGatewayPricer) wins over the table and counts as exact. A model a +// gateway serves without a price returns (0, false): see +// EstimateCostWithSource for callers that must say "price unknown". func EstimateCostUSD(model string, inputTok, outputTok, cacheReadTok, cacheWriteTok int64) (usd float64, exact bool) { - p, ok := LookupPrice(model) - if !ok { - p, _ = FallbackPrice(model) - } - usd = float64(inputTok)/tokensPerMillion*p.InputPerMTok + - float64(outputTok)/tokensPerMillion*p.OutputPerMTok + - float64(cacheReadTok)/tokensPerMillion*p.CacheReadPerMTok + - float64(cacheWriteTok)/tokensPerMillion*p.CacheWritePerMTok - return usd, ok + usd, src := EstimateCostWithSource(model, false, inputTok, outputTok, cacheReadTok, cacheWriteTok) + return usd, src.Exact() } // ModelCost is the estimated dollar cost for one model or agent, plus whether @@ -336,6 +335,13 @@ func EstimateCostUSD(model string, inputTok, outputTok, cacheReadTok, cacheWrite type ModelCost struct { USD float64 `json:"usd"` Priced bool `json:"priced"` + // PriceSource is where the price came from (gateway | table | tier | + // unknown). For an agent spanning several models it is the least certain + // source among them. unknown means USD excludes those tokens. + PriceSource PriceSource `json:"price_source,omitempty"` + // UnknownPriceModels lists, for an agent row, the models whose price is + // unknown (their tokens are not in USD). + UnknownPriceModels []string `json:"unknown_price_models,omitempty"` // Token splits are echoed so the UI can show the basis of the estimate. Input int64 `json:"input"` Output int64 `json:"output"` @@ -353,7 +359,11 @@ type EstimatedCost struct { ByModel map[string]ModelCost `json:"by_model"` ByAgent map[string]ModelCost `json:"by_agent"` UnpricedModels []string `json:"unpriced_models"` - PriceTableDate string `json:"price_table_date"` + // UnknownPriceModels are gateway models with no published or configured + // price. Their tokens are NOT in TotalUSD, so the total is a floor; the + // UI says "price unknown" for them instead of showing $0. + UnknownPriceModels []string `json:"unknown_price_models"` + PriceTableDate string `json:"price_table_date"` } // EstimateFromSummary computes the total, per-model, and per-agent estimated @@ -366,16 +376,26 @@ type EstimatedCost struct { // (hivecommons/hive#10025). func EstimateFromSummary(agg *AggregateSummary) EstimatedCost { out := EstimatedCost{ - ByModel: make(map[string]ModelCost), - ByAgent: make(map[string]ModelCost), - UnpricedModels: []string{}, - PriceTableDate: priceTableDate, + ByModel: make(map[string]ModelCost), + ByAgent: make(map[string]ModelCost), + UnpricedModels: []string{}, + UnknownPriceModels: []string{}, + PriceTableDate: priceTableDate, } if agg == nil { return out } unpricedSeen := make(map[string]bool) + unknownSeen := make(map[string]bool) + // Models seen on the inference (gateway) path are never tier-guessed: + // they are priced from their gateway, the table, or reported unknown. + viaGateway := make(map[string]bool) + for _, s := range agg.Sessions { + if s.Backend == BackendInference { + viaGateway[normalizeModelID(s.Model)] = true + } + } // Merge raw per-model buckets by NORMALIZED model id first. The scanners key // tokens by the exact model string, so the same model shows up under both @@ -418,25 +438,34 @@ func EstimateFromSummary(agg *AggregateSummary) EstimatedCost { } // Per-model estimated cost — the authoritative source for the grand total. - for _, m := range merged { - usd, exact := EstimateCostUSD(m.display, m.input, m.output, m.cacheRead, m.cacheCreate) + for key, m := range merged { + usd, src := EstimateCostWithSource(m.display, viaGateway[key], m.input, m.output, m.cacheRead, m.cacheCreate) out.ByModel[m.display] = ModelCost{ USD: usd, - Priced: exact, + Priced: src.Exact(), + PriceSource: src, Input: m.input, Output: m.output, CacheRead: m.cacheRead, CacheCreate: m.cacheCreate, } - // Every model contributes to the total now — exact where we have a list - // price, tier estimate otherwise. UnpricedModels lists the estimated - // ones so the UI can mark them ~approximate (not exclude them). + // Every model with a price contributes to the total — exact where the + // gateway or table publishes one, tier estimate otherwise. + // UnpricedModels lists the tier-estimated ones so the UI can mark them + // ~approximate. A gateway model with NO price is excluded and listed + // in UnknownPriceModels: counting it as $0 or a tier guess would + // silently misstate gateway spend. out.TotalUSD += usd - if !exact && m.display != "" && !unpricedSeen[m.display] { + switch { + case src == PriceSourceUnknown && m.display != "" && !unknownSeen[m.display]: + unknownSeen[m.display] = true + out.UnknownPriceModels = append(out.UnknownPriceModels, m.display) + case src == PriceSourceTier && m.display != "" && !unpricedSeen[m.display]: unpricedSeen[m.display] = true out.UnpricedModels = append(out.UnpricedModels, m.display) } } + sort.Strings(out.UnknownPriceModels) // Price each agent by summing each of its sessions at that session's own // model, instead of pricing the whole agent bucket at one "dominant" @@ -446,6 +475,8 @@ func EstimateFromSummary(agg *AggregateSummary) EstimatedCost { // with total_usd. agentUSD := make(map[string]float64, len(agg.ByAgentDetail)) agentExact := make(map[string]bool, len(agg.ByAgentDetail)) + agentSource := make(map[string]PriceSource, len(agg.ByAgentDetail)) + agentUnknown := make(map[string][]string) for agent := range agg.ByAgentDetail { agentExact[agent] = true } @@ -453,24 +484,33 @@ func EstimateFromSummary(agg *AggregateSummary) EstimatedCost { if _, ok := agg.ByAgentDetail[s.Agent]; !ok { continue } - usd, exact := EstimateCostUSD(s.Model, s.InputTokens, s.OutputTokens, s.CacheRead, s.CacheCreate) + via := s.Backend == BackendInference || viaGateway[normalizeModelID(s.Model)] + usd, src := EstimateCostWithSource(s.Model, via, s.InputTokens, s.OutputTokens, s.CacheRead, s.CacheCreate) agentUSD[s.Agent] += usd - if !exact { + if !src.Exact() { agentExact[s.Agent] = false } + agentSource[s.Agent] = leastCertainSource(agentSource[s.Agent], src) + if src == PriceSourceUnknown && s.Model != "" && !containsString(agentUnknown[s.Agent], s.Model) { + agentUnknown[s.Agent] = append(agentUnknown[s.Agent], s.Model) + } } for agent, b := range agg.ByAgentDetail { if b == nil { continue } + unknown := agentUnknown[agent] + sort.Strings(unknown) out.ByAgent[agent] = ModelCost{ - USD: agentUSD[agent], - Priced: agentExact[agent], - Input: b.Input, - Output: b.Output, - CacheRead: b.CacheRead, - CacheCreate: b.CacheCreate, + USD: agentUSD[agent], + Priced: agentExact[agent], + PriceSource: agentSource[agent], + UnknownPriceModels: unknown, + Input: b.Input, + Output: b.Output, + CacheRead: b.CacheRead, + CacheCreate: b.CacheCreate, } } From 8d0cf0797aacb1eea3ea74bc547e04dad59b8ee2 Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 13:41:48 -0500 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=90=9B=20Fix=20gateway=20price=20test?= =?UTF-8?q?=20config=20literal=20and=20API=20reference=20citation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andy Anderson --- src/pkg/dashboard/gateway_prices_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/pkg/dashboard/gateway_prices_test.go b/src/pkg/dashboard/gateway_prices_test.go index 695ddf8757..889efbdf64 100644 --- a/src/pkg/dashboard/gateway_prices_test.go +++ b/src/pkg/dashboard/gateway_prices_test.go @@ -111,7 +111,8 @@ func TestConfiguredGatewayPriceOverride(t *testing.T) { func gatewayPriceServer(t *testing.T, gws []config.GatewayConfig, agents map[string]config.AgentConfig) *Server { t.Helper() - cfg := &config.Config{Agents: agents} + cfg := &config.Config{} + cfg.Agents = agents cfg.Governor.Gateways = gws return &Server{deps: &Dependencies{Config: cfg}} } From 3465a98499efb787eca51f3a81cbad0748651889 Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 14:07:10 -0500 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=90=9B=20Install=20the=20gateway=20pr?= =?UTF-8?q?icer=20at=20boot,=20allowlist=20prices:=20in=20settings=20parit?= =?UTF-8?q?y?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andy Anderson --- src/cmd/hive/boot_launch_deps.go | 7 ++++++- src/pkg/dashboard/gateway_prices.go | 8 +++++--- src/pkg/dashboard/server.go | 5 ----- src/pkg/dashboard/settings_parity_allowlist.txt | 6 ++++++ 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/cmd/hive/boot_launch_deps.go b/src/cmd/hive/boot_launch_deps.go index 4c78db7872..fa42736344 100644 --- a/src/cmd/hive/boot_launch_deps.go +++ b/src/cmd/hive/boot_launch_deps.go @@ -50,6 +50,11 @@ func defaultBootLaunchDeps() bootLaunchDeps { } }, startAgent: func(ctx context.Context, mgr crux.Runtime, name string) error { return mgr.Start(ctx, name) }, - serve: (*dashboard.Server).Start, + // Gateway pricing is process-wide (tokens.SetGatewayPricer), so the + // production boot installs it, not Server.Start, which tests call. + serve: func(s *dashboard.Server) error { + s.InstallGatewayPricer() + return s.Start() + }, } } diff --git a/src/pkg/dashboard/gateway_prices.go b/src/pkg/dashboard/gateway_prices.go index 4829cf04fb..73682a8ded 100644 --- a/src/pkg/dashboard/gateway_prices.go +++ b/src/pkg/dashboard/gateway_prices.go @@ -308,9 +308,11 @@ func (s *Server) gatewayModelPrice(model string) (tokens.ModelPrice, bool, bool) return tokens.ModelPrice{}, false, false } -// installGatewayPricer makes every cost estimate in this process consult the -// configured gateways' prices. -func (s *Server) installGatewayPricer() { +// InstallGatewayPricer makes every cost estimate in this process consult the +// configured gateways' prices (OpenRouter catalog, LiteLLM /model/info, +// prices: overrides). The pricer is process-wide, so cmd/hive installs it +// once at boot; unit tests that build servers keep the static price table. +func (s *Server) InstallGatewayPricer() { tokens.SetGatewayPricer(s.gatewayModelPrice) } diff --git a/src/pkg/dashboard/server.go b/src/pkg/dashboard/server.go index 7457134bc9..d2cad90cf9 100644 --- a/src/pkg/dashboard/server.go +++ b/src/pkg/dashboard/server.go @@ -1801,11 +1801,6 @@ func (s *Server) registerCoreRoutes() { } func (s *Server) Start() error { - // Price gateway models from their gateways (OpenRouter catalog, LiteLLM - // /model/info, prices: overrides) in every cost estimate this process - // makes. Installed here, not in RegisterAPI, so unit tests that only wire - // deps keep the static price table. - s.installGatewayPricer() staticContent, err := fs.Sub(staticFS, "static") if err != nil { return fmt.Errorf("loading embedded static files: %w", err) diff --git a/src/pkg/dashboard/settings_parity_allowlist.txt b/src/pkg/dashboard/settings_parity_allowlist.txt index 672eea5fe6..566fd37f74 100644 --- a/src/pkg/dashboard/settings_parity_allowlist.txt +++ b/src/pkg/dashboard/settings_parity_allowlist.txt @@ -275,6 +275,12 @@ governor.gateways[].endpoint # TODO #11854 governor.gateways[].key_name # TODO #11854 governor.gateways[].kind # TODO #11854 governor.gateways[].name # TODO #11854 +governor.gateways[].prices.cached_input # TODO #11854 +governor.gateways[].prices.input # TODO #11854 +governor.gateways[].prices.models..cached_input # TODO #11854 +governor.gateways[].prices.models..input # TODO #11854 +governor.gateways[].prices.models..output # TODO #11854 +governor.gateways[].prices.output # TODO #11854 governor.gateways[].project_id # TODO #11854 governor.gateways[].region # TODO #11854 governor.health.healthcheck_interval # TODO #11854