fix(instrumentation-bedrock): avoid NaN total_tokens for Titan embedding responses#1032
Conversation
…ing responses Titan embedding responses carry inputTextTokenCount but no output-token field, so computing total_tokens as input + undefined produced NaN, which was serialized as the string "NaN" on exported spans. Guard both the streaming and non-streaming Amazon paths: omit output_tokens when the response has none and compute total_tokens as input + (output ?? 0). Fixes traceloop#1031 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughChangesTitan token accounting
Estimated code review effort: 2 (Simple) | ~10 minutes Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint install timed out. The project may have too many dependencies for the sandbox. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
Fixes #1031.
Titan embedding responses (e.g.
amazon.titan-embed-text-v2:0) carryinputTextTokenCountbut no output-token field — there is no generated text. Both Amazon response paths computedtotal_tokensasinput + undefined, which evaluates toNaNand gets serialized as the string"NaN"on the exported span. In our production telemetry, 100% of spans carryingtotal_tokensfrom this path had the value"NaN".Changes
titanOutputTokens(response.results?.[0]?.tokenCount) doesn't exist for embeddings. Now omitsgen_ai.usage.output_tokenswhen absent and computestotal_tokens = input + (output ?? 0).totalOutputTextTokenCount.total_tokens = inputis the semantically correct value for them.Tests
Added
tests/titan-embedding-tokens.test.ts(same direct-handler style ascache-token-fold-in.test.ts) covering:total_tokensequal to input,output_tokensomitted, never NaNinput + outputbehavior (regression)nx test @traceloop/instrumentation-bedrock: 82 passing (78 existing + 4 new). Lint clean.🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
NaNvalues.Tests