Skip to content

Commit d36887b

Browse files
feat(typesafe): Record token usage (#7719)
Record `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` attributes.
1 parent 166b85a commit d36887b

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
@@ -221,6 +221,16 @@ def wrap_system_one(self: "TypeSafeClient", *args: "Any", **kwargs: "Any") -> "A
221221

222222
set_on_span(SPANDATA.GEN_AI_RESPONSE_MODEL, response.model)
223223

224+
if response.usage.input_tokens is not None:
225+
set_on_span(
226+
SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, response.usage.input_tokens
227+
)
228+
229+
if response.usage.output_tokens is not None:
230+
set_on_span(
231+
SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, response.usage.output_tokens
232+
)
233+
224234
return response
225235

226236
return wrap_system_one
@@ -297,6 +307,16 @@ async def wrap_system_one_async(
297307

298308
set_on_span(SPANDATA.GEN_AI_RESPONSE_MODEL, response.model)
299309

310+
if response.usage.input_tokens is not None:
311+
set_on_span(
312+
SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, response.usage.input_tokens
313+
)
314+
315+
if response.usage.output_tokens is not None:
316+
set_on_span(
317+
SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, response.usage.output_tokens
318+
)
319+
300320
return response
301321

302322
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
@@ -122,6 +122,9 @@ def test_system_one(
122122

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

125+
assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 12
126+
assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 3
127+
125128
assert json.loads(span["attributes"][SPANDATA.GEN_AI_INPUT_MESSAGES]) == [
126129
{
127130
"type": "evaluation",
@@ -205,6 +208,9 @@ def test_system_one(
205208

206209
assert span["data"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "jev-latest"
207210

211+
assert span["data"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 12
212+
assert span["data"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 3
213+
208214
assert json.loads(span["data"][SPANDATA.GEN_AI_INPUT_MESSAGES]) == [
209215
{
210216
"type": "evaluation",
@@ -318,6 +324,9 @@ async def test_system_one_async(
318324

319325
assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "jev-latest"
320326

327+
assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 12
328+
assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 3
329+
321330
assert json.loads(span["attributes"][SPANDATA.GEN_AI_INPUT_MESSAGES]) == [
322331
{
323332
"type": "evaluation",
@@ -404,6 +413,9 @@ async def test_system_one_async(
404413

405414
assert span["data"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "jev-latest"
406415

416+
assert span["data"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 12
417+
assert span["data"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 3
418+
407419
assert json.loads(span["data"][SPANDATA.GEN_AI_INPUT_MESSAGES]) == [
408420
{
409421
"type": "evaluation",

0 commit comments

Comments
 (0)