The world's first agentic reverse engineer.
feat: provider-aware cost tracking with zero pricing for CUSTOM
Amruthb4ea772a56ce
diff --git a/kong/llm/usage.py b/kong/llm/usage.pyindex ba4d25a..5247cc5 100644--- a/kong/llm/usage.py+++ b/kong/llm/usage.py@@ -4,6 +4,8 @@ from __future__ import annotationsfrom dataclasses import dataclass, field+from kong.config import LLMProvider+@dataclass(frozen=True)class PricingTier:@@ -36,11 +38,20 @@ PRICING_REGISTRY: dict[str, PricingTier] = {}-def get_pricing(model: str) -> PricingTier:- """Look up pricing for a model, falling back to a sensible default."""+_ZERO_PRICING = PricingTier(0.0, 0.0, 0.0, 0.0)+++def get_pricing(model: str, provider: LLMProvider | None = None) -> PricingTier:+ if provider is LLMProvider.CUSTOM:+ return _ZERO_PRICINGreturn PRICING_REGISTRY.get(model, _DEFAULT_PRICING)+def register_custom_model(model: str) -> None:+ if model not in PRICING_REGISTRY:+ PRICING_REGISTRY[model] = _ZERO_PRICING++@dataclassclass ModelTokenUsage:input_tokens: int = 0diff --git a/tests/test_llm_client.py b/tests/test_llm_client.pyindex 2e56965..114f304 100644--- a/tests/test_llm_client.py+++ b/tests/test_llm_client.py@@ -235,3 +235,44 @@ class TestAnalyzeFunctionBatch:assert client.usage.input_tokens == 500assert client.usage.output_tokens == 200assert client.usage.calls == 1+++class TestProviderAwarePricing:+ def test_custom_provider_returns_zero_pricing(self):+ from kong.config import LLMProvider+ from kong.llm.usage import get_pricing++ tier = get_pricing("any-model", provider=LLMProvider.CUSTOM)+ assert tier.input_rate == 0.0+ assert tier.output_rate == 0.0++ def test_known_model_unaffected(self):+ from kong.llm.usage import PRICING_REGISTRY, get_pricing++ tier = get_pricing("gpt-4o")+ assert tier == PRICING_REGISTRY["gpt-4o"]++ def test_unknown_first_party_model_uses_default(self):+ from kong.config import LLMProvider+ from kong.llm.usage import get_pricing++ tier = get_pricing("claude-future-model", provider=LLMProvider.ANTHROPIC)+ assert tier.input_rate == 3.0+ assert tier.output_rate == 15.0++ def test_register_custom_model_adds_zero_pricing(self):+ from kong.llm.usage import PRICING_REGISTRY, get_pricing, register_custom_model++ register_custom_model("my-local-llama")+ tier = get_pricing("my-local-llama")+ assert tier.input_rate == 0.0+ assert tier.output_rate == 0.0+ del PRICING_REGISTRY["my-local-llama"]++ def test_cost_usd_zero_for_registered_custom_model(self):+ from kong.llm.usage import PRICING_REGISTRY, ModelTokenUsage, register_custom_model++ register_custom_model("test-custom-model")+ mu = ModelTokenUsage(input_tokens=1_000_000, output_tokens=1_000_000)+ assert mu.cost_usd("test-custom-model") == 0.0+ del PRICING_REGISTRY["test-custom-model"]