litellm/tests/integration/providers/test_count_tokens_model_wire.py
Mateo Wang 75faaeab64
feat(count_tokens)!: configurable token_count_framing and token_count_model, count source in the response (#46777)
* feat(count_tokens): configurable token_count_framing and token_count_fallback_model, count source in the response

Count endpoints now say where each number came from in litellm_count_source
(provider, fallback_model, local_estimate) and name the fallback model in
litellm_count_fallback_model. When the provider answers 400 or 404 for a model
it cannot count, the proxy counts the same request with token_count_fallback_model.
token_count_framing adds a per-model constant to local estimates. Both settings
ship in the cost map for Bedrock Claude Sonnet/Haiku 5.5, Bedrock Kimi K3, and
Databricks Kimi K3 and DeepSeek V4 Flash, and can be overridden in model_info.

* test(count_tokens): classify the new cost map keys in the schema generator and expect count_source in exact-shape tests

* test(count_tokens): e2e matrix against real inference and integration wire tests for count source, fallback and framing

* fix(count_tokens): keep the provider error when local counting is disabled, name the fallback error, keep settings in cooldown

Moves the cost map lookup off the event loop

* ci(cost map): bound token_count_framing to non-negative and token_count_fallback_model to non-empty

* fix(count_tokens): keep a failed fallback's own status, ship fallbacks only where they count, add system.ai framing

A fallback model that answers 404 returns the requested model's error unchanged, and any other fallback
failure answers with its own status and message. The eu, apac, au and jp Sonnet and Haiku 5.5 entries
no longer ship the Opus 5.5 fallback, which AWS serves only in us-east-1. A configured empty
token_count_fallback_model turns the shipped fallback off. Config framing accepts whole-number floats and
rejects negatives. The Databricks system.ai Kimi K3 and DeepSeek V4 Flash names carry the same framing as
their serving endpoint names, and the e2e matrix runs on Bedrock Haiku 5.5 too.

* test(databricks): list the system.ai Kimi K3 and DeepSeek V4 Flash names with their published DBU rates

* fix(count_tokens): skip a token count fallback model for an hour after it answers 404 on that deployment

A us. or global. Sonnet 5.5 deployment outside us-east-1 paid two extra Bedrock
calls and about 200 ms on every count, since Opus 5.5 cannot count there either.
The Kimi K3 e2e also runs the large system prompt shape now.

* test(count_tokens): use the shared integration port allocator in the fallback wire test

* fix(cost map): keep the Databricks system.ai entries to framing only, so their spend stays as today

* test(count_tokens): cover the fallback 404 skip, inference profile base_model, shipped framing, and deployment-scoped token count settings

Adds integration cells for a fallback model that answers 404, an application inference profile ARN deployment whose base_model carries the shipped fallback, and the shipped Kimi K3 framing with token_count_framing 0 turning it off. Adds unit tests that keep token count settings off the shared backend cost map key and that store a token count setting on PATCH only when the operator changed it

* fix(count_tokens): keep the counter's own 400 and the provider's error when a framed estimate cannot read the body

A 400 the OpenAI or Gemini counter returns before calling the provider is the caller's answer, so it no longer tries the fallback model or a framed estimate. When the framed local estimate cannot read a content block, the client gets the provider's error instead of a 500. Adds /audit round 2's integration tests for the fallback leg's converted body, the Anthropic envelope on fallback failures, and both framing cases.

* fix(count_tokens): answer the provider's error whenever a framed estimate raises

A tool schema the local estimate cannot format, such as a boolean subschema or an array with no items, raised AttributeError or KeyError and answered a 500 on a framed deployment whose provider had already answered 400. Any estimate failure now answers the provider's own error.

* fix(count_tokens): load the custom tokenizer inside the guarded framed estimate

A framed deployment whose custom_tokenizer could not load answered a 500 or 503 where the provider had already answered 400, because the tokenizer was selected outside the estimate's error handling. Selecting it inside answers the provider's error, logs a warning with the estimate's error, and still raises the Rust extension's fork-guard errors.

* fix(count_tokens): look up framing under the deployment's custom_llm_provider

* fix(count_tokens): share one tokenizer download per worker and bound the wait after a provider error

A framed deployment whose custom_tokenizer is not cached locally made every
count wait out the Hugging Face hub's whole retry cycle when the hub could not
be reached, and a burst of those loads held the default thread limiter that
every count's cost map lookup also needs. Counts on a worker now share one
download per minute, a failed download is reused for the rest of that minute,
and after a provider error a count waits at most 5 s before answering the
provider's error. The swallowed-estimate warning logs only the error type.

* fix(count_tokens): keep one tokenizer download per key running and load the local route per request

* test(count_tokens): type the scripted counter and wait on events instead of wall time where possible

* fix(count_tokens): run framed tokenizer loads on their own capped threads and answer a tokenizer panic with the provider error

Framed loads used anyio's default 40 threads, so a hub that drops packets let about 40 hanging loads stall every count on the worker. Loads now run on a per-worker pool of 4, and a count that finds 4 loads running answers the provider's error at once. Any finished load is reused for a minute after it finishes, so a load that fell back to tiktoken is not retried on every count. A pyo3 PanicException from the tokenizer, while loading or counting, now answers the provider's error instead of a plain 500

* feat(proxy): count tokens with token_count_model in its pinned region

token_count_model {model, region} replaces token_count_fallback_model. Bedrock Sonnet and Haiku 5.5 count with Opus 5.5 in us-east-1, or Melbourne for au. profiles, and a failure names the region to enable

* fix(count_tokens): use the renamed count model constant in the e2e test and list token_count_model with the object keys

* fix(proxy): keep the upstream error envelope when adding the region hint

* test(proxy): annotate the token count setting echo test

* ci(cost map): require non-blank token_count_model strings and test the region rules

* fix(count_tokens): answer the token_count_model's error even when framing is set

* fix(count_tokens): keep the provider error for Google contents instead of a framed estimate

* fix(count_tokens): estimate Google contents the route translated to messages

* fix(count_tokens): keep the provider error for Google contents with non-text parts

* test(proxy): drive tokenizer wait and cost map tests by events and a skippable loop clock

* fix(count_tokens): check Google text-only parts without a KeysView comparison basedpyright flags

* feat(proxy): count Bedrock Kimi K3 tokens exactly with its pinned tokenizer

* test(bedrock): keep the Kimi K3 image check in-process

* fix(count_tokens): join the region hint to an upstream message that already ends with a period

Also pins that a blank token_count_model in config falls through to the cost map default

* fix(count_tokens): count Gemini tool-call turns and Responses tools, and stop tokenizer retries from stalling counts

The Google countTokens route estimates contents with functionCall, functionResponse and thoughtSignature parts again, and still answers the provider error for media, thought and unknown parts

The Kimi K3 exact count renders Responses API and Anthropic tools through the chat tool translation the Vertex count already used, now shared from the prompt factory, and falls back to the estimate when the Converse body cannot carry a tool

A failed Kimi K3 tokenizer load is retried in the background without making counts wait, and a load still running after the wait logs a warning

* fix(count_tokens): count Kimi K3 tools the way Bedrock inference sends them

A flat Responses API function or custom tool becomes the chat tool the
Responses route sends to inference, and every other tool goes to the
Converse body as is, so hosted tools Bedrock drops are left out of the
count instead of being counted or turning it into an estimate.

* test(count_tokens): annotate new token counter test locals with Final

* fix(count_tokens): keep shipped count regions inside the deployment's geography

A plain Bedrock Sonnet or Haiku 5.5 deployment in a non-US region no longer sends its count prompt to us-east-1. Global count models and configured token_count_model keep their pinned region. Also expect litellm_count_source in the Vertex Llama count test main added

* fix(count_tokens): resolve count settings off the event loop
2026-10-17 17:11:27 -07:00

719 lines
30 KiB
Python

import json
import re
from collections.abc import Callable, Iterator, Mapping
from itertools import chain
from pathlib import Path
from typing import Final
from uuid import uuid4
import anthropic
import httpx
import pytest
from anthropic.types import MessageCountTokensToolParam, MessageTokensCount
from integration._support.bedrock_runtime_peer import target_of
from integration._support.client import Gateway, Scenario, gateway_from_environment
from integration._support.ports import allocate_port
from integration._support.process import OwnedProxy, 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 pydantic import JsonValue, TypeAdapter
pytestmark = pytest.mark.timeout(2 * graceful_stop_seconds() + 120)
_SONNET: Final = "us.anthropic.claude-sonnet-5-5"
_SONNET_BASE: Final = "anthropic.claude-sonnet-5-5"
_OPUS: Final = "us.anthropic.claude-opus-5-5"
_OPUS_BASE: Final = "anthropic.claude-opus-5-5"
_NOVA: Final = "amazon.nova-lite-v1:0"
_KIMI: Final = "us.moonshotai.kimi-k3"
_KIMI_BASE: Final = "moonshotai.kimi-k3"
_PROFILE_ID: Final = "lit9563count"
_PROFILE_ARN: Final = f"arn:aws:bedrock:us-east-1:123456789012:application-inference-profile/{_PROFILE_ID}"
_COST_MAP_PATH: Final = Path(__file__).resolve().parents[3] / "model_prices_and_context_window.json"
_REGION: Final = "us-west-2"
_COUNT_REGION: Final = "us-east-1"
_ACCESS_KEY: Final = "AKIAINTEGRATIONCOUNT"
_SECRET_KEY: Final = "integration-count-secret"
_PROVIDER_COUNT: Final = 1345
_COUNT_MODEL_COUNT: Final = 2718
_FRAMING: Final = 50
_UNSUPPORTED: Final = "The provided model doesn't support counting tokens."
_COUNT_TARGET: Final = re.compile(r"^/model/(.+)/count-tokens$")
_SCRIPTED_STATUS: Final = re.compile(r"status=(\d{3})")
_SCRIPTED_COUNT_MODEL_STATUS: Final = re.compile(r"count_model=(\d{3})")
_CREDENTIAL: Final = re.compile(r"Credential=([^/]+)/\d+/([^/]+)/")
_JSON_OBJECT: Final = TypeAdapter(dict[str, JsonValue])
_COST_MAP: Final = TypeAdapter(dict[str, dict[str, JsonValue]])
_SYSTEM: Final = "You are a terse assistant that answers in one sentence"
_WEATHER_SCHEMA: Final[dict[str, JsonValue]] = {
"type": "object",
"properties": {"city": {"type": "string", "description": "City to look up"}},
"required": ["city"],
}
_SDK_TOOLS: Final[list[MessageCountTokensToolParam]] = [
{
"name": "get_weather",
"description": "Look up the current weather for a city",
"input_schema": {
"type": "object",
"properties": {"city": {"type": "string", "description": "City to look up"}},
"required": ["city"],
},
}
]
_COUNT_MODEL_SOURCE: Final = {"litellm_count_source": "token_count_model", "litellm_count_model": _OPUS}
_OPENAI_PRIMARY: Final = "gpt-count-primary"
_OPENAI_COUNT_MODEL: Final = "gpt-count-model"
_OPENAI_COUNT_TARGET: Final = "/v1/responses/input_tokens"
_COUNTED_FIELDS: Final = ("input", "instructions", "tools", "text")
_ANSWER_SCHEMA: Final[dict[str, JsonValue]] = {"type": "object", "properties": {"answer": {"type": "string"}}}
_ANTHROPIC_TOOL: Final[dict[str, JsonValue]] = {
"name": "get_weather",
"description": "Look up the current weather for a city",
"input_schema": _WEATHER_SCHEMA,
}
_TOOL_TURNS: Final[list[JsonValue]] = [
{"role": "user", "content": "What is the weather in Paris?"},
{
"role": "assistant",
"content": [{"type": "tool_use", "id": "toolu_01", "name": "get_weather", "input": {"city": "Paris"}}],
},
{"role": "user", "content": [{"type": "tool_result", "tool_use_id": "toolu_01", "content": "Sunny"}]},
]
_LOCAL_SOURCE: Final = {"litellm_count_source": "local_estimate"}
def _prompt(status: int, count_model_status: int | None = None) -> str:
scripted: Final = "" if count_model_status is None else f" count_model={count_model_status}"
return f"Count this message status={status}{scripted}"
def _converse_body(prompt: str) -> dict[str, JsonValue]:
return {
"input": {
"converse": {
"messages": [{"role": "user", "content": [{"text": prompt}]}],
"system": [{"text": _SYSTEM}],
"toolConfig": {
"tools": [
{
"toolSpec": {
"name": "get_weather",
"description": "Look up the current weather for a city",
"inputSchema": {"json": _WEATHER_SCHEMA},
}
}
]
},
}
}
}
def _json_reply(status: int, payload: Mapping[str, JsonValue]) -> Reply:
return Reply(status=status, body=json.dumps(payload).encode())
def _counted_model(request: Request) -> str:
counted: Final = _COUNT_TARGET.match(target_of(request))
return target_of(request) if counted is None else counted.group(1)
def _scripted(pattern: re.Pattern[str], request: Request, default: int) -> int:
scripted: Final = pattern.search(request.body.decode(errors="replace"))
return default if scripted is None else int(scripted.group(1))
def _runtime(request: Request) -> Reply:
model: Final = _counted_model(request)
if model == _OPUS_BASE:
count_model_status: Final = _scripted(_SCRIPTED_COUNT_MODEL_STATUS, request, 200)
if count_model_status == 200:
return _json_reply(200, {"inputTokens": _COUNT_MODEL_COUNT})
return _json_reply(count_model_status, {"message": f"scripted runtime {count_model_status} for {model}"})
status: Final = _scripted(_SCRIPTED_STATUS, request, 500)
if status == 200:
return _json_reply(200, {"inputTokens": _PROVIDER_COUNT})
return _json_reply(status, {"message": _UNSUPPORTED if status == 400 else f"scripted runtime {status}"})
def _mantle(request: Request) -> Reply:
model: Final = _JSON_OBJECT.validate_json(request.body).get("model")
return _json_reply(
404, {"type": "error", "error": {"type": "not_found_error", "message": f"scripted mantle 404 for {model}"}}
)
def _signer(request: Request) -> tuple[str, str]:
credential: Final = _CREDENTIAL.search(request.headers.get("authorization", ""))
return ("", "") if credential is None else (credential.group(1), credential.group(2))
def _runtime_counts(wire: Wire) -> tuple[tuple[str, tuple[str, str], dict[str, JsonValue]], ...]:
return tuple(
(_counted_model(request), _signer(request), _JSON_OBJECT.validate_json(request.body))
for request in wire.drain()
)
def _runtime_models(wire: Wire) -> tuple[str, ...]:
return tuple(_counted_model(request) for request in wire.drain())
def _mantle_models(wire: Wire) -> tuple[JsonValue, ...]:
return tuple(_JSON_OBJECT.validate_json(request.body).get("model") for request in wire.drain())
def _url(port: int) -> str:
return f"http://127.0.0.1:{port}"
def _litellm_params(model: str, api_base: str) -> dict[str, JsonValue]:
return {
"model": f"bedrock/{model}",
"api_base": api_base,
"aws_access_key_id": _ACCESS_KEY,
"aws_secret_access_key": _SECRET_KEY,
"aws_region_name": _REGION,
}
@pytest.fixture(scope="module")
def runtime_port() -> int:
return allocate_port()
@pytest.fixture(scope="module")
def mantle_port() -> int:
return allocate_port()
@pytest.fixture(scope="module")
def counting_proxy(tmp_path_factory: pytest.TempPathFactory, mantle_port: int) -> Iterator[OwnedProxy]:
with (
gateway_from_environment() as gateway,
owned_proxy_process(
gateway,
tmp_path_factory.mktemp("count-model"),
{
"BEDROCK_MANTLE_API_BASE": _url(mantle_port),
"LITELLM_LOCAL_MODEL_COST_MAP": "True",
"HF_HUB_OFFLINE": "1",
},
workers=1,
remove_environment=("AWS_BEARER_TOKEN_BEDROCK",),
) as owned,
):
yield owned
def _deployment(
scenario: Scenario, runtime_port: int, model: str = _SONNET, model_info: Mapping[str, JsonValue] | None = None
) -> str:
return scenario.model(model_info=model_info, api_key=None, **_litellm_params(model, _url(runtime_port)))
def _count(gateway: Gateway, model: str, status: int, count_model_status: int | None = None) -> MessageTokensCount:
with anthropic.Anthropic(base_url=str(gateway.client.base_url), api_key=gateway.key, max_retries=0) as client:
return client.messages.count_tokens(
model=model,
messages=[{"role": "user", "content": _prompt(status, count_model_status)}],
system=_SYSTEM,
tools=_SDK_TOOLS,
)
def _messages_estimate(gateway: Gateway, model: str) -> tuple[int, JsonValue]:
counted: Final = _count(gateway, model, 400)
return counted.input_tokens, (counted.model_extra or {}).get("litellm_count_source")
def _utils_estimate(gateway: Gateway, model: str) -> tuple[int, JsonValue]:
response: Final = gateway.request(
"POST", "/utils/token_counter", {"model": model, "messages": [{"role": "user", "content": _prompt(400)}]}
)
assert response.status_code == 200, response.text
body: Final = _JSON_OBJECT.validate_json(response.content)
total: Final = body["total_tokens"]
assert isinstance(total, int), body
return total, body.get("count_source")
def _shipped_framing(model: str) -> JsonValue:
return _COST_MAP.validate_json(_COST_MAP_PATH.read_bytes())[model].get("token_count_framing")
def test_shipped_token_count_model_counts_the_request_in_its_region_without_trying_the_model(
counting_proxy: OwnedProxy, runtime_port: int, mantle_port: int
) -> None:
gateway: Final = counting_proxy.gateway
with (
wire_server(_runtime, port=runtime_port) as runtime,
wire_server(_mantle, port=mantle_port) as mantle,
gateway.scenario() as scenario,
):
counted: Final = _count(gateway, _deployment(scenario, runtime_port), 404)
assert (counted.input_tokens, counted.model_extra) == (_COUNT_MODEL_COUNT, _COUNT_MODEL_SOURCE), counted
assert _runtime_counts(runtime) == ((_OPUS_BASE, (_ACCESS_KEY, _COUNT_REGION), _converse_body(_prompt(404))),)
assert _mantle_models(mantle) == ()
@pytest.mark.parametrize(
("count_model_status", "error", "message"),
[
(401, anthropic.AuthenticationError, f"scripted runtime 401 for {_OPUS_BASE}"),
(429, anthropic.RateLimitError, f"scripted runtime 429 for {_OPUS_BASE}"),
(404, anthropic.NotFoundError, f"scripted mantle 404 for {_OPUS_BASE}"),
],
)
def test_token_count_model_failure_answers_its_error_and_names_the_region_to_enable(
counting_proxy: OwnedProxy,
runtime_port: int,
mantle_port: int,
count_model_status: int,
error: type[anthropic.APIStatusError],
message: str,
) -> None:
gateway: Final = counting_proxy.gateway
with (
wire_server(_runtime, port=runtime_port) as runtime,
wire_server(_mantle, port=mantle_port) as mantle,
gateway.scenario() as scenario,
):
with pytest.raises(error) as refused:
_count(gateway, _deployment(scenario, runtime_port), 200, count_model_status=count_model_status)
assert message in str(refused.value) and f"{_OPUS} in {_COUNT_REGION}" in str(refused.value), refused.value
assert _runtime_models(runtime) == (_OPUS_BASE,)
assert _mantle_models(mantle) == ((_OPUS_BASE,) if count_model_status == 404 else ())
def test_framing_is_added_to_the_local_estimate_when_the_runtime_cannot_count(
counting_proxy: OwnedProxy, runtime_port: int, mantle_port: int
) -> None:
gateway: Final = counting_proxy.gateway
with (
wire_server(_runtime, port=runtime_port) as runtime,
wire_server(_mantle, port=mantle_port) as mantle,
gateway.scenario() as scenario,
):
framed: Final = _count(
gateway, _deployment(scenario, runtime_port, _NOVA, {"token_count_framing": _FRAMING}), 400
)
unframed: Final = _count(gateway, _deployment(scenario, runtime_port, _NOVA, {"token_count_framing": 0}), 400)
assert (framed.model_extra, unframed.model_extra) == (_LOCAL_SOURCE, _LOCAL_SOURCE), (framed, unframed)
assert framed.input_tokens - unframed.input_tokens == _FRAMING, (framed, unframed)
assert _runtime_models(runtime) == (_NOVA, _NOVA)
assert _mantle_models(mantle) == ()
def test_framing_on_one_deployment_leaves_a_sibling_of_the_same_model_raising_the_400(
counting_proxy: OwnedProxy, runtime_port: int, mantle_port: int
) -> None:
gateway: Final = counting_proxy.gateway
with (
wire_server(_runtime, port=runtime_port) as runtime,
wire_server(_mantle, port=mantle_port) as mantle,
gateway.scenario() as scenario,
):
framed: Final = _deployment(scenario, runtime_port, _NOVA, {"token_count_framing": _FRAMING})
sibling: Final = _deployment(scenario, runtime_port, _NOVA)
assert _count(gateway, framed, 400).model_extra == _LOCAL_SOURCE
with pytest.raises(anthropic.BadRequestError, match=re.escape(_UNSUPPORTED)):
_count(gateway, sibling, 400)
assert _runtime_models(runtime) == (_NOVA, _NOVA)
assert _mantle_models(mantle) == ()
def test_empty_configured_token_count_model_counts_the_model_itself_in_the_deployment_region(
counting_proxy: OwnedProxy, runtime_port: int, mantle_port: int
) -> None:
gateway: Final = counting_proxy.gateway
with (
wire_server(_runtime, port=runtime_port) as runtime,
wire_server(_mantle, port=mantle_port) as mantle,
gateway.scenario() as scenario,
):
counted: Final = _count(gateway, _deployment(scenario, runtime_port, model_info={"token_count_model": {}}), 200)
assert (counted.input_tokens, counted.model_extra) == (_PROVIDER_COUNT, {"litellm_count_source": "provider"})
assert _runtime_counts(runtime) == ((_SONNET_BASE, (_ACCESS_KEY, _REGION), _converse_body(_prompt(200))),)
assert _mantle_models(mantle) == ()
@pytest.mark.parametrize("base_model_in", ["model_info", "litellm_params"])
def test_inference_profile_arn_counts_with_the_token_count_model_of_its_base_model(
counting_proxy: OwnedProxy, runtime_port: int, mantle_port: int, base_model_in: str
) -> None:
gateway: Final = counting_proxy.gateway
base_model: Final[dict[str, JsonValue]] = {"base_model": f"bedrock/{_SONNET}"}
with (
wire_server(_runtime, port=runtime_port) as runtime,
wire_server(_mantle, port=mantle_port) as mantle,
gateway.scenario() as scenario,
):
model: Final = scenario.model(
model_info=base_model if base_model_in == "model_info" else None,
api_key=None,
**_litellm_params(_PROFILE_ARN, _url(runtime_port)),
**(base_model if base_model_in == "litellm_params" else {}),
)
counted: Final = _count(gateway, model, 404)
assert (counted.input_tokens, counted.model_extra) == (_COUNT_MODEL_COUNT, _COUNT_MODEL_SOURCE), counted
assert _runtime_models(runtime) == (_OPUS_BASE,)
assert _mantle_models(mantle) == ()
@pytest.mark.parametrize(
("estimate", "runtime_calls"),
[(_messages_estimate, (_KIMI_BASE, _KIMI_BASE)), (_utils_estimate, ())],
ids=["messages_count_tokens", "utils_token_counter"],
)
def test_shipped_framing_is_added_to_the_estimate_and_a_configured_zero_turns_it_off(
counting_proxy: OwnedProxy,
runtime_port: int,
mantle_port: int,
estimate: Callable[[Gateway, str], tuple[int, JsonValue]],
runtime_calls: tuple[str, ...],
) -> None:
gateway: Final = counting_proxy.gateway
with (
wire_server(_runtime, port=runtime_port) as runtime,
wire_server(_mantle, port=mantle_port) as mantle,
gateway.scenario() as scenario,
):
shipped: Final = estimate(gateway, _deployment(scenario, runtime_port, _KIMI))
unframed: Final = estimate(gateway, _deployment(scenario, runtime_port, _KIMI, {"token_count_framing": 0}))
assert (shipped[1], unframed[1]) == ("local_estimate", "local_estimate"), (shipped, unframed)
framing: Final = _shipped_framing(_KIMI)
assert isinstance(framing, int) and framing > 0, framing
assert shipped[0] - unframed[0] == framing, (shipped, unframed)
assert _runtime_models(runtime) == runtime_calls
assert _mantle_models(mantle) == ()
def _openai_count(request: Request) -> Reply:
model: Final = _JSON_OBJECT.validate_json(request.body).get("model")
if request.target == _OPENAI_COUNT_TARGET and model == _OPENAI_COUNT_MODEL:
return _json_reply(200, {"object": "response.input_tokens", "input_tokens": _COUNT_MODEL_COUNT})
return _json_reply(
404,
{"error": {"message": f"scripted openai 404 for {model}", "type": "invalid_request_error", "code": None}},
)
def _counted_fields(body: Mapping[str, JsonValue]) -> dict[str, JsonValue]:
return {key: body[key] for key in _COUNTED_FIELDS if key in body}
def _openai_counts(wire: Wire) -> tuple[tuple[str, JsonValue, dict[str, JsonValue]], ...]:
bodies: Final = tuple((request.target, _JSON_OBJECT.validate_json(request.body)) for request in wire.drain())
return tuple((target, body.get("model"), _counted_fields(body)) for target, body in bodies)
def _openai_deployment(scenario: Scenario, wire: Wire, model_info: Mapping[str, JsonValue]) -> str:
return scenario.model(model=f"openai/{_OPENAI_PRIMARY}", api_base=f"{wire.url}/v1", model_info=model_info)
def test_token_count_model_counts_the_anthropic_body_converted_like_the_provider_count(
counting_proxy: OwnedProxy,
) -> None:
gateway: Final = counting_proxy.gateway
with wire_server(_openai_count) as openai, gateway.scenario() as scenario:
model: Final = _openai_deployment(scenario, openai, {"token_count_model": {"model": _OPENAI_COUNT_MODEL}})
response: Final = gateway.request(
"POST",
"/v1/messages/count_tokens",
{
"model": model,
"system": _SYSTEM,
"tools": [_ANTHROPIC_TOOL],
"output_config": {"format": {"type": "json_schema", "schema": _ANSWER_SCHEMA}},
"messages": _TOOL_TURNS,
},
)
assert response.status_code == 200, response.text
assert _JSON_OBJECT.validate_json(response.content) == {
"input_tokens": _COUNT_MODEL_COUNT,
"litellm_count_source": "token_count_model",
"litellm_count_model": _OPENAI_COUNT_MODEL,
}
counts: Final = _openai_counts(openai)
assert tuple((target, counted) for target, counted, _ in counts) == (
(_OPENAI_COUNT_TARGET, _OPENAI_COUNT_MODEL),
), counts
primary: Final = counts[0][2]
assert primary["instructions"] == _SYSTEM, primary
text: Final = primary.get("text")
assert isinstance(text, dict) and isinstance(text.get("format"), dict), primary
input_items: Final = primary["input"]
assert isinstance(input_items, list), primary
item_types: Final = {item.get("type") for item in input_items if isinstance(item, dict)}
assert {"function_call", "function_call_output"} <= item_types, input_items
@pytest.mark.parametrize(
("body", "named"),
[
(
{
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "Summarize the notes."},
{
"type": "search_result",
"source": "https://example.com/notes",
"title": "Notes",
"content": [],
},
],
}
]
},
"search_result",
),
(
{"messages": [{"role": "user", "content": "hello"}, {"content": "no role"}]},
"messages.1.role: Field required",
),
],
ids=["unconvertible_block", "missing_role"],
)
def test_framing_keeps_the_400_for_a_body_the_openai_count_rejects_before_calling_openai(
counting_proxy: OwnedProxy, body: Mapping[str, JsonValue], named: str
) -> None:
gateway: Final = counting_proxy.gateway
with wire_server(_openai_count) as openai, gateway.scenario() as scenario:
model: Final = _openai_deployment(scenario, openai, {"token_count_framing": _FRAMING})
rejected: Final = gateway.request("POST", "/v1/messages/count_tokens", {"model": model, **body})
assert rejected.status_code == 400, rejected.text
error: Final = _JSON_OBJECT.validate_json(rejected.content)["error"]
assert isinstance(error, dict) and named in str(error.get("message")), error
assert openai.drain() == ()
estimated: Final = gateway.request(
"POST", "/v1/messages/count_tokens", {"model": model, "messages": [{"role": "user", "content": "hello"}]}
)
assert estimated.status_code == 200, estimated.text
assert _JSON_OBJECT.validate_json(estimated.content).get("litellm_count_source") == "local_estimate"
assert tuple(counted for _, counted, _ in _openai_counts(openai)) == (_OPENAI_PRIMARY,)
def _runtime_cannot_count(_request: Request) -> Reply:
return _json_reply(400, {"message": _UNSUPPORTED})
_REPLAYED_WEB_SEARCH: Final[list[JsonValue]] = [
{"role": "user", "content": "What is the newest LiteLLM release?"},
{
"role": "assistant",
"content": [
{"type": "server_tool_use", "id": "srvtoolu_01", "name": "web_search", "input": {"query": "LiteLLM"}},
{
"type": "web_search_tool_result",
"tool_use_id": "srvtoolu_01",
"content": [
{
"type": "web_search_result",
"url": "https://docs.litellm.ai/release_notes",
"title": "Release notes",
"encrypted_content": "",
}
],
},
{"type": "text", "text": "The newest release is in the release notes."},
],
},
{"role": "user", "content": "Thanks."},
]
_BOOLEAN_SUBSCHEMA_TOOL: Final[dict[str, JsonValue]] = {
"name": "lookup",
"description": "Look up a value",
"input_schema": {"type": "object", "properties": {"query": True}},
}
_UNREADABLE_BODIES: Final[dict[str, dict[str, JsonValue]]] = {
"web_search_replay": {"messages": _REPLAYED_WEB_SEARCH},
"boolean_subschema_tool": {
"messages": [{"role": "user", "content": "Look up the value."}],
"tools": [_BOOLEAN_SUBSCHEMA_TOOL],
},
}
_COUNT_ENDPOINTS: Final[dict[str, tuple[str, str]]] = {
"messages_count_tokens": ("/v1/messages/count_tokens", "litellm_count_source"),
"utils_token_counter": ("/utils/token_counter?call_endpoint=true", "count_source"),
}
def _count_answer(response: httpx.Response, source_key: str) -> tuple[int, str]:
if response.status_code not in (200, 400):
return response.status_code, response.text
body: Final = _JSON_OBJECT.validate_json(response.content)
error: Final = body.get("error")
if response.status_code == 400 and isinstance(error, dict):
return 400, str(error.get("message"))
return response.status_code, str(body.get(source_key))
@pytest.mark.parametrize("endpoint", sorted(_COUNT_ENDPOINTS))
@pytest.mark.parametrize("unreadable", sorted(_UNREADABLE_BODIES))
def test_shipped_framing_never_answers_a_500_for_a_body_the_local_count_cannot_read(
counting_proxy: OwnedProxy, runtime_port: int, mantle_port: int, unreadable: str, endpoint: str
) -> None:
gateway: Final = counting_proxy.gateway
path, source_key = _COUNT_ENDPOINTS[endpoint]
with (
wire_server(_runtime_cannot_count, port=runtime_port) as runtime,
wire_server(_mantle, port=mantle_port) as mantle,
gateway.scenario() as scenario,
):
response: Final = gateway.request(
"POST", path, {"model": _deployment(scenario, runtime_port, _KIMI), **_UNREADABLE_BODIES[unreadable]}
)
status, answer = _count_answer(response, source_key)
assert (status, answer) == (200, "local_estimate") or (status == 400 and _UNSUPPORTED in answer), (
response.status_code,
response.text,
)
assert _runtime_models(runtime) == (_KIMI_BASE,)
assert _mantle_models(mantle) == ()
_GEMINI_MODEL: Final = "gemini-4-argon"
_GEMINI_COUNT_MODEL: Final = "gemini-3.8-flash"
_VERTEX_PROJECT: Final = "scripted-project"
_COMPACT_UNDER_THE_MINIMUM_TRIGGER: Final[dict[str, JsonValue]] = {
"edits": [{"type": "compact_20260112", "trigger": {"type": "input_tokens", "value": 1000}}]
}
_CLEAR_OLDER_TOOL_RESULTS: Final[dict[str, JsonValue]] = {
"edits": [
{
"type": "clear_tool_uses_20250919",
"trigger": {"type": "input_tokens", "value": 500},
"keep": {"type": "tool_uses", "value": 1},
}
]
}
_READ_FILE_TOOL: Final[dict[str, JsonValue]] = {
"name": "read_file",
"description": "Read a file",
"input_schema": {"type": "object", "properties": {"path": {"type": "string"}}},
}
_FILE_TEXT: Final = "line of a long file that the agent read earlier. " * 180
_FILES: Final = 3
def _google_cannot_count(_request: Request) -> Reply:
return _json_reply(400, {"error": {"code": 400, "message": "scripted count 400", "status": "INVALID_ARGUMENT"}})
def _google_counts_only_the_count_model(request: Request) -> Reply:
if request.target.endswith(f"/{_GEMINI_COUNT_MODEL}:countTokens"):
return _json_reply(200, {"totalTokens": _COUNT_MODEL_COUNT})
return _google_cannot_count(request)
def _google_deployment(
gateway: Gateway, scenario: Scenario, wire: Wire, provider: str, model_info: dict[str, JsonValue]
) -> str:
if provider == "gemini":
return scenario.model(
model=f"gemini/{_GEMINI_MODEL}", api_base=wire.url, api_key="scripted-gemini-key", model_info=model_info
)
return scenario.model(
model=f"vertex_ai/{_GEMINI_MODEL}",
api_base=wire.url,
api_key=None,
vertex_project=_VERTEX_PROJECT,
vertex_location="us-east5",
vertex_credentials=service_account_json(_VERTEX_PROJECT, gateway.upstream_url.rstrip("/")),
model_info=model_info,
)
def _google_count_targets(wire: Wire) -> tuple[bool, ...]:
return tuple(request.target.endswith(":countTokens") for request in wire.drain())
def _read_file_round(marker: str, index: int) -> tuple[JsonValue, ...]:
return (
{"role": "user", "content": f"Read file {index}."},
{
"role": "assistant",
"content": [
{"type": "tool_use", "id": f"toolu_{index}", "name": "read_file", "input": {"path": f"{index}.txt"}}
],
},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": f"toolu_{index}",
"content": f"<file {marker}-{index}>{_FILE_TEXT}",
}
],
},
)
def _agent_messages(marker: str) -> list[JsonValue]:
rounds: Final = tuple(chain.from_iterable(_read_file_round(marker, index) for index in range(_FILES)))
return [*rounds, {"role": "user", "content": "Summarize what you read."}]
def _counted_files(request: Request, marker: str) -> tuple[bool, tuple[bool, ...]]:
sent: Final = json.dumps(_JSON_OBJECT.validate_json(request.body))
return (
request.target.endswith(f"/{_GEMINI_COUNT_MODEL}:countTokens"),
tuple(f"<file {marker}-{index}>" in sent for index in range(_FILES)),
)
@pytest.mark.parametrize("provider", ["gemini", "vertex_ai"])
def test_framing_keeps_the_400_for_a_context_edit_the_gemini_count_rejects_before_calling_google(
counting_proxy: OwnedProxy, provider: str
) -> None:
gateway: Final = counting_proxy.gateway
hello: Final[list[JsonValue]] = [{"role": "user", "content": "hello"}]
with wire_server(_google_cannot_count) as google, gateway.scenario() as scenario:
model: Final = _google_deployment(gateway, scenario, google, provider, {"token_count_framing": _FRAMING})
rejected: Final = gateway.request(
"POST",
"/v1/messages/count_tokens",
{"model": model, "messages": hello, "context_management": _COMPACT_UNDER_THE_MINIMUM_TRIGGER},
)
assert rejected.status_code == 400, rejected.text
assert _google_count_targets(google) == ()
estimated: Final = gateway.request("POST", "/v1/messages/count_tokens", {"model": model, "messages": hello})
assert estimated.status_code == 200, estimated.text
assert _JSON_OBJECT.validate_json(estimated.content).get("litellm_count_source") == "local_estimate"
assert _google_count_targets(google) == (True,)
@pytest.mark.parametrize("provider", ["gemini", "vertex_ai"])
def test_token_count_model_counts_the_body_left_after_the_context_management_edits(
counting_proxy: OwnedProxy, provider: str
) -> None:
gateway: Final = counting_proxy.gateway
marker: Final = uuid4().hex
with wire_server(_google_counts_only_the_count_model) as google, gateway.scenario() as scenario:
model: Final = _google_deployment(
gateway, scenario, google, provider, {"token_count_model": {"model": _GEMINI_COUNT_MODEL}}
)
counted: Final = gateway.request(
"POST",
"/v1/messages/count_tokens",
{
"model": model,
"tools": [_READ_FILE_TOOL],
"messages": _agent_messages(marker),
"context_management": _CLEAR_OLDER_TOOL_RESULTS,
},
)
assert counted.status_code == 200, counted.text
assert _JSON_OBJECT.validate_json(counted.content) == {
"input_tokens": _COUNT_MODEL_COUNT,
"litellm_count_source": "token_count_model",
"litellm_count_model": _GEMINI_COUNT_MODEL,
}
assert tuple(_counted_files(request, marker) for request in google.drain()) == ((True, (False, False, True)),)