Skip to content

Commit fc33f1c

Browse files
authored
fix(langchain): emit cache creation tokens (#4261)
1 parent aa4a469 commit fc33f1c

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

  • packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain

packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/span_utils.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,8 @@ def set_chat_response_usage(
393393
input_tokens = 0
394394
output_tokens = 0
395395
total_tokens = 0
396-
cache_read_tokens = 0
396+
cache_read_tokens = None
397+
cache_creation_tokens = None
397398

398399
# Early return if no generations to avoid potential issues
399400
if not response.generations:
@@ -423,7 +424,12 @@ def set_chat_response_usage(
423424
input_token_details = generation.message.usage_metadata.get(
424425
"input_token_details", {}
425426
)
426-
cache_read_tokens += input_token_details.get("cache_read", 0)
427+
raw_cache_read = input_token_details.get("cache_read")
428+
if isinstance(raw_cache_read, (int, float)):
429+
cache_read_tokens = (cache_read_tokens or 0) + raw_cache_read
430+
raw_cache_creation = input_token_details.get("cache_creation")
431+
if isinstance(raw_cache_creation, (int, float)):
432+
cache_creation_tokens = (cache_creation_tokens or 0) + raw_cache_creation
427433
except Exception as e:
428434
# If there's any issue processing usage metadata, continue without it
429435
logger.warning("Error processing usage metadata: %s", e)
@@ -433,7 +439,8 @@ def set_chat_response_usage(
433439
input_tokens > 0
434440
or output_tokens > 0
435441
or total_tokens > 0
436-
or cache_read_tokens > 0
442+
or cache_read_tokens is not None
443+
or cache_creation_tokens is not None
437444
):
438445
_set_span_attribute(
439446
span,
@@ -455,6 +462,11 @@ def set_chat_response_usage(
455462
GenAIAttributes.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
456463
cache_read_tokens,
457464
)
465+
_set_span_attribute(
466+
span,
467+
GenAIAttributes.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS,
468+
cache_creation_tokens,
469+
)
458470
if record_token_usage:
459471
vendor = span.attributes.get(GenAIAttributes.GEN_AI_PROVIDER_NAME, "langchain")
460472

0 commit comments

Comments
 (0)