From 6a60d1cd2a091cf4763315ac98c2bbfccb7abcff Mon Sep 17 00:00:00 2001 From: Rui Date: Sun, 17 May 2026 03:13:45 +0200 Subject: [PATCH] feat: implement 429 auto-retry exponential backoff to handle free tier Gemini rate limits --- templates/agent_executor.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/templates/agent_executor.py b/templates/agent_executor.py index 69c6e66..0c9de87 100644 --- a/templates/agent_executor.py +++ b/templates/agent_executor.py @@ -133,9 +133,19 @@ class SwarmAgent: "tools": tools } - req = urllib.request.Request(url, data=json.dumps(payload).encode('utf-8'), headers=headers, method='POST') - with urllib.request.urlopen(req) as res: - return json.loads(res.read().decode('utf-8')) + for attempt in range(5): + try: + req = urllib.request.Request(url, data=json.dumps(payload).encode('utf-8'), headers=headers, method='POST') + with urllib.request.urlopen(req) as res: + return json.loads(res.read().decode('utf-8')) + except urllib.error.HTTPError as e: + if e.code == 429: + wait_time = (attempt + 1) * 8 + print(f" [W] Rate Limit (429) detectado. Aguardando {wait_time} segundos antes de tentar novamente (tentativa {attempt + 1}/5)...") + time.sleep(wait_time) + else: + raise e + raise Exception("Falha após 5 tentativas devido a limite de requisições (429).") def execute(self): self.setup_workspace()