Skip to content

Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)

Python

Add cost tracking to token usage output for all providers

Claude6caa890fa546
diff --git a/backend/agent/providers/anthropic.py b/backend/agent/providers/anthropic.py
index 73021673..0e10769b 100644
--- a/backend/agent/providers/anthropic.py
+++ b/backend/agent/providers/anthropic.py
@@ -13,6 +13,7 @@ from openai.types.chat import ChatCompletionMessageParam
from PIL import Image
from agent.providers.base import (
+ MODEL_PRICING,
EventSink,
ExecutedToolCall,
ProviderSession,
@@ -341,10 +342,13 @@ class AnthropicProviderSession(ProviderSession):
async def close(self) -> None:
u = self._total_usage
+ model_name = self._model.value
+ pricing = MODEL_PRICING.get(model_name)
+ cost_str = f" cost=${u.cost(pricing):.4f}" if pricing else ""
print(
- f"[TOKEN USAGE] provider=anthropic model={self._model.value} | "
+ f"[TOKEN USAGE] provider=anthropic model={model_name} | "
f"input={u.input} output={u.output} "
f"cache_read={u.cache_read} cache_write={u.cache_write} "
- f"total={u.total}"
+ f"total={u.total}{cost_str}"
)
await self._client.close()
diff --git a/backend/agent/providers/base.py b/backend/agent/providers/base.py
index 33a820ff..b1da4676 100644
--- a/backend/agent/providers/base.py
+++ b/backend/agent/providers/base.py
@@ -1,5 +1,5 @@
from dataclasses import dataclass
-from typing import Any, Awaitable, Callable, Literal, Optional, Protocol
+from typing import Any, Awaitable, Callable, Dict, Literal, Optional, Protocol
from agent.tools import ToolCall, ToolExecutionResult
@@ -45,6 +45,63 @@ class TokenUsage:
self.cache_write += other.cache_write
self.total += other.total
+ def cost(self, pricing: "ModelPricing") -> float:
+ """Compute cost in USD using per-million-token rates."""
+ return (
+ self.input * pricing.input
+ + self.output * pricing.output
+ + self.cache_read * pricing.cache_read
+ + self.cache_write * pricing.cache_write
+ ) / 1_000_000
+
+
+@dataclass
+class ModelPricing:
+ """Per-million-token pricing in USD."""
+
+ input: float = 0.0
+ output: float = 0.0
+ cache_read: float = 0.0
+ cache_write: float = 0.0
+
+
+# Pricing keyed by the API model name string sent to the provider.
+MODEL_PRICING: Dict[str, ModelPricing] = {
+ # --- OpenAI ---
+ "gpt-4.1-2025-04-14": ModelPricing(
+ input=2.00, output=8.00, cache_read=0.50
+ ),
+ "gpt-5.2-codex": ModelPricing(
+ input=1.75, output=14.00, cache_read=0.4375
+ ),
+ "gpt-5.3-codex": ModelPricing(
+ input=1.75, output=14.00, cache_read=0.4375
+ ),
+ # --- Anthropic ---
+ "claude-sonnet-4-6": ModelPricing(
+ input=3.00, output=15.00, cache_read=0.30, cache_write=3.75
+ ),
+ "claude-sonnet-4-5-20250929": ModelPricing(
+ input=3.00, output=15.00, cache_read=0.30, cache_write=3.75
+ ),
+ "claude-opus-4-5-20251101": ModelPricing(
+ input=5.00, output=25.00, cache_read=0.50, cache_write=6.25
+ ),
+ "claude-opus-4-6": ModelPricing(
+ input=5.00, output=25.00, cache_read=0.50, cache_write=6.25
+ ),
+ # --- Gemini ---
+ "gemini-3-flash-preview": ModelPricing(
+ input=0.50, output=3.00, cache_read=0.05
+ ),
+ "gemini-3-pro-preview": ModelPricing(
+ input=2.00, output=12.00, cache_read=0.20
+ ),
+ "gemini-3.1-pro-preview": ModelPricing(
+ input=2.00, output=12.00, cache_read=0.20
+ ),
+}
+
@dataclass
class ProviderTurn:
diff --git a/backend/agent/providers/gemini.py b/backend/agent/providers/gemini.py
index 22d805f6..a941c92f 100644
--- a/backend/agent/providers/gemini.py
+++ b/backend/agent/providers/gemini.py
@@ -10,6 +10,7 @@ from google.genai import types
from openai.types.chat import ChatCompletionMessageParam
from agent.providers.base import (
+ MODEL_PRICING,
EventSink,
ExecutedToolCall,
ProviderSession,
@@ -340,9 +341,11 @@ class GeminiProviderSession(ProviderSession):
async def close(self) -> None:
u = self._total_usage
model_name = _get_gemini_api_model_name(self._model)
+ pricing = MODEL_PRICING.get(model_name)
+ cost_str = f" cost=${u.cost(pricing):.4f}" if pricing else ""
print(
f"[TOKEN USAGE] provider=gemini model={model_name} | "
f"input={u.input} output={u.output} "
f"cache_read={u.cache_read} cache_write={u.cache_write} "
- f"total={u.total}"
+ f"total={u.total}{cost_str}"
)
diff --git a/backend/agent/providers/openai.py b/backend/agent/providers/openai.py
index 5735db45..91b56057 100644
--- a/backend/agent/providers/openai.py
+++ b/backend/agent/providers/openai.py
@@ -9,6 +9,7 @@ from openai import AsyncOpenAI
from openai.types.chat import ChatCompletionMessageParam
from agent.providers.base import (
+ MODEL_PRICING,
EventSink,
ExecutedToolCall,
ProviderSession,
@@ -465,10 +466,12 @@ class OpenAIProviderSession(ProviderSession):
async def close(self) -> None:
u = self._total_usage
model_name = get_openai_api_name(self._model)
+ pricing = MODEL_PRICING.get(model_name)
+ cost_str = f" cost=${u.cost(pricing):.4f}" if pricing else ""
print(
f"[TOKEN USAGE] provider=openai model={model_name} | "
f"input={u.input} output={u.output} "
f"cache_read={u.cache_read} cache_write={u.cache_write} "
- f"total={u.total}"
+ f"total={u.total}{cost_str}"
)
await self._client.close()