Skip to content

Commit 43bad0c

Browse files
committed
make cost declarative in embeddings
1 parent 1cba433 commit 43bad0c

2 files changed

Lines changed: 13 additions & 12 deletions

File tree

sentry_sdk/ai/span_config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,7 @@ def set_response_span_data(
105105
output_tokens=get_first_from_sources(
106106
response, usage_config.get("output_tokens", [])
107107
),
108+
total_tokens=get_first_from_sources(
109+
response, usage_config.get("total_tokens", [])
110+
),
108111
)

sentry_sdk/integrations/cohere/__init__.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import sys
22
from functools import wraps
33

4-
from sentry_sdk.ai.monitoring import record_token_usage
54
from sentry_sdk.consts import OP, SPANDATA
6-
from sentry_sdk.ai.span_config import set_request_span_data
5+
from sentry_sdk.ai.span_config import set_request_span_data, set_response_span_data
76

87
from typing import TYPE_CHECKING
98

@@ -42,6 +41,12 @@ def _normalize_embedding_input(texts):
4241
),
4342
"message_target": SPANDATA.GEN_AI_EMBEDDINGS_INPUT,
4443
"truncation_fn": None,
44+
"response": {
45+
"usage": {
46+
"input_tokens": [("meta", "billed_units", "input_tokens")],
47+
"total_tokens": [("meta", "billed_units", "input_tokens")],
48+
},
49+
},
4550
}
4651

4752

@@ -102,16 +107,9 @@ def new_embed(*args, **kwargs):
102107
_capture_exception(e)
103108
reraise(*exc_info)
104109

105-
if (
106-
hasattr(res, "meta")
107-
and hasattr(res.meta, "billed_units")
108-
and hasattr(res.meta.billed_units, "input_tokens")
109-
):
110-
record_token_usage(
111-
span,
112-
input_tokens=res.meta.billed_units.input_tokens,
113-
total_tokens=res.meta.billed_units.input_tokens,
114-
)
110+
set_response_span_data(
111+
span, res, False, COHERE_EMBED_CONFIG["response"]
112+
)
115113
return res
116114

117115
return new_embed

0 commit comments

Comments
 (0)