Skip to content

Fix LangChain JS token total metrics#2181

Merged
Stephen Belanger (Qard) merged 6 commits into
mainfrom
langchain-js-token-total-metrics
Jul 17, 2026
Merged

Fix LangChain JS token total metrics#2181
Stephen Belanger (Qard) merged 6 commits into
mainfrom
langchain-js-token-total-metrics

Conversation

@Qard

Copy link
Copy Markdown
Collaborator

Summary

  • Emit canonical metrics.tokens for LangChain JS LLM spans.
  • Prefer explicit LangChain totals when present.
  • Synthesize tokens from prompt and completion counts when LangChain omits an explicit total.
  • Preserve existing cache token mappings.

Root Cause

LangChain JS callback spans were already logging prompt and completion token metrics, but the callback handler only emitted the legacy total_tokens field for totals. Braintrust cost/token insights expect canonical metrics.tokens, so spans with complete prompt/completion data could still look like they were missing total token usage.

Validation

  • mise exec -- pnpm --dir js exec vitest run src/wrappers/langchain/callback-handler.test.ts
  • mise exec -- pnpm --dir js exec vitest run src/instrumentation/plugins/langchain-plugin.test.ts
  • mise exec -- pnpm exec prettier --check js/src/wrappers/langchain/callback-handler.ts js/src/wrappers/langchain/callback-handler.test.ts .changeset/langchain-js-token-total-metrics.md
  • git diff --check

@Qard
Stephen Belanger (Qard) force-pushed the langchain-js-token-total-metrics branch from 0f590ca to 98e3bed Compare July 1, 2026 17:53
Comment thread e2e/scenarios/wrap-langchain-js-traces/__snapshots__/span-tree.json Outdated
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. If this PR is still relevant, please leave a comment, push an update, or remove the stale label. Thank you for your contributions!

@github-actions github-actions Bot added the stale label Jul 17, 2026
@Qard
Stephen Belanger (Qard) force-pushed the langchain-js-token-total-metrics branch from 720fdf9 to bbc497c Compare July 17, 2026 11:04
@Qard
Stephen Belanger (Qard) marked this pull request as ready for review July 17, 2026 11:08

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bbc497cbb5

ℹ️ 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 (@codex) review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "Codex (@codex) address that feedback".

Comment thread e2e/scenarios/langgraph-auto-instrumentation/__snapshots__/span-tree.txt Outdated
@Qard
Stephen Belanger (Qard) merged commit 2b6b73b into main Jul 17, 2026
48 checks passed
@Qard
Stephen Belanger (Qard) deleted the langchain-js-token-total-metrics branch July 17, 2026 12:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants