(
stream_usage: UsageMetrics | None,
*,
stream_chunks: list[bytes] | None = None,
)
| 5278 | try: |
| 5279 | if is_streaming: |
| 5280 | async def _record_stream_success( |
| 5281 | stream_usage: UsageMetrics | None, |
| 5282 | *, |
| 5283 | stream_chunks: list[bytes] | None = None, |
| 5284 | ) -> None: |
| 5285 | stream_actual_cost: float | None = None |
| 5286 | stream_ttft_ms: float | None = None |
| 5287 | stream_tps: float | None = None |
| 5288 | if stream_usage is not None: |
| 5289 | stream_actual_cost = ( |
| 5290 | stream_usage.actual_cost |
| 5291 | if stream_usage.actual_cost is not None |
| 5292 | else _estimate_cost_from_usage(selected_model, stream_usage) |
| 5293 | ) |
| 5294 | stream_ttft_ms = stream_usage.ttft_ms |
| 5295 | stream_tps = stream_usage.tps |
| 5296 | |
| 5297 | if is_virtual: |
| 5298 | _model_experience.observe( |
| 5299 | selected_model, |
| 5300 | mode_value, |
| 5301 | tier_value, |
| 5302 | success=True, |
| 5303 | ttft_ms=stream_ttft_ms, |
| 5304 | tps=stream_tps, |
| 5305 | total_input_tokens=stream_usage.input_tokens_total if stream_usage else None, |
| 5306 | uncached_input_tokens=stream_usage.input_tokens_uncached if stream_usage else None, |
| 5307 | cache_read_tokens=stream_usage.cache_read_input_tokens if stream_usage else 0, |
| 5308 | cache_write_tokens=stream_usage.cache_write_input_tokens if stream_usage else 0, |
| 5309 | input_cost_multiplier=stream_usage.input_cost_multiplier if stream_usage else None, |
| 5310 | ) |
| 5311 | if request_id: |
| 5312 | _feedback.rebind_request( |
| 5313 | request_id, |
| 5314 | model=selected_model, |
| 5315 | tier=tier_value, |
| 5316 | mode=mode_value, |
| 5317 | ) |
| 5318 | combined_cost = ( |
| 5319 | (stream_actual_cost if stream_actual_cost is not None else main_estimated_cost) |
| 5320 | + (sidechannel_actual_cost if sidechannel_actual_cost is not None else sidechannel_estimated_cost) |
| 5321 | ) |
| 5322 | await _spend_reservation.settle( |
| 5323 | request_id, |
| 5324 | combined_cost, |
| 5325 | model=selected_model, |
| 5326 | action="chat", |
| 5327 | ) |
| 5328 | _record_route_trace( |
| 5329 | status_code=200, |
| 5330 | actual_cost=stream_actual_cost, |
| 5331 | usage_metrics=stream_usage, |
| 5332 | streaming=True, |
| 5333 | stream_chunks=stream_chunks, |
| 5334 | ) |
| 5335 | else: |
| 5336 | _record_route_trace( |
| 5337 | status_code=200, |
no test coverage detected