Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)
Python
updates
Abi Raja1268414d3232
diff --git a/backend/agent/providers/base.py b/backend/agent/providers/base.pyindex b1da4676..2274522e 100644--- a/backend/agent/providers/base.py+++ b/backend/agent/providers/base.py@@ -24,12 +24,26 @@ class StreamEvent:class TokenUsage:"""Unified token usage across all providers.+ Log line example:+ [TOKEN USAGE] provider=gemini model=... | input=1000 output=500+ cache_read=200 cache_write=0 total=1700 cost=$0.0020+Fields:- input: Non-cached input (prompt) tokens.- output: Output tokens (includes thinking/reasoning where applicable).- cache_read: Cached input tokens read.- cache_write: Cached input tokens written (Anthropic only).- total: Total tokens as reported by the provider, or computed.+ input: Non-cached input tokens (billed at full input rate).+ For providers whose API includes cached tokens in the+ prompt count (OpenAI, Gemini), cached tokens are+ subtracted so this is always *exclusive* of cache_read.+ output: Output tokens including thinking/reasoning (billed at+ output rate).+ cache_read: Input tokens served from cache (billed at reduced rate).+ cache_write: Input tokens written to cache (Anthropic only).+ total: All tokens as reported by the provider API. Equals+ input + cache_read + output (+ thinking for Gemini).++ Total input sent to the model = input + cache_read.+ Cost = (input * input_rate + output * output_rate+ + cache_read * cache_read_rate + cache_write * cache_write_rate)+ / 1_000_000"""input: int = 0diff --git a/backend/agent/providers/gemini.py b/backend/agent/providers/gemini.pyindex a941c92f..1c0c3b49 100644--- a/backend/agent/providers/gemini.py+++ b/backend/agent/providers/gemini.py@@ -188,16 +188,22 @@ def _extract_usage(chunk: types.GenerateContentResponse) -> TokenUsage | None:Gemini reports thinking tokens separately; they are folded into ``output``to match the unified schema used by the other providers.++ ``prompt_token_count`` *includes* ``cached_content_token_count``, so we+ subtract cached tokens to get the non-cached input count (same approach+ as the OpenAI provider)."""meta = chunk.usage_metadataif meta is None:return Nonecandidates = meta.candidates_token_count or 0thoughts = meta.thoughts_token_count or 0+ prompt_tokens = meta.prompt_token_count or 0+ cached_tokens = meta.cached_content_token_count or 0return TokenUsage(- input=meta.prompt_token_count or 0,+ input=prompt_tokens - cached_tokens,output=candidates + thoughts,- cache_read=meta.cached_content_token_count or 0,+ cache_read=cached_tokens,cache_write=0,total=meta.total_token_count or 0,)diff --git a/backend/tests/test_token_usage.py b/backend/tests/test_token_usage.pynew file mode 100644index 00000000..b896361f--- /dev/null+++ b/backend/tests/test_token_usage.py@@ -0,0 +1,284 @@+"""Tests for unified token usage tracking and cost computation."""++from types import SimpleNamespace++from agent.providers.base import MODEL_PRICING, ModelPricing, TokenUsage+from agent.providers.anthropic import _extract_anthropic_usage+from agent.providers.gemini import _extract_usage as _extract_gemini_usage+from agent.providers.openai import _extract_openai_usage+++# ---------------------------------------------------------------------------+# TokenUsage.accumulate+# ---------------------------------------------------------------------------+++class TestAccumulate:+ def test_sums_all_fields(self) -> None:+ a = TokenUsage(input=100, output=50, cache_read=20, cache_write=10, total=180)+ b = TokenUsage(input=200, output=80, cache_read=30, cache_write=5, total=315)+ a.accumulate(b)+ assert a == TokenUsage(+ input=300, output=130, cache_read=50, cache_write=15, total=495+ )++ def test_accumulate_zero_is_noop(self) -> None:+ a = TokenUsage(input=100, output=50, cache_read=20, total=170)+ a.accumulate(TokenUsage())+ assert a == TokenUsage(input=100, output=50, cache_read=20, total=170)++ def test_multiple_accumulations(self) -> None:+ total = TokenUsage()+ for i in range(1, 4):+ total.accumulate(TokenUsage(input=i * 10, output=i * 5, total=i * 15))+ # input: 10+20+30=60, output: 5+10+15=30, total: 15+30+45=90+ assert total.input == 60+ assert total.output == 30+ assert total.total == 90+++# ---------------------------------------------------------------------------+# TokenUsage.cost+# ---------------------------------------------------------------------------+++class TestCost:+ def test_basic_cost(self) -> None:+ usage = TokenUsage(input=1_000_000, output=1_000_000, total=2_000_000)+ pricing = ModelPricing(input=2.00, output=8.00)+ # 1M * $2 + 1M * $8 = $10+ assert usage.cost(pricing) == 10.0++ def test_zero_tokens_zero_cost(self) -> None:+ usage = TokenUsage()+ pricing = ModelPricing(input=5.00, output=25.00, cache_read=0.50)+ assert usage.cost(pricing) == 0.0++ def test_cache_heavy_scenario(self) -> None:+ # 100k non-cached input, 900k cached, 500k output+ usage = TokenUsage(+ input=100_000, output=500_000, cache_read=900_000, total=1_500_000+ )+ pricing = ModelPricing(input=2.00, output=8.00, cache_read=0.50)+ # 100k * $2/M + 500k * $8/M + 900k * $0.50/M+ # = $0.20 + $4.00 + $0.45 = $4.65+ expected = (100_000 * 2.00 + 500_000 * 8.00 + 900_000 * 0.50) / 1_000_000+ assert abs(usage.cost(pricing) - expected) < 1e-9++ def test_anthropic_with_cache_write(self) -> None:+ usage = TokenUsage(+ input=500_000,+ output=200_000,+ cache_read=300_000,+ cache_write=100_000,+ total=1_100_000,+ )+ pricing = ModelPricing(+ input=3.00, output=15.00, cache_read=0.30, cache_write=3.75+ )+ expected = (+ 500_000 * 3.00+ + 200_000 * 15.00+ + 300_000 * 0.30+ + 100_000 * 3.75+ ) / 1_000_000+ assert abs(usage.cost(pricing) - expected) < 1e-9+++# ---------------------------------------------------------------------------+# Gemini: _extract_usage+# ---------------------------------------------------------------------------+++def _gemini_chunk(+ prompt: int = 0,+ candidates: int = 0,+ thoughts: int = 0,+ cached: int = 0,+ total: int = 0,+) -> SimpleNamespace:+ """Build a fake Gemini GenerateContentResponse with usage_metadata."""+ return SimpleNamespace(+ usage_metadata=SimpleNamespace(+ prompt_token_count=prompt,+ candidates_token_count=candidates,+ thoughts_token_count=thoughts,+ cached_content_token_count=cached,+ total_token_count=total,+ )+ )+++class TestGeminiExtract:+ def test_normal_response(self) -> None:+ chunk = _gemini_chunk(+ prompt=1000, candidates=400, thoughts=100, cached=200, total=1500+ )+ usage = _extract_gemini_usage(chunk) # type: ignore[arg-type]+ assert usage is not None+ assert usage.input == 800 # 1000 - 200+ assert usage.output == 500 # 400 + 100+ assert usage.cache_read == 200+ assert usage.cache_write == 0+ assert usage.total == 1500++ def test_no_cache(self) -> None:+ chunk = _gemini_chunk(prompt=500, candidates=200, thoughts=50, total=750)+ usage = _extract_gemini_usage(chunk) # type: ignore[arg-type]+ assert usage is not None+ assert usage.input == 500+ assert usage.cache_read == 0++ def test_no_usage_metadata_returns_none(self) -> None:+ chunk = SimpleNamespace(usage_metadata=None)+ assert _extract_gemini_usage(chunk) is None # type: ignore[arg-type]++ def test_none_subfields_default_to_zero(self) -> None:+ chunk = SimpleNamespace(+ usage_metadata=SimpleNamespace(+ prompt_token_count=None,+ candidates_token_count=None,+ thoughts_token_count=None,+ cached_content_token_count=None,+ total_token_count=None,+ )+ )+ usage = _extract_gemini_usage(chunk) # type: ignore[arg-type]+ assert usage == TokenUsage()+++# ---------------------------------------------------------------------------+# OpenAI: _extract_openai_usage+# ---------------------------------------------------------------------------+++def _openai_response(+ input_tokens: int = 0,+ output_tokens: int = 0,+ total_tokens: int = 0,+ cached_tokens: int = 0,+) -> SimpleNamespace:+ """Build a fake OpenAI response.completed event payload."""+ return SimpleNamespace(+ usage=SimpleNamespace(+ input_tokens=input_tokens,+ output_tokens=output_tokens,+ total_tokens=total_tokens,+ input_tokens_details=SimpleNamespace(cached_tokens=cached_tokens),+ )+ )+++class TestOpenAIExtract:+ def test_normal_response(self) -> None:+ resp = _openai_response(+ input_tokens=1000, output_tokens=500, total_tokens=1500, cached_tokens=300+ )+ usage = _extract_openai_usage(resp)+ assert usage.input == 700 # 1000 - 300+ assert usage.output == 500+ assert usage.cache_read == 300+ assert usage.cache_write == 0+ assert usage.total == 1500++ def test_no_cache(self) -> None:+ resp = _openai_response(+ input_tokens=800, output_tokens=200, total_tokens=1000+ )+ usage = _extract_openai_usage(resp)+ assert usage.input == 800+ assert usage.cache_read == 0++ def test_no_usage_returns_empty(self) -> None:+ resp = SimpleNamespace() # no .usage attribute+ usage = _extract_openai_usage(resp)+ assert usage == TokenUsage()++ def test_no_input_tokens_details(self) -> None:+ resp = SimpleNamespace(+ usage=SimpleNamespace(+ input_tokens=500,+ output_tokens=200,+ total_tokens=700,+ )+ )+ usage = _extract_openai_usage(resp)+ assert usage.input == 500+ assert usage.cache_read == 0+++# ---------------------------------------------------------------------------+# Anthropic: _extract_anthropic_usage+# ---------------------------------------------------------------------------+++def _anthropic_message(+ input_tokens: int = 0,+ output_tokens: int = 0,+ cache_read: int = 0,+ cache_write: int = 0,+) -> SimpleNamespace:+ """Build a fake Anthropic final message with usage."""+ return SimpleNamespace(+ usage=SimpleNamespace(+ input_tokens=input_tokens,+ output_tokens=output_tokens,+ cache_read_input_tokens=cache_read,+ cache_creation_input_tokens=cache_write,+ )+ )+++class TestAnthropicExtract:+ def test_normal_response(self) -> None:+ msg = _anthropic_message(+ input_tokens=1000, output_tokens=500, cache_read=200, cache_write=50+ )+ usage = _extract_anthropic_usage(msg)+ assert usage.input == 1000+ assert usage.output == 500+ assert usage.cache_read == 200+ assert usage.cache_write == 50+ assert usage.total == 1750 # sum of all fields++ def test_no_cache(self) -> None:+ msg = _anthropic_message(input_tokens=600, output_tokens=300)+ usage = _extract_anthropic_usage(msg)+ assert usage.input == 600+ assert usage.cache_read == 0+ assert usage.cache_write == 0+ assert usage.total == 900++ def test_no_usage_returns_empty(self) -> None:+ msg = SimpleNamespace() # no .usage attribute+ usage = _extract_anthropic_usage(msg)+ assert usage == TokenUsage()+++# ---------------------------------------------------------------------------+# MODEL_PRICING lookup+# ---------------------------------------------------------------------------+++class TestModelPricing:+ def test_known_models_have_pricing(self) -> None:+ for name in [+ "gpt-4.1-2025-04-14",+ "gpt-5.2-codex",+ "claude-opus-4-6",+ "claude-sonnet-4-6",+ "gemini-3-flash-preview",+ "gemini-3-pro-preview",+ ]:+ assert name in MODEL_PRICING, f"missing pricing for {name}"++ def test_unknown_model_returns_none(self) -> None:+ assert MODEL_PRICING.get("nonexistent-model") is None++ def test_anthropic_has_cache_write_rate(self) -> None:+ for name in ["claude-opus-4-6", "claude-sonnet-4-6"]:+ assert MODEL_PRICING[name].cache_write > 0++ def test_openai_gemini_no_cache_write(self) -> None:+ for name in ["gpt-4.1-2025-04-14", "gemini-3-flash-preview"]:+ assert MODEL_PRICING[name].cache_write == 0.0