How to Build a Plan and Execute AI Agent That Doesn't Lose Track
Blog Post

How to Build a Plan and Execute AI Agent That Doesn't Lose Track

Jake McCluskey
Back to blog

When your ReAct agent starts a complex task and forgets the original goal halfway through, you're hitting the limits of single-loop agent architectures. Plan-and-execute agents solve this by splitting the work into distinct roles: a planner that maps out all steps upfront, an executor that runs each step with tool access, and a replanner that adjusts the plan based on what actually happened. This separation keeps your agent focused on the original goal even when individual steps take dozens of tool calls or reasoning loops.

You're not alone if you've watched an agent successfully complete five subtasks, then veer off into a completely unrelated direction on step six. That's context drift. And honestly, it's the primary failure mode for agents handling tasks that span more than a few reasoning steps.

Why Do AI Agents Fail at Multi-Step Tasks

ReAct agents operate in a tight loop: observe the current state, reason about what to do next, act using a tool, then repeat. This works brilliantly for straightforward tasks like "look up the weather and send an email." It falls apart when the task requires coordinating multiple sub-goals that depend on each other.

The core problem is that ReAct agents make decisions one step at a time without a persistent plan. Each reasoning step consumes context window space with tool outputs, intermediate results, and reasoning traces. By step eight or nine, the original instruction might be 4,000 tokens back in the context, and the model starts optimizing for local coherence instead of global goal achievement.

In testing with GPT-4 and Claude 3.5 Sonnet, ReAct agents maintaining goal coherence drops to roughly 40% success rate on tasks requiring seven or more dependent steps. The agent completes steps successfully but loses track of why it's doing them. Like following a recipe where you forget you're making bread and start adding ingredients for a cake halfway through.

Context drift accelerates when tool outputs are verbose. If your agent calls an API that returns 2KB of JSON, then queries a database that returns another 1.5KB, you've burned through context space without actually reasoning about the high-level goal. The model sees recent details clearly but the original objective becomes fuzzy.

What Is Plan-and-Execute Agent Architecture

Plan-and-execute architecture splits agent behavior into three specialized components. The planner receives the user's goal and generates a complete, ordered list of steps before any execution begins. The executor takes one step at a time, performs tool calls, reports results. The replanner reviews what happened and adjusts remaining steps if reality didn't match expectations.

This isn't just ReAct with extra steps. It's a fundamentally different control flow. Instead of "reason-act-observe" happening in a single continuous loop, you get "plan everything, execute one step, replan if needed, execute next step." The original goal stays explicit in the plan document rather than implicit in the conversation history.

Think of it like the difference between improvising a road trip and using GPS. ReAct agents improvise at every intersection. Plan-and-execute agents plot the whole route, then follow it turn by turn, recalculating only when they hit unexpected road closures.

The planner component typically uses a language model with a prompt optimized for decomposition: "Break this complex goal into 5-8 discrete, ordered steps. Each step should have a clear success condition." The executor uses a different prompt optimized for single-step completion: "Execute only this one step. Use available tools. Report what you learned." The replanner gets both the original plan and execution results: "Given what we learned, should we modify remaining steps?"

How the Three Components Prevent Context Loss

The planner's job is to think strategically before getting bogged down in details. When you ask it to "research competitors, analyze pricing, and generate a report," it might produce:


steps:
  1. Search for top 5 competitors in [industry]
  2. For each competitor, extract pricing page URL
  3. For each pricing page, extract tier names and prices
  4. Calculate average price per tier across competitors
  5. Generate comparison table
  6. Write 3-paragraph analysis of pricing patterns
  7. Format as PDF report

This plan becomes a persistent artifact. It doesn't get buried in conversation history. Each component references it explicitly.

The executor focuses on exactly one step. Its prompt includes the current step, available tools, nothing else. It doesn't need to remember the overall goal because that's the planner's job. This narrow focus means the executor can use its full context window for the immediate task: parsing API responses, handling errors, extracting specific data points.

The replanner runs after each step completes. If step 2 was "extract pricing page URLs" but the executor discovered that three competitors don't list prices publicly, the replanner might modify step 3 to "extract available pricing info and note which competitors require contact for quotes." This adjustment happens explicitly, not through vague context-dependent reasoning.

In production systems handling tasks with 10+ steps, this architecture maintains goal coherence above 85% compared to ReAct's 40%. The difference comes from making plans explicit and editable rather than implicit in model weights.

Plan and Execute Agent Architecture Tutorial

You can build a working plan-and-execute agent in about 150 lines of Python using LangChain and Groq's free API. Groq offers fast inference for Llama models at no cost for development, which makes it ideal for testing agentic patterns without burning through API credits.

Set Up Your Environment

Install the required packages:


pip install langchain langchain-groq langchain-community tavily-python

Get a free API key from console.groq.com and from tavily.com (for web search). Set them as environment variables:


export GROQ_API_KEY="your_key_here"
export TAVILY_API_KEY="your_key_here"

Create the Planner

The planner takes a user goal and outputs structured steps. You want JSON output so the executor can parse it reliably:


from langchain_groq import ChatGroq
from langchain.prompts import ChatPromptTemplate
import json

llm = ChatGroq(model="llama-3.1-70b-versatile", temperature=0)

planner_prompt = ChatPromptTemplate.from_messages([
    ("system", """You are a planning agent. Break down the user's goal into 5-8 specific, ordered steps.
Output valid JSON with this structure:
{
  "steps": [
    {"id": 1, "action": "specific action to take", "success_criteria": "how to know it worked"},
    ...
  ]
}"""),
    ("user", "{goal}")
])

def create_plan(goal):
    response = llm.invoke(planner_prompt.format(goal=goal))
    return json.loads(response.content)

Build the Executor

The executor needs access to tools. Start with a simple web search tool using Tavily:


from langchain_community.tools import TavilySearchResults
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain.prompts import ChatPromptTemplate

search = TavilySearchResults(max_results=3)
tools = [search]

executor_prompt = ChatPromptTemplate.from_messages([
    ("system", """You are an execution agent. Complete ONLY the specific step provided.
Use available tools. Report what you learned in 2-3 sentences."""),
    ("user", "Step to execute: {step}\nSuccess criteria: {criteria}"),
    ("placeholder", "{agent_scratchpad}")
])

agent = create_tool_calling_agent(llm, tools, executor_prompt)
executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

def execute_step(step_data):
    result = executor.invoke({
        "step": step_data["action"],
        "criteria": step_data["success_criteria"]
    })
    return result["output"]

Add the Replanner

After each step, the replanner decides if remaining steps need adjustment:


replanner_prompt = ChatPromptTemplate.from_messages([
    ("system", """You are a replanning agent. Given the original plan and what just happened,
decide if remaining steps need modification. Output JSON:
{
  "modify": true/false,
  "updated_steps": [...] (only if modify is true)
}"""),
    ("user", "Original plan: {plan}\nCompleted step: {step}\nResult: {result}\nRemaining steps: {remaining}")
])

def replan(original_plan, completed_step, result, remaining_steps):
    response = llm.invoke(replanner_prompt.format(
        plan=json.dumps(original_plan),
        step=json.dumps(completed_step),
        result=result,
        remaining=json.dumps(remaining_steps)
    ))
    return json.loads(response.content)

Wire It Together

The main loop coordinates all three components:


def run_plan_and_execute(goal):
    plan = create_plan(goal)
    steps = plan["steps"]
    results = []
    
    for i, step in enumerate(steps):
        print(f"\n--- Executing Step {step['id']} ---")
        result = execute_step(step)
        results.append({"step": step, "result": result})
        
        remaining = steps[i+1:]
        if remaining:
            replan_decision = replan(plan, step, result, remaining)
            if replan_decision["modify"]:
                print("Replanning remaining steps...")
                steps = steps[:i+1] + replan_decision["updated_steps"]
    
    return results

# Test it
results = run_plan_and_execute(
    "Find the three most popular Python web frameworks and compare their GitHub stars"
)

This basic implementation demonstrates the core pattern. For production use, you'd add error handling, step timeouts, persistent storage for plans. If you're working on running multiple agents at scale, you'll want to store plans in a database so you can resume interrupted workflows.

ReAct Agent vs Plan and Execute Agent Comparison

ReAct agents excel at tasks with 1-4 steps where the path forward is clear from the start. Need to look up a fact and format it? ReAct is faster and simpler. The overhead of planning is wasted when the task is straightforward.

Plan-and-execute agents shine when tasks have 5+ steps, when steps depend on each other in non-obvious ways, or when you need to prove that the agent followed a specific procedure. The explicit plan serves as both a roadmap and an audit trail.

Consider token efficiency. A ReAct agent doing 8 steps might consume 15,000 tokens as it repeatedly processes the growing conversation history. A plan-and-execute agent uses roughly 8,000 tokens: 2,000 for initial planning, then 750 per step execution since each step starts with a clean context. The savings come from not re-processing previous steps in every reasoning loop.

ReAct handles dynamic environments better when the goal itself might change based on what you discover. If you're exploring an API and deciding what to do based on what endpoints exist, ReAct's flexibility helps. Plan-and-execute works better when the goal is fixed but the path is complex.

For debugging, plan-and-execute wins decisively. When a ReAct agent fails at step 6 of 9, you have to trace through the entire conversation to understand where it went wrong. With plan-and-execute, you can see exactly which step failed, what the expected outcome was, what actually happened. This matters enormously when you're trying to improve agent reliability in production.

You can also combine both patterns. Use plan-and-execute for the high-level workflow, but let the executor use ReAct loops for individual complex steps. This hybrid approach gives you strategic coherence with tactical flexibility. The ReAct pattern still has value as a component within a larger architecture.

How to Build AI Agents That Follow Complex Goals

Look, beyond choosing the right architecture, you need to design for goal persistence. Store the original user intent as a separate field that every component can access. When the replanner considers modifications, it should evaluate them against the original goal, not just the most recent step.

Use explicit success criteria for each step. Instead of "research competitors," write "identify 5 competitor names and their primary product URLs." This specificity helps both the executor know when it's done and the replanner evaluate if the outcome was sufficient.

Implement step timeouts. If a single step runs for more than 10 tool calls, something's wrong. Either the step was too vague, or the executor is stuck in a loop. Surface this to the replanner so it can break the problematic step into smaller pieces.

Add memory tools that write facts to a key-value store instead of relying on context. When your agent learns "competitor A charges $49/month," write that to a database with a key like "competitor_a_pricing." Later steps can retrieve this fact without keeping it in the conversation history. This pattern, related to techniques in agentic RAG systems, dramatically reduces context bloat.

Test with progressively longer task chains. Start with 3-step tasks, verify 100% success, then try 5-step tasks. Find the point where your agent starts losing coherence and add architectural improvements there. Most agents hit problems between 6-8 steps, which is where plan-and-execute architecture provides the most value.

Consider adding a "goal checker" component that runs after every few steps. It compares progress so far against the original goal and raises a flag if the agent has drifted. This is cheaper than full replanning but catches drift before it compounds.

For tasks requiring external memory or knowledge retrieval, make sure your executor can access those systems without bloating the context. A well-designed tool that returns "found 3 relevant documents" is better than one that dumps 5KB of text into the conversation. The executor should summarize tool outputs before passing them to the replanner.

When you're building agents that need to maintain context across dozens of steps or multiple sessions, the architectural choices you make early determine whether you'll spend months fighting context drift or shipping reliable automation. Plan-and-execute isn't the only pattern that works, but it's the most straightforward solution to the specific problem of goal coherence in complex workflows. The explicit separation of planning, execution, replanning gives you clear points to optimize, debug, improve as your tasks grow more sophisticated.

Ready to stop reading and start shipping?

Get a free AI-powered SEO audit of your site

We'll crawl your site, benchmark your local pack, and hand you a prioritized fix list in minutes. No call required.

Run my free audit
WANT THE SHORTCUT

Need help applying this to your business?

The post above is the framework. Spend 30 minutes with me and we'll map it to your specific stack, budget, and timeline. No pitch, just a real scoping conversation.