0
0
Agentic AIml~20 mins

Plan-and-execute pattern in Agentic AI - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Plan-and-execute pattern
Problem:You want to build an AI agent that can plan a sequence of steps to solve a task and then execute those steps correctly.
Current Metrics:The agent completes tasks with 60% success rate but often fails to follow the planned steps exactly.
Issue:The agent's execution is inconsistent, causing errors even when the plan is good. This shows a gap between planning and execution.
Your Task
Improve the agent so it follows its plan more accurately, increasing task success rate to at least 85%.
You can only modify the execution module, not the planning module.
Keep the overall architecture of the agent the same.
Hint 1
Hint 2
Hint 3
Solution
Agentic AI
class PlanExecuteAgent:
    def __init__(self, planner, executor):
        self.planner = planner
        self.executor = executor

    def run_task(self, task):
        plan = self.planner.create_plan(task)
        for step in plan:
            success = False
            attempts = 0
            while not success and attempts < 3:
                result = self.executor.execute_step(step)
                if self.verify_step(step, result):
                    success = True
                else:
                    attempts += 1
            if not success:
                print(f"Failed to execute step: {step.name}")
                return False
        return True

    def verify_step(self, step, result):
        # Simple verification: check if result matches expected outcome
        return result == step.expected_outcome


# Example usage:
class DummyPlanner:
    def create_plan(self, task):
        # Returns a list of steps with expected outcomes
        return [Step("step1", "done1"), Step("step2", "done2")]

class DummyExecutor:
    def execute_step(self, step):
        # Simulate execution with 80% chance of success
        import random
        if random.random() < 0.8:
            return step.expected_outcome
        else:
            return "error"

class Step:
    def __init__(self, name, expected_outcome):
        self.name = name
        self.expected_outcome = expected_outcome


planner = DummyPlanner()
executor = DummyExecutor()
agent = PlanExecuteAgent(planner, executor)

success_count = 0
trials = 100
for _ in range(trials):
    if agent.run_task("dummy_task"):
        success_count += 1

print(f"Task success rate: {success_count/trials*100:.2f}%")
Added a verification step after each execution to check if the step was done correctly.
Implemented a retry mechanism to attempt executing a step up to 3 times if it fails.
Kept the planner module unchanged and focused improvements on the executor interaction.
Results Interpretation

Before: 60% task success rate, frequent execution errors.

After: 98% task success rate, fewer execution errors due to retries and verification.

Separating planning and execution with verification and retries helps reduce errors and improves overall task success in AI agents.
Bonus Experiment
Try adding a learning component that updates the executor based on past failures to reduce retries.
💡 Hint
Use simple memory to remember which steps often fail and adjust execution strategy accordingly.