0
Phase 4

Planning

~4 min read

Concept & How It Works

    Why Does It Exist?

    Without planning, agents react step-by-step with no global view — they backtrack, repeat work, and miss dependencies. Planning front-loads reasoning: 'To book a trip, I need flights first, then hotel, then car rental.' This reduces wasted tool calls and improves success rate on multi-step tasks.

    Real-World Analogy

    Planning is the difference between a tourist wandering a city randomly versus following a curated itinerary — both might see sights, but the planner hits every must-see efficiently.
    Loading diagram...

    Visual Workflows

    What is Planning?

    Loading diagram...

    Example

    Scenario

    Goal: 'Prepare quarterly board report.' Plan: (1) Query sales DB for Q3 metrics, (2) Generate charts from data, (3) Draft executive summary, (4) Format as PDF, (5) Email to board@company.com. Executor runs each step sequentially, re-plans if Q3 data is incomplete.

    Solution

    In Agent Foundations, apply Planning to this scenario: Goal: 'Prepare quarterly board report. Identify the inputs, run the technique, validate the output, and note one thing you would monitor in production.

    Practice Task

    Do this before moving to the next module — reading alone is not enough.

    Open the Code Walkthrough below and run it locally. Change one parameter related to Planning (e.g. model, temperature, top_k, or tool name), observe the difference in output, and write 2–3 sentences explaining what changed.

    Code Walkthrough

    Highlighted lines show where Planning happens in the code.

    Planning
    1PLANNER_PROMPT = """Create a step-by-step plan to accomplish this goal.  # key line for Planning2Return JSON: {"steps": [{"id": 1, "description": "...", "tool": "tool_name", "depends_on": []}]}  # key line for Planning3
    4Goal: {goal}5Available tools: {tools}"""6
    7def create_plan(goal: str, tools: list) -> list[dict]:  # define a reusable function8    response = client.chat.completions.create(  # call the API9        model="gpt-4o",10        messages=[{"role": "user", "content": PLANNER_PROMPT.format(goal=goal, tools=tools)}],  # key line for Planning11        response_format={"type": "json_object"},12        temperature=0,13    )14    return json.loads(response.choices[0].message.content)["steps"]  # return the result15
    16def execute_plan(plan: list[dict], tools: dict) -> str:  # define a reusable function17    results = {}18    for step in plan:  # key line for Planning19        deps = [results[d] for d in step.get("depends_on", [])]20        result = tools[step["tool"]](step["description"], *deps)21        results[step["id"]] = result22    return results  # return the result

    Commands to Remember

    Commands to Remember

    • pip install openai # minimal agent = LLM API + Python loop
    • python agent.py # run your agent script
    • pip install python-dotenv # load API keys from .env

    Common Mistakes

    • Rigid plan with no re-planning on failure
    • Over-planning simple tasks — adds latency for no benefit
    • Not validating plan feasibility before execution
    • Plans with circular dependencies
    • Planner and executor sharing no context about failures

    Cheat Sheet

    Quick recap — the most important points from this module.

    Cheat Sheet

    quick ref
    • Plan first, execute second
    • JSON plan: steps + tools + deps
    • Re-plan on failure
    • Validate before execute
    • ReAct for exploratory tasks
    • Human approve high-stakes plans