Skip to content

fix: return 0 completion tokens for empty content - #1633

Merged
steebchen merged 1 commit into
mainfrom
fix-zero-completion-tokens
Feb 10, 2026
Merged

steebchen merged 1 commit into
mainfrom
fix-zero-completion-tokens

Conversation

@steebchen

@steebchen steebchen commented Feb 10, 2026

Copy link
Copy Markdown
Member

Summary

  • estimateTokensFromContent("") was returning 1 instead of 0 due to Math.max(1, ...), causing error and content_filter responses to report 1 completion token instead of 0
  • Added early return of 0 for empty content strings

Test plan

  • Updated unit tests to verify empty content returns 0 tokens
  • All existing unit tests pass

🤖 Generated with Claude Code

Summary by CodeRabbit

Bug Fixes

  • Improved token estimation accuracy for empty content. Empty inputs now correctly estimate to 0 tokens instead of 1, preventing inflated token counts and providing more precise usage tracking.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings February 10, 2026 17:36
@steebchen
steebchen enabled auto-merge February 10, 2026 17:36
@coderabbitai

coderabbitai Bot commented Feb 10, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

This 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

Cohort / File(s) Summary
Token Estimation Implementation
apps/gateway/src/chat/tools/estimate-tokens-from-content.ts
Adds early guard to return 0 tokens for falsy/empty content, replacing the prior behavior that enforced a minimum of 1 token.
Token Estimation Tests
apps/gateway/src/lib/prompt-tokens.spec.ts
Updates test expectations for empty string from 1 to 0 tokens; replaces assertion of constant minimum with separate tests for empty (0) and non-empty (≥1) content.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: return 0 completion tokens for empty content' directly and clearly summarizes the main change in the pull request, which adds an early guard to return 0 tokens for empty strings instead of 1.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-zero-completion-tokens

No actionable comments were generated in the recent review. 🎉

🧹 Recent nitpick comments
apps/gateway/src/lib/prompt-tokens.spec.ts (1)

11-11: Minor test duplication: empty string assertion appears twice.

Line 11 and lines 19–21 both assert estimateTokensFromContent("") === 0. This is harmless but redundant — consider removing one to keep the suite concise.

Also applies to: 19-21


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI 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.

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 estimateTokensFromContent to 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.

@steebchen
steebchen added this pull request to the merge queue Feb 10, 2026
Merged via the queue into main with commit 968890e Feb 10, 2026
19 of 20 checks passed
@steebchen
steebchen deleted the fix-zero-completion-tokens branch February 10, 2026 18:00
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.

2 participants