feat: adopt current OTEL cached token conventions#5447
Draft
bml1g12 wants to merge 2 commits intolivekit:mainfrom
Draft
feat: adopt current OTEL cached token conventions#5447bml1g12 wants to merge 2 commits intolivekit:mainfrom
bml1g12 wants to merge 2 commits intolivekit:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
Today, when we emit the
gen_ai.usage.input_tokensmetric, we calculate this already correctly in LiveKit (as defined by https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-spans/) however two gaps exist in LiveKit today:Summary
Aligns realtime and related LLM spans with current OpenTelemetry GenAI usage conventions and Langfuse OTEL ingestion (langfuse#13110): top-level
gen_ai.usage.input_tokens/output_tokensrepresent text counts; cache read uses the standard dotted key; audio breakdowns usegen_ai.usage.details.*so backends can normalize usage without double-counting cache or mixing modalities.Internal metrics (
RealtimeModelMetrics,ModelUsageCollector, Prometheus counters) are unchanged—only trace attributes andtrace_typesconstants are updated.Attribute mapping
Realtime spans (
record_realtime_metrics) now set:RealtimeModelMetricsgen_ai.usage.input_tokensinput_token_details.text_tokensgen_ai.usage.output_tokensoutput_token_details.text_tokensgen_ai.usage.cache_read.input_tokenscached_tokens_details.text_tokens(if non-zero)gen_ai.usage.details.input_audio_tokensinput_token_details.audio_tokensgen_ai.usage.details.cache_audio_read_tokenscached_tokens_details.audio_tokensgen_ai.usage.details.output_audio_tokensoutput_token_details.audio_tokensgen_ai.usage.cache_creation.input_tokensis not set until a provider exposes cache-creation counts in our metrics model.Removed trace type constants (not in the OTEL spec)
These
ATTR_GEN_AI_USAGE_*aliases were removed because their string keys were informal (LiveKit/Langfuse-style flat names) and not defined in OpenTelemetry GenAI semantic conventions:ATTR_GEN_AI_USAGE_INPUT_TEXT_TOKENSgen_ai.usage.input_text_tokensATTR_GEN_AI_USAGE_INPUT_AUDIO_TOKENSgen_ai.usage.input_audio_tokensATTR_GEN_AI_USAGE_INPUT_CACHED_TOKENSgen_ai.usage.input_cached_tokensATTR_GEN_AI_USAGE_OUTPUT_TEXT_TOKENSgen_ai.usage.output_text_tokensATTR_GEN_AI_USAGE_OUTPUT_AUDIO_TOKENSgen_ai.usage.output_audio_tokensTop-level
gen_ai.usage.input_tokensandgen_ai.usage.output_tokensremain; they now map to text token fields only on realtime spans, consistent with semconv + Langfuse normalization.Other call sites
llm.py): setgen_ai.usage.cache_read.input_tokenswhenprompt_cached_tokens > 0.run_result.py): same cache-read key; redundant duplicate text keys removed.Tests
record_realtime_metrics(including omission of zero breakdowns).Breaking note: External code that imported the five removed
ATTR_GEN_AI_USAGE_*names must switch to the new constants (ATTR_GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,ATTR_GEN_AI_USAGE_DETAILS_*, etc.).