PatternsAgent Patterns

Evaluator-Optimizer Pattern

Loading pattern preview

Loading preview…

Problems solved

Technical challenges this implementation handles out of the box.
  • Evaluate draft quality with a dedicated evaluator tool
  • Optimize content iteratively from evaluation feedback
  • Loop evaluator and optimizer tools under one orchestrator agent
  • Stop once quality thresholds or step limits are reached

Use cases

Products and workflows this pattern is designed to support.
  • Marketing copy optimization loops
  • Technical documentation review agents
  • Translation quality control workflows
  • Creative writing refinement assistants

Setup

Requirements, wiring steps, and what this pattern adds to your project.

Getting started

Pick how you want to pull this pattern in — then wire env vars and routes the same way.

  1. 1

    Install from the preview toolbar

    Shadcn Icon

    Copy the install command above and run it in your project — Pro patterns include a short-lived token.

  2. 2

    Add environment variables to .env.local

    OPENAI_API_KEY

  3. 3

    Point client fetches at your API routes

    Replace preview `/view/.../api/...` paths with `/api/...` in the installed files.

  4. 4

    Customize the agent and tool files

    Adapt prompts, tools, and stop conditions for your product — Copy for AI in the toolbar helps seed that work.

  5. 5

    Run your dev server and open the pattern route

    Install dependencies if needed, then start the app and verify responses.

Environment variables

Capabilities

AI SDK APIs

AgenttoolstepCountIsExperimental_Agentgateway

Providers

OpenAI

Files added

14 files
  • app/page.tsx
  • app/layout.tsx
  • app/api/evaluator-optimizer/route.ts
  • lib/multi-agent-orchestrator.ts
  • lib/multi-agent-tool-types.ts
  • lib/ai-agent-evaluator-optimizer-tools/evaluator.ts
  • lib/ai-agent-evaluator-optimizer-tools/optimizer.ts
  • lib/ai-agent-evaluator-optimizer-tools/schema.ts
  • lib/ai-agent-evaluator-optimizer-tools/communication.ts
  • components/evaluator-optimizer-chat.tsx
  • components/tool-views/evaluator-view.tsx
  • components/tool-views/optimizer-view.tsx
  • README.md
  • AGENT_FLOW.md

Dependencies

9 total

npm packages

6
AISDKlucide-reactNext.jsReactsonnerZod

Shadcn IconRegistry components

3
buttoncollapsibleempty

Critical files

  • lib/multi-agent-orchestrator.ts

    Core orchestrator agent definition using Experimental_Agent. Configures the evaluator-optimizer coordination loop with model, instructions, tools, and stepCountIs(15) stop condition.

  • ai-agent-evaluator-optimizer-tools/evaluator.ts

    Evaluator tool that assesses content quality against criteria. Uses generator pattern to yield loading/ready states. Returns quality scores, issues, suggestions, and threshold checks.

  • ai-agent-evaluator-optimizer-tools/optimizer.ts

    Optimizer tool that takes evaluation feedback and iteratively improves content. Uses generator pattern for streaming state transitions.

  • ai-agent-evaluator-optimizer-tools/communication.ts

    Inter-agent communication tools (sendEvaluation, requestEvaluation, sendOptimization) that enable the evaluator-optimizer message passing loop.

  • ai-agent-evaluator-optimizer-tools/schema.ts

    Shared Zod schemas (EvaluationResult, OptimizationParams, OptimizationResult, EvaluationCriteria) defining data contracts between evaluator and optimizer agents.

Requirements

Complexity
Advanced
Setup time
~15 minutes
Node
24+
Memory
1GB