:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(gateway): add support for glm 5.2 (#66093)
Joshua Snyder386775e2a6a6
diff --git a/services/llm-gateway/src/llm_gateway/rate_limiting/cost_refresh.py b/services/llm-gateway/src/llm_gateway/rate_limiting/cost_refresh.pyindex 20567a82d3ae..acf1a5a6f908 100644--- a/services/llm-gateway/src/llm_gateway/rate_limiting/cost_refresh.py+++ b/services/llm-gateway/src/llm_gateway/rate_limiting/cost_refresh.py@@ -17,11 +17,12 @@ CACHE_TTL_SECONDS = 300# Models on non-canonical litellm providers (e.g. Cloudflare via openai/ prefix) don't match their# cost map entry. Map the key we pass to litellm → the canonical cost-map key.#-# CF entries: litellm publishes per-model CF prices only for a few legacy Llama-2/Mistral variants,-# so we alias to the vendor's direct rate (e.g. moonshot/kimi-k2.6) as a proxy — not CF's actual-# resold rate, and may drift if CF adds markup or flat-rate billing.+# CF entries: alias to litellm's native `cloudflare/@cf/...` price where it carries one (CF's actual+# resold rate). For models litellm only prices under the vendor's own key, alias to that direct rate+# as a proxy — not CF's resold rate, and may drift if CF adds markup or flat-rate billing.COST_ALIASES: dict[str, str] = {"openai/@cf/moonshotai/kimi-k2.6": "moonshot/kimi-k2.6",+ "openai/@cf/zai-org/glm-5.2": "cloudflare/@cf/zai-org/glm-5.2",}# For aliased models, litellm's reported (provider, model) labels don't match what the user asked@@ -29,6 +30,7 @@ COST_ALIASES: dict[str, str] = {# Separate from COST_ALIASES: cost lookups key on litellm.model_cost, metric labels on user intent.ALIAS_METRIC_LABELS: dict[str, tuple[str, str]] = {"openai/@cf/moonshotai/kimi-k2.6": ("cloudflare", "@cf/moonshotai/kimi-k2.6"),+ "openai/@cf/zai-org/glm-5.2": ("cloudflare", "@cf/zai-org/glm-5.2"),}diff --git a/services/llm-gateway/tests/integration/conftest.py b/services/llm-gateway/tests/integration/conftest.pyindex 58d73702124a..a0ecaf271ec9 100644--- a/services/llm-gateway/tests/integration/conftest.py+++ b/services/llm-gateway/tests/integration/conftest.py@@ -33,6 +33,10 @@ TEST_POSTHOG_API_KEY = "phx_fake_personal_api_key"CLOUDFLARE_REQUEST_TIMEOUT = 120.0CLOUDFLARE_MAX_RETRIES = 0+# The CF-hosted models the gateway prices and allowlists (see COST_ALIASES). Smoke-tested end-to-end+# so a routing/pricing regression for any one model fails CI rather than surfacing only in prod.+CLOUDFLARE_SMOKE_MODELS = ["@cf/moonshotai/kimi-k2.6", "@cf/zai-org/glm-5.2"]+MOCK_MODEL_COSTS = {"gpt-4o": {"litellm_provider": "openai",@@ -99,6 +103,14 @@ MOCK_MODEL_COSTS = {"input_cost_per_token": 0.00000095,"output_cost_per_token": 0.000004,},+ "openai/@cf/zai-org/glm-5.2": {+ "litellm_provider": "openai",+ "max_input_tokens": 262144,+ "supports_function_calling": True,+ "mode": "chat",+ "input_cost_per_token": 0.0000014,+ "output_cost_per_token": 0.0000044,+ },}diff --git a/services/llm-gateway/tests/integration/test_anthropic_sdk.py b/services/llm-gateway/tests/integration/test_anthropic_sdk.pyindex f46fdc9d4544..cfe0368918e6 100644--- a/services/llm-gateway/tests/integration/test_anthropic_sdk.py+++ b/services/llm-gateway/tests/integration/test_anthropic_sdk.py@@ -23,6 +23,7 @@ from .conftest import (CLOUDFLARE_API_KEY,CLOUDFLARE_MAX_RETRIES,CLOUDFLARE_REQUEST_TIMEOUT,+ CLOUDFLARE_SMOKE_MODELS,TEST_POSTHOG_API_KEY,)@@ -158,6 +159,21 @@ class TestAnthropicMessages:text_block = _get_text_block(response)assert text_block.text is not None+ @pytest.mark.skipif(not CLOUDFLARE_CONFIGURED, reason="CLOUDFLARE_API_KEY/ACCOUNT_ID not set")+ @pytest.mark.parametrize("model", CLOUDFLARE_SMOKE_MODELS)+ def test_each_cloudflare_model_routes_and_bills(self, cloudflare_anthropic_client: Anthropic, model: str):+ response = cloudflare_anthropic_client.messages.create(+ model=model,+ messages=[{"role": "user", "content": "Say 'hello' and nothing else."}],+ max_tokens=300,+ )++ text_block = _get_text_block(response)+ assert text_block.text is not None+ assert response.usage is not None+ assert response.usage.input_tokens > 0+ assert response.usage.output_tokens > 0+class TestAnthropicMultipleModels:def test_basic_request(self, sdk_config: SDKTestConfig):diff --git a/services/llm-gateway/tests/integration/test_openai_sdk.py b/services/llm-gateway/tests/integration/test_openai_sdk.pyindex 58bc50546242..0b19c918d804 100644--- a/services/llm-gateway/tests/integration/test_openai_sdk.py+++ b/services/llm-gateway/tests/integration/test_openai_sdk.py@@ -15,7 +15,7 @@ from openai import BadRequestError, OpenAIfrom openai.types import Modelfrom openai.types.chat import ChatCompletionToolChoiceOptionParam, ChatCompletionToolParam-from .conftest import CLOUDFLARE_ACCOUNT_ID, CLOUDFLARE_API_KEY+from .conftest import CLOUDFLARE_ACCOUNT_ID, CLOUDFLARE_API_KEY, CLOUDFLARE_SMOKE_MODELSOPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")CLOUDFLARE_CONFIGURED = bool(CLOUDFLARE_API_KEY and CLOUDFLARE_ACCOUNT_ID)@@ -156,6 +156,20 @@ class TestChatCompletions:assert response is not Noneassert response.choices[0].message.content is not None+ @pytest.mark.skipif(not CLOUDFLARE_CONFIGURED, reason="CLOUDFLARE_API_KEY/ACCOUNT_ID not set")+ @pytest.mark.parametrize("model", CLOUDFLARE_SMOKE_MODELS)+ def test_each_cloudflare_model_routes_and_bills(self, cloudflare_openai_client: OpenAI, model: str):+ response = cloudflare_openai_client.chat.completions.create(+ model=model,+ messages=[{"role": "user", "content": "Say 'hello' and nothing else."}],+ max_tokens=300,+ )++ assert response.choices[0].message.content is not None+ assert response.usage is not None+ assert response.usage.prompt_tokens > 0+ assert response.usage.completion_tokens > 0+class TestMultipleModels:def test_basic_request(self, oai_sdk_config: OpenAISDKTestConfig):diff --git a/services/llm-gateway/tests/test_cloudflare.py b/services/llm-gateway/tests/test_cloudflare.pyindex 57b910fc5144..ac3c876c4894 100644--- a/services/llm-gateway/tests/test_cloudflare.py+++ b/services/llm-gateway/tests/test_cloudflare.py@@ -27,10 +27,12 @@ def test_allowlist_derived_from_cost_aliases() -> None:expected = {alias.removeprefix("openai/") for alias in COST_ALIASES if alias.startswith("openai/@cf/")}assert CLOUDFLARE_ALLOWED_MODELS == expectedassert "@cf/moonshotai/kimi-k2.6" in CLOUDFLARE_ALLOWED_MODELS+ assert "@cf/zai-org/glm-5.2" in CLOUDFLARE_ALLOWED_MODELS-def test_ensure_cloudflare_model_allowed_accepts_priced_model() -> None:- ensure_cloudflare_model_allowed("@cf/moonshotai/kimi-k2.6")[email protected]("model", ["@cf/moonshotai/kimi-k2.6", "@cf/zai-org/glm-5.2"])+def test_ensure_cloudflare_model_allowed_accepts_priced_model(model: str) -> None:+ ensure_cloudflare_model_allowed(model)@pytest.mark.parametrize(diff --git a/services/llm-gateway/tests/test_cost_refresh.py b/services/llm-gateway/tests/test_cost_refresh.pyindex ad4a40fe0a12..e1fd7d0ad2c8 100644--- a/services/llm-gateway/tests/test_cost_refresh.py+++ b/services/llm-gateway/tests/test_cost_refresh.py@@ -53,6 +53,11 @@ class TestApplyCostAliases:apply_cost_aliases(cost)assert cost["openai/@cf/moonshotai/kimi-k2.6"]["input_cost_per_token"] == 0.999+ @patch.dict(+ "llm_gateway.rate_limiting.cost_refresh.COST_ALIASES",+ {"openai/@cf/moonshotai/kimi-k2.6": "moonshot/kimi-k2.6"},+ clear=True,+ )@patch("llm_gateway.rate_limiting.cost_refresh.logger")def test_warns_when_canonical_missing(self, mock_logger: MagicMock) -> None:cost: dict[str, Any] = {"gpt-4o": {}}@@ -64,6 +69,11 @@ class TestApplyCostAliases:canonical="moonshot/kimi-k2.6",)+ @patch.dict(+ "llm_gateway.rate_limiting.cost_refresh.COST_ALIASES",+ {"openai/@cf/moonshotai/kimi-k2.6": "moonshot/kimi-k2.6"},+ clear=True,+ )@patch("llm_gateway.rate_limiting.cost_refresh.logger")def test_does_not_warn_when_alias_already_present(self, mock_logger: MagicMock) -> None:cost: dict[str, Any] = {"openai/@cf/moonshotai/kimi-k2.6": {"input_cost_per_token": 0.999}}