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
"""Served service_tier drives billing on streamed calls, complete and disconnected.
The scripted upstream answers OpenAI-compatible /chat/completions with SSE chunks
that carry service_tier "priority" and terminal usage. The deployment registers
distinct default and *_priority rates, so a bill computed on the wrong tier cannot
match the hand-computed expectation. /v1/messages deployments on hosted_vllm have
no anthropic-messages provider config, so they take the chat adapter: the
streamed response is an AnthropicStreamWrapper under AnthropicSSEStream, wrapped
by AnthropicMessagesStreamCacheWriter when litellm.cache is on and then by the
router's FallbackAwareAnthropicMessagesStream; each layer must delegate the
inner stream's chunks for disconnect billing to find them.
Azure streams run the same OpenAI chunk path against /openai/deployments, so the
served tier must reach the spend row there too (LIT-2850). Databricks streams go
through DatabricksChatResponseIterator.chunk_parser and the databricks branch of
cost_per_token (LIT-8121). The responses bridge relays Responses API SSE as chat
chunks, so the served tier remembered from response.created must land on both
the chunks and the row. Gemini reports capacity as usageMetadata.trafficType, which maps to
service_tier "flex" and the *_flex rates (LIT-6287, LIT-6292).
"""
import json
from collections.abc import Callable
from hashlib import sha256
from typing import Final
from uuid import uuid4
import pytest
from integration._support.client import Gateway, Scenario, eventually, object_value
from integration._support.database import read_rows
from integration._support.wire import Reply, Request, Wire, wire_server
from pydantic import JsonValue
PROMPT_TOKENS: Final = 30
COMPLETION_TOKENS: Final = 40
INPUT_RATE: Final = 0.001
OUTPUT_RATE: Final = 0.002
PRIORITY_INPUT_RATE: Final = 0.01
PRIORITY_OUTPUT_RATE: Final = 0.02
EXPECTED_FULL_SPEND: Final = PROMPT_TOKENS * PRIORITY_INPUT_RATE + COMPLETION_TOKENS * PRIORITY_OUTPUT_RATE
FLEX_INPUT_RATE: Final = 0.0005
FLEX_OUTPUT_RATE: Final = 0.001
EXPECTED_FLEX_SPEND: Final = PROMPT_TOKENS * FLEX_INPUT_RATE + COMPLETION_TOKENS * FLEX_OUTPUT_RATE
def _sse_frame(payload: dict[str, JsonValue]) -> bytes:
return f"data: {json.dumps(payload, separators=(',', ':'))}\n\n".encode()
def _chat_chunk(request_id: str, upstream_model: str, content: str, served_tier: str) -> dict[str, JsonValue]:
return {
"id": request_id,
"object": "chat.completion.chunk",
"created": 1,
"model": upstream_model,
"service_tier": served_tier,
"choices": [{"index": 0, "delta": {"role": "assistant", "content": content}, "finish_reason": None}],
}
def _respond_for(
request_id: str,
prompt: str,
*,
expected_target: str = "/v1/chat/completions",
pause: float = 0.4,
served_tier: str = "priority",
expected_requested_tier: str | None = None,
) -> Callable[[Request], Reply]:
def respond(request: Request) -> Reply:
if request.target == "/v1/models":
return Reply(
body=json.dumps({"object": "list", "data": [{"id": "gpt-4o-mini", "object": "model"}]}).encode()
)
assert request.target.startswith(expected_target), request.target
body: Final = json.loads(request.body)
assert body["messages"] == [{"role": "user", "content": prompt}], body
if expected_requested_tier is not None:
assert body.get("service_tier") == expected_requested_tier, body
upstream_model: Final = str(body["model"])
terminal: Final[dict[str, JsonValue]] = {
"id": request_id,
"object": "chat.completion.chunk",
"created": 1,
"model": upstream_model,
"service_tier": served_tier,
"choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}],
"usage": {
"prompt_tokens": PROMPT_TOKENS,
"completion_tokens": COMPLETION_TOKENS,
"total_tokens": PROMPT_TOKENS + COMPLETION_TOKENS,
},
}
return Reply(
content_type="text/event-stream",
chunks=(
_sse_frame(_chat_chunk(request_id, upstream_model, "first", served_tier)),
_sse_frame(_chat_chunk(request_id, upstream_model, "second", served_tier)),
_sse_frame(_chat_chunk(request_id, upstream_model, "third", served_tier)),
_sse_frame(terminal),
b"data: [DONE]\n\n",
),
pause_between_chunks=pause,
)
return respond
def _tiered_model(
scenario: Scenario,
wire: Wire,
*,
litellm_model: str,
api_base: str | None = None,
**extra: JsonValue,
) -> str:
return scenario.model(
model=litellm_model,
api_base=api_base or f"{wire.url}/v1",
input_cost_per_token=INPUT_RATE,
output_cost_per_token=OUTPUT_RATE,
input_cost_per_token_priority=PRIORITY_INPUT_RATE,
output_cost_per_token_priority=PRIORITY_OUTPUT_RATE,
input_cost_per_token_flex=FLEX_INPUT_RATE,
output_cost_per_token_flex=FLEX_OUTPUT_RATE,
**extra,
)
def _events(lines: list[str]) -> list[dict[str, JsonValue]]:
return [
object_value(json.loads(line.removeprefix("data:")))
for line in lines
if line.startswith("data:") and line.removeprefix("data:").strip() != "[DONE]"
]
def _rows_for_key(key: str) -> list[dict[str, JsonValue]]:
return read_rows(
'SELECT request_id, status, prompt_tokens, completion_tokens, spend, metadata FROM "LiteLLM_SpendLogs" '
"WHERE api_key=%s",
(sha256(key.encode()).hexdigest(),),
)
def _single_spend_row(key: str) -> dict[str, JsonValue]:
rows: Final = eventually(lambda: _rows_for_key(key), lambda values: len(values) == 1, seconds=70)
return rows[0]
def _cost_breakdown(row: dict[str, JsonValue]) -> dict[str, JsonValue]:
metadata: Final = row["metadata"]
parsed: Final = json.loads(metadata) if isinstance(metadata, str) else object_value(metadata)
return object_value(parsed["cost_breakdown"])
@pytest.mark.timeout(120)
def test_completed_chat_stream_bills_the_served_tier(gateway: Gateway) -> None:
prompt: Final = f"tier control {uuid4().hex[:8]}"
request_id: Final = f"chatcmpl-{uuid4().hex[:8]}"
with (
wire_server(_respond_for(request_id, prompt)) as wire,
gateway.scenario() as scenario,
):
model: Final = _tiered_model(scenario, wire, litellm_model="openai/gpt-4o-mini")
key: Final = scenario.key(models=[model])
response: Final = gateway.request(
"POST",
"/v1/chat/completions",
{"model": model, "messages": [{"role": "user", "content": prompt}], "stream": True},
key=key,
)
assert response.status_code == 200, response.text
chunks: Final = _events(list(response.iter_lines()))
assert len(chunks) == 4, chunks
tiers: Final = {chunk.get("service_tier") for chunk in chunks}
assert tiers == {"priority"}, f"every relayed chunk must carry the served tier: {tiers}"
row: Final = _single_spend_row(key)
assert row["status"] == "success", row
assert row["request_id"] == request_id, row
assert row["prompt_tokens"] == PROMPT_TOKENS, row
assert row["completion_tokens"] == COMPLETION_TOKENS, row
assert float(str(row["spend"])) == pytest.approx(EXPECTED_FULL_SPEND), row
breakdown: Final = _cost_breakdown(row)
assert breakdown["service_tier"] == "priority", breakdown
assert len(wire.drain()) == 1
@pytest.mark.timeout(120)
def test_disconnected_chat_stream_bills_partial_usage_at_the_served_tier(gateway: Gateway) -> None:
prompt: Final = f"tier control {uuid4().hex[:8]}"
request_id: Final = f"chatcmpl-{uuid4().hex[:8]}"
with (
wire_server(_respond_for(request_id, prompt, pause=2.0)) as wire,
gateway.scenario() as scenario,
):
model: Final = _tiered_model(scenario, wire, litellm_model="openai/gpt-4o-mini")
key: Final = scenario.key(models=[model])
with gateway.client.stream(
"POST",
"/v1/chat/completions",
json={
"model": model,
"messages": [{"role": "user", "content": prompt}],
"stream": True,
},
headers={"Authorization": f"Bearer {key}"},
) as response:
assert response.status_code == 200, response.read().decode()
first_event: Final = next(line for line in response.iter_lines() if line.startswith("data:"))
assert object_value(json.loads(first_event.removeprefix("data:")))["id"] == request_id
row: Final = _single_spend_row(key)
assert row["status"] == "success", row
assert int(row["prompt_tokens"]) > 0, row
assert int(row["completion_tokens"]) == 1, row
assert float(str(row["spend"])) == pytest.approx(
int(row["prompt_tokens"]) * PRIORITY_INPUT_RATE + PRIORITY_OUTPUT_RATE
), row
breakdown: Final = _cost_breakdown(row)
assert breakdown["service_tier"] == "priority", breakdown
assert len(wire.drain()) == 1
@pytest.mark.timeout(120)
def test_completed_messages_stream_bills_the_served_tier(gateway: Gateway) -> None:
prompt: Final = f"tier control {uuid4().hex[:8]}"
with (
wire_server(_respond_for(f"chatcmpl-{uuid4().hex[:8]}", prompt)) as wire,
gateway.scenario() as scenario,
):
model: Final = _tiered_model(scenario, wire, litellm_model="hosted_vllm/gpt-4o-mini")
key: Final = scenario.key(models=[model])
with gateway.client.stream(
"POST",
"/v1/messages",
json={
"model": model,
"messages": [{"role": "user", "content": prompt}],
"max_tokens": COMPLETION_TOKENS,
"stream": True,
},
headers={"Authorization": f"Bearer {key}"},
) as response:
assert response.status_code == 200, response.read().decode()
events: Final = _events(list(response.iter_lines()))
assert events[0]["type"] == "message_start", events
assert any(event["type"] == "message_delta" for event in events), events
row: Final = _single_spend_row(key)
assert row["status"] == "success", row
assert row["prompt_tokens"] == PROMPT_TOKENS, row
assert row["completion_tokens"] == COMPLETION_TOKENS, row
assert float(str(row["spend"])) == pytest.approx(EXPECTED_FULL_SPEND), row
breakdown: Final = _cost_breakdown(row)
assert breakdown["service_tier"] == "priority", breakdown
assert len(wire.drain()) == 1
@pytest.mark.timeout(120)
def test_disconnected_messages_stream_bills_partial_usage_at_the_served_tier(gateway: Gateway) -> None:
prompt: Final = f"tier control {uuid4().hex[:8]}"
with (
wire_server(_respond_for(f"chatcmpl-{uuid4().hex[:8]}", prompt, pause=2.0)) as wire,
gateway.scenario() as scenario,
):
model: Final = _tiered_model(scenario, wire, litellm_model="hosted_vllm/gpt-4o-mini")
key: Final = scenario.key(models=[model])
with gateway.client.stream(
"POST",
"/v1/messages",
json={
"model": model,
"messages": [{"role": "user", "content": prompt}],
"max_tokens": COMPLETION_TOKENS,
"stream": True,
},
headers={"Authorization": f"Bearer {key}"},
) as response:
assert response.status_code == 200, response.read().decode()
first_event: Final = next(line for line in response.iter_lines() if line.startswith("data:"))
assert object_value(json.loads(first_event.removeprefix("data:")))["type"] == "message_start", first_event
row: Final = _single_spend_row(key)
assert row["status"] == "success", row
assert float(str(row["spend"])) > 0, row
assert int(row["completion_tokens"]) < COMPLETION_TOKENS, row
breakdown: Final = _cost_breakdown(row)
assert breakdown["service_tier"] == "priority", breakdown
assert len(wire.drain()) == 1
def _responses_frame(event: str, payload: dict[str, JsonValue]) -> bytes:
return f"event: {event}\ndata: {json.dumps(payload, separators=(',', ':'))}\n\n".encode()
def _respond_responses_for(response_id: str, prompt: str) -> Callable[[Request], Reply]:
def respond(request: Request) -> Reply:
assert request.target == "/v1/responses", request.target
body: Final = json.loads(request.body)
assert prompt in json.dumps(body["input"]), body["input"]
assert body["stream"] is True, body
upstream_model: Final = str(body["model"])
text: Final = "firstsecondthird"
response_payload: Final[dict[str, JsonValue]] = {
"id": response_id,
"object": "response",
"model": upstream_model,
"status": "in_progress",
"service_tier": "priority",
"output": [],
}
message_item: Final[dict[str, JsonValue]] = {
"type": "message",
"id": "msg_1",
"status": "completed",
"role": "assistant",
"content": [{"type": "output_text", "text": text, "annotations": []}],
}
return Reply(
content_type="text/event-stream",
chunks=(
_responses_frame("response.created", {"type": "response.created", "response": response_payload}),
_responses_frame(
"response.output_item.added",
{
"type": "response.output_item.added",
"output_index": 0,
"item": {
"type": "message",
"id": "msg_1",
"status": "in_progress",
"role": "assistant",
"content": [],
},
},
),
_responses_frame(
"response.content_part.added",
{
"type": "response.content_part.added",
"item_id": "msg_1",
"output_index": 0,
"content_index": 0,
"part": {"type": "output_text", "text": ""},
},
),
*(
_responses_frame(
"response.output_text.delta",
{
"type": "response.output_text.delta",
"item_id": "msg_1",
"output_index": 0,
"content_index": 0,
"delta": delta,
},
)
for delta in ("first", "second", "third")
),
_responses_frame(
"response.output_text.done",
{
"type": "response.output_text.done",
"item_id": "msg_1",
"output_index": 0,
"content_index": 0,
"text": text,
},
),
_responses_frame(
"response.content_part.done",
{
"type": "response.content_part.done",
"item_id": "msg_1",
"output_index": 0,
"content_index": 0,
"part": {"type": "output_text", "text": text},
},
),
_responses_frame(
"response.output_item.done",
{"type": "response.output_item.done", "output_index": 0, "item": message_item},
),
_responses_frame(
"response.completed",
{
"type": "response.completed",
"response": {
**response_payload,
"status": "completed",
"output": [message_item],
"usage": {
"input_tokens": PROMPT_TOKENS,
"output_tokens": COMPLETION_TOKENS,
"total_tokens": PROMPT_TOKENS + COMPLETION_TOKENS,
},
},
},
),
),
)
return respond
def _gemini_chunk(text: str) -> dict[str, JsonValue]:
return {"candidates": [{"index": 0, "content": {"role": "model", "parts": [{"text": text}]}}]}
def _respond_gemini_for(prompt: str) -> Callable[[Request], Reply]:
def respond(request: Request) -> Reply:
assert request.target.startswith("/models/gemini-2.5-flash:streamGenerateContent"), request.target
body: Final = json.loads(request.body)
assert prompt in json.dumps(body["contents"]), body["contents"]
terminal: Final[dict[str, JsonValue]] = {
"candidates": [{"index": 0, "content": {"role": "model", "parts": [{"text": ""}]}, "finishReason": "STOP"}],
"usageMetadata": {
"promptTokenCount": PROMPT_TOKENS,
"candidatesTokenCount": COMPLETION_TOKENS,
"totalTokenCount": PROMPT_TOKENS + COMPLETION_TOKENS,
"trafficType": "ON_DEMAND_FLEX",
},
}
return Reply(
content_type="text/event-stream",
chunks=(
_sse_frame(_gemini_chunk("first")),
_sse_frame(_gemini_chunk("second")),
_sse_frame(_gemini_chunk("third")),
_sse_frame(terminal),
),
)
return respond
@pytest.mark.timeout(120)
def test_azure_chat_stream_bills_the_served_tier(gateway: Gateway) -> None:
prompt: Final = f"tier control {uuid4().hex[:8]}"
request_id: Final = f"chatcmpl-{uuid4().hex[:8]}"
with (
wire_server(
_respond_for(request_id, prompt, expected_target="/openai/deployments/gpt-4o-mini/chat/completions")
) as wire,
gateway.scenario() as scenario,
):
model: Final = _tiered_model(
scenario,
wire,
litellm_model="azure/gpt-4o-mini",
api_base=wire.url,
api_version="2024-10-21",
)
key: Final = scenario.key(models=[model])
response: Final = gateway.request(
"POST",
"/v1/chat/completions",
{"model": model, "messages": [{"role": "user", "content": prompt}], "stream": True},
key=key,
)
assert response.status_code == 200, response.text
chunks: Final = _events(list(response.iter_lines()))
assert len(chunks) == 4, chunks
tiers: Final = {chunk.get("service_tier") for chunk in chunks}
assert tiers == {"priority"}, f"every relayed chunk must carry the served tier: {tiers}"
row: Final = _single_spend_row(key)
assert row["status"] == "success", row
assert row["prompt_tokens"] == PROMPT_TOKENS, row
assert row["completion_tokens"] == COMPLETION_TOKENS, row
assert float(str(row["spend"])) == pytest.approx(EXPECTED_FULL_SPEND), row
breakdown: Final = _cost_breakdown(row)
assert breakdown["service_tier"] == "priority", breakdown
assert len(wire.drain()) == 1
@pytest.mark.timeout(120)
def test_databricks_chat_stream_bills_the_served_tier(gateway: Gateway) -> None:
prompt: Final = f"tier control {uuid4().hex[:8]}"
request_id: Final = f"chatcmpl-{uuid4().hex[:8]}"
with (
wire_server(_respond_for(request_id, prompt, expected_target="/serving-endpoints/chat/completions")) as wire,
gateway.scenario() as scenario,
):
model: Final = _tiered_model(
scenario,
wire,
litellm_model="databricks/dbrx-instruct",
api_base=f"{wire.url}/serving-endpoints",
)
key: Final = scenario.key(models=[model])
response: Final = gateway.request(
"POST",
"/v1/chat/completions",
{"model": model, "messages": [{"role": "user", "content": prompt}], "stream": True},
key=key,
)
assert response.status_code == 200, response.text
chunks: Final = _events(list(response.iter_lines()))
assert len(chunks) == 4, chunks
tiers: Final = {chunk.get("service_tier") for chunk in chunks}
assert tiers == {"priority"}, f"every relayed chunk must carry the served tier: {tiers}"
row: Final = _single_spend_row(key)
assert row["status"] == "success", row
assert row["prompt_tokens"] == PROMPT_TOKENS, row
assert row["completion_tokens"] == COMPLETION_TOKENS, row
assert float(str(row["spend"])) == pytest.approx(EXPECTED_FULL_SPEND), row
breakdown: Final = _cost_breakdown(row)
assert breakdown["service_tier"] == "priority", breakdown
assert len(wire.drain()) == 1
@pytest.mark.timeout(120)
def test_responses_bridge_stream_bills_the_served_tier(gateway: Gateway) -> None:
prompt: Final = f"tier control {uuid4().hex[:8]}"
with (
wire_server(_respond_responses_for(f"resp_{uuid4().hex[:8]}", prompt)) as wire,
gateway.scenario() as scenario,
):
model: Final = _tiered_model(scenario, wire, litellm_model="openai/responses/gpt-4o-mini")
key: Final = scenario.key(models=[model])
response: Final = gateway.request(
"POST",
"/v1/chat/completions",
{"model": model, "messages": [{"role": "user", "content": prompt}], "stream": True},
key=key,
)
assert response.status_code == 200, response.text
chunks: Final = _events(list(response.iter_lines()))
assert len(chunks) >= 4, chunks
tiers: Final = {chunk.get("service_tier") for chunk in chunks}
assert tiers == {"priority"}, f"every relayed chunk must carry the served tier: {tiers}"
row: Final = _single_spend_row(key)
assert row["status"] == "success", row
assert row["prompt_tokens"] == PROMPT_TOKENS, row
assert row["completion_tokens"] == COMPLETION_TOKENS, row
assert float(str(row["spend"])) == pytest.approx(EXPECTED_FULL_SPEND), row
breakdown: Final = _cost_breakdown(row)
assert breakdown["service_tier"] == "priority", breakdown
assert len(wire.drain()) == 1
@pytest.mark.timeout(120)
def test_gemini_chat_stream_bills_the_flex_tier(gateway: Gateway) -> None:
prompt: Final = f"tier control {uuid4().hex[:8]}"
with (
wire_server(_respond_gemini_for(prompt)) as wire,
gateway.scenario() as scenario,
):
model: Final = _tiered_model(
scenario,
wire,
litellm_model="gemini/gemini-2.5-flash",
api_base=wire.url,
)
key: Final = scenario.key(models=[model])
response: Final = gateway.request(
"POST",
"/v1/chat/completions",
{"model": model, "messages": [{"role": "user", "content": prompt}], "stream": True},
key=key,
)
assert response.status_code == 200, response.text
chunks: Final = _events(list(response.iter_lines()))
assert len(chunks) >= 2, chunks
row: Final = _single_spend_row(key)
assert row["status"] == "success", row
assert row["prompt_tokens"] == PROMPT_TOKENS, row
assert row["completion_tokens"] == COMPLETION_TOKENS, row
assert float(str(row["spend"])) == pytest.approx(EXPECTED_FLEX_SPEND), row
breakdown: Final = _cost_breakdown(row)
assert breakdown["service_tier"] == "flex", breakdown
assert len(wire.drain()) == 1
@pytest.mark.timeout(120)
def test_requested_priority_downgraded_to_default_bills_base_rates(gateway: Gateway) -> None:
prompt: Final = f"tier control {uuid4().hex[:8]}"
request_id: Final = f"chatcmpl-{uuid4().hex[:8]}"
with (
wire_server(
_respond_for(request_id, prompt, served_tier="default", expected_requested_tier="priority")
) as wire,
gateway.scenario() as scenario,
):
model: Final = _tiered_model(scenario, wire, litellm_model="openai/gpt-4o-mini")
key: Final = scenario.key(models=[model])
response: Final = gateway.request(
"POST",
"/v1/chat/completions",
{
"model": model,
"messages": [{"role": "user", "content": prompt}],
"stream": True,
"service_tier": "priority",
},
key=key,
)
assert response.status_code == 200, response.text
chunks: Final = _events(list(response.iter_lines()))
assert len(chunks) == 4, chunks
tiers: Final = {chunk.get("service_tier") for chunk in chunks}
assert tiers == {"default"}, f"every relayed chunk must carry the served tier: {tiers}"
row: Final = _single_spend_row(key)
assert row["status"] == "success", row
assert row["request_id"] == request_id, row
assert row["prompt_tokens"] == PROMPT_TOKENS, row
assert row["completion_tokens"] == COMPLETION_TOKENS, row
assert float(str(row["spend"])) == pytest.approx(
PROMPT_TOKENS * INPUT_RATE + COMPLETION_TOKENS * OUTPUT_RATE
), row
breakdown: Final = _cost_breakdown(row)
assert breakdown.get("service_tier") != "priority", breakdown
assert len(wire.drain()) == 1
@pytest.mark.timeout(120)
def test_requested_priority_with_auto_echo_bills_priority(gateway: Gateway) -> None:
prompt: Final = f"tier control {uuid4().hex[:8]}"
request_id: Final = f"chatcmpl-{uuid4().hex[:8]}"
with (
wire_server(_respond_for(request_id, prompt, served_tier="auto")) as wire,
gateway.scenario() as scenario,
):
model: Final = _tiered_model(scenario, wire, litellm_model="openai/gpt-4o-mini")
key: Final = scenario.key(models=[model])
response: Final = gateway.request(
"POST",
"/v1/chat/completions",
{
"model": model,
"messages": [{"role": "user", "content": prompt}],
"stream": True,
"service_tier": "priority",
},
key=key,
)
assert response.status_code == 200, response.text
chunks: Final = _events(list(response.iter_lines()))
assert len(chunks) == 4, chunks
row: Final = _single_spend_row(key)
assert row["status"] == "success", row
assert row["request_id"] == request_id, row
assert float(str(row["spend"])) == pytest.approx(EXPECTED_FULL_SPEND), row
breakdown: Final = _cost_breakdown(row)
assert breakdown["service_tier"] == "priority", breakdown
assert len(wire.drain()) == 1