Skip to content

Commit 17261c5

Browse files
feat(typesafe): Set token usage attributes
1 parent 0c39434 commit 17261c5

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

‎sentry_sdk/integrations/typesafe.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,16 @@ def wrap_system_one(self: "TypeSafeClient", *args: "Any", **kwargs: "Any") -> "A
212212
if not isinstance(response, SystemOneResponse):
213213
return response
214214

215+
if response.usage.input_tokens is not None:
216+
set_on_span(
217+
SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, response.usage.input_tokens
218+
)
219+
220+
if response.usage.output_tokens is not None:
221+
set_on_span(
222+
SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, response.usage.output_tokens
223+
)
224+
215225
return response
216226

217227
return wrap_system_one
@@ -280,6 +290,16 @@ async def wrap_system_one_async(
280290
if not isinstance(response, SystemOneResponse):
281291
return response
282292

293+
if response.usage.input_tokens is not None:
294+
set_on_span(
295+
SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, response.usage.input_tokens
296+
)
297+
298+
if response.usage.output_tokens is not None:
299+
set_on_span(
300+
SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, response.usage.output_tokens
301+
)
302+
283303
return response
284304

285305
return wrap_system_one_async

‎tests/integrations/typesafe/test_typesafe.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ def test_system_one(
121121

122122
assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "jev-latest"
123123

124+
assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 12
125+
assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 3
126+
124127
assert json.loads(span["attributes"][SPANDATA.GEN_AI_INPUT_MESSAGES]) == {
125128
"type": "evaluation",
126129
"state": {
@@ -208,6 +211,9 @@ def test_system_one(
208211

209212
assert span["data"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "jev-latest"
210213

214+
assert span["data"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 12
215+
assert span["data"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 3
216+
211217
assert json.loads(span["data"][SPANDATA.GEN_AI_INPUT_MESSAGES]) == {
212218
"type": "evaluation",
213219
"state": {
@@ -324,6 +330,9 @@ async def test_system_one_async(
324330

325331
assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "jev-latest"
326332

333+
assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 12
334+
assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 3
335+
327336
assert json.loads(span["attributes"][SPANDATA.GEN_AI_INPUT_MESSAGES]) == {
328337
"type": "evaluation",
329338
"state": {
@@ -414,6 +423,9 @@ async def test_system_one_async(
414423

415424
assert span["data"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "jev-latest"
416425

426+
assert span["data"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 12
427+
assert span["data"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 3
428+
417429
assert json.loads(span["data"][SPANDATA.GEN_AI_INPUT_MESSAGES]) == {
418430
"type": "evaluation",
419431
"state": {

0 commit comments

Comments
 (0)