mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-30 01:08:33 +00:00
* fix(lens): price agent traces by joining gen_ai.response.id to spend logs Trace spend now joins each model call to spend_logs on one key: the span's response id (gen_ai.response.id or the id the normalizers read from OpenInference/LangChain output) against spend_logs.response_id or the upstream id embedded in a managed resp_ id. The litellm.call_id and traceparent transport join paths and the per-row ownership gate are removed; the spend SQL still restricts rows to what the reader can see. A run with some unpriced calls now reports the sum of its priced calls plus priced_calls, instead of an unknown total. * test(lens): cover response id spend join and partial trace totals * chore(lens): regenerate trace types for priced_calls * feat(lens): show partial run cost as a lower bound with priced call count * fix(lens): treat litellm.call_id as the same assigned call id for spend joins The id LiteLLM assigned to a call is either the response id it returned (gen_ai.response.id -> spend_logs.response_id) or its gateway call id (litellm.call_id -> spend_logs.litellm_call_id). Both are exact ids the gateway mints and logs, so the join stays one rule. Transport span matching and the ownership gate stay removed. * test(lens): cover litellm.call_id spend joins and restore captured totals * feat(lens): link each priced model call to its spend log Spans gain spend_log_request_id, the spend_logs.request_id the call was priced from, and spend_match, which says whether a model call matched or why not (no assigned id on the span, no spend log with that id, or an ambiguous match). A model call span is priced from the same ids as the run total, so its cost and the total agree. * test(lens): cover spend log links on model call spans * chore(lens): regenerate trace types for spend log links * feat(lens): open the matched spend log from an LLM step An LLM step's header now shows a Spend log chip with the matched request id and cost; clicking it opens the request log drawer over the run, fetched by the exact spend_logs.request_id instead of the span's own response id. Unpriced steps say why (no assigned id on the span, or no spend log with it). Tree rows show each model call's cost, and a partial run cost shows its priced call count inline. * test(lens): cover the spend log link and unmatched cost reasons * feat(lens): show the spend log link as a bordered LiteLLM Spend Log button * feat(lens): add a back link from the spend log drawer to the agent trace * feat(lens): label the spend log back link Back to Lens trace with the Lens icon * fix(lens): ignore assigned ids that name no spend log when pricing a call An id that names no row no longer vetoes the call, so a span carrying both a response id and a call id still prices from a spend row logged before litellm_call_id existed. An id naming two or more rows makes the call ambiguous, and the match reason comes from the same per-id result, so a single matched row with no cost is reported as matched. * fix(lens): price a trace only from spend logs in its own team A reader with several teams could see the same assigned id in another team's spend log; only rows from the trace's team now price it. The user and key ownership gate stays removed. * perf(lens): resolve each model call's spend once per trace Model call matches are computed once when the trace is resolved and looked up by span index, instead of scanning the model call list for every span and walking the graph again for spans, agents and the run total. * fix(lens): hide a step's Cost fact only when its spend log link shows the cost * chore(lens): drop narrative doc comments from the spend join * fix(lens): price a model call only when its ids agree on one spend log per span * fix(lens): keep pricing spend logs written before litellm_call_id by their request id * fix(lens): price every attempt a model call's ids name when they agree
164 lines
5.6 KiB
Python
164 lines
5.6 KiB
Python
from types import MappingProxyType
|
|
from typing import Final
|
|
|
|
from litellm.rust_bridge.messages.route_host import arguments, response
|
|
from litellm.rust_bridge.messages.entrypoints import LiteLLMMessagesRequest
|
|
from dataclasses import astuple
|
|
import pytest
|
|
import litellm
|
|
from litellm.rust_bridge.messages import route_host
|
|
|
|
|
|
def test_response_is_a_detached_public_messages_dict() -> None:
|
|
native: Final = MappingProxyType(
|
|
{
|
|
"id": "msg_native",
|
|
"type": "message",
|
|
"role": "assistant",
|
|
"model": "claude-sonnet-4-5",
|
|
"content": [{"type": "text", "text": "native"}],
|
|
"stop_reason": "end_turn",
|
|
"usage": {"input_tokens": 2, "output_tokens": 3},
|
|
}
|
|
)
|
|
|
|
built: Final = response(native)
|
|
|
|
assert built == dict(native)
|
|
assert isinstance(built, dict)
|
|
built["_hidden_params"] = {"annotated": True}
|
|
assert "_hidden_params" not in native
|
|
|
|
|
|
def test_arguments_are_the_public_kwargs_view() -> None:
|
|
kwargs: Final = MappingProxyType({"litellm_metadata": {"user_id": "u"}})
|
|
request: Final = LiteLLMMessagesRequest(
|
|
model="claude-sonnet-4-5",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
max_tokens=16,
|
|
stream=None,
|
|
api_key=None,
|
|
api_base=None,
|
|
custom_llm_provider="anthropic",
|
|
kwargs=kwargs,
|
|
)
|
|
|
|
assert arguments(request) is kwargs
|
|
|
|
|
|
pytestmark = pytest.mark.usefixtures("local_model_cost_map")
|
|
|
|
|
|
def _flag_model(monkeypatch: pytest.MonkeyPatch, name: str, **flags: bool) -> None:
|
|
monkeypatch.setitem(
|
|
litellm.model_cost,
|
|
name,
|
|
{
|
|
"litellm_provider": "anthropic",
|
|
"mode": "chat",
|
|
"input_cost_per_token": 0,
|
|
"output_cost_per_token": 0,
|
|
**flags,
|
|
},
|
|
)
|
|
|
|
|
|
def test_capabilities_come_from_the_model_map_under_the_callers_provider(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
_flag_model(
|
|
monkeypatch,
|
|
"claude-test-adaptive",
|
|
supports_reasoning=True,
|
|
supports_adaptive_thinking=True,
|
|
supports_output_config=True,
|
|
supports_xhigh_reasoning_effort=True,
|
|
supports_sampling_params=False,
|
|
)
|
|
|
|
capabilities: Final = route_host.model_capabilities("anthropic/claude-test-adaptive", None)
|
|
|
|
assert capabilities.supports_adaptive_thinking
|
|
assert capabilities.supports_output_config
|
|
assert not capabilities.supports_legacy_thinking
|
|
assert not capabilities.supports_sampling_params
|
|
assert capabilities.effort_tiers.xhigh
|
|
assert not capabilities.effort_tiers.max
|
|
|
|
|
|
def test_unmapped_model_keeps_sampling_params_and_no_reasoning_features() -> None:
|
|
capabilities: Final = route_host.model_capabilities("anthropic/not-a-real-model", None)
|
|
|
|
assert capabilities.supports_sampling_params
|
|
assert not capabilities.supports_reasoning
|
|
assert not capabilities.supports_adaptive_thinking
|
|
assert not any(astuple(capabilities.effort_tiers))
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("global_flag", "kwargs", "expected"),
|
|
[
|
|
(False, {}, False),
|
|
(True, {}, True),
|
|
(False, {"drop_params": "true"}, True),
|
|
(False, {"drop_params": "nonsense"}, False),
|
|
(False, {"drop_params": False}, False),
|
|
],
|
|
)
|
|
def test_drop_params_merges_the_global_flag_with_the_request(
|
|
monkeypatch: pytest.MonkeyPatch, global_flag: bool, kwargs: dict[str, object], expected: bool
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "drop_params", global_flag)
|
|
|
|
assert route_host.shaping("anthropic/not-a-real-model", None, kwargs)["drop_params"] is expected
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("configured", "expected"),
|
|
[
|
|
(["tools[*].input_examples", 3, "metadata.user_id"], ("tools[*].input_examples", "metadata.user_id")),
|
|
("tools", ()),
|
|
(None, ()),
|
|
],
|
|
)
|
|
def test_additional_drop_params_keep_only_string_paths(configured: object, expected: tuple[str, ...]) -> None:
|
|
shaping: Final = route_host.shaping("anthropic/not-a-real-model", None, {"additional_drop_params": configured})
|
|
|
|
assert shaping["additional_drop_params"] == expected
|
|
|
|
|
|
def test_native_request_rejections_map_to_the_public_400() -> None:
|
|
from types import MappingProxyType
|
|
|
|
from litellm.rust_bridge.messages.entrypoints import LiteLLMMessagesRequest
|
|
|
|
request: Final = LiteLLMMessagesRequest(
|
|
model="anthropic/claude-sonnet-5",
|
|
messages=(),
|
|
max_tokens=8,
|
|
stream=None,
|
|
api_key=None,
|
|
api_base=None,
|
|
custom_llm_provider=None,
|
|
kwargs=MappingProxyType({}),
|
|
)
|
|
rejected: Final = ValueError("claude-sonnet-5 does not support top_k=5")
|
|
rejected.messages_request_error = True # pyright: ignore[reportAttributeAccessIssue] # marker the native host sets
|
|
|
|
mapped: Final = route_host.map_failure(rejected, request, "anthropic")
|
|
|
|
assert isinstance(mapped, litellm.BadRequestError)
|
|
assert mapped.status_code == 400
|
|
assert "does not support top_k=5" in mapped.message
|
|
assert mapped.model == "claude-sonnet-5"
|
|
assert not isinstance(route_host.map_failure(ValueError("plain"), request, "anthropic"), litellm.BadRequestError)
|
|
|
|
|
|
def test_stream_hidden_params_projects_upstream_headers_the_way_the_python_handler_does() -> None:
|
|
hidden: Final = route_host.stream_hidden_params(
|
|
(("request-id", "req_upstream_123"), ("x-ratelimit-remaining-requests", "41"))
|
|
)
|
|
|
|
additional: Final = hidden["additional_headers"]
|
|
assert isinstance(additional, dict)
|
|
assert additional["llm_provider-request-id"] == "req_upstream_123"
|
|
assert additional["x-ratelimit-remaining-requests"] == "41"
|
|
assert additional["request-id"] == additional["llm_provider-request-id"]
|