Skip to content

fix(langchain): record audio token metrics from usage metadata - #2843

Open
BASIL K AJI (basil-k-aji-dev) wants to merge 1 commit into
braintrustdata:mainfrom
basil-k-aji-dev:fix/langchain-audio-tokens
Open

BASIL K AJI (basil-k-aji-dev) wants to merge 1 commit into
braintrustdata:mainfrom
basil-k-aji-dev:fix/langchain-audio-tokens

Conversation

@basil-k-aji-dev

Copy link
Copy Markdown

Summary

The JS LangChain callback handler read usage_metadata.input_token_details and output_token_details for cache and reasoning tokens but ignored their audio entries, so audio-model LangChain spans lost audio-token cost attribution. The Python SDK records both (#1166), and the other JS providers use the same metric names.

getMetricsFromResponse now emits prompt_audio_tokens from input_token_details.audio and completion_audio_tokens from output_token_details.audio, next to the cache and reasoning handling and behind the same guards. Non-numbers are dropped and 0 is kept. Zero is emitted because Python emits audio whenever it is not None, and this function already emits prompt_cached_tokens: 0 and completion_reasoning_tokens: 0 the same way, so many OpenAI-style spans will now also carry prompt_audio_tokens: 0.

Tests

callback-handler.test.ts: an end-to-end case plus six it.each cases (absent details, details without audio, non-number audio, non-record details, input-only, output-only). One existing test, "reports zero reasoning tokens rather than dropping the field", already fed audio: 0 on both sides and expected no audio metrics; it now expects prompt_audio_tokens: 0 and completion_audio_tokens: 0.

  • Without the change: 4 tests fail, with the audio keys missing from the metrics.
  • With the change: the file's 66 tests pass, and check:typings, lint, the formatting check and validate-changesets are clean.
  • Dropping either line, swapping input and output, wrong names, coercing strings or dropping zeros each fail a test.

This branch has not been deployed

No deployments
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