Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0b1ccf8
fix(streaming): keep the provider's served service_tier on streamed c…
devin-ai-integration[bot] Sep 24, 2026
e6722a9
fix(streaming): satisfy type-discipline and strict ruff budgets
devin-ai-integration[bot] Sep 24, 2026
69fe084
fix(streaming): stamp the served service_tier on every Responses brid…
devin-ai-integration[bot] Sep 24, 2026
7039b6d
fix(anthropic-adapter): expose streamed chunks so disconnects bill pa…
kerry-berri Sep 25, 2026
909bb4c
test(service-tier): cover anthropic and responses served-tier billing…
kerry-berri Sep 25, 2026
bf86914
fix(anthropic-adapter): return a chunks-exposing stream so disconnect…
kerry-berri Sep 25, 2026
047b506
test(service-tier): bill disconnects through the router's anthropic s…
kerry-berri Sep 25, 2026
38ee5f1
Merge remote-tracking branch 'origin/main' into litellm_stream_served…
kerry-berri Sep 25, 2026
13377df
style: apply ruff format to the anthropic stream changes
kerry-berri Sep 25, 2026
2f7e0e7
test(coverage): ignore delegating properties the ast scan cannot see
kerry-berri Sep 25, 2026
8a563ef
style: keep the cast-ok reasons on the cast call line
kerry-berri Sep 25, 2026
d3b8af9
test(integration): cover served service_tier billing for streamed cha…
kerry-berri Sep 25, 2026
d6acd53
fix(anthropic-cache): delegate chunks/messages/model through the mess…
kerry-berri Sep 25, 2026
d343d3f
fix(streaming): keep service_tier on OpenAI-compatible parsed chunks
kerry-berri Sep 25, 2026
818e9e4
refactor(streaming): parameterize delegated chunks and messages types
kerry-berri Sep 25, 2026
3d909b9
Merge remote-tracking branch 'origin/main' into litellm_stream_served…
kerry-berri Sep 25, 2026
d07a7c5
Merge remote-tracking branch 'origin/main' into litellm_stream_served…
kerry-berri Sep 25, 2026
cc446c6
Merge remote-tracking branch 'origin/main' into litellm_stream_served…
kerry-berri Sep 26, 2026
9e0d6f5
Merge remote-tracking branch 'origin/main' into litellm_stream_served…
kerry-berri Sep 26, 2026
7276946
fix(tests): follow the anthropic pass_through rename after merging main
kerry-berri Sep 26, 2026
761f369
test(anthropic): drain the logging worker between response cache tests
kerry-berri Sep 26, 2026
0d8fad7
test(spend): cover azure, databricks, responses bridge and gemini ser…
kerry-berri Sep 27, 2026
fc268b1
fix(databricks): keep the served service_tier on streamed chunks and …
kerry-berri Sep 27, 2026
bc9cb9f
fix(databricks): type the served service_tier chunk without a loose k…
kerry-berri Sep 27, 2026
4db1654
fix(cost): bill the served service_tier over the requested one
kerry-berri Sep 29, 2026
aaf6c94
refactor(cost): drop explanatory comment from the tier resolution
kerry-berri Sep 29, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,7 @@ def __init__(
):
super().__init__(streaming_response, sync_stream, json_mode)
self._chat_completion_id: str | None = None
self._served_service_tier: str | None = None
self._tool_call_index_map: dict[int, int] = {} # mutable-ok: per-stream accumulator state

def _handle_string_chunk(
Expand Down Expand Up @@ -1598,6 +1599,7 @@ def translate_responses_chunk_to_openai_stream(

usage = ResponseAPILoggingUtils._transform_response_api_usage_to_chat_usage(response_data.get("usage"))
provider_metadata: Final = _provider_metadata(response_data)
served_service_tier: Final = response_data.get("service_tier")
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
return ModelResponseStream(
choices=[
StreamingChoices(
Expand All @@ -1611,6 +1613,11 @@ def translate_responses_chunk_to_openai_stream(
],
usage=usage,
provider_specific_fields=dict(provider_metadata) or None, # mutable-ok: field is typed dict
**(
MappingProxyType({"service_tier": served_service_tier})
if isinstance(served_service_tier, str)
else MappingProxyType({})
),
)
else:
pass
Expand Down Expand Up @@ -1639,12 +1646,28 @@ def chunk_parser(self, chunk: dict[str, object]) -> "ModelResponseStream":
ModelResponseStream: OpenAI-formatted streaming chunk
"""
verbose_logger.debug("Chat provider: transform_streaming_response called with chunk: %s", chunk)
return self._with_stream_scoped_id(
OpenAiResponsesToChatCompletionStreamIterator.translate_responses_chunk_to_openai_stream(
chunk, tool_call_index_map=self._tool_call_index_map
self._remember_served_service_tier(chunk)
return self._with_served_service_tier(
self._with_stream_scoped_id(
OpenAiResponsesToChatCompletionStreamIterator.translate_responses_chunk_to_openai_stream(
chunk, tool_call_index_map=self._tool_call_index_map
)
)
)

def _remember_served_service_tier(self, chunk: dict[str, object]) -> None:
response_payload: Final = chunk.get("response")
if not isinstance(response_payload, dict):
return
served_tier: Final = response_payload.get("service_tier")
if isinstance(served_tier, str) and served_tier:
self._served_service_tier = served_tier

def _with_served_service_tier(self, chunk: "ModelResponseStream") -> "ModelResponseStream":
if self._served_service_tier is not None and chunk.model_dump().get("service_tier") is None:
setattr(chunk, "service_tier", self._served_service_tier) # noqa: B010 # pydantic extra, not a declared field
return chunk

def _with_stream_scoped_id(self, chunk: "ModelResponseStream") -> "ModelResponseStream":
if self._chat_completion_id is None:
self._chat_completion_id = chunk.id
Expand Down
14 changes: 14 additions & 0 deletions litellm/litellm_core_utils/streaming_chunk_builder_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class _BaseChunk(TypedDict, total=False):
created: ReadOnly[int]
model: ReadOnly[str]
system_fingerprint: ReadOnly[str | None]
service_tier: ReadOnly[str | None]
choices: ReadOnly[Required[Sequence[StreamingChoices]]]
_hidden_params: ReadOnly[_ChunkHiddenParams]

Expand Down Expand Up @@ -369,6 +370,13 @@ def _get_model_from_chunks(chunks: Sequence["_BaseChunk"], first_chunk_model: st
# Fall back to first chunk's model if no different model found
return first_chunk_model

@staticmethod
def _get_service_tier_from_chunks(chunks: Sequence["_BaseChunk"]) -> str | None:
return next(
(tier for chunk in reversed(chunks) if isinstance(tier := chunk.get("service_tier"), str) and tier),
None,
)

def build_base_response(self, chunks: Sequence["_BaseChunk"]) -> ModelResponse:
chunk = self.first_chunk
id: Final = ChunkProcessor._get_chunk_id(chunks)
Expand All @@ -378,6 +386,7 @@ def build_base_response(self, chunks: Sequence["_BaseChunk"]) -> ModelResponse:
# Get the actual model - for Azure Model Router, this finds the real model from later chunks
model: Final = ChunkProcessor._get_model_from_chunks(chunks, first_chunk_model)
system_fingerprint: Final = chunk.get("system_fingerprint", None)
service_tier: Final = ChunkProcessor._get_service_tier_from_chunks(chunks)

role: Final = ChunkProcessor._get_role_from_chunks(chunks)
finish_reason = "stop"
Expand All @@ -399,6 +408,11 @@ def build_base_response(self, chunks: Sequence["_BaseChunk"]) -> ModelResponse:
"created": created,
"model": model,
"system_fingerprint": system_fingerprint,
**(
MappingProxyType({"service_tier": service_tier})
if service_tier is not None
else MappingProxyType({})
),
"choices": [
{
"index": 0,
Expand Down
8 changes: 8 additions & 0 deletions litellm/litellm_core_utils/streaming_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ def _next_sync_or_exhausted(it: Any) -> object:
return _SYNC_ITER_EXHAUSTED


def _stamp_served_service_tier(response: ModelResponseStream, complete_streaming_response: ModelResponse) -> None:
served_tier: Final = complete_streaming_response.model_dump().get("service_tier")
if isinstance(served_tier, str) and served_tier:
setattr(response, "service_tier", served_tier) # noqa: B010 # pydantic extra, not a declared field


def is_async_iterable(obj: object) -> bool:
"""
Check if an object is an async iterable (can be used with 'async for').
Expand Down Expand Up @@ -1873,6 +1879,7 @@ def __next__(self) -> "ModelResponseStream":
"usage",
getattr(complete_streaming_response, "usage"),
)
_stamp_served_service_tier(response, complete_streaming_response)
try:
_cache_copy = complete_streaming_response.model_copy(deep=True)
_log_copy = complete_streaming_response.model_copy(deep=True)
Expand Down Expand Up @@ -2124,6 +2131,7 @@ async def _finalize_completed_stream(self, cache_hit: bool) -> "ModelResponseStr
"usage",
getattr(complete_streaming_response, "usage"),
)
_stamp_served_service_tier(response, complete_streaming_response)
try:
_copy = complete_streaming_response.model_copy(deep=True)
except RuntimeError:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ def __init__(self, completion_stream: _ChunkStream):
self._async_iter: AsyncIterator[ModelResponseStream] | None = None
self._buffer: deque[ModelResponseStream] = deque()

@property
def chunks(self) -> list | None:
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
return getattr(self._stream, "chunks", None)

@property
def messages(self) -> list | None:
return getattr(self._stream, "messages", None)

@staticmethod
def _is_combined(chunk: "ModelResponseStream") -> bool:
"""True if ``chunk`` carries response content AND a finish_reason."""
Expand Down Expand Up @@ -355,6 +363,14 @@ def __init__(
text="",
)

@property
def chunks(self) -> list | None:
return getattr(self.completion_stream, "chunks", None)

@property
def messages(self) -> list | None:
return getattr(self.completion_stream, "messages", None)
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved

def _merge_usage_into_held_stop_reason_chunk(self, chunk: Any) -> MessageBlockDelta:
"""Merge usage data from ``chunk`` into the held ``message_delta`` chunk.

Expand Down
3 changes: 2 additions & 1 deletion litellm/proxy/proxy_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9181,9 +9181,10 @@ def _fast_serialize_simple_model_response_stream(
"object": getattr(chunk, "object", None),
"created": getattr(chunk, "created", None),
"model": model,
"service_tier": getattr(chunk, "service_tier", None),
"choices": [choice_dict],
}
for top_level_key in ("id", "object", "created"):
for top_level_key in ("id", "object", "created", "service_tier"):
if payload[top_level_key] is None:
payload.pop(top_level_key)
return orjson.dumps(payload)
Expand Down
1 change: 1 addition & 0 deletions tests/e2e/coverage_registry/llm_conversational.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- {id: llm.chat_completions.openai.vision.nonstream.works, module: llm, tier: P0, subject_endpoint: chat_completions, route: openai, capability: vision, streaming: nonstream, assertions: [works], source: "model_prices json", rationale: "gpt-4o vision; high usage"}
- {id: llm.chat_completions.openai.prompt_cache_5m.nonstream.works, module: llm, tier: P0, subject_endpoint: chat_completions, route: openai, capability: prompt_cache_5m, streaming: nonstream, assertions: [works], source: "model_prices json", rationale: "Prompt caching cost optimization"}
- {id: llm.chat_completions.openai.service_tier.nonstream.works, module: llm, tier: P1, subject_endpoint: chat_completions, route: openai, capability: service_tier, streaming: nonstream, assertions: [works], source: "OpenAI service_tier param", rationale: "OpenAI scale-tier request option is forwarded and echoed"}
- {id: llm.chat_completions.openai.service_tier.stream.echoes_served_tier, module: llm, tier: P1, subject_endpoint: chat_completions, route: openai, capability: service_tier, streaming: stream, assertions: [works], source: "litellm_core_utils/streaming_handler.py", fail_before_fix: proven, rationale: "Every relayed stream chunk carries the service_tier OpenAI stamped on it, so a streaming caller can see which tier served the request"}
- {id: llm.chat_completions.openai.thinking.nonstream.works, module: llm, tier: P1, subject_endpoint: chat_completions, route: openai, capability: thinking, streaming: nonstream, assertions: [works], source: "model_prices json", rationale: "o-series reasoning; emerging"}
- {id: llm.chat_completions.openai.structured_output.nonstream.works, module: llm, tier: P1, subject_endpoint: chat_completions, route: openai, capability: structured_output, streaming: nonstream, assertions: [works], source: "model_prices json", rationale: "response_schema extraction"}
- {id: llm.chat_completions.anthropic.basic.nonstream.works, module: llm, tier: P0, subject_endpoint: chat_completions, route: anthropic, capability: basic, streaming: nonstream, assertions: [works], source: "proxy_server.py:8455", rationale: "P0 route translated to Anthropic"}
Expand Down
3 changes: 3 additions & 0 deletions tests/e2e/coverage_registry/quota_management.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
- {id: quota_management.spend_tracking.stream_cache_read.bills_cache_read_rate, module: quota_management, tier: P1, behavior: spend_tracking, variant: stream_cache_read, assertions: [bills_cache_read_rate], exercised_on: [chat_completions], source: "litellm_core_utils/streaming_chunk_builder_utils.py", rationale: "A streamed call's reassembled usage keeps the cached-token detail so cache reads bill at the cache-read discount, not full input price (#34812)"}
- {id: quota_management.spend_tracking.messages_bridge.keeps_cache_tokens, module: quota_management, tier: P1, behavior: spend_tracking, variant: messages_bridge, assertions: [keeps_cache_tokens], exercised_on: [messages], source: "llms/anthropic/experimental_pass_through/responses_adapters/handler.py", rationale: "A /v1/messages request served by a Responses-only OpenAI model keeps its cache-read tokens and their discounted billing across the bridge (#34957)"}
- {id: quota_management.spend_tracking.service_tier.bills_tier_rates, module: quota_management, tier: P1, behavior: spend_tracking, variant: service_tier, assertions: [bills_tier_rates], exercised_on: [chat_completions], source: "cost_calculator.py", rationale: "A priority service_tier call bills input, output, and reasoning at the deployment's *_priority rates and records the tier on the row (#35923, #35925)"}
- {id: quota_management.spend_tracking.service_tier_stream.records_served_tier, module: quota_management, tier: P1, behavior: spend_tracking, variant: service_tier_stream, assertions: [records_served_tier], exercised_on: [chat_completions], source: "litellm_core_utils/streaming_chunk_builder_utils.py", fail_before_fix: proven, rationale: "A streamed call with no service_tier requested bills at the rates of the tier OpenAI stamps on its chunks and records that served tier on the row; the reassembled stream dropped the provider tier so the row recorded none and priced at the default rates"}
- {id: quota_management.spend_tracking.service_tier_stream.responses_records_served_tier, module: quota_management, tier: P1, behavior: spend_tracking, variant: service_tier_stream, assertions: [records_served_tier], exercised_on: [responses], source: "responses/streaming_iterator.py", rationale: "A streamed /v1/responses call bills at the tier carried on the response.completed event's inner response and records that served tier on the spend row"}
- {id: quota_management.spend_tracking.service_tier_stream.messages_records_served_tier, module: quota_management, tier: P1, behavior: spend_tracking, variant: service_tier_stream, assertions: [records_served_tier], exercised_on: [messages], source: "llms/anthropic/experimental_pass_through/adapters/streaming_iterator.py", rationale: "A streamed /v1/messages call on an OpenAI-backed deployment bills at the tier OpenAI served; the Anthropic wire format has no tier field, so the spend row is the only record of it"}
- {id: quota_management.spend_tracking.cost_headers.additive_components, module: quota_management, tier: P1, behavior: spend_tracking, variant: cost_headers, assertions: [additive_components], exercised_on: [chat_completions], source: "proxy/common_request_processing.py", rationale: "The x-litellm-response-cost-* component headers sum to the total, input covers only fresh tokens, and reasoning stays a subset of output (#36965)"}
- {id: quota_management.spend_tracking.passthrough_stream.injects_usage_cost, module: quota_management, tier: P1, behavior: spend_tracking, variant: passthrough_stream, assertions: [injects_usage_cost], exercised_on: [openai_passthrough], source: "proxy/pass_through_endpoints/streaming_handler.py", rationale: "With include_cost_in_streaming_usage on, the /openai passthrough's final streaming usage frame carries the proxy-computed cost (#36503). Uncovered: the flag is only settable in litellm_settings, and the shared e2e stack does not turn it on yet"}
- {id: quota_management.spend_tracking.websearch_interception.bills_under_request_session, module: quota_management, tier: P1, behavior: spend_tracking, variant: websearch_interception, assertions: [bills_under_request_session], exercised_on: [messages], source: "integrations/websearch_interception/handler.py", fail_before_fix: proven, rationale: "A web_search server tool the proxy intercepts into litellm.asearch writes its own asearch spend row, and that row carries the parent request's session_id so the session view counts the search and its cost next to the turn that triggered it (LIT-8063)"}
Expand Down
11 changes: 11 additions & 0 deletions tests/e2e/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,17 @@ class AnthropicMessagesBody(BaseModel):
cache: dict[str, bool] | None = {"no-cache": True}


class ResponsesStreamBody(BaseModel):
"""POST /v1/responses body in the subset the spend tests stream with.
`input` stays a plain string: the tests only drive single-turn prompts."""

model: str
input: str
stream: bool = True
max_output_tokens: int | None = None
cache: dict[str, bool] | None = {"no-cache": True}


class CountTokensBody(BaseModel):
"""POST /v1/messages/count_tokens body: the /v1/messages shape minus
max_tokens (the endpoint only counts the prompt)."""
Expand Down
4 changes: 4 additions & 0 deletions tests/e2e/proxy_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
OcrResponse,
RerankBody,
RerankResponse,
ResponsesStreamBody,
RouterCurrentValues,
RouterSettingsResponse,
SearchToolCreateBody,
Expand Down Expand Up @@ -970,6 +971,9 @@ def chat_stream(self, key: str, body: ChatBody) -> StreamingResponse:
def messages_stream(self, key: str, body: AnthropicMessagesBody) -> StreamingResponse:
return self.transport.stream("/v1/messages", headers=self.transport.bearer(key), json=body)

def responses_stream(self, key: str, body: ResponsesStreamBody) -> StreamingResponse:
return self.transport.stream("/v1/responses", headers=self.transport.bearer(key), json=body)

def embed(self, key: str, body: EmbedBody) -> Result[EmbedResponse]:
return self.transport.post(
"/embeddings",
Expand Down
Loading
Loading