litellm/tests/integration/spend/test_disconnected_responses_stream_billing.py
martenj4 b099ac8118
fix(responses): bill the provider-reported input on a cut bridged stream (#53394)
* fix(responses): bill the provider-reported input on a cut bridged stream

A /v1/responses stream that runs through the chat bridge and is cut mid-answer now bills the input and cache counts the provider sent before the cut, and the spend row carries the 499 ClientDisconnected marker that /v1/responses reads from litellm_metadata

* fix(responses): bill cut Claude streams on relayed providers and build the partial off the event loop

* fix(responses): log the cost of the provider-reported usage on a cut stream

The cut partial took its usage from the rebuild over every kept chunk but kept
the response_cost the first build stamped from the estimate, and the logger
uses a stamped cost before it prices the usage. The partial now carries the
rebuild's cost along with its usage.

* fix(responses): drop the message_start placeholder on every stream Anthropic's parser reads

Anthropic's stream parser marks its message_start chunk, and the placeholder reset reads that mark
instead of a provider list plus a Claude model name. Bedrock application inference profile ARNs and
OpenCode Go's non-Claude models stream Anthropic's events too, and kept the 1-token output placeholder
on a cut stream before.

* fix(responses): bill a stream cut before its first relayed chunk

A client that drops a bridged /v1/responses stream after the provider's first usage event but before any text logged no spend row, since the partial response was built only from relayed chunks. The partial now falls back to the chunks the stream wrapper kept, so the row bills the provider's input and cache counts with no output text

* fix(responses): bill a cut background cancel or fallback once

A cancelled background response whose stream teardown had already billed the cut also logged a second, failed 499 row. The background task now skips its own failure log once the teardown billed. A client cut after a mid-stream fallback billed both deployments' tokens at the fallback's price with no cache split, and now sums both partials' tokens, cache counts and cost the way MCP rounds are billed

* fix(responses): charge the fixed cost margin once on a cut fallback stream

Each deployment's partial price already includes the fixed fee from cost_margin_config, so adding the first deployment's price to the fallback's charged that fee twice. The sum now takes off one fee, and the logged cost breakdown matches the billed total

* test: annotate the new disconnect test bindings as Final

* fix(router): bill a cut fallback's first deployment at its own price

A client cut after a mid-stream fallback now prices the first deployment's
partial when the fallback starts, before the fallback changes the shared
logging object's model, so its tokens keep their own price. An unpriced
deployment bills nothing and keeps the other side's cost breakdown. The
fixed fee is charged once at the final round's amount, and a fallback that
fails after the cut leaves no breakdown from the first deployment's partial.

* fix(router): bill the first deployment at its own price when a client cuts while the fallback connects

* fix(router): report a cut fallback's billed cost as its usage cost

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-23 06:20:25 +00:00

121 lines
4.8 KiB
Python

import json
import uuid
from hashlib import sha256
from typing import Final
import pytest
from integration._support.client import Gateway, eventually, object_value
from integration._support.database import read_rows
from integration._support.wire import Reply, Request, wire_server
from pydantic import JsonValue
ANTHROPIC_MODEL: Final = "claude-haiku-4-5"
UNCACHED_INPUT_TOKENS: Final = 30
CACHE_READ_TOKENS: Final = 2000
CACHE_WRITE_TOKENS: Final = 100
INPUT_RATE: Final = 0.001
OUTPUT_RATE: Final = 0.002
CACHE_READ_RATE: Final = 0.0001
CACHE_WRITE_RATE: Final = 0.004
def _anthropic_event(payload: dict[str, JsonValue]) -> bytes:
return f"event: {payload['type']}\ndata: {json.dumps(payload, separators=(',', ':'))}\n\n".encode()
def _message_start(message_id: str) -> bytes:
return _anthropic_event(
{
"type": "message_start",
"message": {
"id": message_id,
"type": "message",
"role": "assistant",
"model": ANTHROPIC_MODEL,
"content": [],
"stop_reason": None,
"stop_sequence": None,
"usage": {
"input_tokens": UNCACHED_INPUT_TOKENS,
"cache_read_input_tokens": CACHE_READ_TOKENS,
"cache_creation_input_tokens": CACHE_WRITE_TOKENS,
"output_tokens": 1,
},
},
}
) + _anthropic_event({"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}})
def _text_delta(text: str) -> bytes:
return _anthropic_event({"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": text}})
def _terminal_usage() -> bytes:
return (
_anthropic_event({"type": "content_block_stop", "index": 0})
+ _anthropic_event({"type": "message_delta", "delta": {"stop_reason": "end_turn"}, "usage": {"output_tokens": 400}})
+ _anthropic_event({"type": "message_stop"})
)
@pytest.mark.timeout(120)
def test_a_responses_stream_cut_mid_answer_bills_the_input_and_cache_tokens_anthropic_reported(gateway: Gateway) -> None:
def respond(request: Request) -> Reply:
assert request.target.endswith("/v1/messages"), request.target
return Reply(
content_type="text/event-stream",
chunks=(
_message_start(f"msg_{uuid.uuid4().hex}") + _text_delta("first"),
_text_delta(" second"),
_text_delta(" third"),
_terminal_usage(),
),
pause_between_chunks=1.0,
)
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model=f"anthropic/{ANTHROPIC_MODEL}",
api_base=wire.url,
api_key="synthetic-anthropic-key",
input_cost_per_token=INPUT_RATE,
output_cost_per_token=OUTPUT_RATE,
cache_read_input_token_cost=CACHE_READ_RATE,
cache_creation_input_token_cost=CACHE_WRITE_RATE,
)
key: Final = scenario.key(models=[model])
with gateway.client.stream(
"POST",
"/v1/responses",
json={"model": model, "input": "disconnect control", "stream": True},
headers={"Authorization": f"Bearer {key}"},
) as response:
assert response.status_code == 200, response.read().decode()
first_delta: Final = next(
line for line in response.iter_lines() if line.startswith("data:") and "output_text.delta" in line
)
assert json.loads(first_delta.removeprefix("data:"))["delta"] == "first", first_delta
rows: Final = eventually(
lambda: read_rows(
'SELECT status, prompt_tokens, completion_tokens, spend, metadata FROM "LiteLLM_SpendLogs" '
"WHERE api_key=%s",
(sha256(key.encode()).hexdigest(),),
),
lambda values: len(values) == 1,
seconds=70,
)
row: Final = rows[0]
assert row["status"] == "success", row
assert row["prompt_tokens"] == UNCACHED_INPUT_TOKENS + CACHE_READ_TOKENS + CACHE_WRITE_TOKENS, row
assert float(str(row["spend"])) == pytest.approx(
UNCACHED_INPUT_TOKENS * INPUT_RATE
+ CACHE_READ_TOKENS * CACHE_READ_RATE
+ CACHE_WRITE_TOKENS * CACHE_WRITE_RATE
+ int(str(row["completion_tokens"])) * OUTPUT_RATE
), row
metadata: Final = row["metadata"]
parsed: Final = object_value(json.loads(metadata) if isinstance(metadata, str) else metadata)
error_information: Final = object_value(parsed["error_information"])
assert (error_information["error_code"], error_information["error_class"]) == ("499", "ClientDisconnected")
assert len(wire.drain()) == 1