Skip to content

Commit 0da2362

Browse files
wrap finally in capture internal exceptions
1 parent f36dd5d commit 0da2362

File tree

1 file changed

+40
-38
lines changed

1 file changed

+40
-38
lines changed

sentry_sdk/integrations/anthropic.py

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -398,26 +398,27 @@ def _wrap_synchronous_message_iterator(
398398
)
399399
yield event
400400
finally:
401-
# Anthropic's input_tokens excludes cached/cache_write tokens.
402-
# Normalize to total input tokens for correct cost calculations.
403-
total_input = (
404-
usage.input_tokens
405-
+ (usage.cache_read_input_tokens or 0)
406-
+ (usage.cache_write_input_tokens or 0)
407-
)
401+
with capture_internal_exceptions():
402+
# Anthropic's input_tokens excludes cached/cache_write tokens.
403+
# Normalize to total input tokens for correct cost calculations.
404+
total_input = (
405+
usage.input_tokens
406+
+ (usage.cache_read_input_tokens or 0)
407+
+ (usage.cache_write_input_tokens or 0)
408+
)
408409

409-
_set_output_data(
410-
span=span,
411-
integration=integration,
412-
model=model,
413-
input_tokens=total_input,
414-
output_tokens=usage.output_tokens,
415-
cache_read_input_tokens=usage.cache_read_input_tokens,
416-
cache_write_input_tokens=usage.cache_write_input_tokens,
417-
content_blocks=[{"text": "".join(content_blocks), "type": "text"}],
418-
finish_span=True,
419-
response_id=response_id,
420-
)
410+
_set_output_data(
411+
span=span,
412+
integration=integration,
413+
model=model,
414+
input_tokens=total_input,
415+
output_tokens=usage.output_tokens,
416+
cache_read_input_tokens=usage.cache_read_input_tokens,
417+
cache_write_input_tokens=usage.cache_write_input_tokens,
418+
content_blocks=[{"text": "".join(content_blocks), "type": "text"}],
419+
finish_span=True,
420+
response_id=response_id,
421+
)
421422

422423

423424
async def _wrap_asynchronous_message_iterator(
@@ -466,26 +467,27 @@ async def _wrap_asynchronous_message_iterator(
466467
)
467468
yield event
468469
finally:
469-
# Anthropic's input_tokens excludes cached/cache_write tokens.
470-
# Normalize to total input tokens for correct cost calculations.
471-
total_input = (
472-
usage.input_tokens
473-
+ (usage.cache_read_input_tokens or 0)
474-
+ (usage.cache_write_input_tokens or 0)
475-
)
470+
with capture_internal_exceptions():
471+
# Anthropic's input_tokens excludes cached/cache_write tokens.
472+
# Normalize to total input tokens for correct cost calculations.
473+
total_input = (
474+
usage.input_tokens
475+
+ (usage.cache_read_input_tokens or 0)
476+
+ (usage.cache_write_input_tokens or 0)
477+
)
476478

477-
_set_output_data(
478-
span=span,
479-
integration=integration,
480-
model=model,
481-
input_tokens=total_input,
482-
output_tokens=usage.output_tokens,
483-
cache_read_input_tokens=usage.cache_read_input_tokens,
484-
cache_write_input_tokens=usage.cache_write_input_tokens,
485-
content_blocks=[{"text": "".join(content_blocks), "type": "text"}],
486-
finish_span=True,
487-
response_id=response_id,
488-
)
479+
_set_output_data(
480+
span=span,
481+
integration=integration,
482+
model=model,
483+
input_tokens=total_input,
484+
output_tokens=usage.output_tokens,
485+
cache_read_input_tokens=usage.cache_read_input_tokens,
486+
cache_write_input_tokens=usage.cache_write_input_tokens,
487+
content_blocks=[{"text": "".join(content_blocks), "type": "text"}],
488+
finish_span=True,
489+
response_id=response_id,
490+
)
489491

490492

491493
def _set_output_data(

0 commit comments

Comments
 (0)