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

# Recipes

> Short patterns for common jobs.

## Diff, then validate what changed

Use the diff to find candidates, then the validator to test them on the trained model:

```python gpu theme={null}
from circuitcheck.diff import diff_graphs
from circuitcheck.tracer import TracerBackend, nodes_for_units
from circuitcheck.validate import GroupSpec, validate

report = diff_graphs(before, after)
units = [tuple(map(int, c.unit.split(":"))) for c in report.top]
backend = TracerBackend(trained_model)
nodes = nodes_for_units(backend, prompt, units, positions="all")
result = validate(backend, prompt, nodes, metric=metric, groups=[GroupSpec(name="top changes", nodes=nodes)])
```

## Label the features a diff found

```python theme={null}
import gzip
import json

from circuitcheck.diff import diff_graphs, load_summaries
from circuitcheck.labels import FeatureData

g = load_summaries("examples/data/rl_hack_graphs.json.gz")
report = diff_graphs(g["installed/before"], g["installed/after"])
wanted = {c.unit for c in report.top}
print(sorted(wanted))
# then: FeatureData(unit=u, records=...) for each, from your model's activations, and label_features(...)
```

## Save and reload any report

```python theme={null}
from circuitcheck.validate import Report, logit_diff, validate
from circuitcheck.validate.toy import demo, demo_nodes

backend = demo()
report = validate(backend, "dallas", demo_nodes(backend),
                  metric=logit_diff(backend.token_id(" Austin"), backend.token_id(" Sacramento")))
text = report.model_dump_json(indent=1)
again = Report.model_validate_json(text)
assert again.groups[0].verdict == report.groups[0].verdict
```

## A custom metric

A metric is any function of the last position's logits:

```python theme={null}
import numpy as np

from circuitcheck.validate import Metric

keyword_ids, alternative = [101, 202, 303], 404
metric = Metric(
    "logsumexp(keywords) - logit(alternative)",
    lambda x: float(np.log(np.exp(x[keyword_ids]).sum()) - x[alternative]),
)
print(metric(np.zeros(500)))
```

## Make the HTML pages

```python theme={null}
from circuitcheck.report import render_validate, save_html
from circuitcheck.validate import logit_diff, validate
from circuitcheck.validate.toy import demo, demo_nodes

backend = demo()
report = validate(backend, "dallas", demo_nodes(backend),
                  metric=logit_diff(backend.token_id(" Austin"), backend.token_id(" Sacramento")))
save_html(render_validate(report), "validate.html")
```


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