From 89fa750d721b9ad3890fb855a01de3209fd844ae Mon Sep 17 00:00:00 2001 From: hhh2210 Date: Tue, 17 Nov 2026 13:35:36 +0800 Subject: [PATCH] api: expose usage estimate rates in model listings Add estimated_cost to OpenAI model listings and single-model responses from the existing usage pricing table. Search aliases share base rates; context tiers include cached input and use a strict input-size threshold. Validation: internal/api, internal/pricing and internal/requestdb tests passed with the repository's pinned Darwin ARM64 static libraries. TestModelEstimatedCost fails with the original openAIModelObject (no estimated_cost), and passes with this change at 200000/200001 input tokens. Unknown model prices remain omitted. --- docs/logging.md | 2 ++ internal/api/model_cost_test.go | 61 +++++++++++++++++++++++++++++++++ internal/api/openai.go | 5 +++ internal/pricing/pricing.go | 13 +++++++ 4 files changed, 81 insertions(+) create mode 100644 internal/api/model_cost_test.go diff --git a/docs/logging.md b/docs/logging.md index 8a70e310..7b71be63 100644 --- a/docs/logging.md +++ b/docs/logging.md @@ -286,6 +286,8 @@ WARN account@example.com 账号切换 model=gemini-3.7-flash status=403 code=7 ## 用量账本 +`GET /v1/models` 与 `GET /v1/models/{model}` 的 `estimated_cost` 提供用量页采用的每百万 token 美元估算单价:`input`、`output`(含思考)与 `cache_read`。长上下文模型的 `tiers` 按 `above` 从小到大排列,输入(含缓存命中)严格超过该阈值时整次请求使用该档单价。搜索型号使用基础型号的单价;未计价模型省略 `estimated_cost`。 + `runtime/requests.db` 是本地 SQLite 账本。`/v1` 与 `/v1beta` 通过 API key 校验的每个 POST 请求完成后写入一行:请求 ID、完成时间、协议、路径、模型、账户、通道、HTTP 状态、`request.state`、耗时、首个上游事件、排队时间、token 用量(含缓存命中数)、工具调用数、错误摘要,以及最终结果之前未成功的上游尝试。同一事务按 UTC 小时与服务器本地日累加汇总与耗时分布:完整落在范围与单个分桶内的本地日读取日汇总,其余整小时读取小时汇总,范围边缘与跨分桶的部分读取原始记录。服务器时区变化后,下次启动时从小时汇总与原始记录重建本地日汇总。写入由后台协程批量提交,不阻塞响应:等待写入的记录超过 4096 条时丢弃新记录,写入恢复后输出丢弃数。记录与汇总按服务器本地日整日保留 90 天。管理页的用量看板通过 `GET /api/usage` 与 `GET /api/usage/records` 查询。看板的估算费用按 Gemini API 与 Vertex AI 公布的当前文本单价从原始记录逐条计算,缓存命中的输入按缓存单价计算。 ```text diff --git a/internal/api/model_cost_test.go b/internal/api/model_cost_test.go new file mode 100644 index 00000000..cee8754a --- /dev/null +++ b/internal/api/model_cost_test.go @@ -0,0 +1,61 @@ +package api + +import ( + "encoding/json" + "math" + "testing" + + "github.com/Mag1cFall/AIStudio2API/internal/aistudio" + "github.com/Mag1cFall/AIStudio2API/internal/pricing" +) + +func TestModelEstimatedCost(t *testing.T) { + for _, id := range []string{"gemini-4-hydrogen", "gemini-3.1-pro-preview", "gemini-decision-1"} { + for _, suffix := range []string{"", "-search"} { + t.Run(id+suffix, func(t *testing.T) { + body, err := json.Marshal(openAIModelObject(aistudio.Model{ID: id + suffix})) + if err != nil { + t.Fatal(err) + } + type rate struct { + Input, Output float64 + CacheRead float64 `json:"cache_read"` + } + var model struct { + Cost *struct { + rate + Tiers []struct { + rate + Above int + } + } `json:"estimated_cost"` + } + if err := json.Unmarshal(body, &model); err != nil { + t.Fatal(err) + } + if model.Cost == nil { + t.Fatalf("%s has no estimated_cost", body) + } + for _, input := range []int{200000, 200001, 1000000} { + r := model.Cost.rate + for _, tier := range model.Cost.Tiers { + if input > tier.Above { + r = tier.rate + } + } + cached, output := 100000, 2500 + got := (float64(input-cached)*r.Input + float64(cached)*r.CacheRead + float64(output)*r.Output) / 1e6 + want := pricing.Cost(id, int64(input), int64(cached), int64(output)) + if math.Abs(got-want) > 1e-12 { + t.Fatalf("input=%d: estimated_cost=%g, usage=%g", input, got, want) + } + } + }) + } + } + for _, id := range []string{"unknown", "unknown-search", "gemini-4-hydrogen-build", "gemini-2.5-flash-image"} { + if _, ok := openAIModelObject(aistudio.Model{ID: id})["estimated_cost"]; ok { + t.Fatalf("%s should be unpriced", id) + } + } +} diff --git a/internal/api/openai.go b/internal/api/openai.go index 2bf0d7da..c99128f4 100644 --- a/internal/api/openai.go +++ b/internal/api/openai.go @@ -17,6 +17,7 @@ import ( "time" "github.com/Mag1cFall/AIStudio2API/internal/aistudio" + "github.com/Mag1cFall/AIStudio2API/internal/pricing" ) type chatRequest struct { @@ -124,6 +125,10 @@ func openAIModelObject(model aistudio.Model) map[string]any { "supported_generation_methods": model.Methods, "input_token_limit": model.InputTokenLimit, "output_token_limit": model.OutputTokenLimit, } + base, _ := aistudio.ResolveSearchModelAlias(model.ID) + if cost := pricing.ModelCost(base); cost != nil { + item["estimated_cost"] = cost + } if len(model.Capabilities) > 0 { item["capabilities"] = model.Capabilities } diff --git a/internal/pricing/pricing.go b/internal/pricing/pricing.go index 927f22ad..334cac30 100644 --- a/internal/pricing/pricing.go +++ b/internal/pricing/pricing.go @@ -38,6 +38,19 @@ func Priced(model string) bool { return ok } +// ModelCost 返回用量页采用的每百万 token 美元估算单价与长上下文分档 +func ModelCost(model string) map[string]any { + value, ok := prices[model] + if !ok { + return nil + } + result := map[string]any{"input": value.input, "output": value.output, "cache_read": value.cached} + if value.longInput > 0 { + result["tiers"] = []map[string]any{{"above": LongContext, "input": value.longInput, "output": value.longOutput, "cache_read": value.longCached}} + } + return result +} + // Cost 返回一个请求按模型单价估算的美元费用,cached 为 input 中命中缓存的部分,output 为思考与回复 token 之和,模型没有单价时为 0 func Cost(model string, input, cached, output int64) float64 { return TierCost(model, input > LongContext, input, cached, output)