Skip to content

fix(langsmith): record reasoning tokens as completion_reasoning_tokens - #2844

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

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

Conversation

@basil-k-aji-dev

Copy link
Copy Markdown

Summary

The LangSmith instrumentation extracted prompt_cached_tokens and prompt_cache_creation_tokens from usage_metadata.input_token_details but never read output_token_details, so reasoning tokens were missing from LangSmith-routed traces. The LangChain handler already reads the same shape and emits completion_reasoning_tokens.

extractMetrics now records completion_reasoning_tokens from usage_metadata.output_token_details.reasoning, using the file's existing assignFirstMetric helper (a finite number >= 0 is recorded, so 0 is kept; strings and absent values are skipped).

Tests

langsmith-instrumentation.test.ts: the lifecycle test now carries output_token_details: { reasoning: 7 } and expects completion_reasoning_tokens: 7, and a new test covers 0, no output_token_details, a string, and reasoning only under input_token_details.

  • Without the change: 2 tests fail. With it: the file's 8 tests pass; check:typings, lint, the formatting check and validate-changesets are clean.
  • Dropping the call, a wrong key, reading input_token_details, a wrong metric name, dropping zeros or accepting strings each fail a test.
  • The providers directory passes apart from a 15s timeout in livekit-plugin.test.ts, which does not touch this code and passes on its own.

Things to know

  • The LangSmith e2e fixtures only supply prompt_tokens_details.cached_tokens, so I expect no snapshot drift, but I could not run the e2e scenarios locally. Re-recording against a real OpenAI response could add completion_reasoning_tokens: 0 to those snapshots.
  • Other LangSmith usage fields are still unread (audio entries, service-tier-prefixed keys such as flex_reasoning, and the *_cost fields); I kept this to reasoning tokens.

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