Skip to content

fix(llm): attribute fallback inference telemetry to providers - #2454

Open
chasef07 wants to merge 1 commit into
livekit:mainfrom
chasef07:codex/fix-fallback-model-telemetry
Open

fix(llm): attribute fallback inference telemetry to providers#2454
chasef07 wants to merge 1 commit into
livekit:mainfrom
chasef07:codex/fix-fallback-model-telemetry

Conversation

@chasef07

@chasef07 chasef07 commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Description

An LLM FallbackAdapter currently reports a second llm_request and llm_metrics record alongside the real provider request. For example, one primary request using 10 input and 2 output tokens produces another identical usage record for model FallbackAdapter, provider unknown. The enclosing llm_node also copies the wrapper model name into its inference attributes.

Keep fallback orchestration visible while attributing inference and usage to the actual providers. Agent-start metadata describes the configured primary; individual request spans retain the actual primary or backup model used.

Changes Made

  • Add an LLMStream adapter-span hook and use llm_fallback_adapter for the fallback stream. Forward its chunks without emitting another inference metrics record or duplicating GenAI attributes.
  • Mark adapter orchestration in the existing inference context so llm_node does not claim the configured wrapper as a model. Resolve nested adapters to their configured primary for agent-start metadata.
  • Regenerate the API report and add nine regression cases covering primary use, failover, token aggregation, span hierarchy, nested adapters, failure, cancellation, voice generation, and agent startup.

Pre-Review Checklist

  • Build passes: Agents package build/typecheck, workspace lint, formatting, and API check pass locally.
  • AI-generated code reviewed: Standards and spec reviews completed; remaining wrapper attribution found during review was fixed and regression-tested.
  • Changes explained: Behavior and validation described here.
  • Scope appropriate: Changes address fallback model attribution and duplicate inference accounting.
  • Video demo: Not applicable; this is a telemetry change, verified with in-memory traces and synthetic providers.

Testing

  • Automated tests added. The original primary/failover tests failed with extra FallbackAdapter usage records and request spans before the fix. Voice-generation and agent-start tests separately reproduced the remaining wrapper attribution before its fix.
  • pnpm test agents --silent: 2,499 passed, 5 skipped.
  • pnpm --filter @livekit/agents build
  • pnpm --filter @livekit/agents typecheck
  • pnpm --filter @livekit/agents api:check
  • pnpm -w lint:fix and pnpm format:check (existing lint warnings only).
  • Restaurant/realtime agent demos: Not run; no credentials or live provider calls are needed for these regression tests.

Additional Notes

FallbackAdapter.model and label() retain their wrapper identity. Retry order and provider error behavior are unchanged. Consumers that counted wrapper-generated metrics will see those duplicate records disappear. Live Cloud analytics UI behavior has not been independently verified; this PR verifies the emitted telemetry and usage collector inputs.


Note to reviewers: Please ensure the pre-review checklist is completed before starting your review.

@chasef07
chasef07 requested a review from a team as a code owner September 9, 2026 00:31
@changeset-bot

changeset-bot Bot commented Sep 9, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 5f7e798

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 9, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-09T00:35:28.452040Z 5f7e798 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

Comment thread agents/src/llm/llm.ts
Comment on lines +382 to +384
if (this.adapterSpanName !== undefined) {
this.#llmRequestSpan?.end();
return;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Closed fallback streams retain orchestration spans

close() during a pending provider request leaves monitorMetrics blocked until the adapter queue closes. The abort signal closes only the output queue, while run and its child stream continue. The orchestration span outlives the canceled request, potentially through every provider timeout.

Prompt for agents
Fallback adapter cancellation does not propagate from LLMStream.close() to the active child stream in agents/src/llm/fallback_adapter.ts. monitorMetrics in agents/src/llm/llm.ts waits for the adapter queue to close before ending llm_fallback_adapter, so closing the consumer while a provider request is pending leaves the span and provider work active until completion or timeout. Update the adapter cancellation flow so close wakes the metrics monitor, terminates the orchestration span, and closes or aborts the current child stream without starting further fallback or recovery work.
Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant