From 3bd9d35c5f06e1daa7632c8c3732110e2163a573 Mon Sep 17 00:00:00 2001 From: Lanre Shittu <136805224+Shizoqua@users.noreply.github.com> Date: Sun, 22 Nov 2026 09:11:17 +0000 Subject: [PATCH] pricing: price an OpenRouter model variant as its model `resolve_model` drops a router's provider prefix (`openai/gpt-6.1-sol` is `gpt-6.1-sol`), since the prefix names a route, not a price. OpenRouter also adds a variant after a colon: `:free`, `:nitro`, `:floor`, `:online` (its SDK documentation: "Supports variant suffixes (e.g. openai/gpt-4:free)"). The variant chooses how the request is routed or billed, not the model, but it was kept, so `openai/gpt-6.1-sol:nitro` found no price: `weights_for` raised, and the ledger priced the session on the fallback curve (0.10x cache hits and 6x output, where gpt-6.1-sol is 0.05x and 5x). Claude ids escaped only because they are matched by substring. The variant is now dropped with the provider. No price-table key holds a colon, so this changes only ids that had no price; a colon in an id no router named (`llama3:8b`) is left as it is, and an unknown model still fails loudly. --- tests/test_pricing_router_variants.py | 37 +++++++++++++++++++++++++++ tokeneconomizer/pricing.py | 6 +++-- 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 tests/test_pricing_router_variants.py diff --git a/tests/test_pricing_router_variants.py b/tests/test_pricing_router_variants.py new file mode 100644 index 00000000..64a6fb08 --- /dev/null +++ b/tests/test_pricing_router_variants.py @@ -0,0 +1,37 @@ +"""A router's model variant is priced as its model. + +A router names a model by `provider/model`, and OpenRouter adds a variant after a colon: +`openai/gpt-4:free`, `:nitro`, `:floor`, `:online` ("Supports variant suffixes (e.g. +openai/gpt-4:free)", OpenRouter's SDK documentation). The variant chooses how the request is +routed or billed, not which model runs, so its token price ratios are the model's.""" +import pytest + +from tokeneconomizer import pricing +from tokeneconomizer.plugin import accounting + +USAGE = {"input_tokens": 100, "cache_read_input_tokens": 10_000, + "cache_creation_input_tokens": 0, "output_tokens": 10} + + +@pytest.mark.parametrize("routed, model", [ + ("openai/gpt-6.1-sol:nitro", "gpt-6.1-sol"), ("openai/gpt-6.1-sol:floor", "gpt-6.1-sol"), + ("google/gemini-4-argon:free", "gemini-4-argon"), ("openai/gpt-5.5:online", "gpt-5.5"), + ("anthropic/claude-opus-5.5:thinking", "claude-opus-5.5"), +]) +def test_a_variant_of_a_routed_model_is_priced_as_the_model(routed, model): + assert pricing.resolve_model({"model": routed}) == model + assert pricing.weights_for({"model": routed}) == pricing.weights_for({"model": model}) + assert accounting.effective(dict(USAGE, model=routed)) == accounting.effective( + dict(USAGE, model=model)) + assert accounting.effective(dict(USAGE, model=routed))[1] == "model" + + +def test_an_id_that_is_not_routed_keeps_its_colon(): + # A colon outside a router's `provider/model` id is not a variant (`llama3:8b` is an Ollama + # tag); such an id is unpriced either way, and stays as it is. + assert pricing.resolve_model({"model": "llama3:8b"}) == "llama3:8b" + + +def test_an_unknown_routed_model_still_fails_loudly(): + with pytest.raises(ValueError): + pricing.weights_for({"model": "acme/some-new-model:nitro"}) diff --git a/tokeneconomizer/pricing.py b/tokeneconomizer/pricing.py index 9e14a3df..4371b449 100644 --- a/tokeneconomizer/pricing.py +++ b/tokeneconomizer/pricing.py @@ -187,8 +187,10 @@ def resolve_model(rec): # the tail's. Stripping it here is safe because no MODEL_WEIGHTS key contains a # slash -- it can only affect an id that would otherwise hard-fail below, and # hard-failing a correctly-configured BYOK row at the END of a paid rollout is the - # outcome this avoids. - return m.rsplit("/", 1)[-1] if "/" in m else m + # outcome this avoids. OpenRouter also adds a variant after a colon (`openai/gpt-4:free`, + # `:nitro`, `:floor`, `:online`), which chooses the route or the billing, not the model: + # it is dropped with the provider. A colon in an id no router named is left alone. + return m.rsplit("/", 1)[-1].split(":", 1)[0] if "/" in m else m return AGENT_DEFAULT_MODEL.get((rec.get("agent") or "").lower())