# How-to guides

Goal-oriented directions for getting a specific job done with `@demlik/tea`.
Grouped by the job, not by the module — reach for the guide whose title matches
what you are trying to do, and it names the subpath you need. The words the
guides use are each defined in the [glossary](../glossary.md).

## Upgrade

- [Migrate from 0.15 to 0.19](./migrate-from-0-15.md) — every removed or
  reshaped API, before and after: [`run`](../glossary.md#run) on `@demlik/tea/promise`,
  [handlers](../glossary.md#handler) and [Sub](../glossary.md#sub) runners at `run`, Subs as `{ _tag, deps }` data, handler outcomes, the end
  of `provide`, `mount*` and the `with*` wrappers, `drive` on
  `@demlik/tea/testing/promise`, and `store.migrate`'s `Refusal`.

## Run a machine somewhere

- [Ignore an answer that arrives too late](./ignore-a-late-answer.md) — keep
  newer search results when replies arrive out of order with the machine's
  `identity`, including a host that steps by hand with `matchesIdentity`.
- [Run a machine on the Promise engine](./run-on-the-promise-engine.md) — hand
  `run` from `@demlik/tea/promise` Promise handlers for a
  [machine](../glossary.md#machine) file that
  imports only `@demlik/tea`.
- [Run a machine on the Effect engine](./run-on-the-effect-engine.md) — run the
  same machine file with `run` from `@demlik/tea/effect`: Effect handlers,
  services from your Layers, and interruption when the scope closes.
- [Run many machines under one parent](./run-many-machines.md) — start each
  [child](../glossary.md#child) with `spawn` from the Effect [engine](../glossary.md#engine), keep your own table of live
  children, stop one with `stop`, and tell the parent when one stops with
  `tell`, with no supervisor from tea.
- [Make a machine durable and crash-recoverable](./make-durable.md) — if your
  storage can append atomically and read in order, write a `Journal` and use
  `eventSourcedStore` for a crash-safe log whose owed work survives. If you only
  keep the latest State, use a [`Store`](../glossary.md#store).
- [Reopen an investigation with retained history](./reopen-a-retained-case.md) —
  run a file-journal case, recover settled evidence after completion, and attach
  a client to complete committed views.
- [Deploy an agent to a Durable Object](./deploy-an-agent-to-a-durable-object.md) —
  run a `defineAgent` [agent](../glossary.md#agent) inside a Cloudflare Durable Object with `doStore`
  as its `Store`, so an eviction mid-run resumes on the next request instead of
  starting over.
- [Show a "couldn't restore" view](./restore-or-refuse.md) — `refuse` saved
  bytes `migrate` can't read, catch the `StoreRefusedError` `run` fails with,
  and start a run with no store so nothing saves over them.
- [Change a saved state's shape without losing old saves](./migrate-a-saved-state.md) —
  keep a `version` in the Model and walk an old save up one step at a time
  inside `migrate`, refusing a version no step reads.
- [Skip saving short-lived states](./skip-saving-transient-states.md) — wrap
  your `Store` so `save` does nothing while a reply streams, on either engine,
  with no tea option behind it.
- [Show a run's progress while it runs](./show-a-run-in-progress.md) — pass
  `onEvent` to `agent.run` and observe each model call and [tool](../glossary.md#tool) call start and
  settle, and the run's one `RunDone`, as the run produces them, instead of
  waiting on the one promise that resolves at the end, and `onChunk` for the
  token deltas below a turn.
- [Trace an agent run in Langfuse](./trace-an-agent-run-in-langfuse.md) — hand
  `traceAgent` from `@demlik/tea/otel` a tracer whose provider exports through
  `LangfuseSpanProcessor`, and every run lands as one trace — a run span, a
  generation per turn, a tool span per call — that a resume from the `Store`
  keeps writing into. Any OpenTelemetry backend reads the same spans.
- [Drive a machine from React](./drive-from-react.md) — use `useMachine` from
  `@demlik/tea/react` to own a [runtime](../glossary.md#runtime) for a component's lifetime and get a
  `[state, send]` pair, with a returnless sender for click handlers.

## Give an agent a model

- [Supply the agent's model](./supply-the-agents-model.md) — the contract tea's
  model function, `async (messages) => AgentTurn`, holds you to (`toolCalls`, the `provider`
  round-trip, the `async` requirement, the streaming arity form), and one
  adapter over the `openai` SDK that reaches every provider behind an
  OpenAI-compatible endpoint such as Cloudflare AI Gateway or OpenRouter.

## Test and verify

- [Replay a recorded run in a test](./replay-in-a-test.md) — assert what a machine
  did by [re-folding](../glossary.md#fold) its messages with [`replay`](../glossary.md#replay) and the `@demlik/tea/testing`
  assertions — pure, synchronous, no effects.
- [Test a machine with `drive`](./test-a-machine-with-drive.md) — hand `drive`
  from `@demlik/tea/testing/promise` or `@demlik/tea/testing/effect` fake
  [handlers](../glossary.md#handler), then assert on the final Model, on the
  whole [Cmds](../glossary.md#cmd) that ran in order, and on each failure path.
- [Find a wrong machine with a law](./find-a-wrong-machine-with-a-law.md) —
  state a [law](../glossary.md#law) such as "every `charge_card` Cmd carries the
  amount the order holds", draw Msg lists whose answers come from each Cmd's
  Effect Schema with `arbSchemaGuidedSequence` from `@demlik/tea/testing/effect`,
  and read the smallest list that breaks it when a machine charges the wrong
  amount.
- [Gate a refactor on a parity check](./gate-a-refactor-on-parity.md) — record a run
  with `@demlik/tea/parity`'s `recordRun`, re-fold it through the new machine with
  `goldenReplay`, and take a normalized GO/NO-GO verdict from `parityEqual`.

## Build an agent

- [Handle a tool failure](./handle-a-tool-failure.md) — declare an `err` tag, fail
  with it from the handler, and read the `ToolOutcome` the model gets back —
  including the six failures you never declared.
- [Bound a `defineAgent` run](./bound-a-run.md) — stop a run that goes on too
  long by setting `maxTurns`, `deadlineMs`, `maxElapsedMs` and `stopWhen` one
  step at a time, then all four on one agent, with a token budget on the way.
- [Wrap one tool's interpret cell](./wrap-one-tool-cell.md) — use
  `defineAgent(cfg).with({ interpret })` to give one tool a behaviour the [lid](../glossary.md#lid) has
  no option for — a queue, an audit log — without rebuilding the agent with
  `createAgent`.
- [Delegate to a sub-agent](./delegate-to-a-sub-agent.md) — wrap a child
  `defineAgent` with `agentTool` so a parent agent calls it like a tool and
  waits for its typed result, with the child's run under its own Store, so an
  evicted parent resumes the child instead of restarting it. Covers the Promise
  engine and a Durable Object.

## Compose batteries in one Model

- [Compose two battery slices in one Model](./compose-two-battery-slices.md) —
  thread each [battery](../glossary.md#battery) [verb](../glossary.md#verb)'s `[slice, cmds]` back into the host Model with
  `liftSlice`, whose key is checked against the Model, and state a read that
  spans both [slices](../glossary.md#battery-slice) as a named `readInOrder` precedence you can assert on
  instead of two `if` statements nothing can name.

## Harden a call

- [Add retry and backoff to a call](./add-resilience.md) — fold
  `@demlik/tea/retry-backoff`'s pure ops into [`update`](../glossary.md#update) so a flaky call schedules a
  backed-off retry without your `update` authoring any timing. Ends with the
  one-field version for a `defineAgent` model call, and the policy to declare
  against a provider 429/529.
- [Hand-wire a resilient call](./hand-wire-a-resilient-call.md) — wire
  what `@demlik/tea/resilience`'s `createResilientCall` returns into your own
  `update` [cell](../glossary.md#cell) by cell, with `settle` and an `onSettle` helper of your own, so a call
  gets retry, backoff and a circuit breaker from plain functions.

## Classify something

- [Ask Jev a typed question](./ask-jev-a-typed-question.md) — send a rubric to
  TypeSafe [Jev](../glossary.md#jev) with `@demlik/tea/jev` and get the answer back narrowed to the
  criteria keys you wrote, with the HTTP call as a handler you write so the whole
  call replays in a test without a key.

*Guides are added as the how-to quadrant grows.*
