feat(metrics): expose reasoning tokens in LLM metrics and traces - #2393
Open
rosetta-livekit-bot[bot] wants to merge 1 commit into
Open
feat(metrics): expose reasoning tokens in LLM metrics and traces#2393rosetta-livekit-bot[bot] wants to merge 1 commit into
rosetta-livekit-bot[bot] wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: 426ad82 The changes in this PR will be included in the next version bump. This PR includes changesets to release 39 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
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.
Summary
Port livekit/agents#7068 so OpenAI-compatible inference usage carries reasoning tokens through completion usage, LLM metrics, model usage aggregation, metric logs, and the
gen_ai.usage.reasoning_tokenstrace attribute. Reasoning tokens remain a subset of completion/output tokens and are never added to totals.Validation
pnpm build:agentspnpm test agents --silent(2324 passed, 5 skipped)pnpm --filter @livekit/agents lint(passes with existing warnings)pnpm --filter @livekit/agents typecheckpnpm --filter @livekit/agents throws:checkpnpm format:checkapi:checkstill reports unrelated pre-existing API report drift on the base branch. The API report changes in this PR are intentionally scoped to the new reasoning-token fields and trace constant.Source diff coverage
livekit-agents/livekit/agents/inference/llm.pyagents/src/inference/llm.ts: mapcompletion_tokens_details.reasoning_tokenstoreasoningTokens, defaulting to 0.livekit-agents/livekit/agents/llm/llm.pyagents/src/llm/llm.ts: expose completion usage, propagate it to LLM metrics, and conditionally set the reasoning-token span attribute.livekit-agents/livekit/agents/metrics/base.pyagents/src/metrics/base.ts: expose documentedreasoningTokens; optional TypeScript input preserves compatibility while emitted metrics normalize it to 0.livekit-agents/livekit/agents/metrics/usage.pyagents/src/metrics/model_usage.ts: expose and aggregateoutputReasoningTokensseparately fromoutputTokens.livekit-agents/livekit/agents/metrics/utils.pyagents/src/metrics/utils.ts: include camel-casereasoningTokensin LLM metric logs.livekit-agents/livekit/agents/telemetry/trace_types.pyagents/src/telemetry/trace_types.ts: addATTR_GEN_AI_USAGE_REASONING_TOKENS.tests/test_inference_llm_usage.pyagents/src/inference/llm.test.ts: port both detailed-reasoning and omitted-details cases using the existing stubbed stream helper.tests/test_metrics_usage.pyagents/src/llm/llm.test.tsandagents/src/metrics/model_usage.test.ts: port default, carry-through, aggregation, and subset-of-output assertions.tests/test_trace_types_pii.pyagents/src/telemetry/trace_types.test.ts: add the trace attribute to the exhaustive safe-key list.No source files are not applicable. Target-only generated/supporting changes are
agents/etc/agents.api.mdand.changeset/expose-reasoning-tokens.md. The shared agent-session protobuf has no reasoning-token usage field in either SDK, so remote-session serialization remains outside the source PR scope.Source PR: livekit/agents#7068
Ported from livekit/agents#7068
Original PR description
Carries
reasoning_tokensfrom OpenAI-compatible usage payloads through toLLMMetrics,LLMModelUsage, thelog_metricsline, and a newgen_ai.usage.reasoning_tokensspan attribute. Reasoning is a subset ofcompletion_tokens, never additive.The inference gateway already returns
completion_tokens_details.reasoning_tokens, butinference/llm.pynever read it, so the value was dropped before reaching metrics or traces.