Skip to content
Draft
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
6 changes: 6 additions & 0 deletions sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,12 @@ class SPANDATA:
Example: "conv_abc123"
"""

GEN_AI_COST_TOTAL_TOKENS = "gen_ai.cost.total_tokens"
"""
The total cost of the AI model call.
Example: 0.0015
"""

GEN_AI_CHOICE = "gen_ai.choice"
"""
The model's response message.
Expand Down
22 changes: 18 additions & 4 deletions sentry_sdk/integrations/litellm.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ def _success_callback(
return

try:
set_on_span = (
span.set_attribute if isinstance(span, StreamedSpan) else span.set_data
)

# Record model information
if hasattr(completion_response, "model"):
set_data_normalized(
Expand Down Expand Up @@ -254,14 +258,24 @@ def _success_callback(
span, SPANDATA.GEN_AI_RESPONSE_TEXT, response_messages
)

# Record the final response cost. Prefer the top-level value so callers
# can use LiteLLM's logging hooks to replace its calculated cost before
# observability callbacks run.
response_cost = kwargs.get("response_cost")
if response_cost is None:
standard_logging_object = kwargs.get("standard_logging_object")
if isinstance(standard_logging_object, dict):
response_cost = standard_logging_object.get("response_cost")

if isinstance(response_cost, (int, float)) and not isinstance(
response_cost, bool
):
set_on_span(SPANDATA.GEN_AI_COST_TOTAL_TOKENS, response_cost)

# Record token usage
if hasattr(completion_response, "usage"):
usage = completion_response.usage

set_on_span = (
span.set_attribute if isinstance(span, StreamedSpan) else span.set_data
)

input_tokens = getattr(usage, "prompt_tokens", None)
if input_tokens is not None:
set_on_span(SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, input_tokens)
Expand Down
6 changes: 6 additions & 0 deletions tests/integrations/litellm/test_litellm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2665,6 +2665,8 @@ def test_response_without_usage(
kwargs = {
"model": "gpt-3.5-turbo",
"messages": messages,
"response_cost": 0.123,
"standard_logging_object": {"response_cost": 0.001},
}

_input_callback(kwargs)
Expand All @@ -2681,13 +2683,16 @@ def test_response_without_usage(
# Span should still be created even without usage info
assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT
assert span["name"] == "chat gpt-3.5-turbo"
assert span["attributes"][SPANDATA.GEN_AI_COST_TOTAL_TOKENS] == 0.123
else:
events = capture_events()

with start_transaction(name="litellm test"):
kwargs = {
"model": "gpt-3.5-turbo",
"messages": messages,
"response_cost": 0.123,
"standard_logging_object": {"response_cost": 0.001},
}

_input_callback(kwargs)
Expand All @@ -2704,6 +2709,7 @@ def test_response_without_usage(
# Span should still be created even without usage info
assert span["op"] == OP.GEN_AI_CHAT
assert span["description"] == "chat gpt-3.5-turbo"
assert span["data"][SPANDATA.GEN_AI_COST_TOTAL_TOKENS] == 0.123


def test_integration_setup(sentry_init):
Expand Down
Loading