Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
29 changes: 22 additions & 7 deletions litellm/integrations/arize/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,28 @@ def _set_usage_outputs(span: "Span", response_obj, span_attrs):
prompt_tokens = usage.get("prompt_tokens") or usage.get("input_tokens")
if prompt_tokens:
safe_set_attribute(span, span_attrs.LLM_TOKEN_COUNT_PROMPT, prompt_tokens)
reasoning_tokens = usage.get("output_tokens_details", {}).get("reasoning_tokens")
if reasoning_tokens:
safe_set_attribute(
span,
span_attrs.LLM_TOKEN_COUNT_COMPLETION_DETAILS_REASONING,
reasoning_tokens,
)
completion_tokens_details = usage.get("completion_tokens_details") or usage.get(
"output_tokens_details"
)
if completion_tokens_details is not None:
reasoning_tokens = getattr(completion_tokens_details, "reasoning_tokens", None)
if reasoning_tokens:
safe_set_attribute(
span,
span_attrs.LLM_TOKEN_COUNT_COMPLETION_DETAILS_REASONING,
reasoning_tokens,
)
prompt_tokens_details = usage.get("prompt_tokens_details") or usage.get(
"input_tokens_details"
)
if prompt_tokens_details is not None:
cached_tokens = getattr(prompt_tokens_details, "cached_tokens", None)
if cached_tokens:
safe_set_attribute(
span,
span_attrs.LLM_TOKEN_COUNT_PROMPT_DETAILS_CACHE_READ,
cached_tokens,
)


def _infer_open_inference_span_kind(call_type: Optional[str]) -> str:
Expand Down
124 changes: 124 additions & 0 deletions tests/test_litellm/integrations/arize/test_arize_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,127 @@ def test_construct_dynamic_arize_headers():
"arize-space-id": "test_space_key",
"api_key": "test_api_key"
}


def test_set_usage_outputs_chat_completion_tokens_details():
"""
Test that _set_usage_outputs correctly extracts reasoning_tokens from
completion_tokens_details (Chat Completions API) and cached_tokens from
prompt_tokens_details.
"""
from unittest.mock import MagicMock

from litellm.integrations.arize._utils import _set_usage_outputs
from litellm.types.utils import (
CompletionTokensDetailsWrapper,
ModelResponse,
PromptTokensDetailsWrapper,
Usage,
)

span = MagicMock()

response_obj = ModelResponse(
usage=Usage(
total_tokens=200,
completion_tokens=120,
prompt_tokens=80,
completion_tokens_details=CompletionTokensDetailsWrapper(
reasoning_tokens=45
),
prompt_tokens_details=PromptTokensDetailsWrapper(cached_tokens=30),
),
choices=[
Choices(
message={"role": "assistant", "content": "test"}, finish_reason="stop"
)
],
model="gpt-4o",
)

_set_usage_outputs(span, response_obj, SpanAttributes)

span.set_attribute.assert_any_call(SpanAttributes.LLM_TOKEN_COUNT_TOTAL, 200)
span.set_attribute.assert_any_call(SpanAttributes.LLM_TOKEN_COUNT_COMPLETION, 120)
span.set_attribute.assert_any_call(SpanAttributes.LLM_TOKEN_COUNT_PROMPT, 80)
span.set_attribute.assert_any_call(
SpanAttributes.LLM_TOKEN_COUNT_COMPLETION_DETAILS_REASONING, 45
)
span.set_attribute.assert_any_call(
SpanAttributes.LLM_TOKEN_COUNT_PROMPT_DETAILS_CACHE_READ, 30
)


def test_set_usage_outputs_responses_api_output_tokens_details():
"""
Test that _set_usage_outputs falls back to output_tokens_details (Responses API)
when completion_tokens_details is not present.
"""
from unittest.mock import MagicMock

from litellm.integrations.arize._utils import _set_usage_outputs
from litellm.types.llms.openai import (
OutputTokensDetails,
ResponseAPIUsage,
ResponsesAPIResponse,
)

span = MagicMock()

response_obj = ResponsesAPIResponse(
id="response-456",
created_at=1625247600,
output=[],
usage=ResponseAPIUsage(
input_tokens=100,
output_tokens=200,
total_tokens=300,
output_tokens_details=OutputTokensDetails(reasoning_tokens=150),
),
)

_set_usage_outputs(span, response_obj, SpanAttributes)

span.set_attribute.assert_any_call(SpanAttributes.LLM_TOKEN_COUNT_TOTAL, 300)
span.set_attribute.assert_any_call(SpanAttributes.LLM_TOKEN_COUNT_COMPLETION, 200)
span.set_attribute.assert_any_call(SpanAttributes.LLM_TOKEN_COUNT_PROMPT, 100)
span.set_attribute.assert_any_call(
SpanAttributes.LLM_TOKEN_COUNT_COMPLETION_DETAILS_REASONING, 150
)
Comment on lines +428 to +463
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Missing coverage for input_tokens_details.cached_tokens (Responses API path)

The new code in _set_usage_outputs falls back to usage.get("input_tokens_details") for cached_tokens when prompt_tokens_details is absent. ResponseAPIUsage has input_tokens_details: Optional[InputTokensDetails], and InputTokensDetails carries cached_tokens: int = 0.

test_set_usage_outputs_responses_api_output_tokens_details only verifies the output_tokens_details branch; input_tokens_details is never set in the test so the new or usage.get("input_tokens_details") branch is never exercised. A future rename of that key or a copy-paste mistake in the fallback chain would pass all tests silently.

Consider extending this test (or adding a dedicated one) that populates input_tokens_details with a non-zero cached_tokens and asserts that LLM_TOKEN_COUNT_PROMPT_DETAILS_CACHE_READ is set to the expected value.



def test_set_usage_outputs_no_token_details():
"""
Test that _set_usage_outputs works when neither completion_tokens_details
nor prompt_tokens_details are present (basic usage without details).
"""
from unittest.mock import MagicMock

from litellm.integrations.arize._utils import _set_usage_outputs
from litellm.types.utils import ModelResponse, Usage

span = MagicMock()

response_obj = ModelResponse(
usage=Usage(
total_tokens=100,
completion_tokens=60,
prompt_tokens=40,
),
choices=[
Choices(
message={"role": "assistant", "content": "test"}, finish_reason="stop"
)
],
model="gpt-4o",
)

_set_usage_outputs(span, response_obj, SpanAttributes)

span.set_attribute.assert_any_call(SpanAttributes.LLM_TOKEN_COUNT_TOTAL, 100)
span.set_attribute.assert_any_call(SpanAttributes.LLM_TOKEN_COUNT_COMPLETION, 60)
span.set_attribute.assert_any_call(SpanAttributes.LLM_TOKEN_COUNT_PROMPT, 40)
# reasoning and cached should NOT be set
for call in span.set_attribute.call_args_list:
assert call[0][0] != SpanAttributes.LLM_TOKEN_COUNT_COMPLETION_DETAILS_REASONING
assert call[0][0] != SpanAttributes.LLM_TOKEN_COUNT_PROMPT_DETAILS_CACHE_READ
Loading