Learn how AI agents execute multi-step plans sequentially, handle failures gracefully, and adapt when things go wrong. Includes practical Python examples with Claude Sonnet 4.5.

This article is part of the free-to-read AI Agent Handbook
Plan and Execute
In the previous chapter, we learned how to break down complex tasks into manageable steps. Our assistant can now look at a request like "Plan my weekend getaway" and decompose it into a clear sequence: find destinations, check travel options, book accommodations, and plan activities. But having a plan is only half the battle. Now we need to actually execute it.
Think of it like following a recipe. You've read through the instructions and understand the steps, but you still need to gather ingredients, measure them out, and cook them in the right order. If you discover you're out of eggs halfway through, you'll need to adapt. The same goes for our AI agent. This chapter explores how an agent takes its plan and turns it into action, handling each step sequentially while remaining flexible enough to deal with the unexpected.
From Plan to Action
When our assistant has a plan, it needs to work through each step systematically. Let's continue with the weekend getaway example from the previous chapter. The agent created this plan:
- Find potential destinations within 3 hours of the user's location
- Check available flights or train options
- Search for hotels with availability
- Suggest activities at the destination
Now comes the execution phase. The agent starts with step 1, uses the tools at its disposal (perhaps a location search API), gets results, and moves to step 2. This sequential approach ensures that each step builds on the previous one. You can't book a hotel before you know which city you're visiting.
Here's a simple implementation of a plan executor:
Notice how each step receives the results from previous steps. This context is crucial. When the agent searches for hotels in step 3, it needs to know which destination was selected in step 1. The agent maintains this running context throughout execution.
Handling Failures Gracefully
Plans rarely execute perfectly. APIs go down, data is missing, or assumptions turn out to be wrong. A robust agent needs to handle these situations without completely falling apart.
Let's say our agent is executing step 2 (checking travel options) and the flight API returns an error. What should happen? The agent has several options:
Option 1: Retry the step. Maybe it was a temporary network issue. Try the same step again, perhaps with a small delay.
Option 2: Use an alternative approach. If the flight API is down, maybe try a train search API instead.
Option 3: Skip and continue. If this step isn't critical, mark it as failed and move on. The user can book their own travel.
Option 4: Re-plan. If the failure makes the rest of the plan impossible, go back to the planning phase and create a new approach.
Here's how we might implement basic error handling:
This approach gives the agent some autonomy in handling failures. Instead of hard-coding every possible error scenario, we let the agent reason about what makes sense given the specific situation.
Maintaining Flexibility
The best plans are flexible. As the agent executes steps, it might discover new information that suggests a better approach. Maybe step 1 reveals that the user's preferred destination is fully booked this weekend. A rigid executor would continue anyway, trying to book unavailable hotels. A flexible one would recognize the problem and adjust.
We can build this flexibility into our executor by periodically checking if the plan still makes sense:
This creates a feedback loop. The agent executes a step, evaluates whether the plan still makes sense, and adjusts if needed. It's like driving with GPS. The GPS gives you a route, but if you miss a turn or encounter road construction, it recalculates.
Bringing It Together: A Complete Example
Let's see a full execution cycle for our weekend getaway assistant. We'll use the plan from the previous chapter and execute it with all the error handling and flexibility we've discussed.
When you run this, you'll see the agent working through its plan step by step. It might produce output like this:
The agent moved through each step, used tools when needed, and synthesized everything into a coherent recommendation. If any step had failed (say, no hotels were available in Napa), the agent could have fallen back to the second destination on its list.
When to Re-Plan
Sometimes execution reveals that the original plan won't work. Maybe the user's requirements were unclear, or the situation changed. In these cases, the agent needs to go back to the planning phase.
Here are signs that re-planning might be necessary:
- Multiple consecutive failures: If several steps in a row fail, the plan might be fundamentally flawed.
- Changed requirements: The user provides new information that invalidates the plan.
- Impossible constraints: The agent discovers that the goal can't be achieved as originally envisioned.
When re-planning, the agent should use everything it learned from the failed execution. This is valuable information. If the hotel search failed because everywhere is booked, the new plan might include alternative dates or nearby cities.
This creates a learning loop. The agent tries a plan, sees what works and what doesn't, and adjusts accordingly. Over time, with enough examples, an agent can get better at creating plans that are more likely to succeed on the first try.
Practical Considerations
As you build plan-and-execute agents, keep these principles in mind:
Start simple. Begin with straightforward sequential execution. Add error handling and flexibility only when you need it. A simple executor that works is better than a complex one that doesn't.
Log everything. When debugging why a plan failed, you'll want to see exactly what happened at each step. Log the plan, each step's input and output, any errors, and the final result.
Set timeouts. Some steps might take a long time or get stuck. Set reasonable timeouts so your agent doesn't wait forever for a response that will never come.
Consider costs. Each step might involve API calls to your language model and external services. If a plan has 20 steps, that's 20+ API calls. Sometimes it's worth combining steps or using a simpler approach.
Test edge cases. What happens if step 1 returns zero results? What if a tool returns malformed data? Your executor should handle these gracefully rather than crashing.
Looking Ahead
We now have an agent that can plan and execute multi-step tasks. It breaks down complex requests, works through them systematically, and handles problems along the way. This is a significant capability. Our personal assistant can now tackle requests that would have been impossible with simple question-answering.
But we've been assuming our agent works alone. What if we had multiple agents, each specialized in different tasks? One agent could focus on planning while another handles execution. Or different agents could work on different parts of the plan in parallel. That's the world of multi-agent systems, which we'll explore in the next chapter.
For now, try building a plan-and-execute agent for a domain you care about. Maybe it's a research assistant that plans and executes literature reviews, or a personal finance agent that plans and executes budget analyses. The pattern is the same: break it down, execute step by step, and stay flexible.
Glossary
Execution: The process of carrying out each step in a plan, using available tools and resources to accomplish the intended goal.
Sequential Execution: Performing plan steps one after another in order, where each step can build on the results of previous steps.
Retry Logic: A strategy for handling failures by attempting a failed operation multiple times before giving up or trying an alternative approach.
Graceful Degradation: The ability of a system to continue operating in a limited capacity when parts of it fail, rather than crashing completely.
Re-planning: The process of creating a new plan when execution reveals that the original plan won't work or needs significant adjustment.
Feedback Loop: A cycle where the results of actions inform future decisions, allowing the agent to learn and adapt during execution.
Context Propagation: Passing the results and information from earlier steps to later steps so the agent maintains awareness of what has happened so far.
Quiz
Ready to test your understanding? Take this quick quiz to reinforce what you've learned about plan execution in AI agents.





Comments