Skip to content

Agent Zero AI framework

multi api keys

frdel3fe2c209b4c5
diff --git a/models.py b/models.py
index 25e2e5b00..3606d3b02 100644
--- a/models.py
+++ b/models.py
@@ -87,15 +87,22 @@ class ChatChunk(TypedDict):
rate_limiters: dict[str, RateLimiter] = {}
-
+api_keys_round_robin: dict[str, int] = {}
def get_api_key(service: str) -> str:
- return (
+ # get api key for the service
+ key = (
dotenv.get_dotenv_value(f"API_KEY_{service.upper()}")
or dotenv.get_dotenv_value(f"{service.upper()}_API_KEY")
or dotenv.get_dotenv_value(f"{service.upper()}_API_TOKEN")
or "None"
)
+ # if the key contains a comma, use round-robin
+ if "," in key:
+ api_keys = [k.strip() for k in key.split(",") if k.strip()]
+ api_keys_round_robin[service] = api_keys_round_robin.get(service, -1) + 1
+ key = api_keys[api_keys_round_robin[service] % len(api_keys)]
+ return key
def get_rate_limiter(
diff --git a/python/helpers/settings.py b/python/helpers/settings.py
index 9ef32703c..421332952 100644
--- a/python/helpers/settings.py
+++ b/python/helpers/settings.py
@@ -582,7 +582,7 @@ def convert_out(settings: Settings) -> SettingsOutput:
api_keys_section: SettingsSection = {
"id": "api_keys",
"title": "API Keys",
- "description": "API keys for model providers and services used by Agent Zero.",
+ "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.",
"fields": api_keys_fields,
"tab": "external",
}