# HRPO (Hierarchical Reflective Prompt Optimizer)

> Learn how to use HRPO (Hierarchical Reflective Prompt Optimizer) to improve prompts through systematic root cause analysis of failure modes and targeted refinement.

`HRPO` (Hierarchical Reflective Prompt Optimizer) uses hierarchical root cause analysis to identify and address
specific failure modes in your prompts. It analyzes evaluation results, identifies patterns in
failures, and generates targeted improvements to address each failure mode systematically.

<Note>
  In code, use the `HRPO` class (for example `from opik_optimizer import HRPO`). The underlying module name still uses
  `hierarchical_reflective_optimizer` for backwards compatibility.
</Note>

<Note>
  `HRPO` is ideal when you have a complex prompt that you want to refine
  based on understanding *why* it's failing. Unlike optimizers that generate many random variations,
  this optimizer systematically analyzes failures, identifies root causes, and makes surgical
  improvements to address each specific issue.
</Note>

## How It Works

HRPO (Hierarchical Reflective Prompt Optimizer) has been developed by the Opik team to improve prompts that
might have already gone through a few rounds of manual prompt engineering. It focuses on identifying
why a prompt is failing and then updating the prompts to address the issues.

As datasets can be large, we split the analysis into batches and analyze them in parallel. We then
synthesize the findings across all batches to identify the core issues with the prompt.

<Frame>
  <img src="https://files.buildwithfern.com/https://opik.docs.buildwithfern.com/docs/opik/2026-01-30T17:33:07.444Z/img/agent_optimization/hierarchical_reflective_optimizer.png" alt="HRPO (Hierarchical Reflective Prompt Optimizer)" />
</Frame>

<Tip>
  The optimizer is open-source, you can check out the root cause analysis code and prompts in the
  [Opik repository](https://github.com/comet-ml/opik/tree/main/sdks/opik_optimizer/src/opik_optimizer/algorithms/hierarchical_reflective_optimizer).
</Tip>

## Quickstart

You can use `HRPO` to optimize a prompt:

```python maxLines=1000
from opik_optimizer import HRPO, ChatPrompt, datasets
from opik.evaluation.metrics.score_result import ScoreResult