fix: return 0 completion tokens for empty content - #1633
Conversation
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
WalkthroughThis PR modifies the token estimation utility to return 0 tokens for empty content instead of the previous minimum of 1 token. The implementation adds an early guard clause, and corresponding test cases are updated to reflect this new behavior. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 🧹 Recent nitpick comments
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 |
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug in token estimation where empty content was incorrectly reporting 1 token instead of 0. The issue affected error responses and content_filter responses that have no content. The fix adds an early return for empty content before the Math.max(1, ...) calculation that was forcing a minimum of 1 token.
Changes:
- Modified
estimateTokensFromContentto return 0 for empty content instead of 1 - Updated and added unit tests to verify empty content returns 0 tokens and non-empty content returns at least 1 token
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| apps/gateway/src/chat/tools/estimate-tokens-from-content.ts | Added early return to output 0 tokens for empty content |
| apps/gateway/src/lib/prompt-tokens.spec.ts | Updated tests to expect 0 tokens for empty content and added dedicated test cases |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Summary
estimateTokensFromContent("")was returning 1 instead of 0 due toMath.max(1, ...), causing error and content_filter responses to report 1 completion token instead of 0Test plan
🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes