[PR #4459] [CLOSED] Fix TPS Miscalculation for Gemini #4631

Closed
opened 2026-02-22 18:36:13 -05:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Mintplex-Labs/anything-llm/pull/4459
Author: @angelplusultra
Created: 9/30/2025
Status: Closed

Base: masterHead: 4449-incorrect-token-metrics-for-gemini


📄 Description

Pull Request Type

  • feat
  • 🐛 fix
  • ♻️ refactor
  • 💄 style
  • 🔨 chore
  • 📝 docs

Relevant Issues

resolves #4449

What is in this change?

This PR fixes under-reported Tokens Per Second (TPS) and completion token counts, especially for Gemini, by estimating tokens from the full generated text when provider usage is missing or unavailable during streaming.

server/utils/helpers/chat/responses.js

  • Estimate completion_tokens from the accumulated full streamed text using TokenManager on finish, abort, and error paths.
  • Keep existing prompt token estimation.
  • Maintain SSE behavior and metrics emission format.

server/utils/AiProviders/gemini/index.js

  • Add non-stream fallback: when usage.completion_tokens is missing, tokenize textResponse to estimate completion tokens.
  • Recompute total_tokens and outputTps using the fallback value.

Additional Information

Developer Validations

  • I ran yarn lint from the root of the repo & committed changes
  • Relevant documentation has been updated
  • I have tested my code functionality
  • Docker build succeeds locally

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Mintplex-Labs/anything-llm/pull/4459 **Author:** [@angelplusultra](https://github.com/angelplusultra) **Created:** 9/30/2025 **Status:** ❌ Closed **Base:** `master` ← **Head:** `4449-incorrect-token-metrics-for-gemini` --- ### 📄 Description ### Pull Request Type <!-- For change type, change [ ] to [x]. --> - [ ] ✨ feat - [x] 🐛 fix - [ ] ♻️ refactor - [ ] 💄 style - [ ] 🔨 chore - [ ] 📝 docs ### Relevant Issues <!-- Use "resolves #xxx" to auto resolve on merge. Otherwise, please use "connect #xxx" --> resolves #4449 ### What is in this change? This PR fixes under-reported Tokens Per Second (TPS) and completion token counts, especially for Gemini, by estimating tokens from the full generated text when provider usage is missing or unavailable during streaming. <!-- Describe the changes in this PR that are impactful to the repo. --> `server/utils/helpers/chat/responses.js` - Estimate `completion_tokens` from the accumulated full streamed text using `TokenManager` on finish, abort, and error paths. - Keep existing prompt token estimation. - Maintain SSE behavior and metrics emission format. `server/utils/AiProviders/gemini/index.js` - Add non-stream fallback: when `usage.completion_tokens` is missing, tokenize `textResponse` to estimate completion tokens. - Recompute `total_tokens` and `outputTps` using the fallback value. ### Additional Information <!-- Add any other context about the Pull Request here that was not captured above. --> ### Developer Validations <!-- All of the applicable items should be checked. --> - [x] I ran `yarn lint` from the root of the repo & committed changes - [x] Relevant documentation has been updated - [x] I have tested my code functionality - [ ] Docker build succeeds locally --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-02-22 18:36:13 -05:00
yindo closed this issue 2026-02-22 18:36:13 -05:00
yindo changed title from [PR #4459] Fix TPS Miscalculation for Gemini to [PR #4459] [CLOSED] Fix TPS Miscalculation for Gemini 2026-06-05 15:19:33 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Mintplex-Labs/anything-llm#4631