> ## 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.

# Concepts

> Map, reduce, and the hierarchy of keys that connects them.

A TracePress pipeline maps an LLM over rows of a trace, then reduces groups of rows into
summaries, climbing a hierarchy such as step → trial → task.

## Tables

A source gives you **tables** of rows. A Harbor job has `steps`, `trials` and `tests`:

```python theme={null}
import tracepress as tp

tp.configure(lm="openrouter:openai/gpt-6-luna")
src = tp.read.harbor("jobs/my-job")
src.steps       # one row per agent step
```

Tables are immutable and lazy: every operator returns a new table, and nothing runs until you
call `.collect()` or `.to_pandas()`.

## Language Model Map

`lm_map` makes one LLM call per row. You describe the call with a one-line
signature:

```python theme={null}
notes = src.steps.lm_map(
    "step -> what_happened, is_mistake: bool",
    instructions="Summarize this step in one sentence and say whether it was a mistake.")
```

Inputs on the left bind to columns by name, and outputs on the right become new columns. An
input named after the row's level (`step` on a steps table) gets the whole row: its message,
reasoning, tool calls and output. Outputs are strings unless you give a type (`bool`,
`list[str]`, `Literal['a', 'b']`, …).

Two relatives of `lm_map`:

* `lm_filter("message -> is_error: bool")` keeps only rows where the one bool output is true.
* `lm_extract("step -> failures: list[str]")` makes one new row per item of a list output.

Plain-Python `filter` and `map` work the same way without an LLM, which makes them the cheap
way to drop rows before you pay for calls:

```python theme={null}
user_steps = src.steps.filter(lambda r: r.source == "user")
```

## Language Model Reduce

`group_by(...).lm_reduce(...)` makes one LLM call per group and returns one row per group:

```python theme={null}
per_trial = notes.group_by("trial").lm_reduce(
    "what_happened, is_mistake -> summary, failure_mode")
```

Inside a reduce, an input naming a column of the rows being reduced gets **every row's
value**, kept aligned so the LLM sees which `is_mistake` goes with which `what_happened`. An
input naming the group key, or a column with one value per group (like a trial's `reward`),
gets that **single value**. Groups too big for one prompt are split into chunks and the
results merged.

Use `group_by(...).reduce(fn, columns=...)` for a plain-Python reduce, such as counting, and
`group_by()` with no names to reduce the whole table into one row.

## Hierarchies

Every row carries a **hierarchical key**. A Harbor step is keyed `dataset > task > trial > step`.
A row built from other rows inherits their key: grouping steps by trial gives rows keyed
`dataset > task > trial`, and reducing those by task gives rows keyed `dataset > task`. So climbing the hierarchy is just reducing again:

```python theme={null}
per_task = per_trial.group_by("task").lm_reduce(
    "task, summary, failure_mode -> common_failures: list[str]")

overall = per_task.group_by().lm_reduce("task, common_failures -> top_failure_modes: list[str]")
```

`group_by` takes key levels (`"trial"`) or any other column (`"model"`).

**Joins** bring facts from one level down to another. `join(other, on="trial")` adds each
step's trial columns (its `instruction`, `reward`, …) to the step, and in a reduce those columns
then bind as one value per trial:

```python theme={null}
per_trial = (notes.join(src.trials, on="trial")
    .group_by("trial")
    .lm_reduce("instruction, reward, what_happened, is_mistake -> summary, failure_mode"))
```

Because keys carry through, you can always **drill down** from a result to the rows behind it:

```python theme={null}
top = per_task.collect(lineage=True)[0]
top.inputs()                  # the per-trial rows behind it
top.inputs()[0].inputs()      # ...down to a run's steps
```

## Language Model Fold

`group_by(...).lm_fold(...)` walks a group's rows in key order, one call per row, carrying
state. Fields on both sides of the arrow are the state:

```python theme={null}
progress = src.steps.group_by("trial").lm_fold("progress, step -> progress, stuck: bool")
```

Each call reads the previous `progress` and the newest step, and writes the new `progress`.
Folds are what you use to follow a run as it happens.

## Batch and live run the same pipeline

Sources emit events: a row arrived, or a group closed (a trial finished). A stored source emits
every row and then every close; a live one emits them as they happen. The engine can't tell
the difference, so every pipeline above runs unchanged on a live stream. `.collect()` returns
final rows, and `.events()` streams every result as it's produced. See
[Live pipelines](https://docs.tracepress.ai/cookbooks/live.md).

## Reruns and the cache

LLM calls are cached, so rerunning a pipeline only pays for prompts that changed.
`table.rerun(edit=...)` uses that to answer what-if questions: it returns the table as if some
rows upstream had been different, such as `{("trial", "t1"): ("trial", "t2")}` to make run t1
look like run t2. `tp.last_run()` shows the calls, tokens and cost of the latest run.

## Next steps

> [card] [Why did my runs fail?](https://docs.tracepress.ai/cookbooks/failure-modes.md)
>
> The full step → trial → task pipeline.

> [card] [Python SDK](https://docs.tracepress.ai/reference/python.md)
>
> Every operator and its arguments.


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