|
34 | 34 | from sentry_sdk.ai._openai_responses_api import ( |
35 | 35 | _transform_tool_definitions as _transform_tool_definitions_responses, |
36 | 36 | ) |
37 | | -from sentry_sdk.ai.monitoring import record_token_usage |
38 | 37 | from sentry_sdk.ai.utils import ( |
39 | 38 | get_start_span_function, |
40 | 39 | normalize_message_roles, |
@@ -239,21 +238,36 @@ def _calculate_completions_token_usage( |
239 | 238 | if hasattr(choice, "message") and hasattr(choice.message, "content"): |
240 | 239 | output_tokens += count_tokens(choice.message.content) |
241 | 240 |
|
| 241 | + set_on_span = ( |
| 242 | + span.set_attribute if isinstance(span, StreamedSpan) else span.set_data |
| 243 | + ) |
| 244 | + |
242 | 245 | # Do not set token data if it is 0 |
243 | 246 | input_tokens = input_tokens or None |
| 247 | + if input_tokens is not None: |
| 248 | + set_on_span(SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, input_tokens) |
| 249 | + |
244 | 250 | input_tokens_cached = input_tokens_cached or None |
| 251 | + if input_tokens_cached is not None: |
| 252 | + set_on_span(SPANDATA.GEN_AI_USAGE_INPUT_TOKENS_CACHED, input_tokens_cached) |
| 253 | + |
245 | 254 | output_tokens = output_tokens or None |
| 255 | + if output_tokens is not None: |
| 256 | + set_on_span(SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens) |
| 257 | + |
246 | 258 | output_tokens_reasoning = output_tokens_reasoning or None |
| 259 | + if output_tokens_reasoning is not None: |
| 260 | + set_on_span( |
| 261 | + SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING, |
| 262 | + output_tokens_reasoning, |
| 263 | + ) |
| 264 | + |
247 | 265 | total_tokens = total_tokens or None |
| 266 | + if total_tokens is None and input_tokens is not None and output_tokens is not None: |
| 267 | + total_tokens = input_tokens + output_tokens |
248 | 268 |
|
249 | | - record_token_usage( |
250 | | - span, |
251 | | - input_tokens=input_tokens, |
252 | | - input_tokens_cached=input_tokens_cached, |
253 | | - output_tokens=output_tokens, |
254 | | - output_tokens_reasoning=output_tokens_reasoning, |
255 | | - total_tokens=total_tokens, |
256 | | - ) |
| 269 | + if total_tokens is not None: |
| 270 | + set_on_span(SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, total_tokens) |
257 | 271 |
|
258 | 272 |
|
259 | 273 | def _calculate_responses_token_usage( |
@@ -317,21 +331,36 @@ def _calculate_responses_token_usage( |
317 | 331 | if hasattr(content_item, "text"): |
318 | 332 | output_tokens += count_tokens(content_item.text) |
319 | 333 |
|
| 334 | + set_on_span = ( |
| 335 | + span.set_attribute if isinstance(span, StreamedSpan) else span.set_data |
| 336 | + ) |
| 337 | + |
320 | 338 | # Do not set token data if it is 0 |
321 | 339 | input_tokens = input_tokens or None |
| 340 | + if input_tokens is not None: |
| 341 | + set_on_span(SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, input_tokens) |
| 342 | + |
322 | 343 | input_tokens_cached = input_tokens_cached or None |
| 344 | + if input_tokens_cached is not None: |
| 345 | + set_on_span(SPANDATA.GEN_AI_USAGE_INPUT_TOKENS_CACHED, input_tokens_cached) |
| 346 | + |
323 | 347 | output_tokens = output_tokens or None |
| 348 | + if output_tokens is not None: |
| 349 | + set_on_span(SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens) |
| 350 | + |
324 | 351 | output_tokens_reasoning = output_tokens_reasoning or None |
| 352 | + if output_tokens_reasoning is not None: |
| 353 | + set_on_span( |
| 354 | + SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING, |
| 355 | + output_tokens_reasoning, |
| 356 | + ) |
| 357 | + |
325 | 358 | total_tokens = total_tokens or None |
| 359 | + if total_tokens is None and input_tokens is not None and output_tokens is not None: |
| 360 | + total_tokens = input_tokens + output_tokens |
326 | 361 |
|
327 | | - record_token_usage( |
328 | | - span, |
329 | | - input_tokens=input_tokens, |
330 | | - input_tokens_cached=input_tokens_cached, |
331 | | - output_tokens=output_tokens, |
332 | | - output_tokens_reasoning=output_tokens_reasoning, |
333 | | - total_tokens=total_tokens, |
334 | | - ) |
| 362 | + if total_tokens is not None: |
| 363 | + set_on_span(SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, total_tokens) |
335 | 364 |
|
336 | 365 |
|
337 | 366 | def _set_responses_api_input_data( |
|
0 commit comments