Agentic AIC++verifiedVerified

Plan-and-Execute Pattern in C++

Separate high-level planning from step-by-step execution: one LLM call produces a structured plan, then individual executor calls carry out each step, with replanning triggered by unexpected results.

How to Implement the Plan-and-Execute Pattern in C++

1Step 1: Define Step and Plan structures

enum class StepStatus { Pending, Running, Done, Failed };

struct Step {
    std::string id;
    std::string description;
    StepStatus status = StepStatus::Pending;
    std::string result;
};

struct Plan {
    std::string goal;
    std::vector<Step> steps;
};

using PlannerFn  = std::function<std::vector<Step>(const std::string&)>;
using ExecutorFn = std::function<std::string(const Step&)>;

2Step 2: Implement the plan-and-execute pipeline

Plan planAndExecute(const std::string& goal,
                    PlannerFn planner,
                    ExecutorFn executor) {
    Plan plan{goal, planner(goal)};

    for (auto& step : plan.steps) {
        step.status = StepStatus::Running;
        try {
            step.result = executor(step);
            step.status = StepStatus::Done;
        } catch (const std::exception& e) {
            step.result = e.what();
            step.status = StepStatus::Failed;
            break;
        }
    }
    return plan;
}

3Step 3: Run with sample data

int main() {
    auto plan = planAndExecute(
        "Deploy a service",
        [](const std::string&) -> std::vector<Step> {
            return {{"1", "Build image", StepStatus::Pending, ""},
                    {"2", "Run tests",   StepStatus::Pending, ""},
                    {"3", "Deploy",      StepStatus::Pending, ""}};
        },
        [](const Step& s) { return "Completed: " + s.description; }
    );

    for (const auto& s : plan.steps) {
        const char* st = s.status == StepStatus::Done ? "DONE" : "FAIL";
        std::cout << "[" << st << "] " << s.description << "\n";
    }
}

Plan-and-Execute Pattern Architecture

hourglass_empty

Rendering diagram...

lightbulb

Plan-and-Execute Pattern in the Real World

A building contractor (Planner) reviews the architectural blueprints and produces a phased construction schedule: foundation, framing, electrical, finishing. Individual trade crews (Executors) carry out each phase. If an inspection fails (unexpected result), the contractor revises the remaining schedule rather than demolishing the entire building and starting over.