Skip to main content
Prompt Optimizer rewrites your agent’s instructions and keeps the version that scores highest on your eval.

Quick Start

1

Optimise with the LLM Judge

Score each candidate with the built-in Judge using plain-English criteria.
2

Score with a numeric metric

Supply a metric(output, expected) -> float to replace the Judge with your own gold-data score.
3

Preview without applying

Set apply=False to keep the original instructions and inspect the winner first.

How It Works

The optimiser runs in five steps:

Configuration Options

agent.optimize_instructions(...) accepts these parameters:
aoptimize_instructions(...) is the async twin — it offloads the synchronous run to a worker thread so async callers never block the event loop.

OptimizeResult

optimize_instructions(...) returns an OptimizeResult:

Common Patterns

Judge-based scoring for open-ended tasks

Use criteria when there is no single gold answer.

Numeric-metric scoring with gold data

Plug in any empirical score — rouge_l, accuracy, latency, or a custom function.

Async usage

Dry-run then apply manually

A user typically runs the optimiser once on a small eval, inspects result.trials to see which variants were tried and how they scored, adjusts the eval set, then re-runs until the winner is good enough to apply.

Best Practices

The optimiser runs the agent (1 + n_candidates) × len(evalset) times. A small, representative eval keeps cost and latency low while still surfacing the best variant.
A metric is cheaper and more reproducible than the LLM Judge. Use criteria only for open-ended tasks where no single gold answer exists.
Increase n_candidates when your instructions are short or generic; decrease it when they are already specific and you only want small refinements.
result.trials lists every candidate and its score. Persist it to review which rewrites were tried and how they compared to the base.

Evaluation Loop

Iteratively run, judge, and keep the best output

Learn Skill

Sibling self-improvement method on the Agent