From 1e140fc6d8130618c461d87dc72647525f0c796c Mon Sep 17 00:00:00 2001 From: Eduardo Chiarotti Date: Thu, 13 Feb 2025 20:38:50 -0300 Subject: [PATCH] feat: add max_depth --- src/crewai/llm.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/crewai/llm.py b/src/crewai/llm.py index 9cf868824..99f5138d5 100644 --- a/src/crewai/llm.py +++ b/src/crewai/llm.py @@ -543,7 +543,11 @@ class LLM: agent = None task = None - while caller_frame: + # Add a maximum depth to prevent infinite loops + max_depth = 100 # Reasonable limit for call stack depth + current_depth = 0 + + while caller_frame and current_depth < max_depth: if "self" in caller_frame.f_locals: caller_self = caller_frame.f_locals["self"] if isinstance(caller_self, AgentExecutorProtocol): @@ -551,6 +555,7 @@ class LLM: task = caller_self.task break caller_frame = caller_frame.f_back + current_depth += 1 return agent, task