title: Plan Then Execute slug: plan-then-execute category: workflow-pattern status: battle-tested difficulty: beginner tags: [planning, plan-mode, structured-approach, reliability] prerequisites: [basic-cli-usage] estimated_time: 5min to learn, immediate use cost_per_use: "$0.05-$0.50"
Plan Then Execute
Problem
Complex tasks fail when the agent starts coding immediately. It picks a suboptimal approach, gets halfway through, realizes the design is wrong, and either backtracks messily or produces tangled code. The larger the task, the more likely this happens. You need the agent to think before it acts.
Solution
Use a two-phase approach: planning with no code changes, then execution of the approved plan.
Step-by-Step
- Plan phase: Ask the agent to outline its approach. Specify "do not write any code yet."
- Review: Read the plan. Ask clarifying questions. Suggest adjustments.
- Approve: Confirm the plan or iterate until it is right.
- Execute: Tell the agent to implement the approved plan step by step.
- Checkpoint: After each major step, verify progress matches the plan.
When to Use
- Any task touching more than 2-3 files
- Architectural changes or new features
- Refactoring with multiple moving parts
- When you are unsure of the best approach yourself
- When the agent has failed on a first attempt
When NOT to Use
- Simple, well-defined single-file edits
- Tasks where you already have a precise specification
- Quick exploratory/throwaway work
Example: Claude Code
# Phase 1: Plan (using plan mode if available)
claude -p "I need to add WebSocket support to our Express API server. \
Current REST endpoints are in src/routes/. \
Plan the implementation: which files to create, which to modify, \
what libraries to use, and in what order. \
Do NOT write any code yet. Output a numbered step-by-step plan."
# Or use the --plan flag if your version supports it:
# claude --plan "Add WebSocket support to the Express API server."
# Phase 2: Execute the plan
claude -p "Implement the following plan for adding WebSocket support. \
Work through each step in order. Commit after each major step.
Plan:
1. Install ws library (npm install ws)
2. Create src/websocket/server.ts — WebSocket server setup
3. Create src/websocket/handlers.ts — message handlers
4. Modify src/index.ts — attach WS server to HTTP server
5. Add tests in tests/websocket.test.ts
6. Update README with WebSocket docs"
# Interactive version with natural back-and-forth
claude
# > Plan how to add WebSocket support to this Express app.
# > Don't write code yet, just outline the approach.
# (review the plan)
# > Good plan, but use Socket.IO instead of raw ws. Update the plan.
# (review again)
# > Approved. Implement it step by step. Commit after each step.
Example: Codex CLI
# Phase 1: Plan
codex -q "Plan how to add WebSocket support to this Express API. \
List files to create and modify in order. Do not write code yet." \
> implementation-plan.txt
cat implementation-plan.txt
# Phase 2: Execute
codex -q "Execute this implementation plan step by step:
$(cat implementation-plan.txt)"
Cost Estimate
| Phase | Typical Cost |
|---|---|
| Planning | ~$0.03-$0.10 |
| Execution | ~$0.10-$0.80 |
| Total | ~$0.13-$0.90 |
Planning adds minimal cost (often under 10% of total) but dramatically reduces wasted execution from wrong approaches.
Maturity Notes
Status: Battle-tested. This is the single most impactful pattern for improving agent reliability on non-trivial tasks. Teams that adopt plan-then-execute report 40-60% fewer "start over" moments. The plan serves double duty as documentation of what was done and why. Works across all agent tools and models.