Opik Optimizer
Purpose
Design, run, and interpret Opik Optimizer workflows for prompts, tools, and model parameters with consistent dataset/metric wiring and reproducible evaluation.
When to use
Use this skill when a user asks for:
- Choosing and configuring Opik Optimizer algorithms for prompt/agent optimization.
- Writing
ChatPrompt-based optimization runs and custom metric functions.
- Optimizing with tools (function calling or MCP), selected prompt roles, or prompt segments.
- Tuning LLM call parameters with
optimize_parameter.
- Comparing optimizer outputs and interpreting
OptimizationResult.
Workflow
- Select optimizer strategy (
MetaPromptOptimizer, FewShotBayesianOptimizer, HRPO, etc.) based on the target optimization goal.
- Build prompt/dataset/metric wiring and validate placeholder-field alignment.
- Run prompt, tool, or parameter optimization with explicit controls (
n_threads, n_samples, max_trials, seed).
- Inspect
OptimizationResult and compare score deltas against initial baselines.
- Summarize recommendations, risks, and next experiments.
Inputs
- Target optimization objective (prompt/tool/parameter) and success metric.
- Dataset source and expected schema fields.
- Model/provider constraints and runtime limits.
- Optional scope constraints (
optimize_prompts segments, tool fields, project names).
Outputs
- Optimizer run configuration and rationale.
- Result interpretation (
score, initial_score, history trends).
- Recommended next changes and follow-up experiment plan.
Use the reference files in this skill for details before implementing code:
references/algorithms.md
references/prompt_agent_workflow.md
references/example_patterns.md
Opik Optimizer quickstart
- Install and import:
pip install opik-optimizer
from opik_optimizer import ChatPrompt, MetaPromptOptimizer, HRPO, FewShotBayesianOptimizer
from opik_optimizer import datasets
- Build a prompt and metric:
from opik.evaluation.metrics import LevenshteinRatio
prompt = ChatPrompt(
system="You are a concise answerer.",
user="{question}",
)
def metric(dataset_item: dict, output: str) -> float:
return LevenshteinRatio().score(
reference=dataset_item["answer"],
output=output,
).value
- Load dataset and run:
dataset = datasets.hotpot(count=30)
result = MetaPromptOptimizer(model="openai/gpt-5-nano").optimize_prompt(
prompt=prompt,
dataset=dataset,
metric=metric,
n_samples=20,
max_trials=10,
)
result.display()
Core workflow you should follow
- Pick optimizer class:
- Few-shot examples + Bayesian selection:
FewShotBayesianOptimizer
- LLM meta-reasoning:
MetaPromptOptimizer
- Genetic + MOO / LLM crossover:
EvolutionaryOptimizer
- Hierarchical reflective diagnostics:
HierarchicalReflectiveOptimizer (HRPO)
- Pareto-based genetic strategy:
GepaOptimizer
- Parameter tuning only:
ParameterOptimizer
- Define a single
ChatPrompt (or dict of prompts for multi-prompt cases).
- Provide a dataset from
opik_optimizer.datasets.
- Provide metric callable with signature
(dataset_item, llm_output) -> float (or ScoreResult/list of ScoreResult).
- Set optimizer controls (
n_threads, n_samples, max_trials, seed, etc.).
- Run one of:
optimize_prompt(...) for prompt/system behavior changes.
optimize_parameter(...) for model-call hyperparameters.
- Inspect
OptimizationResult (score, initial_score, history, optimization_id, get_optimized_parameters).
Key execution details to enforce
- Prefer explicit
project_name for Opik tracking if you are using org-level observability.
- Keep placeholders in prompts aligned with dataset fields (for example
{question}).
- Start with
optimize_prompts="system" or "user" when scope should be constrained.
- Keep
model names in MetaPrompt/reasoning calls provider-compatible for your account.
- Validate multimodal input payloads by preserving non-empty content segments only.
- For small datasets, use
n_samples and n_samples_strategy carefully; over-allocation auto-falls back to full set.
Tooling and segment-based control
- Tools can be optimized with MCP/function schema fields, not only by changing prompt wording.
- For fine-grained text updates, use
optimize_prompts values and helper functions from prompt_segments:
extract_prompt_segments(ChatPrompt) to inspect stable segment IDs.
apply_segment_updates(ChatPrompt, updates) for deterministic edits.
- Tool optimization is distinct from prompt optimization.
Runnable examples live upstream in the Opik repo:
If you need local runnable scripts, vendor the upstream examples into a scripts/ folder and keep references one level deep.
Common mistakes to avoid
- Passing empty dataset or mismatched placeholder names.
- Mixing deprecated constructor arg
num_threads with n_threads.
- Assuming tool optimization is the same as agent function-calling optimization.
- Running
ParameterOptimizer.optimize_prompt (it raises and should not be used).
Next actions
- For in-depth behavior and per-class parameter tables:
references/algorithms.md
- For exact
optimize_prompt signatures, prompts, tool constraints, and result usage: references/prompt_agent_workflow.md
- For pattern examples and source-backed workflows:
references/example_patterns.md
1---2name: opik-optimizer3description: Optimize LLM prompts, tools, and agents in Opik using standardized optimizer workflows (prompt optimization, tool optimization, and parameter tuning), dataset/metric wiring, and result interpretation.4license: MIT5---67# Opik Optimizer89## Purpose1011Design, run, and interpret Opik Optimizer workflows for prompts, tools, and model parameters with consistent dataset/metric wiring and reproducible evaluation.1213## When to use1415Use this skill when a user asks for:1617- Choosing and configuring Opik Optimizer algorithms for prompt/agent optimization.18- Writing `ChatPrompt`-based optimization runs and custom metric functions.19- Optimizing with tools (function calling or MCP), selected prompt roles, or prompt segments.20- Tuning LLM call parameters with `optimize_parameter`.21- Comparing optimizer outputs and interpreting `OptimizationResult`.2223## Workflow24251. Select optimizer strategy (`MetaPromptOptimizer`, `FewShotBayesianOptimizer`, `HRPO`, etc.) based on the target optimization goal.262. Build prompt/dataset/metric wiring and validate placeholder-field alignment.273. Run prompt, tool, or parameter optimization with explicit controls (`n_threads`, `n_samples`, `max_trials`, seed).284. Inspect `OptimizationResult` and compare score deltas against initial baselines.295. Summarize recommendations, risks, and next experiments.3031## Inputs3233- Target optimization objective (prompt/tool/parameter) and success metric.34- Dataset source and expected schema fields.35- Model/provider constraints and runtime limits.36- Optional scope constraints (`optimize_prompts` segments, tool fields, project names).3738## Outputs3940- Optimizer run configuration and rationale.41- Result interpretation (`score`, `initial_score`, history trends).42- Recommended next changes and follow-up experiment plan.4344Use the reference files in this skill for details before implementing code:4546- `references/algorithms.md`47- `references/prompt_agent_workflow.md`48- `references/example_patterns.md`4950## Opik Optimizer quickstart51521. Install and import:5354```bash55pip install opik-optimizer56```5758```python59from opik_optimizer import ChatPrompt, MetaPromptOptimizer, HRPO, FewShotBayesianOptimizer60from opik_optimizer import datasets61```62632. Build a prompt and metric:6465```python66from opik.evaluation.metrics import LevenshteinRatio6768prompt = ChatPrompt(69 system="You are a concise answerer.",70 user="{question}",71)7273def metric(dataset_item: dict, output: str) -> float:74 return LevenshteinRatio().score(75 reference=dataset_item["answer"],76 output=output,77 ).value78```79803. Load dataset and run:8182```python83dataset = datasets.hotpot(count=30)8485result = MetaPromptOptimizer(model="openai/gpt-5-nano").optimize_prompt(86 prompt=prompt,87 dataset=dataset,88 metric=metric,89 n_samples=20,90 max_trials=10,91)92result.display()93```9495## Core workflow you should follow96971. Pick optimizer class:98 - Few-shot examples + Bayesian selection: `FewShotBayesianOptimizer`99 - LLM meta-reasoning: `MetaPromptOptimizer`100 - Genetic + MOO / LLM crossover: `EvolutionaryOptimizer`101 - Hierarchical reflective diagnostics: `HierarchicalReflectiveOptimizer` (`HRPO`)102 - Pareto-based genetic strategy: `GepaOptimizer`103 - Parameter tuning only: `ParameterOptimizer`1042. Define a single `ChatPrompt` (or dict of prompts for multi-prompt cases).1053. Provide a dataset from `opik_optimizer.datasets`.1064. Provide metric callable with signature `(dataset_item, llm_output) -> float` (or `ScoreResult`/list of `ScoreResult`).1075. Set optimizer controls (`n_threads`, `n_samples`, `max_trials`, seed, etc.).1086. Run one of:109 - `optimize_prompt(...)` for prompt/system behavior changes.110 - `optimize_parameter(...)` for model-call hyperparameters.1117. Inspect `OptimizationResult` (`score`, `initial_score`, `history`, `optimization_id`, `get_optimized_parameters`).112113## Key execution details to enforce114115- Prefer explicit `project_name` for Opik tracking if you are using org-level observability.116- Keep placeholders in prompts aligned with dataset fields (for example `{question}`).117- Start with `optimize_prompts="system"` or `"user"` when scope should be constrained.118- Keep `model` names in `MetaPrompt`/`reasoning` calls provider-compatible for your account.119- Validate multimodal input payloads by preserving non-empty content segments only.120- For small datasets, use `n_samples` and `n_samples_strategy` carefully; over-allocation auto-falls back to full set.121122## Tooling and segment-based control123124- Tools can be optimized with MCP/function schema fields, not only by changing prompt wording.125- For fine-grained text updates, use `optimize_prompts` values and helper functions from `prompt_segments`:126 - `extract_prompt_segments(ChatPrompt)` to inspect stable segment IDs.127 - `apply_segment_updates(ChatPrompt, updates)` for deterministic edits.128- Tool optimization is distinct from prompt optimization.129130Runnable examples live upstream in the Opik repo:131132- https://github.com/comet-ml/opik/tree/main/sdks/opik_optimizer/src/opik_optimizer133134If you need local runnable scripts, vendor the upstream examples into a `scripts/` folder and keep references one level deep.135136## Common mistakes to avoid137138- Passing empty dataset or mismatched placeholder names.139- Mixing deprecated constructor arg `num_threads` with `n_threads`.140- Assuming tool optimization is the same as agent function-calling optimization.141- Running `ParameterOptimizer.optimize_prompt` (it raises and should not be used).142143## Next actions144145- For in-depth behavior and per-class parameter tables: `references/algorithms.md`146- For exact `optimize_prompt` signatures, prompts, tool constraints, and result usage: `references/prompt_agent_workflow.md`147- For pattern examples and source-backed workflows: `references/example_patterns.md`