Skip to content

fix(chat): remove unused Anthropic token estimation logic - #385

Merged
steebchen merged 1 commit into
mainfrom
fix/chat-ant
Jun 22, 2025
Merged

steebchen merged 1 commit into
mainfrom
fix/chat-ant

Conversation

@steebchen

@steebchen steebchen commented Jun 22, 2025 •

Copy link
Copy Markdown
Member

Removed outdated logic for estimating prompt tokens when usage data is incomplete for the Anthropic provider. This simplifies and cleans up the chat token handling code.

Summary by CodeRabbit

  • Refactor
    • Removed estimation of prompt tokens from streaming responses for the Anthropic provider.

Removed outdated logic for estimating prompt tokens when usage data is incomplete for the Anthropic provider.
This simplifies and cleans up the chat token handling code.
@coderabbitai

coderabbitai Bot commented Jun 22, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

The change removes logic that estimated prompt tokens for Anthropic streaming responses when only output tokens were available. The code previously inferred prompt token counts by dividing message content lengths, but this estimation step is now deleted. No other modifications to the streaming response handling or exported entities were made.

Changes

File(s) Change Summary
apps/gateway/src/chat/chat.ts Removed estimation and completion of prompt token usage for Anthropic streaming chunk responses

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Gateway
    participant Anthropic

    Client->>Gateway: Send streaming chat request
    Gateway->>Anthropic: Forward request
    Anthropic-->>Gateway: Stream chunk (may lack prompt_tokens)
    Gateway-->>Client: Forward chunk without estimating prompt_tokens
Loading

Possibly related PRs


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ab05ad6 and 5ffe313.

📒 Files selected for processing (1)
  • apps/gateway/src/chat/chat.ts (0 hunks)
💤 Files with no reviewable changes (1)
  • apps/gateway/src/chat/chat.ts
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: claude-review
  • GitHub Check: build / run
  • GitHub Check: e2e / run
  • GitHub Check: test / run
  • GitHub Check: lint / run
  • GitHub Check: generate / run
✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@steebchen
steebchen enabled auto-merge June 22, 2025 01:50
@claude

claude Bot commented Jun 22, 2025 •

Copy link
Copy Markdown
Contributor

Claude finished @steebchen's task —— View job


Code Review Complete ✅

Reviewing PR #385: "fix(chat): remove unused Anthropic token estimation logic"

Tasks:

  • Get PR diff to understand code changes
  • Analyze removed code for potential issues
  • Check if removal affects functionality
  • Verify test coverage considerations
  • Provide review feedback

Review Summary

✅ This is a clean, safe refactoring that removes outdated workaround code.

Analysis

What was removed:

  • 23 lines of Anthropic-specific token estimation logic in apps/gateway/src/chat/chat.ts:1540-1562
  • The code handled the edge case where Anthropic streaming responses provided output_tokens but not prompt_tokens
  • Used a simple estimation: message.content.length / 4 to approximate prompt tokens

Why this removal is safe:

  1. Fallback logic still exists - The code still has token estimation logic at apps/gateway/src/chat/chat.ts:1583-1598 that handles cases where Anthropic doesn't provide any usage data at all.

  2. No test coverage impact - Analysis of 8 test files shows no tests were specifically verifying this removed estimation logic. The main E2E tests use real Anthropic APIs which should provide complete usage data.

  3. Edge case handling - This appeared to be a workaround for incomplete Anthropic usage data that may no longer be relevant if Anthropic now consistently provides complete token counts in streaming responses.

  4. Code simplification - Removing provider-specific workarounds improves maintainability without affecting core functionality.

Recommendation

Approve and merge - This is a clean refactoring that removes unused code without introducing any functional issues.

Optional follow-up: Consider adding monitoring to track if Anthropic ever provides incomplete usage data in production, which would validate that this estimation logic is indeed no longer needed.


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.

1 participant