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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 61 additions & 0 deletions internal/api/model_cost_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
5 changes: 5 additions & 0 deletions internal/api/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"time"

"github.com/Mag1cFall/AIStudio2API/internal/aistudio"
"github.com/Mag1cFall/AIStudio2API/internal/pricing"
)

type chatRequest struct {
Expand Down Expand Up @@ -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
}
Expand Down
13 changes: 13 additions & 0 deletions internal/pricing/pricing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down