Skip to content

Commit 82843e2

Browse files
add unconditional manual token counting for completions
1 parent 3fb20f0 commit 82843e2

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

sentry_sdk/integrations/openai.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,13 @@ def new_iterator() -> "Iterator[ChatCompletionChunk]":
640640
set_data_normalized(
641641
span, SPANDATA.GEN_AI_RESPONSE_TEXT, all_responses
642642
)
643+
_calculate_token_usage(
644+
messages,
645+
response,
646+
span,
647+
all_responses,
648+
integration.count_tokens,
649+
)
643650

644651
if finish_span:
645652
span.__exit__(None, None, None)
@@ -675,6 +682,13 @@ async def new_iterator_async() -> "AsyncIterator[ChatCompletionChunk]":
675682
set_data_normalized(
676683
span, SPANDATA.GEN_AI_RESPONSE_TEXT, all_responses
677684
)
685+
_calculate_token_usage(
686+
messages,
687+
response,
688+
span,
689+
all_responses,
690+
integration.count_tokens,
691+
)
678692

679693
if finish_span:
680694
span.__exit__(None, None, None)

0 commit comments

Comments
 (0)