litellm/tests/unit/responses/mcp
Mateo Wang efaa457b1b
fix(cost): set the SDK stream wrapper's response_cost once the stream ends (#55723)
* fix(cost): set the SDK stream wrapper's response_cost once the stream ends

A streamed completion's wrapper reported response_cost 0.0 when it was
created and kept it after the last chunk, so SDK callers reading it after
the stream saw every call as free. The wrapper now starts at None while
the cost is unknown and takes the finished response's cost when the
stream ends. Cached streams keep their 0.0 and pre-priced replay streams
keep the cost they were built with.

Based on #49397.

Co-authored-by: vanshsingh3711-wq <289212929+vanshsingh3711-wq@users.noreply.github.com>

* fix(streaming): keep the 0.0 start cost and take the final cost at stream end, including Router and provider-billed streams

* fix(streaming): price the stream's final response off the event loop and drop the unobservable pricing guard

The async stream end now runs the cost calculator through asyncify, the same way it builds the final response, so an unpriced or provider-billed model's pricing no longer blocks the event loop. The pricing_left_unrecorded guard is gone, since the logged payloads match with and without it.

* fix(streaming): keep stream-end logging on client disconnect, report the MCP chat stream's billed total, and bill Router fallback streams

* fix(streaming): price the stream end again only when the builder could not, so unpriced chat streams skip a second cost lookup

The stream builder already prices every chat response it assembles. The wrapper now takes that cost, and runs the calculator itself only for a provider that bills from its own usage fields or for a response the builder did not assemble (its failure path), where the builder leaves the cost unset.

* fix(router): count a twice-continued stream's cut legs once in its final response_cost

---------

Co-authored-by: vanshsingh3711-wq <289212929+vanshsingh3711-wq@users.noreply.github.com>
2026-12-03 22:56:07 +00:00
..
__init__.py ci: move provider-independent MCP tests into tests/unit and run mcp-integration from litellm-tests (#42904) 2026-09-24 23:07:48 +00:00
conftest.py refactor(mcp): drop the experimental label from the MCP server and client paths (#48513) 2026-10-27 17:47:50 +00:00
test_aresponses_api_with_mcp.py feat(responses): store each bridged turn's path to the root so a fork replays only its ancestors (#49519) 2026-11-23 12:43:11 -08:00
test_chat_completions_handler.py fix(cost): set the SDK stream wrapper's response_cost once the stream ends (#55723) 2026-12-03 22:56:07 +00:00
test_litellm_proxy_mcp_handler.py fix(proxy): keep a request's slot, TPM and budget holds while it runs MCP tools (#54469) 2026-11-26 08:11:44 +00:00
test_mcp_streaming_iterator.py fix(logging): keep tracebacks in one JSON record and log the token count skip at debug (#54603) 2026-11-30 18:19:05 +00:00
test_parked_stream_dispatch.py fix(mcp): bill both calls of a streamed chat completions MCP auto-execute (#48899) 2026-10-30 07:34:10 -07:00
test_round_billing.py fix(responses): bill the provider-reported input on a cut bridged stream (#53394) 2026-11-23 06:20:25 +00:00
test_tool_precheck.py feat(messages): serve anthropic messages on rust only (#51119) 2026-11-12 00:07:20 +00:00