[PR #5680] [MERGED] router-models: accurate token/message counting for model router decisions #5527

Closed
opened 2026-06-05 15:21:38 -04:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Mintplex-Labs/anything-llm/pull/5680
Author: @timothycarambat
Created: 5/22/2026
Status: Merged
Merged: 5/22/2026
Merged by: @timothycarambat

Base: masterHead: router-fixes


📝 Commits (9)

  • 3c09b48 fix: accurate token/message counting for model router decisions
  • f33f096 fix /chat API counter
  • 4c0ebc2 count current message in message count for router
  • 8089303 add router handler everywhere these is getLLMProvider
  • 6df095f router logic overhaul and refactor
  • 2158533 fix tests
  • 9e02136 Merge branch 'master' of github.com:Mintplex-Labs/anything-llm into router-fixes
  • d174e5e fix logger
  • 4654e94 support generic openAI auto-detection for dropdown in model rules provider - fails over to plaintext input

📊 Changes

17 files changed (+542 additions, -231 deletions)

View changed files

📝 frontend/src/pages/GeneralSettings/LLMPreference/index.jsx (+1 -0)
📝 frontend/src/pages/GeneralSettings/ModelRouters/LLMProviderModelPicker/index.jsx (+3 -2)
📝 frontend/src/pages/GeneralSettings/ModelRouters/NewRouterModal/index.jsx (+1 -5)
📝 server/__tests__/utils/chats/openaiCompatible.test.js (+6 -1)
📝 server/endpoints/api/openai/index.js (+2 -10)
📝 server/endpoints/api/workspace/index.js (+10 -2)
📝 server/utils/agents/aibitat/plugins/memory.js (+9 -5)
📝 server/utils/agents/ephemeral.js (+71 -10)
📝 server/utils/agents/index.js (+19 -5)
📝 server/utils/chats/apiChatHandler.js (+22 -9)
📝 server/utils/chats/embed.js (+61 -71)
📝 server/utils/chats/openaiCompatible.js (+27 -7)
📝 server/utils/chats/stream.js (+51 -95)
📝 server/utils/helpers/customModels.js (+34 -0)
📝 server/utils/helpers/index.js (+87 -2)
📝 server/utils/router/index.js (+132 -3)
📝 server/utils/telegramBot/chat/stream.js (+6 -4)

📄 Description

Description

Previously the router only counted tokens from the current user message, ignoring system prompt, chat history, pinned docs, and parsed files.

Introduce ModelRouterService.gatherRoutingContext() to compute the true conversation token count and reuse the fetched context downstream to avoid redundant DB/filesystem lookups.

Also adds model router support to the OpenAI-compatible and API chat handlers, propagates usageMetrics through ephemeral agents, and fixes the router input to use the attachment-injected message.


🔄 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/5680 **Author:** [@timothycarambat](https://github.com/timothycarambat) **Created:** 5/22/2026 **Status:** ✅ Merged **Merged:** 5/22/2026 **Merged by:** [@timothycarambat](https://github.com/timothycarambat) **Base:** `master` ← **Head:** `router-fixes` --- ### 📝 Commits (9) - [`3c09b48`](https://github.com/Mintplex-Labs/anything-llm/commit/3c09b48db7405a0afa8a8169d59eede0b7fc21cf) fix: accurate token/message counting for model router decisions - [`f33f096`](https://github.com/Mintplex-Labs/anything-llm/commit/f33f0962b4517c37105d587a4d98ac2dd8184a60) fix /chat API counter - [`4c0ebc2`](https://github.com/Mintplex-Labs/anything-llm/commit/4c0ebc2039c9f5b8749f80ae322b93e6edbab9d7) count current message in message count for router - [`8089303`](https://github.com/Mintplex-Labs/anything-llm/commit/80893032419d8b81c761c151c5359757e9f13d0d) add router handler everywhere these is getLLMProvider - [`6df095f`](https://github.com/Mintplex-Labs/anything-llm/commit/6df095f947331440289f34447ba15238a014564b) router logic overhaul and refactor - [`2158533`](https://github.com/Mintplex-Labs/anything-llm/commit/2158533749adfc61123b88d319be27e81ea9fb68) fix tests - [`9e02136`](https://github.com/Mintplex-Labs/anything-llm/commit/9e0213663cceeb36daba6a431939c19e246507af) Merge branch 'master' of github.com:Mintplex-Labs/anything-llm into router-fixes - [`d174e5e`](https://github.com/Mintplex-Labs/anything-llm/commit/d174e5e36c415287418e27d48673b7a8dd061fa4) fix logger - [`4654e94`](https://github.com/Mintplex-Labs/anything-llm/commit/4654e946e819e3d5b8d519442967342a68468473) support generic openAI auto-detection for dropdown in model rules provider - fails over to plaintext input ### 📊 Changes **17 files changed** (+542 additions, -231 deletions) <details> <summary>View changed files</summary> 📝 `frontend/src/pages/GeneralSettings/LLMPreference/index.jsx` (+1 -0) 📝 `frontend/src/pages/GeneralSettings/ModelRouters/LLMProviderModelPicker/index.jsx` (+3 -2) 📝 `frontend/src/pages/GeneralSettings/ModelRouters/NewRouterModal/index.jsx` (+1 -5) 📝 `server/__tests__/utils/chats/openaiCompatible.test.js` (+6 -1) 📝 `server/endpoints/api/openai/index.js` (+2 -10) 📝 `server/endpoints/api/workspace/index.js` (+10 -2) 📝 `server/utils/agents/aibitat/plugins/memory.js` (+9 -5) 📝 `server/utils/agents/ephemeral.js` (+71 -10) 📝 `server/utils/agents/index.js` (+19 -5) 📝 `server/utils/chats/apiChatHandler.js` (+22 -9) 📝 `server/utils/chats/embed.js` (+61 -71) 📝 `server/utils/chats/openaiCompatible.js` (+27 -7) 📝 `server/utils/chats/stream.js` (+51 -95) 📝 `server/utils/helpers/customModels.js` (+34 -0) 📝 `server/utils/helpers/index.js` (+87 -2) 📝 `server/utils/router/index.js` (+132 -3) 📝 `server/utils/telegramBot/chat/stream.js` (+6 -4) </details> ### 📄 Description ### Description Previously the router only counted tokens from the current user message, ignoring system prompt, chat history, pinned docs, and parsed files. Introduce ModelRouterService.gatherRoutingContext() to compute the true conversation token count and reuse the fetched context downstream to avoid redundant DB/filesystem lookups. Also adds model router support to the OpenAI-compatible and API chat handlers, propagates usageMetrics through ephemeral agents, and fixes the router input to use the attachment-injected message. <!-- Describe the changes in this PR that are impactful to the repo. What problem does it solve? --> --- <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-06-05 15:21:38 -04:00
yindo closed this issue 2026-06-05 15:21:38 -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#5527