Просмотр исходного кода

Merge branch 'main' of https://git.yishihui.com/howard/Agent

guantao 3 часов назад
Родитель
Сommit
d63b9cb5a4
2 измененных файлов с 32 добавлено и 9 удалено
  1. 8 7
      agent/llm/pricing.py
  2. 24 2
      config/pricing.yaml

+ 8 - 7
agent/llm/pricing.py

@@ -65,15 +65,16 @@ class ModelPricing:
         cost = 0.0
 
         # 基础输入费用
-        # 如果有缓存,需要分开计算
-        if usage.cache_read_tokens or usage.cache_creation_tokens:
-            # 普通输入 = 总输入 - 缓存读取(缓存读取部分单独计价)
-            regular_input = usage.input_tokens - usage.cache_read_tokens
-            cost += (regular_input / 1_000_000) * self.input_price
+        # 根据 Anthropic 官方文档:
+        # total_input_tokens = input_tokens + cache_read_tokens + cache_creation_tokens
+        # 这三个字段是分开的,不重叠
+        cost += (usage.input_tokens / 1_000_000) * self.input_price
+
+        # 缓存相关费用(如果有)
+        if usage.cache_read_tokens:
             cost += (usage.cache_read_tokens / 1_000_000) * self.get_cache_read_price()
+        if usage.cache_creation_tokens:
             cost += (usage.cache_creation_tokens / 1_000_000) * self.get_cache_creation_price()
-        else:
-            cost += (usage.input_tokens / 1_000_000) * self.input_price
 
         # 输出费用
         # 如果有 reasoning tokens,需要分开计算

+ 24 - 2
config/pricing.yaml

@@ -68,27 +68,37 @@ models:
   - model: claude-3-5-haiku-20241022
     input_price: 0.80
     output_price: 4.00
+    cache_creation_price: 1.00   # 0.80 * 1.25
+    cache_read_price: 0.08       # 0.80 * 0.1
     provider: anthropic
 
   - model: claude-3-opus-20240229
     input_price: 15.00
     output_price: 75.00
+    cache_creation_price: 18.75  # 15.00 * 1.25
+    cache_read_price: 1.50       # 15.00 * 0.1
     provider: anthropic
 
   # Claude 通配符(匹配新版本)
   - model: claude-3-5-sonnet*
     input_price: 3.00
     output_price: 15.00
+    cache_creation_price: 3.75
+    cache_read_price: 0.30
     provider: anthropic
 
   - model: claude-sonnet-4*
     input_price: 3.00
     output_price: 15.00
+    cache_creation_price: 3.75   # 3.00 * 1.25
+    cache_read_price: 0.30       # 3.00 * 0.1
     provider: anthropic
 
   - model: claude-opus-4*
-    input_price: 15.00
-    output_price: 75.00
+    input_price: 5.00
+    output_price: 25.00
+    cache_creation_price: 6.25   # 5.00 * 1.25
+    cache_read_price: 0.50       # 5.00 * 0.1
     provider: anthropic
 
   # ===== Google Gemini =====
@@ -153,31 +163,43 @@ models:
   - model: anthropic/claude-sonnet-4.5
     input_price: 3.00
     output_price: 15.00
+    cache_creation_price: 3.75
+    cache_read_price: 0.30
     provider: openrouter
 
   - model: anthropic/claude-opus-4.5
     input_price: 5.00
     output_price: 25.00
+    cache_creation_price: 6.25
+    cache_read_price: 0.50
     provider: openrouter
 
   - model: anthropic/claude-opus-4.6
     input_price: 5.00
     output_price: 25.00
+    cache_creation_price: 6.25
+    cache_read_price: 0.50
     provider: openrouter
 
   - model: anthropic/claude-haiku-4.5
     input_price: 1.00
     output_price: 5.00
+    cache_creation_price: 1.25
+    cache_read_price: 0.10
     provider: openrouter
 
   - model: anthropic/claude-sonnet-4
     input_price: 3.00
     output_price: 15.00
+    cache_creation_price: 3.75
+    cache_read_price: 0.30
     provider: openrouter
 
   - model: anthropic/claude*
     input_price: 3.00
     output_price: 15.00
+    cache_creation_price: 3.75
+    cache_read_price: 0.30
     provider: openrouter
 
   - model: openai/gpt-4o*