Optimizer

Automatically improve agent performance

Agents can be optimized automatically – tune prompts and settings for better results.

Diagram

Optimization Loop

🤖 Agent

📊 Evaluate

✏️ Adjust

🎯 Improved

Quick Start

  1. Simple Usage
    typescript
    import { Agent, Optimizer } from 'praisonai';
    
    const agent = new Agent({
      instructions: 'You answer questions'
    });
    
    const optimizer = new Optimizer({
      agent,
      examples: [
        { input: 'What is 2+2?', expected: '4' },
        { input: 'Capital of France?', expected: 'Paris' }
      ]
    });
    
    await optimizer.run();
    // Agent instructions are now improved
  2. With Configuration
    typescript
    const optimizer = new Optimizer({
      agent,
      examples: myExamples,
      evaluate: (output, expected) => {
        return output.toLowerCase() === expected.toLowerCase();
      }
    });

User Interaction Flow

Diagram
AgentOptimizerUserAgentOptimizerUserloop[For each example]Start optimizationTest inputOutputScore resultUpdate instructionsOptimization report

Configuration Levels

typescript
// Level 1: Array - Simple examples
const optimizer = new Optimizer({
  agent,
  examples: [
    { input: 'test', expected: 'result' }
  ]
});

// Level 2: Dict - With options
const optimizer = new Optimizer({
  agent,
  examples: myExamples,
  iterations: 5,
  targetScore: 0.9
});

// Level 3: Instance - Full control
const optimizer = new Optimizer({
  agent,
  examples: myExamples,
  iterations: 10,
  evaluate: customEvaluator,
  adjustments: ['instructions', 'temperature'],
  onIteration: (scores) => console.log(scores)
});

How It Works

  1. Test: Run agent on examples
  2. Score: Evaluate output quality
  3. Adjust: Modify prompts/settings
  4. Repeat: Continue until target score

API Reference

Eval Module

Evaluation and optimization


Best Practices

Use diverse examples

Cover different types of inputs the agent will see.

Set a target score

Stop optimization when good enough.

Review changes

Check what the optimizer changed before deploying.


Related