> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tracepress.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Optimization

> Improve an LM step's instructions against your labels with GEPA.

`table.optimize(labels, metric)` tunes the instructions of a table's own LM step against rows
you've labeled, using the GEPA optimizer.

```python theme={null}
import pandas as pd

labels = pd.read_csv("doom_loop_labels.csv")          # trial, doom_loop
detector = src.steps.group_by("trial").lm_reduce("source, message, tool_calls -> doom_loop: bool")
detector = detector.optimize(labels, metric="doom_loop")
print(detector.optimize_report)                       # held-out score before and after
print(detector.instructions)                          # the tuned prompt
```

* **`labels`** is a DataFrame or list of dicts with the table's key column(s), such as `trial`
  (or `trial` and `step` for a step-level table), plus the right answer.
* **`metric`** is an output column to match exactly (strings case-insensitively), or a function
  `fn(row, label)` returning a score, or `(score, feedback)` so GEPA knows what went wrong.
* **`budget`** caps how many example runs GEPA makes (default 60). `val_frac` (default 0.3) is
  the share of labels held out to score before and after.

The result is the same step with the best instructions found. Only that step reruns for each
candidate prompt; its input rows are read once. `.optimize_report` has train and held-out
scores before and after, the candidates, and the LM calls and cost of running the step
(reflection calls aren't counted).

## Example: classify steps

[examples/optimize\_step\_kinds.py](https://github.com/Ziems/tracepress/blob/main/examples/optimize_step_kinds.py)
labels each step from its real tool calls (`edit`, `test`, `explore` or `other`), then tunes a
classifier that only sees the step's one-sentence summary:

```python theme={null}
classify = step_summaries.lm_map(
    "step_summary -> kind: Literal['explore', 'edit', 'test', 'other']",
    instructions="What kind of step is this?")
tuned = classify.optimize(labels, metric="kind", budget=400)
```

GEPA learns the rule's edge cases from its misses, such as that writing a test file counts as
an edit.

## Next steps

> [card] [Choose a model](https://docs.tracepress.ai/models.md)
>
> Pick the model the tuned step runs on.

> [card] [Python SDK](https://docs.tracepress.ai/reference/python.md)
>
> Every `optimize` argument.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.