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 the plan structure

public record PlanStep(string Description, string ToolName, string Input);

public record Plan(IReadOnlyList<PlanStep> Steps);

2Step 2: Planner creates a step-by-step plan

public interface IPlanner
{
    Task<Plan> CreatePlanAsync(string task);
}

3Step 3: Executor runs each step sequentially

public interface IExecutor
{
    Task<string> ExecuteStepAsync(PlanStep step);
}

public static class PlanAndExecute
{
    public static async Task<string> RunAsync(
        string task, IPlanner planner, IExecutor executor)
    {
        // Phase 1: Plan
        var plan = await planner.CreatePlanAsync(task);

        // Phase 2: Execute each step
        var results = new List<string>();
        foreach (var step in plan.Steps)
        {
            var result = await executor.ExecuteStepAsync(step);
            results.Add(result);
        }

        return string.Join("\n", results);
    }
}

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.