feat: 区分 Anthropic 5m/1h 缓存创建 token 的差异化计费
Anthropic API 的 cache_creation 对象区分了 ephemeral_5m 和 ephemeral_1h 两种缓存创建 token,1h 单价远高于 5m(如 claude-3-5-haiku: 5m=$1/MTok, 1h=$6/MTok)。此前系统统一按 5m 单价计费,导致计费偏低。 后端: - pricing_service: 加载 LiteLLM 的 cache_creation_input_token_cost_above_1hr - billing_service: GetModelPricing 启用分类计费(安全守卫 1h>5m), CalculateCost 按 5m/1h 分别计费,无明细时回退到 5m 单价 - gateway_service: parseSSEUsage/handleNonStreamingResponse 用 gjson 提取嵌套 cache_creation 对象的 ephemeral_5m/1h_input_tokens - antigravity_gateway_service: extractSSEUsage/extractClaudeUsage 同步提取 - usage_log: 修复 GORM column tag 确保写入正确的数据库列 - 新增迁移 054: 删除 GORM 自动生成的重复列 前端: - 使用记录 tooltip 展示 5m/1h 缓存创建明细(带彩色 badge 区分) - 表格单元格缓存写入数值旁显示 1h 标识
This commit is contained in:
@@ -4111,6 +4111,15 @@ func (s *AntigravityGatewayService) extractSSEUsage(line string, usage *ClaudeUs
|
|||||||
if v, ok := u["cache_creation_input_tokens"].(float64); ok && int(v) > 0 {
|
if v, ok := u["cache_creation_input_tokens"].(float64); ok && int(v) > 0 {
|
||||||
usage.CacheCreationInputTokens = int(v)
|
usage.CacheCreationInputTokens = int(v)
|
||||||
}
|
}
|
||||||
|
// 解析嵌套的 cache_creation 对象中的 5m/1h 明细
|
||||||
|
if cc, ok := u["cache_creation"].(map[string]any); ok {
|
||||||
|
if v, ok := cc["ephemeral_5m_input_tokens"].(float64); ok {
|
||||||
|
usage.CacheCreation5mTokens = int(v)
|
||||||
|
}
|
||||||
|
if v, ok := cc["ephemeral_1h_input_tokens"].(float64); ok {
|
||||||
|
usage.CacheCreation1hTokens = int(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// extractClaudeUsage 从非流式 Claude 响应提取 usage
|
// extractClaudeUsage 从非流式 Claude 响应提取 usage
|
||||||
@@ -4133,6 +4142,15 @@ func (s *AntigravityGatewayService) extractClaudeUsage(body []byte) *ClaudeUsage
|
|||||||
if v, ok := u["cache_creation_input_tokens"].(float64); ok {
|
if v, ok := u["cache_creation_input_tokens"].(float64); ok {
|
||||||
usage.CacheCreationInputTokens = int(v)
|
usage.CacheCreationInputTokens = int(v)
|
||||||
}
|
}
|
||||||
|
// 解析嵌套的 cache_creation 对象中的 5m/1h 明细
|
||||||
|
if cc, ok := u["cache_creation"].(map[string]any); ok {
|
||||||
|
if v, ok := cc["ephemeral_5m_input_tokens"].(float64); ok {
|
||||||
|
usage.CacheCreation5mTokens = int(v)
|
||||||
|
}
|
||||||
|
if v, ok := cc["ephemeral_1h_input_tokens"].(float64); ok {
|
||||||
|
usage.CacheCreation1hTokens = int(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return usage
|
return usage
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,8 +31,8 @@ type ModelPricing struct {
|
|||||||
OutputPricePerToken float64 // 每token输出价格 (USD)
|
OutputPricePerToken float64 // 每token输出价格 (USD)
|
||||||
CacheCreationPricePerToken float64 // 缓存创建每token价格 (USD)
|
CacheCreationPricePerToken float64 // 缓存创建每token价格 (USD)
|
||||||
CacheReadPricePerToken float64 // 缓存读取每token价格 (USD)
|
CacheReadPricePerToken float64 // 缓存读取每token价格 (USD)
|
||||||
CacheCreation5mPrice float64 // 5分钟缓存创建价格(每百万token)- 仅用于硬编码回退
|
CacheCreation5mPrice float64 // 5分钟缓存创建每token价格 (USD)
|
||||||
CacheCreation1hPrice float64 // 1小时缓存创建价格(每百万token)- 仅用于硬编码回退
|
CacheCreation1hPrice float64 // 1小时缓存创建每token价格 (USD)
|
||||||
SupportsCacheBreakdown bool // 是否支持详细的缓存分类
|
SupportsCacheBreakdown bool // 是否支持详细的缓存分类
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -172,12 +172,20 @@ func (s *BillingService) GetModelPricing(model string) (*ModelPricing, error) {
|
|||||||
if s.pricingService != nil {
|
if s.pricingService != nil {
|
||||||
litellmPricing := s.pricingService.GetModelPricing(model)
|
litellmPricing := s.pricingService.GetModelPricing(model)
|
||||||
if litellmPricing != nil {
|
if litellmPricing != nil {
|
||||||
|
// 启用 5m/1h 分类计费的条件:
|
||||||
|
// 1. 存在 1h 价格
|
||||||
|
// 2. 1h 价格 > 5m 价格(防止 LiteLLM 数据错误导致少收费)
|
||||||
|
price5m := litellmPricing.CacheCreationInputTokenCost
|
||||||
|
price1h := litellmPricing.CacheCreationInputTokenCostAbove1hr
|
||||||
|
enableBreakdown := price1h > 0 && price1h > price5m
|
||||||
return &ModelPricing{
|
return &ModelPricing{
|
||||||
InputPricePerToken: litellmPricing.InputCostPerToken,
|
InputPricePerToken: litellmPricing.InputCostPerToken,
|
||||||
OutputPricePerToken: litellmPricing.OutputCostPerToken,
|
OutputPricePerToken: litellmPricing.OutputCostPerToken,
|
||||||
CacheCreationPricePerToken: litellmPricing.CacheCreationInputTokenCost,
|
CacheCreationPricePerToken: litellmPricing.CacheCreationInputTokenCost,
|
||||||
CacheReadPricePerToken: litellmPricing.CacheReadInputTokenCost,
|
CacheReadPricePerToken: litellmPricing.CacheReadInputTokenCost,
|
||||||
SupportsCacheBreakdown: false,
|
CacheCreation5mPrice: price5m,
|
||||||
|
CacheCreation1hPrice: price1h,
|
||||||
|
SupportsCacheBreakdown: enableBreakdown,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -209,9 +217,14 @@ func (s *BillingService) CalculateCost(model string, tokens UsageTokens, rateMul
|
|||||||
|
|
||||||
// 计算缓存费用
|
// 计算缓存费用
|
||||||
if pricing.SupportsCacheBreakdown && (pricing.CacheCreation5mPrice > 0 || pricing.CacheCreation1hPrice > 0) {
|
if pricing.SupportsCacheBreakdown && (pricing.CacheCreation5mPrice > 0 || pricing.CacheCreation1hPrice > 0) {
|
||||||
// 支持详细缓存分类的模型(5分钟/1小时缓存)
|
// 支持详细缓存分类的模型(5分钟/1小时缓存,价格为 per-token)
|
||||||
breakdown.CacheCreationCost = float64(tokens.CacheCreation5mTokens)/1_000_000*pricing.CacheCreation5mPrice +
|
if tokens.CacheCreation5mTokens == 0 && tokens.CacheCreation1hTokens == 0 && tokens.CacheCreationTokens > 0 {
|
||||||
float64(tokens.CacheCreation1hTokens)/1_000_000*pricing.CacheCreation1hPrice
|
// API 未返回 ephemeral 明细,回退到全部按 5m 单价计费
|
||||||
|
breakdown.CacheCreationCost = float64(tokens.CacheCreationTokens) * pricing.CacheCreation5mPrice
|
||||||
|
} else {
|
||||||
|
breakdown.CacheCreationCost = float64(tokens.CacheCreation5mTokens)*pricing.CacheCreation5mPrice +
|
||||||
|
float64(tokens.CacheCreation1hTokens)*pricing.CacheCreation1hPrice
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// 标准缓存创建价格(per-token)
|
// 标准缓存创建价格(per-token)
|
||||||
breakdown.CacheCreationCost = float64(tokens.CacheCreationTokens) * pricing.CacheCreationPricePerToken
|
breakdown.CacheCreationCost = float64(tokens.CacheCreationTokens) * pricing.CacheCreationPricePerToken
|
||||||
@@ -280,10 +293,12 @@ func (s *BillingService) CalculateCostWithLongContext(model string, tokens Usage
|
|||||||
|
|
||||||
// 范围内部分:正常计费
|
// 范围内部分:正常计费
|
||||||
inRangeTokens := UsageTokens{
|
inRangeTokens := UsageTokens{
|
||||||
InputTokens: inRangeInputTokens,
|
InputTokens: inRangeInputTokens,
|
||||||
OutputTokens: tokens.OutputTokens, // 输出只算一次
|
OutputTokens: tokens.OutputTokens, // 输出只算一次
|
||||||
CacheCreationTokens: tokens.CacheCreationTokens,
|
CacheCreationTokens: tokens.CacheCreationTokens,
|
||||||
CacheReadTokens: inRangeCacheTokens,
|
CacheReadTokens: inRangeCacheTokens,
|
||||||
|
CacheCreation5mTokens: tokens.CacheCreation5mTokens,
|
||||||
|
CacheCreation1hTokens: tokens.CacheCreation1hTokens,
|
||||||
}
|
}
|
||||||
inRangeCost, err := s.CalculateCost(model, inRangeTokens, rateMultiplier)
|
inRangeCost, err := s.CalculateCost(model, inRangeTokens, rateMultiplier)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -349,6 +349,8 @@ type ClaudeUsage struct {
|
|||||||
OutputTokens int `json:"output_tokens"`
|
OutputTokens int `json:"output_tokens"`
|
||||||
CacheCreationInputTokens int `json:"cache_creation_input_tokens"`
|
CacheCreationInputTokens int `json:"cache_creation_input_tokens"`
|
||||||
CacheReadInputTokens int `json:"cache_read_input_tokens"`
|
CacheReadInputTokens int `json:"cache_read_input_tokens"`
|
||||||
|
CacheCreation5mTokens int // 5分钟缓存创建token(来自嵌套 cache_creation 对象)
|
||||||
|
CacheCreation1hTokens int // 1小时缓存创建token(来自嵌套 cache_creation 对象)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ForwardResult 转发结果
|
// ForwardResult 转发结果
|
||||||
@@ -4401,6 +4403,14 @@ func (s *GatewayService) parseSSEUsage(data string, usage *ClaudeUsage) {
|
|||||||
usage.InputTokens = msgStart.Message.Usage.InputTokens
|
usage.InputTokens = msgStart.Message.Usage.InputTokens
|
||||||
usage.CacheCreationInputTokens = msgStart.Message.Usage.CacheCreationInputTokens
|
usage.CacheCreationInputTokens = msgStart.Message.Usage.CacheCreationInputTokens
|
||||||
usage.CacheReadInputTokens = msgStart.Message.Usage.CacheReadInputTokens
|
usage.CacheReadInputTokens = msgStart.Message.Usage.CacheReadInputTokens
|
||||||
|
|
||||||
|
// 解析嵌套的 cache_creation 对象中的 5m/1h 明细
|
||||||
|
cc5m := gjson.Get(data, "message.usage.cache_creation.ephemeral_5m_input_tokens")
|
||||||
|
cc1h := gjson.Get(data, "message.usage.cache_creation.ephemeral_1h_input_tokens")
|
||||||
|
if cc5m.Exists() || cc1h.Exists() {
|
||||||
|
usage.CacheCreation5mTokens = int(cc5m.Int())
|
||||||
|
usage.CacheCreation1hTokens = int(cc1h.Int())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 解析message_delta获取tokens(兼容GLM等把所有usage放在delta中的API)
|
// 解析message_delta获取tokens(兼容GLM等把所有usage放在delta中的API)
|
||||||
@@ -4429,6 +4439,14 @@ func (s *GatewayService) parseSSEUsage(data string, usage *ClaudeUsage) {
|
|||||||
if msgDelta.Usage.CacheReadInputTokens > 0 {
|
if msgDelta.Usage.CacheReadInputTokens > 0 {
|
||||||
usage.CacheReadInputTokens = msgDelta.Usage.CacheReadInputTokens
|
usage.CacheReadInputTokens = msgDelta.Usage.CacheReadInputTokens
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 解析嵌套的 cache_creation 对象中的 5m/1h 明细
|
||||||
|
cc5m := gjson.Get(data, "usage.cache_creation.ephemeral_5m_input_tokens")
|
||||||
|
cc1h := gjson.Get(data, "usage.cache_creation.ephemeral_1h_input_tokens")
|
||||||
|
if cc5m.Exists() || cc1h.Exists() {
|
||||||
|
usage.CacheCreation5mTokens = int(cc5m.Int())
|
||||||
|
usage.CacheCreation1hTokens = int(cc1h.Int())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4449,6 +4467,14 @@ func (s *GatewayService) handleNonStreamingResponse(ctx context.Context, resp *h
|
|||||||
return nil, fmt.Errorf("parse response: %w", err)
|
return nil, fmt.Errorf("parse response: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 解析嵌套的 cache_creation 对象中的 5m/1h 明细
|
||||||
|
cc5m := gjson.GetBytes(body, "usage.cache_creation.ephemeral_5m_input_tokens")
|
||||||
|
cc1h := gjson.GetBytes(body, "usage.cache_creation.ephemeral_1h_input_tokens")
|
||||||
|
if cc5m.Exists() || cc1h.Exists() {
|
||||||
|
response.Usage.CacheCreation5mTokens = int(cc5m.Int())
|
||||||
|
response.Usage.CacheCreation1hTokens = int(cc1h.Int())
|
||||||
|
}
|
||||||
|
|
||||||
// 兼容 Kimi cached_tokens → cache_read_input_tokens
|
// 兼容 Kimi cached_tokens → cache_read_input_tokens
|
||||||
if response.Usage.CacheReadInputTokens == 0 {
|
if response.Usage.CacheReadInputTokens == 0 {
|
||||||
cachedTokens := gjson.GetBytes(body, "usage.cached_tokens").Int()
|
cachedTokens := gjson.GetBytes(body, "usage.cached_tokens").Int()
|
||||||
@@ -4566,10 +4592,12 @@ func (s *GatewayService) RecordUsage(ctx context.Context, input *RecordUsageInpu
|
|||||||
} else {
|
} else {
|
||||||
// Token 计费
|
// Token 计费
|
||||||
tokens := UsageTokens{
|
tokens := UsageTokens{
|
||||||
InputTokens: result.Usage.InputTokens,
|
InputTokens: result.Usage.InputTokens,
|
||||||
OutputTokens: result.Usage.OutputTokens,
|
OutputTokens: result.Usage.OutputTokens,
|
||||||
CacheCreationTokens: result.Usage.CacheCreationInputTokens,
|
CacheCreationTokens: result.Usage.CacheCreationInputTokens,
|
||||||
CacheReadTokens: result.Usage.CacheReadInputTokens,
|
CacheReadTokens: result.Usage.CacheReadInputTokens,
|
||||||
|
CacheCreation5mTokens: result.Usage.CacheCreation5mTokens,
|
||||||
|
CacheCreation1hTokens: result.Usage.CacheCreation1hTokens,
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
cost, err = s.billingService.CalculateCost(result.Model, tokens, multiplier)
|
cost, err = s.billingService.CalculateCost(result.Model, tokens, multiplier)
|
||||||
@@ -4603,6 +4631,8 @@ func (s *GatewayService) RecordUsage(ctx context.Context, input *RecordUsageInpu
|
|||||||
OutputTokens: result.Usage.OutputTokens,
|
OutputTokens: result.Usage.OutputTokens,
|
||||||
CacheCreationTokens: result.Usage.CacheCreationInputTokens,
|
CacheCreationTokens: result.Usage.CacheCreationInputTokens,
|
||||||
CacheReadTokens: result.Usage.CacheReadInputTokens,
|
CacheReadTokens: result.Usage.CacheReadInputTokens,
|
||||||
|
CacheCreation5mTokens: result.Usage.CacheCreation5mTokens,
|
||||||
|
CacheCreation1hTokens: result.Usage.CacheCreation1hTokens,
|
||||||
InputCost: cost.InputCost,
|
InputCost: cost.InputCost,
|
||||||
OutputCost: cost.OutputCost,
|
OutputCost: cost.OutputCost,
|
||||||
CacheCreationCost: cost.CacheCreationCost,
|
CacheCreationCost: cost.CacheCreationCost,
|
||||||
@@ -4747,10 +4777,12 @@ func (s *GatewayService) RecordUsageWithLongContext(ctx context.Context, input *
|
|||||||
} else {
|
} else {
|
||||||
// Token 计费(使用长上下文计费方法)
|
// Token 计费(使用长上下文计费方法)
|
||||||
tokens := UsageTokens{
|
tokens := UsageTokens{
|
||||||
InputTokens: result.Usage.InputTokens,
|
InputTokens: result.Usage.InputTokens,
|
||||||
OutputTokens: result.Usage.OutputTokens,
|
OutputTokens: result.Usage.OutputTokens,
|
||||||
CacheCreationTokens: result.Usage.CacheCreationInputTokens,
|
CacheCreationTokens: result.Usage.CacheCreationInputTokens,
|
||||||
CacheReadTokens: result.Usage.CacheReadInputTokens,
|
CacheReadTokens: result.Usage.CacheReadInputTokens,
|
||||||
|
CacheCreation5mTokens: result.Usage.CacheCreation5mTokens,
|
||||||
|
CacheCreation1hTokens: result.Usage.CacheCreation1hTokens,
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
cost, err = s.billingService.CalculateCostWithLongContext(result.Model, tokens, multiplier, input.LongContextThreshold, input.LongContextMultiplier)
|
cost, err = s.billingService.CalculateCostWithLongContext(result.Model, tokens, multiplier, input.LongContextThreshold, input.LongContextMultiplier)
|
||||||
@@ -4784,6 +4816,8 @@ func (s *GatewayService) RecordUsageWithLongContext(ctx context.Context, input *
|
|||||||
OutputTokens: result.Usage.OutputTokens,
|
OutputTokens: result.Usage.OutputTokens,
|
||||||
CacheCreationTokens: result.Usage.CacheCreationInputTokens,
|
CacheCreationTokens: result.Usage.CacheCreationInputTokens,
|
||||||
CacheReadTokens: result.Usage.CacheReadInputTokens,
|
CacheReadTokens: result.Usage.CacheReadInputTokens,
|
||||||
|
CacheCreation5mTokens: result.Usage.CacheCreation5mTokens,
|
||||||
|
CacheCreation1hTokens: result.Usage.CacheCreation1hTokens,
|
||||||
InputCost: cost.InputCost,
|
InputCost: cost.InputCost,
|
||||||
OutputCost: cost.OutputCost,
|
OutputCost: cost.OutputCost,
|
||||||
CacheCreationCost: cost.CacheCreationCost,
|
CacheCreationCost: cost.CacheCreationCost,
|
||||||
|
|||||||
@@ -27,14 +27,15 @@ var (
|
|||||||
// LiteLLMModelPricing LiteLLM价格数据结构
|
// LiteLLMModelPricing LiteLLM价格数据结构
|
||||||
// 只保留我们需要的字段,使用指针来处理可能缺失的值
|
// 只保留我们需要的字段,使用指针来处理可能缺失的值
|
||||||
type LiteLLMModelPricing struct {
|
type LiteLLMModelPricing struct {
|
||||||
InputCostPerToken float64 `json:"input_cost_per_token"`
|
InputCostPerToken float64 `json:"input_cost_per_token"`
|
||||||
OutputCostPerToken float64 `json:"output_cost_per_token"`
|
OutputCostPerToken float64 `json:"output_cost_per_token"`
|
||||||
CacheCreationInputTokenCost float64 `json:"cache_creation_input_token_cost"`
|
CacheCreationInputTokenCost float64 `json:"cache_creation_input_token_cost"`
|
||||||
CacheReadInputTokenCost float64 `json:"cache_read_input_token_cost"`
|
CacheCreationInputTokenCostAbove1hr float64 `json:"cache_creation_input_token_cost_above_1hr"`
|
||||||
LiteLLMProvider string `json:"litellm_provider"`
|
CacheReadInputTokenCost float64 `json:"cache_read_input_token_cost"`
|
||||||
Mode string `json:"mode"`
|
LiteLLMProvider string `json:"litellm_provider"`
|
||||||
SupportsPromptCaching bool `json:"supports_prompt_caching"`
|
Mode string `json:"mode"`
|
||||||
OutputCostPerImage float64 `json:"output_cost_per_image"` // 图片生成模型每张图片价格
|
SupportsPromptCaching bool `json:"supports_prompt_caching"`
|
||||||
|
OutputCostPerImage float64 `json:"output_cost_per_image"` // 图片生成模型每张图片价格
|
||||||
}
|
}
|
||||||
|
|
||||||
// PricingRemoteClient 远程价格数据获取接口
|
// PricingRemoteClient 远程价格数据获取接口
|
||||||
@@ -45,14 +46,15 @@ type PricingRemoteClient interface {
|
|||||||
|
|
||||||
// LiteLLMRawEntry 用于解析原始JSON数据
|
// LiteLLMRawEntry 用于解析原始JSON数据
|
||||||
type LiteLLMRawEntry struct {
|
type LiteLLMRawEntry struct {
|
||||||
InputCostPerToken *float64 `json:"input_cost_per_token"`
|
InputCostPerToken *float64 `json:"input_cost_per_token"`
|
||||||
OutputCostPerToken *float64 `json:"output_cost_per_token"`
|
OutputCostPerToken *float64 `json:"output_cost_per_token"`
|
||||||
CacheCreationInputTokenCost *float64 `json:"cache_creation_input_token_cost"`
|
CacheCreationInputTokenCost *float64 `json:"cache_creation_input_token_cost"`
|
||||||
CacheReadInputTokenCost *float64 `json:"cache_read_input_token_cost"`
|
CacheCreationInputTokenCostAbove1hr *float64 `json:"cache_creation_input_token_cost_above_1hr"`
|
||||||
LiteLLMProvider string `json:"litellm_provider"`
|
CacheReadInputTokenCost *float64 `json:"cache_read_input_token_cost"`
|
||||||
Mode string `json:"mode"`
|
LiteLLMProvider string `json:"litellm_provider"`
|
||||||
SupportsPromptCaching bool `json:"supports_prompt_caching"`
|
Mode string `json:"mode"`
|
||||||
OutputCostPerImage *float64 `json:"output_cost_per_image"`
|
SupportsPromptCaching bool `json:"supports_prompt_caching"`
|
||||||
|
OutputCostPerImage *float64 `json:"output_cost_per_image"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// PricingService 动态价格服务
|
// PricingService 动态价格服务
|
||||||
@@ -318,6 +320,9 @@ func (s *PricingService) parsePricingData(body []byte) (map[string]*LiteLLMModel
|
|||||||
if entry.CacheCreationInputTokenCost != nil {
|
if entry.CacheCreationInputTokenCost != nil {
|
||||||
pricing.CacheCreationInputTokenCost = *entry.CacheCreationInputTokenCost
|
pricing.CacheCreationInputTokenCost = *entry.CacheCreationInputTokenCost
|
||||||
}
|
}
|
||||||
|
if entry.CacheCreationInputTokenCostAbove1hr != nil {
|
||||||
|
pricing.CacheCreationInputTokenCostAbove1hr = *entry.CacheCreationInputTokenCostAbove1hr
|
||||||
|
}
|
||||||
if entry.CacheReadInputTokenCost != nil {
|
if entry.CacheReadInputTokenCost != nil {
|
||||||
pricing.CacheReadInputTokenCost = *entry.CacheReadInputTokenCost
|
pricing.CacheReadInputTokenCost = *entry.CacheReadInputTokenCost
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,8 +26,8 @@ type UsageLog struct {
|
|||||||
CacheCreationTokens int
|
CacheCreationTokens int
|
||||||
CacheReadTokens int
|
CacheReadTokens int
|
||||||
|
|
||||||
CacheCreation5mTokens int
|
CacheCreation5mTokens int `gorm:"column:cache_creation_5m_tokens"`
|
||||||
CacheCreation1hTokens int
|
CacheCreation1hTokens int `gorm:"column:cache_creation_1h_tokens"`
|
||||||
|
|
||||||
InputCost float64
|
InputCost float64
|
||||||
OutputCost float64
|
OutputCost float64
|
||||||
|
|||||||
14
backend/migrations/054_drop_legacy_cache_columns.sql
Normal file
14
backend/migrations/054_drop_legacy_cache_columns.sql
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
-- Drop legacy cache token columns that lack the underscore separator.
|
||||||
|
-- These were created by GORM's automatic snake_case conversion:
|
||||||
|
-- CacheCreation5mTokens → cache_creation5m_tokens (incorrect)
|
||||||
|
-- CacheCreation1hTokens → cache_creation1h_tokens (incorrect)
|
||||||
|
--
|
||||||
|
-- The canonical columns are:
|
||||||
|
-- cache_creation_5m_tokens (defined in 001_init.sql)
|
||||||
|
-- cache_creation_1h_tokens (defined in 001_init.sql)
|
||||||
|
--
|
||||||
|
-- Migration 009 already copied data from legacy → canonical columns.
|
||||||
|
-- This migration drops the legacy columns to avoid confusion.
|
||||||
|
|
||||||
|
ALTER TABLE usage_logs DROP COLUMN IF EXISTS cache_creation5m_tokens;
|
||||||
|
ALTER TABLE usage_logs DROP COLUMN IF EXISTS cache_creation1h_tokens;
|
||||||
@@ -70,6 +70,7 @@
|
|||||||
<div v-if="row.cache_creation_tokens > 0" class="inline-flex items-center gap-1">
|
<div v-if="row.cache_creation_tokens > 0" class="inline-flex items-center gap-1">
|
||||||
<svg class="h-3.5 w-3.5 text-amber-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" /></svg>
|
<svg class="h-3.5 w-3.5 text-amber-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" /></svg>
|
||||||
<span class="font-medium text-amber-600 dark:text-amber-400">{{ formatCacheTokens(row.cache_creation_tokens) }}</span>
|
<span class="font-medium text-amber-600 dark:text-amber-400">{{ formatCacheTokens(row.cache_creation_tokens) }}</span>
|
||||||
|
<span v-if="row.cache_creation_1h_tokens > 0" class="inline-flex items-center rounded px-1 py-px text-[10px] font-medium leading-tight bg-orange-100 text-orange-600 ring-1 ring-inset ring-orange-200 dark:bg-orange-500/20 dark:text-orange-400 dark:ring-orange-500/30">1h</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -157,9 +158,29 @@
|
|||||||
<span class="text-gray-400">{{ t('admin.usage.outputTokens') }}</span>
|
<span class="text-gray-400">{{ t('admin.usage.outputTokens') }}</span>
|
||||||
<span class="font-medium text-white">{{ tokenTooltipData.output_tokens.toLocaleString() }}</span>
|
<span class="font-medium text-white">{{ tokenTooltipData.output_tokens.toLocaleString() }}</span>
|
||||||
</div>
|
</div>
|
||||||
<div v-if="tokenTooltipData && tokenTooltipData.cache_creation_tokens > 0" class="flex items-center justify-between gap-4">
|
<div v-if="tokenTooltipData && tokenTooltipData.cache_creation_tokens > 0">
|
||||||
<span class="text-gray-400">{{ t('admin.usage.cacheCreationTokens') }}</span>
|
<!-- 有 5m/1h 明细时,展开显示 -->
|
||||||
<span class="font-medium text-white">{{ tokenTooltipData.cache_creation_tokens.toLocaleString() }}</span>
|
<template v-if="tokenTooltipData.cache_creation_5m_tokens > 0 || tokenTooltipData.cache_creation_1h_tokens > 0">
|
||||||
|
<div v-if="tokenTooltipData.cache_creation_5m_tokens > 0" class="flex items-center justify-between gap-4">
|
||||||
|
<span class="text-gray-400 flex items-center gap-1.5">
|
||||||
|
{{ t('admin.usage.cacheCreation5mTokens') }}
|
||||||
|
<span class="inline-flex items-center rounded px-1 py-px text-[10px] font-medium leading-tight bg-amber-500/20 text-amber-400 ring-1 ring-inset ring-amber-500/30">5m</span>
|
||||||
|
</span>
|
||||||
|
<span class="font-medium text-white">{{ tokenTooltipData.cache_creation_5m_tokens.toLocaleString() }}</span>
|
||||||
|
</div>
|
||||||
|
<div v-if="tokenTooltipData.cache_creation_1h_tokens > 0" class="flex items-center justify-between gap-4">
|
||||||
|
<span class="text-gray-400 flex items-center gap-1.5">
|
||||||
|
{{ t('admin.usage.cacheCreation1hTokens') }}
|
||||||
|
<span class="inline-flex items-center rounded px-1 py-px text-[10px] font-medium leading-tight bg-orange-500/20 text-orange-400 ring-1 ring-inset ring-orange-500/30">1h</span>
|
||||||
|
</span>
|
||||||
|
<span class="font-medium text-white">{{ tokenTooltipData.cache_creation_1h_tokens.toLocaleString() }}</span>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
<!-- 无明细时,只显示聚合值 -->
|
||||||
|
<div v-else class="flex items-center justify-between gap-4">
|
||||||
|
<span class="text-gray-400">{{ t('admin.usage.cacheCreationTokens') }}</span>
|
||||||
|
<span class="font-medium text-white">{{ tokenTooltipData.cache_creation_tokens.toLocaleString() }}</span>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div v-if="tokenTooltipData && tokenTooltipData.cache_read_tokens > 0" class="flex items-center justify-between gap-4">
|
<div v-if="tokenTooltipData && tokenTooltipData.cache_read_tokens > 0" class="flex items-center justify-between gap-4">
|
||||||
<span class="text-gray-400">{{ t('admin.usage.cacheReadTokens') }}</span>
|
<span class="text-gray-400">{{ t('admin.usage.cacheReadTokens') }}</span>
|
||||||
|
|||||||
@@ -2360,6 +2360,8 @@ export default {
|
|||||||
inputTokens: 'Input Tokens',
|
inputTokens: 'Input Tokens',
|
||||||
outputTokens: 'Output Tokens',
|
outputTokens: 'Output Tokens',
|
||||||
cacheCreationTokens: 'Cache Creation Tokens',
|
cacheCreationTokens: 'Cache Creation Tokens',
|
||||||
|
cacheCreation5mTokens: 'Cache Write',
|
||||||
|
cacheCreation1hTokens: 'Cache Write',
|
||||||
cacheReadTokens: 'Cache Read Tokens',
|
cacheReadTokens: 'Cache Read Tokens',
|
||||||
failedToLoad: 'Failed to load usage records',
|
failedToLoad: 'Failed to load usage records',
|
||||||
billingType: 'Billing Type',
|
billingType: 'Billing Type',
|
||||||
|
|||||||
@@ -2527,6 +2527,8 @@ export default {
|
|||||||
inputTokens: '输入 Token',
|
inputTokens: '输入 Token',
|
||||||
outputTokens: '输出 Token',
|
outputTokens: '输出 Token',
|
||||||
cacheCreationTokens: '缓存创建 Token',
|
cacheCreationTokens: '缓存创建 Token',
|
||||||
|
cacheCreation5mTokens: '缓存创建',
|
||||||
|
cacheCreation1hTokens: '缓存创建',
|
||||||
cacheReadTokens: '缓存读取 Token',
|
cacheReadTokens: '缓存读取 Token',
|
||||||
failedToLoad: '加载使用记录失败',
|
failedToLoad: '加载使用记录失败',
|
||||||
billingType: '计费类型',
|
billingType: '计费类型',
|
||||||
|
|||||||
@@ -233,6 +233,7 @@
|
|||||||
<span class="font-medium text-amber-600 dark:text-amber-400">{{
|
<span class="font-medium text-amber-600 dark:text-amber-400">{{
|
||||||
formatCacheTokens(row.cache_creation_tokens)
|
formatCacheTokens(row.cache_creation_tokens)
|
||||||
}}</span>
|
}}</span>
|
||||||
|
<span v-if="row.cache_creation_1h_tokens > 0" class="inline-flex items-center rounded px-1 py-px text-[10px] font-medium leading-tight bg-orange-100 text-orange-600 ring-1 ring-inset ring-orange-200 dark:bg-orange-500/20 dark:text-orange-400 dark:ring-orange-500/30">1h</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -350,9 +351,29 @@
|
|||||||
<span class="text-gray-400">{{ t('admin.usage.outputTokens') }}</span>
|
<span class="text-gray-400">{{ t('admin.usage.outputTokens') }}</span>
|
||||||
<span class="font-medium text-white">{{ tokenTooltipData.output_tokens.toLocaleString() }}</span>
|
<span class="font-medium text-white">{{ tokenTooltipData.output_tokens.toLocaleString() }}</span>
|
||||||
</div>
|
</div>
|
||||||
<div v-if="tokenTooltipData && tokenTooltipData.cache_creation_tokens > 0" class="flex items-center justify-between gap-4">
|
<div v-if="tokenTooltipData && tokenTooltipData.cache_creation_tokens > 0">
|
||||||
<span class="text-gray-400">{{ t('admin.usage.cacheCreationTokens') }}</span>
|
<!-- 有 5m/1h 明细时,展开显示 -->
|
||||||
<span class="font-medium text-white">{{ tokenTooltipData.cache_creation_tokens.toLocaleString() }}</span>
|
<template v-if="tokenTooltipData.cache_creation_5m_tokens > 0 || tokenTooltipData.cache_creation_1h_tokens > 0">
|
||||||
|
<div v-if="tokenTooltipData.cache_creation_5m_tokens > 0" class="flex items-center justify-between gap-4">
|
||||||
|
<span class="text-gray-400 flex items-center gap-1.5">
|
||||||
|
{{ t('admin.usage.cacheCreation5mTokens') }}
|
||||||
|
<span class="inline-flex items-center rounded px-1 py-px text-[10px] font-medium leading-tight bg-amber-500/20 text-amber-400 ring-1 ring-inset ring-amber-500/30">5m</span>
|
||||||
|
</span>
|
||||||
|
<span class="font-medium text-white">{{ tokenTooltipData.cache_creation_5m_tokens.toLocaleString() }}</span>
|
||||||
|
</div>
|
||||||
|
<div v-if="tokenTooltipData.cache_creation_1h_tokens > 0" class="flex items-center justify-between gap-4">
|
||||||
|
<span class="text-gray-400 flex items-center gap-1.5">
|
||||||
|
{{ t('admin.usage.cacheCreation1hTokens') }}
|
||||||
|
<span class="inline-flex items-center rounded px-1 py-px text-[10px] font-medium leading-tight bg-orange-500/20 text-orange-400 ring-1 ring-inset ring-orange-500/30">1h</span>
|
||||||
|
</span>
|
||||||
|
<span class="font-medium text-white">{{ tokenTooltipData.cache_creation_1h_tokens.toLocaleString() }}</span>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
<!-- 无明细时,只显示聚合值 -->
|
||||||
|
<div v-else class="flex items-center justify-between gap-4">
|
||||||
|
<span class="text-gray-400">{{ t('admin.usage.cacheCreationTokens') }}</span>
|
||||||
|
<span class="font-medium text-white">{{ tokenTooltipData.cache_creation_tokens.toLocaleString() }}</span>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div v-if="tokenTooltipData && tokenTooltipData.cache_read_tokens > 0" class="flex items-center justify-between gap-4">
|
<div v-if="tokenTooltipData && tokenTooltipData.cache_read_tokens > 0" class="flex items-center justify-between gap-4">
|
||||||
<span class="text-gray-400">{{ t('admin.usage.cacheReadTokens') }}</span>
|
<span class="text-gray-400">{{ t('admin.usage.cacheReadTokens') }}</span>
|
||||||
|
|||||||
Reference in New Issue
Block a user