Fix LangChain JS token total metrics#2181
Draft
Stephen Belanger (Qard) wants to merge 3 commits into
Draft
Conversation
0f590ca to
98e3bed
Compare
Comment on lines
+1266
to
1267
| "tokens": 97, | ||
| "total_tokens": 97 |
There was a problem hiding this comment.
What's the actual diff between tokens and total_tokens?
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
metrics.tokensfor LangChain JS LLM spans.tokensfrom prompt and completion counts when LangChain omits an explicit total.Root Cause
LangChain JS callback spans were already logging prompt and completion token metrics, but the callback handler only emitted the legacy
total_tokensfield for totals. Braintrust cost/token insights expect canonicalmetrics.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.tsmise exec -- pnpm --dir js exec vitest run src/instrumentation/plugins/langchain-plugin.test.tsmise 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.mdgit diff --check