From 9792bde31a91af3b3642918affb6a9dbbc66a2be Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Wed, 25 Mar 2026 16:35:17 -0700 Subject: [PATCH] fix(agent): count compression restarts toward retry limit (#3070) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When context overflow triggers compression, the outer retry loop restarts via continue without incrementing retry_count. If compression reduces messages but not enough to fit the context window, this creates an infinite loop burning API credits: API call → overflow → compress → retry → overflow → compress → ... Increment retry_count on compression restarts so the loop exits after max_retries total attempts. Cherry-picked from PR #2766 by dieutx. --- run_agent.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/run_agent.py b/run_agent.py index 17f1c0189..d65c4c4b3 100644 --- a/run_agent.py +++ b/run_agent.py @@ -6637,6 +6637,11 @@ class AIAgent: if restart_with_compressed_messages: api_call_count -= 1 self.iteration_budget.refund() + # Count compression restarts toward the retry limit to prevent + # infinite loops when compression reduces messages but not enough + # to fit the context window. + retry_count += 1 + restart_with_compressed_messages = False continue if restart_with_length_continuation: