Implement a class AgentOrchestrator that executes tools in a loop with error handling. Truncate conversation history if it exceeds 10 messages. Handle errors with fallbacks, and support defined step and failure limits.
💡 Model Answer
The AgentOrchestrator class should encapsulate the orchestration logic. It maintains a list called history that stores the last 10 messages. The run method takes a list of tools and optional limits for steps and failures. Inside run, a loop iterates over the steps up to the step limit. For each step, it selects the next tool, calls its execute method inside a try/except block, and appends the result to history. If an exception occurs, a fallback tool (e.g., a generic "fallback" tool) is invoked. After each iteration, the history is trimmed to the last 10 entries. The loop stops if the failure count reaches the failure limit or if all steps are completed. Complexity is O(s) time where s is the number of steps, and O(h) space for the history (h ≤ 10). A minimal skeleton:
class AgentOrchestrator:
def __init__(self, tools, step_limit=10, failure_limit=3):
self.tools = tools
self.step_limit = step_limit
self.failure_limit = failure_limit
self.history = []
def run(self, initial_input):
failures = 0
for step in range(self.step_limit):
tool = self.tools[step % len(self.tools)]
try:
result = tool.execute(initial_input)
self.history.append(result)
failures = 0
except Exception as e:
failures += 1
self.history.append(f"Error: {e}")
if failures >= self.failure_limit:
break
self.history = self.history[-10:]
return self.historyThis design keeps the history bounded, handles errors gracefully, and respects the configured limits.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.
Get Assisting AI — Starts at ₹500