---
title: Examples and recipes
description: 'Nine complete weft programs built from real-world agent shapes: approvals, subagent teams, flaky backends, budgets, MCP meshes, governance middleware, durable sessions, structured extraction and tool fan-out, each tested offline.'
url: https://weftgo.dev/docs/examples/
type: how-to
sources:
  - weft/AGENTS.md:20-193 (blocks 1-5, the API the recipes combine)
  - 'weft-arena/internal/suites/realworld.go, realworld_more.go: the rw.* scenarios (inspiration only)'
  - docs-site/examples/cookbook/ (one package per recipe, each with a test)
---

# Examples and recipes

Each recipe is one complete program that combines several weft features to solve a problem real agents have. The shapes come from the `rw.*` scenarios of weft-arena, weft's test harness. Every program runs offline against scripted models, is tested, and records its runs so you can open them in Studio. Pick the recipe closest to what you are building.

| You are building | Recipe | It combines |
|---|---|---|
| An agent whose risky actions wait for a person | [Support desk with human approval](https://weftgo.dev/docs/examples/support-desk/) | `RequireApproval`, a parking middleware, `Approve`/`Deny`/`Resolve` across a JSON round trip, `Output` |
| A lead agent that hands work to specialists | [Research team: a lead agent and specialist subagents](https://weftgo.dev/docs/examples/research-team/) | `Subagent`, `Parallelism`, `Nested` events, usage roll-up, `Output` |
| An agent over backends that fail | [Survive flaky infrastructure](https://weftgo.dev/docs/examples/flaky-infrastructure/) | `ModelRetry`, `Timeout`, panic containment, coded errors, `mw.Retry`, `mw.Fallback` |
| An agent that must not run away | [Guard against runaway agents](https://weftgo.dev/docs/examples/budget-guards/) | `UsageLimit`, `MaxSteps`, `DetectLoops`, `*RunError` with the partial transcript |
| Agents owned by different teams | [An agent mesh over MCP](https://weftgo.dev/docs/examples/mcp-mesh/) | `mcp.Serve`, `mcp.Tools`, `mcp.Prefix`, `ToolSource` |
| An agent inside company policy | [A governed agent stack: every seam on one run](https://weftgo.dev/docs/examples/governed-stack/) | token limiter, response cache, `mw.Audit`, `mw.Allow`, rate limiter, PII scrub, approval, content policy |
| A chat that remembers across restarts | [A multi-turn session that survives a crash](https://weftgo.dev/docs/examples/session-memory/) | `weft/thread` on JSONL, queued sends, `Continue`, custom ledger entries, compaction, branching |
| Typed data out of messy text | [Extract structured data from a messy document](https://weftgo.dev/docs/examples/structured-extraction/) | `Output[T]`, `GenerateAs`, the `INVALID_INPUT` repair loop, `mw.RepairJSON` |
| One step that asks for many tools | [Fan out many tool calls safely](https://weftgo.dev/docs/examples/tool-storm/) | `Parallelism`, a `Sequential` barrier, `MaxResultBytes` |

## How a recipe is laid out

Each page has the same sections: the problem and where it comes from, the program in parts with what each part does, what to look at in Studio, a table of the weft features it combines with the guide that explains each, the complete program with its output, and the gotchas. The recipes link the guides rather than re-explain them; start from the [guides](https://weftgo.dev/docs/guides/) when you need one feature on its own.

## Run a recipe

The programs live in the docs examples module, one package per recipe under `cookbook/`. From a clone of [weftgo.dev](https://github.com/weftgo/weftgo.dev):

```sh
cd docs-site/examples
go run ./cookbook/supportdesk
go test ./cookbook/...
```

The models are scripted with `wefttest.Script`, so no API key is needed and the output is the same on every run. Two lines rest on goroutine timing, each with a wide margin: tool-storm's `get_weather calls overlapped, never more than 3 at once: true` (calls sleep 50 ms, so they overlap) and governed-stack's `rate limiter spaced the calls: true` (the limiter's waits set a floor on the elapsed time). Swap the scripted model for a provider adapter to run one against a real model; see [Connect a model provider](https://weftgo.dev/docs/guides/providers/). Every recipe records with `otel.Install(otel.Local(""))` into `./.weft/weft.db` (or `$WEFT_DB`); open that database with `weft studio` to see the runs, as [Run Studio](https://weftgo.dev/docs/observe/studio/setup/) explains.

## See also

- [weft guides](https://weftgo.dev/docs/guides/): one task per page
- [weft concepts](https://weftgo.dev/docs/concepts/): what the pieces are and how they behave
- [Test an agent with a scripted model](https://weftgo.dev/docs/testing/scripted-models/): how the recipes run offline
- [`AGENTS.md`](https://github.com/weftgo/weft/blob/v0.20.2/AGENTS.md): the whole API on one screen; [pkg.go.dev](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2) for the signatures
