Skip to content

Conversation

@joslat
Copy link
Contributor

@joslat joslat commented Oct 29, 2025

Motivation and Context

This PR adds a new foundational workflow sample that demonstrates an iterative refinement pattern with quality gates - a crucial pattern not covered by existing samples.

Why is this change required?

The current samples demonstrate basic conditional routing (EdgeCondition) and switch-case patterns, but none show how to implement cyclic feedback loops with quality gates, iteration safety mechanisms, and structured decision-making for approval workflows.
Providing a GroupChat Construct is ok but this does not expose the logic inside it and is hard to debug when an issue happens. Also it does not support max iterations, and is hard to customize.
This example aims to change that by implementing a Critic workflow (as in the official first paper) with a maker agent and a critic agent collaborating until the desired outcome is reached, having a final Summarizer agent and executor.
This sets a pattern so everybody can build upon it (as a sub-workflow, add reviewer agents supporting the critic, etc.)
This workflow can be debugged and traced, and in short future, be live-viewed as it runs and supports its rendering as a graph.

What problem does it solve?

Developers building AI workflows often need to implement iterative refinement patterns where:
• Content is created, reviewed, and revised multiple times
• A quality gate agent approves or rejects work with specific feedback
• Safety mechanisms prevent infinite loops
• Iteration state is tracked across multiple executor invocations

What scenario does it contribute to?

Solves #1789

This sample enables real-world scenarios including:
• Content creation pipelines (blog posts, documentation, marketing copy)
• Code review workflows with iterative improvements
• Document review systems (legal, compliance, technical)
• Quality assurance automation
• Design iteration workflows

Description

This sample introduces the Writer-Critic Workflow pattern (08 in Foundational samples), demonstrating:

Core Pattern:
• Writer agent creates initial content
• Critic agent reviews and decides: approve → Summary, or reject → Writer (with feedback)
• Loops until approval or max iterations (3) reached
• Summary executor presents final approved content

Key Technical Contributions:

  1. Conditional Cyclic Routing: Shows backward edges with AddEdge creating a feedback loop
  2. Max Iteration Safety: Demonstrates production-ready pattern to prevent infinite loops and excessive API costs
  3. Multiple Message Handlers: Writer executor handles both string (initial) and CriticDecision (revision) inputs
  4. Scoped Shared State: Tracks iteration count and conversation history across executor invocations
  5. Structured Decision Parsing: Extracts JSON approval decisions from AI responses for routing logic
  6. Quality Gate Pattern: Implements approval/rejection workflow with actionable feedback

Code Highlights:
• Uses FlowState for iteration tracking across workflow executions
• Critic outputs CriticDecision with approved/rejected status and feedback
• Writer has dual handlers for initial requests vs. revision requests
• Streaming support for real-time progress visibility
• Console output clearly shows iteration progression

This pattern complements existing samples:
• vs. EdgeCondition (01): Adds cyclic routing, not just divergent paths
• vs. CustomAgentExecutors: Adds max iteration safety and explicit termination logic
• vs. Loop samples: Goal-oriented iteration vs. collection iteration

Contribution Checklist
• [x] The code builds clean without any errors or warnings
• [x] The PR follows the Contribution Guidelines
• [x] All unit tests pass, and I have added new tests where possible
• [ ] Is this a breaking change? No

…custom executors, agents, switch, custom states, different entry points for the executors.
Copilot AI review requested due to automatic review settings October 29, 2025 23:23
@markwallace-microsoft markwallace-microsoft added documentation Improvements or additions to documentation .NET labels Oct 29, 2025
@github-actions github-actions bot changed the title Add Writer-Critic Iterative Refinement Workflow Sample .NET: Add Writer-Critic Iterative Refinement Workflow Sample Oct 29, 2025
@joslat
Copy link
Contributor Author

joslat commented Oct 29, 2025

More Workflow love, @alliscode & @lokitoth

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a new foundational workflow sample (08_WriterCriticWorkflow) that demonstrates iterative refinement patterns with Writer and Critic agents, showcasing conditional routing, shared state management, and max iteration safety limits.

  • Implements a Writer-Critic feedback loop with conditional workflow routing based on approval decisions
  • Demonstrates multiple message handlers in a single executor and shared state tracking across workflow iterations
  • Includes max iteration safety cap to prevent infinite loops

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
dotnet/samples/GettingStarted/Workflows/_Foundational/08_WriterCriticWorkflow/Program.cs New sample implementation demonstrating Writer-Critic iterative refinement workflow with conditional routing, shared state, and multiple message handlers
dotnet/samples/GettingStarted/Workflows/_Foundational/08_WriterCriticWorkflow/08_WriterCriticWorkflow.csproj Project file for the new sample with required dependencies
dotnet/samples/GettingStarted/Workflows/README.md Added reference to new Writer-Critic Workflow sample
dotnet/agent-framework-dotnet.slnx Added new sample project to solution

Console.WriteLine($"=== Critic (Iteration {state.Iteration}) ===\n");

StringBuilder sb = new();
await foreach (AgentRunResponseUpdate update in this._agent.RunStreamingAsync(message, cancellationToken: cancellationToken))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this use structured output?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation .NET

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants