Plan and Execute › From Plan to Action
AI Agent HandbookAll chapters Plan and Execute › From Plan to ActionOne section · Sections in this chapter2 / 10 ↓
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:
## Using Claude Sonnet 4.5 for its superior planning and tool-use capabilities
import os
from anthropic import Anthropic
client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
def execute_plan(plan_steps, tools):
"""Execute a plan step by step, using available tools."""
results = []
for i, step in enumerate(plan_steps, 1):
print(f"\nExecuting step {i}: {step}")
# Ask the agent to execute this specific step
response = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1024,
tools=tools,
messages=[{
"role": "user",
"content": f"Execute this step: {step}\n\nPrevious results: {results}"
}]
)
# Process tool calls if any
if response.stop_reason == "tool_use":
tool_result = handle_tool_call(response.content[0])
results.append({
"step": step,
"result": tool_result
})
else:
results.append({
"step": step,
"result": response.content[0].text
})
return results
## Example plan from previous chapter
plan = [
"Find destinations within 3 hours",
"Check travel options to top destination",
"Search for available hotels",
"Suggest activities"
]
## Execute the plan
execution_results = execute_plan(plan, available_tools)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.