The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
import json
import re
import threading
import uuid
from collections.abc import Callable, Mapping, Sequence
from concurrent.futures import ThreadPoolExecutor
from contextlib import ExitStack
from dataclasses import dataclass
from pathlib import Path
from queue import SimpleQueue
from types import MappingProxyType
from typing import Final
import httpx
import psutil
import pytest
import yaml
from integration._support.client import Gateway, Scenario, eventually
from integration._support.process import graceful_stop_seconds, owned_proxy_process
from integration._support.vertex import service_account_json
from integration._support.wire import Reply, Request, Wire, wire_server
from integration.providers._count_tokens_system_lift import (
RESPONSES_REQUEST_OPTIONS,
RESPONSES_TOOL_USE,
count_request_fields,
counted_fields,
inference_count_fields,
)
from integration._support.ports import allocate_port
from pydantic import JsonValue, TypeAdapter
from tokenizers import Regex, Tokenizer, models, pre_tokenizers
_BACKEND: Final = "claude-sonnet-4-6"
_PROJECT: Final = "scripted-project"
_LOCATION: Final = "us-east5"
_MODELS_PATH: Final = f"/v1/projects/{_PROJECT}/locations/{_LOCATION}/publishers/anthropic/models"
_COUNT_TARGET: Final = f"{_MODELS_PATH}/count-tokens:rawPredict"
_MESSAGE_TARGET: Final = f"{_MODELS_PATH}/{_BACKEND}:rawPredict"
_STREAM_TARGET: Final = f"{_MODELS_PATH}/{_BACKEND}:streamRawPredict?alt=sse"
_PEER_COUNT: Final = 4242
_REJECTION: Final = "scripted partner rejection"
_REJECT_TEXT: Final = "The peer must reject this message"
_REPLY_TEXT: Final = "scripted reply"
_OWNED_MODEL: Final = "partner-claude"
_OWNED_UNREACHABLE_MODEL: Final = "partner-claude-unreachable"
_JSON_OBJECT: Final = TypeAdapter(dict[str, JsonValue])
_GLM_BACKEND: Final = "zai-org/glm-5.2-maas"
_MISTRAL_BACKEND: Final = "mistral-medium-3"
_LLAMA_BACKEND: Final = "meta/llama-4-maverick-17b-128e-instruct-maas"
_LLAMA_COUNT_TARGET: Final = (
f"/v1/projects/{_PROJECT}/locations/{_LOCATION}/publishers/meta/models/count-tokens:rawPredict"
)
_COUNTED_TEXT: Final = "Count this message"
_TOKENIZER_REPO: Final = "scripted/char-tokenizer"
_TOKENIZER_TARGET: Final = f"/{_TOKENIZER_REPO}/resolve/main/tokenizer.json"
_TOKENIZER_HEADERS: Final = MappingProxyType({"X-Repo-Commit": "0" * 40, "ETag": '"scripted-char-tokenizer"'})
_STARTED_WORKER: Final = re.compile(r"Started server process \[(\d+)\]")
_MESSAGES: Final[list[JsonValue]] = [{"role": "user", "content": _COUNTED_TEXT}]
_SYSTEM: Final = "You are a terse assistant that answers in one sentence"
_SYSTEM_BLOCKS: Final[list[JsonValue]] = [
{"type": "text", "text": "You are a terse assistant"},
{"type": "text", "text": "Answer in one sentence"},
]
_WEATHER_SCHEMA: Final[dict[str, JsonValue]] = {
"type": "object",
"properties": {"city": {"type": "string", "description": "City to look up"}},
"required": ["city"],
}
_TOOLS: Final[list[JsonValue]] = [
{"name": "get_weather", "description": "Look up the current weather for a city", "input_schema": _WEATHER_SCHEMA}
]
_OPENAI_TOOLS: Final[list[JsonValue]] = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Look up the current weather for a city",
"parameters": _WEATHER_SCHEMA,
},
}
]
_RESPONSES_TOOLS: Final[list[JsonValue]] = [
{
"type": "function",
"name": "get_weather",
"description": "Look up the current weather for a city",
"parameters": _WEATHER_SCHEMA,
}
]
_PEER_BARE: Final[dict[str, JsonValue]] = {"model": _BACKEND, "messages": _MESSAGES}
_PEER_FULL: Final[dict[str, JsonValue]] = {**_PEER_BARE, "system": _SYSTEM, "tools": _TOOLS}
_GEMINI_BODY: Final[dict[str, JsonValue]] = {"contents": [{"role": "user", "parts": [{"text": "Count this"}]}]}
_GEMINI_MESSAGES: Final[list[JsonValue]] = [{"role": "user", "content": "Count this"}]
_REPLY: Final[dict[str, JsonValue]] = {
"id": "msg_scripted",
"type": "message",
"role": "assistant",
"model": _BACKEND,
"content": [{"type": "text", "text": _REPLY_TEXT}],
"stop_reason": "end_turn",
"stop_sequence": None,
"usage": {"input_tokens": 5, "output_tokens": 3},
}
_EVENTS: Final[tuple[tuple[str, dict[str, JsonValue]], ...]] = (
(
"message_start",
{
"type": "message_start",
"message": {**_REPLY, "content": [], "stop_reason": None, "usage": {"input_tokens": 5, "output_tokens": 1}},
},
),
("content_block_start", {"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}}),
(
"content_block_delta",
{"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": _REPLY_TEXT}},
),
("content_block_stop", {"type": "content_block_stop", "index": 0}),
(
"message_delta",
{
"type": "message_delta",
"delta": {"stop_reason": "end_turn", "stop_sequence": None},
"usage": {"output_tokens": 3},
},
),
("message_stop", {"type": "message_stop"}),
)
_SSE: Final = tuple(f"event: {name}\ndata: {json.dumps(data)}\n\n".encode() for name, data in _EVENTS)
def _counted(_request: Request) -> Reply:
return Reply(body=json.dumps({"input_tokens": _PEER_COUNT}).encode())
def _rejected(status: int, error_type: str = "invalid_request_error") -> Reply:
return Reply(
status=status,
body=json.dumps({"type": "error", "error": {"type": error_type, "message": _REJECTION}}).encode(),
)
def _rejecting(status: int, error_type: str = "invalid_request_error") -> Callable[[Request], Reply]:
def count(_request: Request) -> Reply:
return _rejected(status, error_type)
return count
def _anthropic_message(message: JsonValue) -> bool:
return isinstance(message, dict) and message.get("role") in ("user", "assistant")
def _anthropic_tool(tool: JsonValue) -> bool:
return isinstance(tool, dict) and isinstance(tool.get("name"), str) and isinstance(tool.get("input_schema"), dict)
def _strict(request: Request) -> Reply:
body: Final = _JSON_OBJECT.validate_json(request.body)
messages: Final = body.get("messages")
tools: Final = body.get("tools", [])
accepted: Final = (
isinstance(messages, list)
and all(map(_anthropic_message, messages))
and isinstance(body.get("system", ""), (str, list))
and isinstance(tools, list)
and all(map(_anthropic_tool, tools))
)
return _counted(request) if accepted else _rejected(400)
def _rejecting_marked_messages(request: Request) -> Reply:
return _rejected(400) if _REJECT_TEXT in request.body.decode() else _counted(request)
def _peer(count: Callable[[Request], Reply] = _counted) -> Callable[[Request], Reply]:
def respond(request: Request) -> Reply:
if request.target.endswith("/count-tokens:rawPredict"):
return count(request)
if request.target.endswith(":streamRawPredict?alt=sse"):
return Reply(content_type="text/event-stream", chunks=_SSE)
if request.target.endswith(f"/{_BACKEND}:rawPredict"):
return Reply(body=json.dumps(_REPLY).encode())
return Reply(status=404, body=json.dumps({"error": f"unscripted target {request.target}"}).encode())
return respond
def _count_requests(requests: Sequence[Request]) -> tuple[Request, ...]:
return tuple(request for request in requests if "count-tokens" in request.target)
def _count_bodies(requests: Sequence[Request], target: str = _COUNT_TARGET) -> tuple[dict[str, JsonValue], ...]:
counts: Final = _count_requests(requests)
for request in counts:
assert (request.method, request.target) == ("POST", target), request.target
assert request.headers["authorization"] == "Bearer scripted-token", request.headers
return tuple(_JSON_OBJECT.validate_json(request.body) for request in counts)
def _counted_bodies(wire: Wire) -> tuple[dict[str, JsonValue], ...]:
return _count_bodies(wire.drain())
def _bare(model: str) -> dict[str, JsonValue]:
return {"model": model, "messages": _MESSAGES}
def _full(model: str) -> dict[str, JsonValue]:
return {**_bare(model), "system": _SYSTEM, "tools": _TOOLS}
def _deployment(gateway: Gateway, scenario: Scenario, api_base: str, **overrides: JsonValue) -> str:
return scenario.model(
**{
"model": f"vertex_ai/{_BACKEND}",
"api_base": api_base,
"api_key": None,
"vertex_project": _PROJECT,
"vertex_location": _LOCATION,
"vertex_credentials": service_account_json(_PROJECT, gateway.upstream_url.rstrip("/")),
**overrides,
}
)
def _count(gateway: Gateway, body: Mapping[str, JsonValue]) -> httpx.Response:
return gateway.request("POST", "/v1/messages/count_tokens", body)
def _payload(response: httpx.Response) -> dict[str, JsonValue]:
assert response.status_code == 200, response.text
return _JSON_OBJECT.validate_json(response.content)
def _local_count(gateway: Gateway, body: Mapping[str, JsonValue]) -> int:
response: Final = gateway.request("POST", "/utils/token_counter", body, params={"call_endpoint": "false"})
payload: Final = _payload(response)
total: Final = payload["total_tokens"]
assert payload["tokenizer_type"] != "vertex_ai_partner_models", response.text
assert isinstance(total, int) and 0 < total != _PEER_COUNT, response.text
return total
@dataclass(frozen=True, slots=True)
class _CountRoute:
path: Callable[[str], str]
body: Callable[[str], dict[str, JsonValue]]
rejection: Callable[[str], dict[str, JsonValue]]
count_field: str
params: Mapping[str, str] = MappingProxyType({})
def send(self, gateway: Gateway, model: str) -> httpx.Response:
return gateway.request("POST", self.path(model), self.body(model), params=self.params)
_MESSAGES_ROUTE: Final = _CountRoute(
path=lambda _model: "/v1/messages/count_tokens",
body=_bare,
rejection=lambda message: {"type": "error", "error": {"type": "invalid_request_error", "message": message}},
count_field="input_tokens",
)
_RESPONSES_ROUTE: Final = _CountRoute(
path=lambda _model: "/v1/responses/input_tokens",
body=lambda model: {"model": model, "input": _COUNTED_TEXT},
rejection=lambda message: {
"error": {"message": message, "type": "invalid_request_error", "param": None, "code": None}
},
count_field="input_tokens",
)
_GOOGLE_ROUTE: Final = _CountRoute(
path=lambda model: f"/v1beta/models/{model}:countTokens",
body=lambda _model: {"contents": [{"role": "user", "parts": [{"text": _COUNTED_TEXT}]}]},
rejection=lambda message: {"error": {"code": 400, "message": message, "status": "INVALID_ARGUMENT"}},
count_field="totalTokens",
)
_UTILS_ROUTE: Final = _CountRoute(
path=lambda _model: "/utils/token_counter",
body=_bare,
rejection=lambda message: {
"error": {"message": message, "type": "token_counting_error", "param": "model", "code": "400"}
},
count_field="total_tokens",
params=MappingProxyType({"call_endpoint": "true"}),
)
_COUNT_ROUTES: Final = (_MESSAGES_ROUTE, _RESPONSES_ROUTE, _GOOGLE_ROUTE, _UTILS_ROUTE)
def _error_message(payload: Mapping[str, JsonValue]) -> str:
error: Final = payload.get("error")
assert isinstance(error, dict), payload
message: Final = error.get("message")
assert isinstance(message, str), payload
return message
def _route_count(gateway: Gateway, route: _CountRoute, model: str) -> tuple[int, JsonValue]:
response: Final = route.send(gateway, model)
return response.status_code, _JSON_OBJECT.validate_json(response.content).get(route.count_field)
def _char_tokenizer() -> bytes:
tokenizer: Final = Tokenizer(models.WordLevel({"[UNK]": 0}, unk_token="[UNK]"))
tokenizer.pre_tokenizer = pre_tokenizers.Split(Regex("."), behavior="isolated")
return tokenizer.to_str().encode()
def _tokenizer_hub(tokenizer: bytes) -> Callable[[Request], Reply]:
def respond(request: Request) -> Reply:
if request.target == _TOKENIZER_TARGET:
return Reply(body=tokenizer, headers=_TOKENIZER_HEADERS)
return Reply(status=404, body=json.dumps({"error": f"unscripted target {request.target}"}).encode())
return respond
def _closed_port_url() -> str:
return f"http://127.0.0.1:{allocate_port()}"
def _clients(stack: ExitStack, base_url: str, count: int) -> tuple[httpx.Client, ...]:
return tuple(
stack.enter_context(httpx.Client(base_url=base_url, timeout=30, trust_env=False)) for _ in range(count)
)
def _counted_on(client: httpx.Client, key: str, body: Mapping[str, JsonValue]) -> tuple[int, JsonValue]:
response: Final = client.post(
"/v1/messages/count_tokens", json=dict(body), headers={"Authorization": f"Bearer {key}"}
)
return response.status_code, _JSON_OBJECT.validate_json(response.content).get("input_tokens")
def _generated_then_counted(
client: httpx.Client, key: str, model: str, body: Mapping[str, JsonValue]
) -> tuple[int, int, JsonValue]:
generated: Final = client.post(
"/v1/messages",
json={
"model": model,
"max_tokens": 16,
"messages": [{"role": "user", "content": f"Generate before counting {uuid.uuid4().hex}"}],
},
headers={"Authorization": f"Bearer {key}"},
)
return generated.status_code, *_counted_on(client, key, body)
def _local_port(client: httpx.Client) -> int:
with client.stream("GET", "/health/liveliness") as response:
port: Final = int(response.extensions["network_stream"].get_extra_info("client_addr")[1])
response.read()
assert response.status_code == 200, response.text
return port
def _counted_or_dropped(client: httpx.Client, key: str, body: Mapping[str, JsonValue]) -> tuple[int, JsonValue] | None:
try:
return _counted_on(client, key, body)
except httpx.TransportError:
return None
def _accepted_client_ports(pid: int, proxy_port: int) -> frozenset[int]:
return frozenset(
connection.raddr.port
for connection in psutil.Process(pid).net_connections(kind="tcp")
if connection.raddr and connection.laddr.port == proxy_port
)
def _owned_config(
path: Path, gateway: Gateway, api_bases: Mapping[str, str], settings: Mapping[str, JsonValue]
) -> Path:
config: Final = yaml.safe_load(Path("tests/integration/proxy_config.yaml").read_text())
path.write_text(
yaml.safe_dump(
{
**config,
"model_list": [
{
"model_name": name,
"litellm_params": {
"model": f"vertex_ai/{_BACKEND}",
"api_base": api_base,
"vertex_project": _PROJECT,
"vertex_location": _LOCATION,
"vertex_credentials": service_account_json(_PROJECT, gateway.upstream_url.rstrip("/")),
},
}
for name, api_base in api_bases.items()
],
"litellm_settings": {**config["litellm_settings"], **settings},
}
)
)
return path
@pytest.mark.parametrize("system", [_SYSTEM, _SYSTEM_BLOCKS], ids=["string", "blocks"])
def test_messages_count_tokens_forwards_system_and_tools(gateway: Gateway, system: JsonValue) -> None:
with wire_server(_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url)
response: Final = _count(gateway, {**_bare(model), "system": system, "tools": _TOOLS})
assert _payload(response) == {"input_tokens": _PEER_COUNT, "litellm_count_source": "provider"}, response.text
assert _counted_bodies(wire) == ({**_PEER_BARE, "system": system, "tools": _TOOLS},)
def test_messages_count_tokens_without_system_or_tools_sends_bare_body(gateway: Gateway) -> None:
with wire_server(_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url)
response: Final = _count(gateway, _bare(model))
assert _payload(response) == {"input_tokens": _PEER_COUNT, "litellm_count_source": "provider"}, response.text
assert _counted_bodies(wire) == (_PEER_BARE,)
def test_utils_token_counter_call_endpoint_forwards_system_and_tools(gateway: Gateway) -> None:
with wire_server(_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url)
response: Final = gateway.request(
"POST", "/utils/token_counter", _full(model), params={"call_endpoint": "true"}
)
payload: Final = _payload(response)
assert (payload["total_tokens"], payload["tokenizer_type"]) == (_PEER_COUNT, "vertex_ai_partner_models")
assert (payload["request_model"], payload["model_used"]) == (model, _BACKEND), response.text
assert _counted_bodies(wire) == (_PEER_FULL,)
def test_utils_token_counter_local_mode_never_calls_the_peer(gateway: Gateway) -> None:
with wire_server(_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url)
assert _local_count(gateway, _full(model)) > 0
assert wire.drain() == ()
def _inference_and_count_fields(wire: Wire) -> tuple[dict[str, JsonValue], ...]:
requests: Final = wire.drain()
(inference,) = (request for request in requests if request.target == _MESSAGE_TARGET)
return (counted_fields(_JSON_OBJECT.validate_json(inference.body)), *map(counted_fields, _count_bodies(requests)))
def test_utils_token_counter_counts_openai_tools_the_way_chat_inference_sends_them(gateway: Gateway) -> None:
with wire_server(_peer(_strict)) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url)
body: Final = {**_bare(model), "tools": _OPENAI_TOOLS}
generated: Final = gateway.request("POST", "/v1/chat/completions", {**body, "max_tokens": 16})
assert generated.status_code == 200, generated.text
response: Final = gateway.request("POST", "/utils/token_counter", body, params={"call_endpoint": "true"})
assert _payload(response)["total_tokens"] == _PEER_COUNT, response.text
inference, counted = _inference_and_count_fields(wire)
assert counted == inference, (counted, inference)
def test_responses_input_tokens_counts_through_the_partner_peer(gateway: Gateway) -> None:
with wire_server(_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url)
response: Final = gateway.request(
"POST", "/v1/responses/input_tokens", {"model": model, "input": "Count this message"}
)
assert _payload(response) == {
"object": "response.input_tokens",
"input_tokens": _PEER_COUNT,
"litellm_count_source": "provider",
}, response.text
assert _counted_bodies(wire) == (_PEER_BARE,)
@pytest.mark.parametrize("request_body", RESPONSES_TOOL_USE.values(), ids=RESPONSES_TOOL_USE.keys())
def test_responses_input_tokens_counts_what_responses_inference_sends(
gateway: Gateway, request_body: dict[str, JsonValue]
) -> None:
with wire_server(_peer(_strict)) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url)
generated: Final = gateway.request(
"POST", "/v1/responses", {**request_body, "model": model, "max_output_tokens": 16}
)
assert generated.status_code == 200, generated.text
response: Final = gateway.request("POST", "/v1/responses/input_tokens", {**request_body, "model": model})
assert _payload(response)["input_tokens"] == _PEER_COUNT, response.text
inference, counted = _inference_and_count_fields(wire)
assert counted == inference, (counted, inference)
def test_responses_input_tokens_surfaces_the_peer_error(gateway: Gateway) -> None:
with wire_server(_peer(_rejecting(400))) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url)
response: Final = gateway.request(
"POST",
"/v1/responses/input_tokens",
{"model": model, "input": "Count this message", "instructions": "Be terse", "tools": _RESPONSES_TOOLS},
)
assert response.status_code == 400, response.text
assert response.json() == _RESPONSES_ROUTE.rejection(_REJECTION), response.text
(sent,) = _counted_bodies(wire)
assert sent["system"] == [{"type": "text", "text": "Be terse"}], sent
assert sent["tools"] == [
{
"name": "get_weather",
"input_schema": _WEATHER_SCHEMA,
"type": "custom",
"description": "Look up the current weather for a city",
}
], sent
def test_responses_input_tokens_rejects_an_unpaired_tool_output_without_calling_vertex(gateway: Gateway) -> None:
with wire_server(_peer(_strict)) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url)
body: Final[dict[str, JsonValue]] = {
"model": model,
"input": [
{"role": "user", "content": "What is the weather in Paris?"},
{"type": "function_call", "call_id": "call_1", "name": "get_weather", "arguments": "Paris?"},
{"type": "function_call_output", "call_id": "call_1", "output": "Sunny"},
],
"tools": _RESPONSES_TOOLS,
}
response: Final = gateway.request("POST", "/v1/responses/input_tokens", body)
assert response.status_code == 400, response.text
assert "get_weather" in _error_message(_JSON_OBJECT.validate_json(response.content)), response.text
assert wire.drain() == ()
def test_responses_input_tokens_types_a_peer_error_by_its_status(gateway: Gateway) -> None:
with wire_server(_peer(_rejecting(404, "not_found_error"))) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url)
response: Final = _RESPONSES_ROUTE.send(gateway, model)
assert response.status_code == 404, response.text
assert response.json() == _RESPONSES_ROUTE.rejection(_REJECTION), response.text
assert _counted_bodies(wire) == (_PEER_BARE,)
@pytest.mark.parametrize(
("backend", "route"),
[
pytest.param(_GLM_BACKEND, _MESSAGES_ROUTE, id="glm-messages"),
pytest.param(_GLM_BACKEND, _RESPONSES_ROUTE, id="glm-responses"),
pytest.param(_GLM_BACKEND, _GOOGLE_ROUTE, id="glm-google"),
pytest.param(_GLM_BACKEND, _UTILS_ROUTE, id="glm-utils"),
pytest.param(_MISTRAL_BACKEND, _MESSAGES_ROUTE, id="mistral-messages"),
],
)
def test_partner_without_a_count_route_answers_400_without_calling_vertex(
gateway: Gateway, backend: str, route: _CountRoute
) -> None:
with wire_server(_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url, model=f"vertex_ai/{backend}")
response: Final = route.send(gateway, model)
assert response.status_code == 400, response.text
payload: Final = _JSON_OBJECT.validate_json(response.content)
message: Final = _error_message(payload)
assert backend in message, message
assert payload == route.rejection(message), response.text
assert wire.drain() == ()
def test_llama_count_still_reaches_the_meta_count_route(gateway: Gateway) -> None:
with wire_server(_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url, model=f"vertex_ai/{_LLAMA_BACKEND}")
response: Final = _count(gateway, _bare(model))
assert _payload(response) == {"input_tokens": _PEER_COUNT, "litellm_count_source": "provider"}, response.text
(sent,) = _count_bodies(wire.drain(), _LLAMA_COUNT_TARGET)
assert sent["messages"] == _MESSAGES, sent
@pytest.mark.timeout(2 * graceful_stop_seconds() + 120)
def test_custom_tokenizer_partner_counts_locally_on_every_count_route(gateway: Gateway, tmp_path: Path) -> None:
environment: Final = {"HF_HOME": str(tmp_path / "hf-home")}
with (
wire_server(_tokenizer_hub(_char_tokenizer())) as hub,
wire_server(_peer()) as vertex,
owned_proxy_process(gateway, tmp_path, {**environment, "HF_ENDPOINT": hub.url}) as owned,
owned.gateway.scenario() as scenario,
):
model: Final = _deployment(
owned.gateway,
scenario,
vertex.url,
model=f"vertex_ai/{_GLM_BACKEND}",
model_info={"custom_tokenizer": {"identifier": _TOKENIZER_REPO, "revision": "main", "auth_token": None}},
)
local: Final = _payload(
owned.gateway.request("POST", "/utils/token_counter", _bare(model), params={"call_endpoint": "false"})
)
assert local["tokenizer_type"] == "huggingface_tokenizer", local
total: Final = local["total_tokens"]
assert isinstance(total, int) and total >= len(_COUNTED_TEXT), local
counts: Final = tuple(_route_count(owned.gateway, route, model) for route in _COUNT_ROUTES)
assert counts == ((200, total),) * len(_COUNT_ROUTES), counts
contents_only: Final = owned.gateway.request(
"POST",
"/utils/token_counter",
{"model": model, "contents": [{"role": "user", "parts": [{"text": _COUNTED_TEXT}]}]},
params={"call_endpoint": "true"},
)
assert contents_only.status_code == 400, contents_only.text
assert _GLM_BACKEND in _error_message(_JSON_OBJECT.validate_json(contents_only.content)), contents_only.text
assert vertex.drain() == ()
assert {request.target for request in hub.drain()} >= {_TOKENIZER_TARGET}
def test_gemini_count_tokens_route_reaches_the_partner_peer(gateway: Gateway) -> None:
with wire_server(_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url)
response: Final = gateway.request("POST", f"/v1beta/models/{model}:countTokens", _GEMINI_BODY)
assert "totalTokens" in _payload(response), response.text
assert _counted_bodies(wire) == ({"model": _BACKEND, "messages": _GEMINI_MESSAGES},)
def test_gemini_count_tokens_route_surfaces_the_peer_error(gateway: Gateway) -> None:
with wire_server(_peer(_rejecting(400))) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url)
response: Final = gateway.request("POST", f"/v1beta/models/{model}:countTokens", _GEMINI_BODY)
assert response.status_code == 400, response.text
assert "totalTokens" not in response.text, response.text
assert _counted_bodies(wire) == ({"model": _BACKEND, "messages": _GEMINI_MESSAGES},)
@pytest.mark.parametrize("status", [400, 500, 503])
def test_messages_count_tokens_surfaces_the_peer_error_when_peer_errors(gateway: Gateway, status: int) -> None:
with wire_server(_peer(_rejecting(status))) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url)
response: Final = _count(gateway, _full(model))
assert response.status_code == status, response.text
assert "input_tokens" not in response.text, response.text
assert _counted_bodies(wire) == (_PEER_FULL,)
def test_messages_count_tokens_surfaces_the_error_when_token_endpoint_rejects(gateway: Gateway) -> None:
def respond(request: Request) -> Reply:
if request.target == "/_oauth/token":
return Reply(
status=400,
body=json.dumps({"error": "invalid_grant", "error_description": "scripted refusal"}).encode(),
)
return _peer()(request)
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = _deployment(
gateway, scenario, wire.url, vertex_credentials=service_account_json(_PROJECT, wire.url)
)
response: Final = _count(gateway, _full(model))
assert 500 <= response.status_code < 600, response.text
assert "input_tokens" not in response.text, response.text
targets: Final = frozenset(request.target for request in wire.drain())
assert targets == {"/_oauth/token"}, targets
def test_messages_count_tokens_errors_when_peer_is_unreachable(gateway: Gateway) -> None:
with gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, _closed_port_url())
response: Final = _count(gateway, _full(model))
assert 500 <= response.status_code < 600, response.text
assert "input_tokens" not in response.text, response.text
@pytest.mark.parametrize(
"tools",
[5, "", "x" * 5120, ["get_weather"]],
ids=["int", "empty_string", "5kb_string", "list_of_strings"],
)
def test_messages_count_tokens_rejects_malformed_tools_without_calling_the_peer(
gateway: Gateway, tools: JsonValue
) -> None:
with wire_server(_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url)
refused: Final = _count(gateway, {**_bare(model), "tools": tools})
assert 400 <= refused.status_code < 600, refused.text
assert "input_tokens" not in refused.text, refused.text
assert wire.drain() == ()
assert _generated_then_counted(gateway.client, gateway.key, model, _bare(model)) == (200, 200, _PEER_COUNT)
assert _counted_bodies(wire) == (_PEER_BARE,)
def test_messages_count_tokens_forwards_an_empty_tools_list(gateway: Gateway) -> None:
with wire_server(_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url)
response: Final = _count(gateway, {**_bare(model), "tools": []})
assert _payload(response) == {"input_tokens": _PEER_COUNT, "litellm_count_source": "provider"}, response.text
assert _counted_bodies(wire) == ({**_PEER_BARE, "tools": []},)
def test_messages_count_tokens_forwards_an_empty_system_string(gateway: Gateway) -> None:
with wire_server(_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url)
response: Final = _count(gateway, {**_bare(model), "system": ""})
assert _payload(response) == {"input_tokens": _PEER_COUNT, "litellm_count_source": "provider"}, response.text
assert _counted_bodies(wire) == ({**_PEER_BARE, "system": ""},)
def test_messages_count_tokens_leaves_null_system_and_tools_out(gateway: Gateway) -> None:
with wire_server(_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url)
response: Final = _count(gateway, {**_bare(model), "system": None, "tools": None})
assert _payload(response) == {"input_tokens": _PEER_COUNT, "litellm_count_source": "provider"}, response.text
assert _counted_bodies(wire) == (_PEER_BARE,)
def test_messages_count_tokens_surfaces_the_peer_error_for_a_non_text_system(gateway: Gateway) -> None:
with wire_server(_peer(_strict)) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url)
response: Final = _count(gateway, {**_bare(model), "system": 5})
assert response.status_code == 400, response.text
assert "input_tokens" not in response.text, response.text
assert _counted_bodies(wire) == ({**_PEER_BARE, "system": 5},)
def test_messages_count_tokens_forwards_a_5kb_system_verbatim(gateway: Gateway) -> None:
system: Final = "Answer in one sentence. " * 214
with wire_server(_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url)
response: Final = _count(gateway, {**_bare(model), "system": system})
assert _payload(response) == {"input_tokens": _PEER_COUNT, "litellm_count_source": "provider"}, response.text
assert _counted_bodies(wire) == ({**_PEER_BARE, "system": system},)
def test_messages_count_tokens_duplicate_system_and_tools_keys_forward_one_value_each(gateway: Gateway) -> None:
with wire_server(_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url)
fields: Final = f'"system": {json.dumps(_SYSTEM)}, "tools": {json.dumps(_TOOLS)}'
response: Final = gateway.client.post(
"/v1/messages/count_tokens",
content=f'{{"model": "{model}", "messages": {json.dumps(_MESSAGES)}, {fields}, {fields}}}',
headers={"Authorization": f"Bearer {gateway.key}", "Content-Type": "application/json"},
)
assert _payload(response) == {"input_tokens": _PEER_COUNT, "litellm_count_source": "provider"}, response.text
(sent,) = _count_requests(wire.drain())
assert _count_bodies((sent,)) == (_PEER_FULL,)
assert (sent.body.count(b'"system"'), sent.body.count(b'"tools"')) == (1, 1), sent.body
def test_messages_count_tokens_unauthenticated_request_never_reaches_the_peer(gateway: Gateway) -> None:
with wire_server(_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url)
response: Final = gateway.request(
"POST", "/v1/messages/count_tokens", _full(model), key="sk-not-a-key-this-proxy-issued"
)
assert response.status_code == 401, response.text
assert "input_tokens" not in response.text, response.text
assert wire.drain() == ()
@pytest.mark.parametrize("fields", [{}, {"messages": []}], ids=["missing", "empty"])
def test_messages_count_tokens_without_messages_is_rejected(gateway: Gateway, fields: dict[str, JsonValue]) -> None:
with wire_server(_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url)
response: Final = _count(gateway, {"model": model, "system": _SYSTEM, "tools": _TOOLS, **fields})
assert response.status_code == 400, response.text
assert "messages parameter is required" in response.text, response.text
assert wire.drain() == ()
def test_count_tokens_location_override_targets_the_count_region(gateway: Gateway) -> None:
with wire_server(_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(
gateway, scenario, wire.url, vertex_location="global", vertex_count_tokens_location="europe-west1"
)
response: Final = _count(gateway, _bare(model))
assert _payload(response) == {"input_tokens": _PEER_COUNT, "litellm_count_source": "provider"}, response.text
target: Final = _COUNT_TARGET.replace(f"/locations/{_LOCATION}/", "/locations/europe-west1/")
assert _count_bodies(wire.drain(), target) == (_PEER_BARE,)
def test_messages_count_tokens_repeated_request_reaches_the_peer_each_time(gateway: Gateway) -> None:
with wire_server(_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url)
answers: Final = tuple(_payload(_count(gateway, _bare(model))) for _ in range(2))
assert answers == ({"input_tokens": _PEER_COUNT, "litellm_count_source": "provider"},) * 2
assert _counted_bodies(wire) == (_PEER_BARE,) * 2
@pytest.mark.timeout(240) # boots an owned two-worker proxy with litellm_settings.disable_token_counter
def test_disabled_token_counter_surfaces_provider_failures_instead_of_counting_locally(
gateway: Gateway, tmp_path: Path
) -> None:
with wire_server(_peer(_rejecting_marked_messages)) as wire:
config: Final = _owned_config(
tmp_path / "disabled-token-counter.yaml",
gateway,
{_OWNED_MODEL: wire.url, _OWNED_UNREACHABLE_MODEL: _closed_port_url()},
{"disable_token_counter": True},
)
with owned_proxy_process(gateway, tmp_path, {}, config=config, workers=2) as owned:
counted: Final = _count(owned.gateway, _full(_OWNED_MODEL))
assert _payload(counted) == {"input_tokens": _PEER_COUNT, "litellm_count_source": "provider"}, counted.text
rejected: Final = _count(
owned.gateway, {**_full(_OWNED_MODEL), "messages": [{"role": "user", "content": _REJECT_TEXT}]}
)
assert rejected.status_code == 400, rejected.text
assert _REJECTION in rejected.text and "input_tokens" not in rejected.text, rejected.text
unreachable: Final = _count(owned.gateway, _full(_OWNED_UNREACHABLE_MODEL))
assert 500 <= unreachable.status_code < 600, unreachable.text
assert "input_tokens" not in unreachable.text, unreachable.text
local: Final = owned.gateway.request(
"POST", "/utils/token_counter", _full(_OWNED_MODEL), params={"call_endpoint": "false"}
)
assert local.status_code == 503, local.text
assert len(_counted_bodies(wire)) == 2
def test_peer_outage_between_concurrent_waves_errors_then_recovers(gateway: Gateway) -> None:
with ExitStack() as stack:
clients: Final = _clients(stack, str(gateway.client.base_url), 8)
pool: Final = stack.enter_context(ThreadPoolExecutor(max_workers=len(clients)))
scenario: Final = stack.enter_context(gateway.scenario())
with wire_server(_peer()) as wire:
port: Final = int(wire.url.rsplit(":", 1)[1])
model: Final = _deployment(gateway, scenario, wire.url)
body: Final = _full(model)
def generate_then_count(client: httpx.Client) -> tuple[int, int, JsonValue]:
return _generated_then_counted(client, gateway.key, model, body)
assert tuple(pool.map(generate_then_count, clients)) == ((200, 200, _PEER_COUNT),) * len(clients)
assert _counted_bodies(wire) == (_PEER_FULL,) * len(clients)
outage: Final = tuple(pool.map(lambda client: _counted_on(client, gateway.key, body), clients))
for status, tokens in outage:
assert 500 <= status < 600 and tokens is None, outage
with wire_server(_peer(), port=port) as revived:
assert tuple(pool.map(generate_then_count, clients)) == ((200, 200, _PEER_COUNT),) * len(clients)
assert _counted_bodies(revived) == (_PEER_FULL,) * len(clients)
def test_slow_peer_holds_concurrent_counts_without_stalling_the_proxy(gateway: Gateway) -> None:
held: Final[SimpleQueue[str]] = SimpleQueue()
release: Final = threading.Event()
def hold(request: Request) -> Reply:
held.put(request.target)
assert release.wait(timeout=20), "Held count was never released"
return _counted(request)
with ExitStack() as stack:
clients: Final = _clients(stack, str(gateway.client.base_url), 6)
wire: Final = stack.enter_context(wire_server(_peer(hold)))
scenario: Final = stack.enter_context(gateway.scenario())
pool: Final = stack.enter_context(ThreadPoolExecutor(max_workers=len(clients)))
model: Final = _deployment(gateway, scenario, wire.url)
try:
futures: Final = tuple(
pool.submit(_generated_then_counted, client, gateway.key, model, _full(model)) for client in clients
)
eventually(held.qsize, lambda size: size == len(clients), seconds=30)
assert gateway.request("GET", "/health/liveliness").status_code == 200
assert _local_count(gateway, _full(model)) > 0
assert not any(future.done() for future in futures)
finally:
release.set()
assert tuple(future.result(timeout=30) for future in futures) == ((200, 200, _PEER_COUNT),) * len(clients)
assert len(_counted_bodies(wire)) == len(clients)
@pytest.mark.timeout(300) # boots an owned two-worker proxy, kills one worker, and waits for its replacement
def test_worker_sigkill_mid_burst_leaves_the_sibling_counting(gateway: Gateway, tmp_path: Path) -> None:
held: Final[SimpleQueue[str]] = SimpleQueue()
release: Final = threading.Event()
def hold(request: Request) -> Reply:
held.put(request.target)
assert release.wait(timeout=60), "Held count was never released"
return _counted(request)
with ExitStack() as stack:
wire: Final = stack.enter_context(wire_server(_peer(hold)))
stack.callback(release.set)
config: Final = _owned_config(tmp_path / "worker-kill.yaml", gateway, {_OWNED_MODEL: wire.url}, {})
owned: Final = stack.enter_context(owned_proxy_process(gateway, tmp_path, {}, config=config, workers=2))
proxy_url: Final = owned.gateway.client.base_url
workers: Final = eventually(
lambda: tuple(int(pid) for pid in _STARTED_WORKER.findall(owned.log.read_text())),
lambda pids: len(pids) == 2,
seconds=30,
)
clients: Final = _clients(stack, str(proxy_url), 12)
pool: Final = stack.enter_context(ThreadPoolExecutor(max_workers=len(clients)))
stack.callback(release.set)
ports: Final = tuple(_local_port(client) for client in clients)
futures: Final = tuple(
pool.submit(_counted_or_dropped, client, gateway.key, _full(_OWNED_MODEL)) for client in clients
)
eventually(held.qsize, lambda size: size == len(clients), seconds=30)
shares: Final = {pid: _accepted_client_ports(pid, proxy_url.port or 0) & frozenset(ports) for pid in workers}
assert sum(map(len, shares.values())) == len(clients), shares
victim: Final = min((pid for pid in workers if shares[pid]), key=lambda pid: len(shares[pid]))
with owned.replacing_worker(victim) as kill:
kill()
release.set()
results: Final = tuple(future.result(timeout=60) for future in futures)
for port, result in zip(ports, results, strict=True):
assert result == (None if port in shares[victim] else (200, _PEER_COUNT)), (port, result, shares)
second_wave: Final = _clients(stack, str(proxy_url), 6)
assert tuple(_counted_on(client, gateway.key, _full(_OWNED_MODEL)) for client in second_wave) == (
(200, _PEER_COUNT),
) * len(second_wave)
assert len(_counted_bodies(wire)) == len(clients) + len(second_wave)
eventually(lambda: len(_STARTED_WORKER.findall(owned.log.read_text())), lambda started: started >= 3, 120)
assert owned.process.poll() is None
@pytest.mark.parametrize("stream", [False, True], ids=["non_stream", "stream"])
def test_chat_completions_on_the_same_deployment_still_generate(gateway: Gateway, stream: bool) -> None:
marker: Final = f"chat control {uuid.uuid4().hex}"
with wire_server(_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url)
response: Final = gateway.request(
"POST",
"/v1/chat/completions",
{
"model": model,
"messages": [{"role": "user", "content": marker}],
"stream": stream,
"cache": {"no-cache": True},
},
)
assert response.status_code == 200, response.text
assert _REPLY_TEXT in response.text, response.text
assert not stream or response.text.rstrip().endswith("data: [DONE]"), response.text
(sent,) = wire.drain()
assert sent.target == (_STREAM_TARGET if stream else _MESSAGE_TARGET), sent.target
assert marker in sent.body.decode(), sent.body
def test_messages_endpoint_on_the_same_deployment_still_generates(gateway: Gateway) -> None:
marker: Final = f"messages control {uuid.uuid4().hex}"
with wire_server(_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url)
response: Final = gateway.request(
"POST",
"/v1/messages",
{"model": model, "max_tokens": 16, "messages": [{"role": "user", "content": marker}]},
)
assert response.status_code == 200, response.text
assert _REPLY_TEXT in response.text, response.text
(sent,) = wire.drain()
assert sent.target == _MESSAGE_TARGET, sent.target
assert marker in sent.body.decode(), sent.body
def test_responses_endpoint_on_the_same_deployment_still_generates(gateway: Gateway) -> None:
marker: Final = f"responses control {uuid.uuid4().hex}"
with wire_server(_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url)
response: Final = gateway.request("POST", "/v1/responses", {"model": model, "input": marker})
assert response.status_code == 200, response.text
assert _REPLY_TEXT in response.text, response.text
(sent,) = wire.drain()
assert sent.target == _MESSAGE_TARGET, sent.target
assert marker in sent.body.decode(), sent.body
def test_responses_input_tokens_counts_the_structured_output_responses_inference_sends(gateway: Gateway) -> None:
request_body: Final = RESPONSES_REQUEST_OPTIONS["text-format"]
with wire_server(_peer(_strict)) as wire, gateway.scenario() as scenario:
model: Final = _deployment(gateway, scenario, wire.url)
generated: Final = gateway.request(
"POST", "/v1/responses", {**request_body, "model": model, "max_output_tokens": 4096}
)
assert generated.status_code == 200, generated.text
response: Final = gateway.request("POST", "/v1/responses/input_tokens", {**request_body, "model": model})
assert _payload(response)["input_tokens"] == _PEER_COUNT, response.text
requests: Final = wire.drain()
(inference,) = (request for request in requests if request.target == _MESSAGE_TARGET)
(counted,) = _count_bodies(requests)
expected: Final = inference_count_fields(_JSON_OBJECT.validate_json(inference.body))
assert count_request_fields(counted) == expected, (counted, expected)