# Reuse the optimized prompt from the first stage
optimized_prompt = prompt.with_messages(meta_result.prompt)

search_space = ParameterSearchSpace(parameters=[
    {"name": "temperature", "distribution": "float", "low": 0.1, "high": 0.9},
    {"name": "top_p", "distribution": "float", "low": 0.7, "high": 1.0},
])

final_result = parameter.optimize_parameter(
    prompt=optimized_prompt,
    dataset=dataset,
    metric=metric,
    parameter_space=search_space,
    max_trials=20,
)
```

## Checklist

* **Freeze datasets and metrics** between stages to keep comparisons fair.
* **Use validation datasets consistently** – if you provide a `validation_dataset` in the first stage, use the same split in subsequent stages to ensure fair comparison and avoid overfitting.
* **Log pipeline metadata** (e.g., `experiment_config={"pipeline": "hierarchical_then_param"}`) so dashboards show lineage.
* **Budget tokens** – chained runs multiply costs; start with smaller `n_samples` and increase once results look promising.
* **Reuse OptimizationResult** – every optimizer returns an `OptimizationResult`, so you can pass `result.prompt` (and `result.details`, `result.history`) directly into the next stage without rebuilding state.

## Automation tips

* Use Makefiles or CI workflows to run stage 1 → stage 2 with clear checkpoints.
* Store intermediate prompts in version control alongside metadata (optimizer, score, dataset).
* Notify stakeholders with summary reports generated from `final_result.history`.

## Related docs

* [Optimize prompts](/agent_optimization/optimization/optimize_prompts)
* [Few-Shot Bayesian optimizer](/agent_optimization/algorithms/fewshot_bayesian_optimizer)
* [HRPO (Hierarchical Reflective Prompt Optimizer)](/agent_optimization/algorithms/hierarchical_adaptive_optimizer)
* [Parameter optimizer](/agent_optimization/algorithms/parameter_optimizer)