multi api keys

frdel committed Aug 6, 2025 at 22:42 UTC 3fe2c209b4c55ee60cc397f70c496830641cc374
2 files changed +10 -3
models.py
+9 -2
@@ -87,15 +87,22 @@ class ChatChunk(TypedDict):
87
88
89 rate_limiters: dict[str, RateLimiter] = {}
90 -
90 +api_keys_round_robin: dict[str, int] = {}
91
92 def get_api_key(service: str) -> str:
93 - return (
93 + # get api key for the service
94 + key = (
95 dotenv.get_dotenv_value(f"API_KEY_{service.upper()}")
96 or dotenv.get_dotenv_value(f"{service.upper()}_API_KEY")
97 or dotenv.get_dotenv_value(f"{service.upper()}_API_TOKEN")
98 or "None"
99 )
100 + # if the key contains a comma, use round-robin
101 + if "," in key:
102 + api_keys = [k.strip() for k in key.split(",") if k.strip()]
103 + api_keys_round_robin[service] = api_keys_round_robin.get(service, -1) + 1
104 + key = api_keys[api_keys_round_robin[service] % len(api_keys)]
105 + return key
106
107
108 def get_rate_limiter(
python/helpers/settings.py
+1 -1
@@ -582,7 +582,7 @@ def convert_out(settings: Settings) -> SettingsOutput:
582 api_keys_section: SettingsSection = {
583 "id": "api_keys",
584 "title": "API Keys",
585 - "description": "API keys for model providers and services used by Agent Zero.",
585 + "description": "API keys for model providers and services used by Agent Zero. You can set multiple API keys separated by a comma (,). They will be used in round-robin fashion.",
586 "fields": api_keys_fields,
587 "tab": "external",
588 }