# Chaining optimizers

> Run multiple optimizers in sequence to balance exploration and fine-tuning.

Some projects benefit from running two or more optimizers back-to-back. For example, use MetaPrompt to improve wording, then Parameter optimizer to fine-tune sampling settings. This guide explains why you might chain runs, the trade-offs, and the APIs you use to pass prompts and metadata between stages.

## Strategy patterns

| Pipeline                         | Why run it                                                                                                                                                  | Pros                                                                                                | Cons                                                                        | Complexity |
| -------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------- | ---------- |
| HRPO → Parameter                 | Existing long/complex prompt scenario: HRPO's reflective analysis finds failure modes; Parameter optimizer then tightens parameters on the improved prompt. | Excellent for legacy prompts with lots of existing complexity. Helps produce explainable changelog. | Requires metrics with rich `reason` strings; two stages increase cost.      | Medium     |
| Evolutionary → Few-Shot Bayesian | Cold-start scenario: explore many prompt architectures first, then let Few-Shot Bayesian pick the best example combination for the winning structure.       | High diversity followed by precise example selection.                                               | Evolutionary runs are expensive; Bayesian stage relies on curated datasets. | High       |
| MetaPrompt → Parameter           | Baseline prompts need polish plus sampling tweaks.                                                                                                          | Quick wins with minimal configuration; can run in under an hour.                                    | Less insight into failure modes than reflective pipelines.                  | Low.       |
| Evolutionary → Parameter         | Hunt for novel prompts, then squeeze out cost/latency by tuning temperature/top\_p.                                                                         | Balances creativity with production readiness.                                                      | Two heavy optimization loops; ensure budget headroom.                       | High.      |

## Example pipeline

```python
from opik_optimizer import MetaPromptOptimizer, ParameterOptimizer, ChatPrompt
from opik_optimizer.parameter_optimizer import ParameterSearchSpace

meta = MetaPromptOptimizer(model="openai/gpt-4o")
parameter = ParameterOptimizer(model="openai/gpt-4o")

meta_result = meta.optimize_prompt(
    prompt=prompt,
    dataset=dataset,
    metric=metric,
    max_trials=4,
)