Skip to content

Commit ebb728a

Browse files
committed
Remove total_tokens
1 parent 4d1497e commit ebb728a

File tree

3 files changed

+18
-44
lines changed

3 files changed

+18
-44
lines changed

instrumentation-genai/opentelemetry-instrumentation-openai-v2/src/opentelemetry/instrumentation/openai_v2/patch.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ def _record_metrics(
340340
)
341341

342342
if result and getattr(result, "usage", None):
343+
# Always record input tokens
343344
input_attributes = {
344345
**common_attributes,
345346
GenAIAttributes.GEN_AI_TOKEN_TYPE: GenAIAttributes.GenAiTokenTypeValues.INPUT.value,
@@ -349,19 +350,18 @@ def _record_metrics(
349350
attributes=input_attributes,
350351
)
351352

352-
token_count = (
353-
result.usage.total_tokens
354-
if operation_name
355-
== GenAIAttributes.GenAiOperationNameValues.EMBEDDINGS.value
356-
else result.usage.completion_tokens
357-
)
358-
attributes = {
359-
**common_attributes,
360-
GenAIAttributes.GEN_AI_TOKEN_TYPE: GenAIAttributes.GenAiTokenTypeValues.COMPLETION.value,
361-
}
362-
instruments.token_usage_histogram.record(
363-
token_count, attributes=attributes
364-
)
353+
# For embeddings, don't record output tokens as all tokens are input tokens
354+
if (
355+
operation_name
356+
!= GenAIAttributes.GenAiOperationNameValues.EMBEDDINGS.value
357+
):
358+
output_attributes = {
359+
**common_attributes,
360+
GenAIAttributes.GEN_AI_TOKEN_TYPE: GenAIAttributes.GenAiTokenTypeValues.COMPLETION.value,
361+
}
362+
instruments.token_usage_histogram.record(
363+
result.usage.completion_tokens, attributes=output_attributes
364+
)
365365

366366

367367
def _set_response_attributes(
@@ -436,11 +436,7 @@ def _set_embeddings_response_attributes(
436436
GenAIAttributes.GEN_AI_USAGE_INPUT_TOKENS,
437437
result.usage.prompt_tokens,
438438
)
439-
set_span_attribute(
440-
span,
441-
GenAIAttributes.GEN_AI_USAGE_OUTPUT_TOKENS,
442-
result.usage.total_tokens,
443-
)
439+
# Don't set output tokens for embeddings as all tokens are input tokens
444440

445441
# Emit events for embeddings if content capture is enabled
446442
if capture_content:

instrumentation-genai/opentelemetry-instrumentation-openai-v2/tests/test_async_embeddings.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -243,25 +243,18 @@ async def test_async_embeddings_token_metrics(
243243

244244
# Find the input token data point
245245
input_token_point = None
246-
output_token_point = None
247246
for point in token_metric.data.data_points:
248247
if (
249248
point.attributes[GenAIAttributes.GEN_AI_TOKEN_TYPE]
250249
== GenAIAttributes.GenAiTokenTypeValues.INPUT.value
251250
):
252251
input_token_point = point
253-
elif (
254-
point.attributes[GenAIAttributes.GEN_AI_TOKEN_TYPE]
255-
== GenAIAttributes.GenAiTokenTypeValues.COMPLETION.value
256-
):
257-
output_token_point = point
252+
break
258253

259254
assert input_token_point is not None, "Input token metric not found"
260-
assert output_token_point is not None, "Total token metric not found"
261255

262256
# Verify the token counts match what was reported in the response
263257
assert input_token_point.sum == response.usage.prompt_tokens
264-
assert output_token_point.sum == response.usage.total_tokens
265258

266259

267260
def assert_embedding_attributes(
@@ -277,7 +270,7 @@ def assert_embedding_attributes(
277270
response_id=None, # Embeddings don't have a response ID
278271
response_model=response.model,
279272
input_tokens=response.usage.prompt_tokens,
280-
output_tokens=response.usage.total_tokens, # Use total_tokens for output_tokens
273+
output_tokens=None, # Embeddings don't have separate output tokens
281274
operation_name="embeddings",
282275
server_address="api.openai.com",
283276
)
@@ -297,10 +290,6 @@ def assert_embedding_attributes(
297290
span.attributes[GenAIAttributes.GEN_AI_USAGE_INPUT_TOKENS]
298291
== response.usage.prompt_tokens
299292
)
300-
assert (
301-
span.attributes[GenAIAttributes.GEN_AI_USAGE_OUTPUT_TOKENS]
302-
== response.usage.total_tokens
303-
)
304293

305294

306295
def assert_all_attributes(

instrumentation-genai/opentelemetry-instrumentation-openai-v2/tests/test_embeddings.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -376,25 +376,18 @@ def test_embeddings_token_metrics(
376376

377377
# Find the input token data point
378378
input_token_point = None
379-
output_token_point = None
380379
for point in token_metric.data.data_points:
381380
if (
382381
point.attributes[GenAIAttributes.GEN_AI_TOKEN_TYPE]
383382
== GenAIAttributes.GenAiTokenTypeValues.INPUT.value
384383
):
385384
input_token_point = point
386-
elif (
387-
point.attributes[GenAIAttributes.GEN_AI_TOKEN_TYPE]
388-
== GenAIAttributes.GenAiTokenTypeValues.COMPLETION.value
389-
):
390-
output_token_point = point
385+
break
391386

392387
assert input_token_point is not None, "Input token metric not found"
393-
assert output_token_point is not None, "Output token metric not found"
394388

395389
# Verify the token counts match what was reported in the response
396390
assert input_token_point.sum == response.usage.prompt_tokens
397-
assert output_token_point.sum == response.usage.total_tokens
398391

399392

400393
def assert_embedding_attributes(
@@ -410,7 +403,7 @@ def assert_embedding_attributes(
410403
response_id=None, # Embeddings don't have a response ID
411404
response_model=response.model,
412405
input_tokens=response.usage.prompt_tokens,
413-
output_tokens=response.usage.total_tokens, # Use total_tokens for output_tokens
406+
output_tokens=None, # Embeddings don't have separate output tokens
414407
operation_name="embeddings",
415408
server_address="api.openai.com",
416409
)
@@ -430,10 +423,6 @@ def assert_embedding_attributes(
430423
span.attributes[GenAIAttributes.GEN_AI_USAGE_INPUT_TOKENS]
431424
== response.usage.prompt_tokens
432425
)
433-
assert (
434-
span.attributes[GenAIAttributes.GEN_AI_USAGE_OUTPUT_TOKENS]
435-
== response.usage.total_tokens
436-
)
437426

438427

439428
def assert_all_attributes(

0 commit comments

Comments
 (0)