CLIO icon indicating copy to clipboard operation
CLIO copied to clipboard

[Feature]: Rotate through API keys

Open hartmund opened this issue 9 months ago • 3 comments

Pre-submission Checklist

  • [x] I have searched existing issues and discussions
  • [x] I have read the documentation
  • [x] This is a feature request, not a bug report

Problem Statement

This is for people hitting token usage limits but having multiple API keys for a provider.

Proposed Solution

I want to be able to add multiple API keys from one provider and then have CLIO switch seamlessly rotate through them, in case resources get exhausted. Mainly using Gemma-27b for now which is free within limits like 15k token per minute / 15k requests a day.

Ideally, all those keys would show up in /api show, and if you're at it, maybe you can make it an overview of all providers where API keys were entered, making it a complete overview.

Which area would this affect?

API / Provider Integration

Alternatives Considered

No response

Examples / References

No response

Would you be willing to contribute?

None

Additional Context

No response

hartmund avatar Mar 04 '26 11:03 hartmund

Automated Triage Summary

Field Value
Classification enhancement
Priority medium
Completeness 95%

Analysis: Feature request: allow CLIO to rotate through multiple API keys for a provider. Investigation shows Config.pm already supports storing multiple per-provider keys (api_keys hash with set_provider_key/get_provider_key/list_provider_keys). However, the runtime only uses a single active key: APIManager retrieves the single current api_key (config->get('api_key')) and _get_endpoint_config_for_provider resolves only one provider key. There is no rotation/fallback logic when a key hits rate limits or auth errors. ResponseHandler parses rate-limit headers and Broker coordinates rate-limit state, which provide natural integration points for a rotation policy. Implementation plan (not requested from reporter): add key-rotation policy in APIManager (or a dedicated key manager) that uses Config::list_provider_keys, update /api show to list stored keys per provider, and trigger rotation on 401/403/429 or when ResponseHandler signals rate_limit. Ready for review and assignment to fewtarius.

Root Cause Analysis

Confidence: high

Relevant files:

  • lib/CLIO/Core/Config.pm
  • lib/CLIO/Core/APIManager.pm
  • lib/CLIO/UI/Commands/API.pm
  • lib/CLIO/Core/API/ResponseHandler.pm
  • lib/CLIO/Coordination/Broker.pm

Relevant functions:

  • CLIO::Core::Config::set_provider_key
  • CLIO::Core::Config::get_provider_key
  • CLIO::Core::Config::list_provider_keys
  • CLIO::Core::APIManager::_get_api_key
  • CLIO::Core::APIManager::_get_endpoint_config_for_provider
  • CLIO::UI::Commands::API::_display_api_config
  • CLIO::Core::API::ResponseHandler::process_rate_limit_headers
  • CLIO::Coordination::Broker (rate limit status handlers)

The codebase already stores per-provider keys in a map (Config.pm: the api_keys hash, with helpers get_provider_key / set_provider_key / list_provider_keys), but API usage and the UI are implemented around a single active key. APIManager->_get_api_key and other runtime paths use the single current 'api_key' (from config->get('api_key')) for requests; cross-provider helper _get_endpoint_config_for_provider resolves a single provider key via get_provider_key($provider_name) and sets auth headers from that single key. There is no code that iterates, rotates, or falls back through multiple keys for a single provider when rate limits or auth failures occur. The ResponseHandler already parses rate-limit headers and Broker coordinates rate-limit state, so integration points exist, but no rotation implementation is present. Consequently, the requested feature (rotate/round-robin through multiple API keys for one provider) is feasible with the existing storage structure but requires adding rotation logic in APIManager (and possibly a policy/strategy), exposing list_provider_keys in the CLI output (/api show) and adding runtime behavior to refresh/switch keys on auth/429/rate-limit events. Confidence: high

Affected Areas

  • API key storage and retrieval (Config)
  • API request orchestration (APIManager)
  • CLI UI for /api show and /api set (UI Commands)
  • Rate-limit handling (ResponseHandler)
  • Multi-agent coordination of rate limits (Broker)

This is an automated analysis. A maintainer will review shortly.

CLIO-Bot avatar Mar 04 '26 12:03 CLIO-Bot

This is interesting, let me think about how to implement it - it probably won't land right away.

fewtarius avatar Mar 04 '26 12:03 fewtarius

I've spent some time thinking about this, and I don't think we'll implement it as it could be used as a method to circumvent provider limits.

fewtarius avatar Mar 06 '26 11:03 fewtarius