# LLM handler helpers

> std/llm/handlers provides small middleware helpers for call handlers that accept a call dict such as {prompt, system, opts} .

Website: https://harnlang.com/llm/handlers.html

This page documents Harn, which is pre-1.0. Language, standard library, and CLI APIs may change. If the intended version is unclear, clarify before using this page.

---

`std/llm/handlers` provides small middleware helpers for call handlers that
accept a call dict such as `{prompt, system, opts}`.

```harn
import { with_circuit_breaker } from "std/llm/handlers"

pipeline default(harness: Harness) {
  const raw_handler = fn(call) {
    return harness.llm.call(call.prompt, call?.system, call.opts)
  }
  const pooled = with_circuit_breaker(raw_handler)
  const shared = with_circuit_breaker(
    raw_handler, {name: "primary-llm", threshold: 3},
  )
}
```

`with_circuit_breaker(handler, options?)` uses a circuit named from each call's
`opts.provider` and `opts.model`, falling back to `"<unset>"` for missing
fields. A failing upstream therefore opens only its own circuit even when many
models share the same wrapper. Pass `name` when a caller deliberately wants one
shared circuit.

Options:

| Option | Default | Description |
|---|---:|---|
| `name` | derived | Manual circuit name for shared state |
| `threshold` | `5` | Consecutive failures before the circuit opens |
| `reset_ms` | `30000` | Time before `circuit_check` reports `half_open` |

---

## Read next

- [Exact token references](https://harnlang.com/llm/tokenizer.md)
- [LLM reranking](https://harnlang.com/llm/rerank.md)
