feat: add multi API key load balancing with quota-aware routing and failover - #185
Open
touful wants to merge 4 commits into
Open
feat: add multi API key load balancing with quota-aware routing and failover#185touful wants to merge 4 commits into
touful wants to merge 4 commits into
Conversation
|
Bugbot is not enabled for this team, so this pull request was not reviewed. Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs. |
|
Bugbot is not enabled for this team, so this pull request was not reviewed. Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds multi API key load balancing to the MCP server, enabling transparent pooling of multiple Tavily API keys with quota-aware routing, automatic exhausted-key detection, and failover — eliminating service interruptions caused by single-key quota exhaustion.
Motivation
Single free-tier API keys (1,000 credits/month) are insufficient for heavy usage. Currently, when a key's quota runs out, users must manually swap the key and restart the service, causing downtime. The existing
TAVILY_API_KEYenv var only supports a single key, and suffixed variants (TAVILY_API_KEY_*) are silently ignored.This PR introduces a
KeyManagerthat pools multiple keys and automatically routes requests to the key with the most remaining quota, detects and cools down exhausted keys (HTTP 432/433), and fails over to other keys on errors — all transparent to the MCP client.Changes
src/keyManager.ts:KeyManagerclass managing the key pool — loads keys from env, queries/usagefor remaining quota, selects keys by highest-remaining-quota-first strategy, handles error codes (432/433 cool down, 429 retry-after, 401 remove), periodic refresh every 5 min.src/index.ts: IntegratesKeyManager; newmakeAuthenticatedRequestmethod unifies key selection + failover retry across all endpoints; research polling now uses the same key as the initial request to avoid 404 task loss.Configuration
Three supported env var formats (by priority):
TAVILY_API_KEYS— comma-separated keys (recommended):"key1,key2,key3"TAVILY_API_KEY_<suffix>— multiple suffixed vars (backward-compatible with existing configs)TAVILY_API_KEY— single key (fully backward-compatible, degrades to original behavior)Load Balancing Strategy
GET /usageper key at startup + every 5 min, selects the key with the mostlimit - usageremaining.limitis null (free-tiertvly-devkeys), falls back to randomized round-robin (per user request: "use quota if available, otherwise average").retry-afterheader, temporary cool down (cap 5 min); 401 → permanently remove key.makeAuthenticatedRequestretries with other keys until success or all keys exhausted.Backward Compatibility
TAVILY_HUMAN_ID,DEFAULT_PARAMETERS, and keyless mode all preserved.Testing
tavily_searchcall verified.tvly-devkeys (one quota-exhausted) — exhausted key correctly detected via 432 and cooled down, requests routed to available keys.Known Limitations
tvly-devkeys returnlimit: nullfrom/usage, so proactive quota detection is unavailable for them; exhausted-key detection relies on 432 error responses (passive). The degraded round-robin + error-driven failover still prevents service interruption.selectKey()is non-atomic; acceptable for stdio MCP's serial request model.