Skip to content

Recipes ​

Short answers to "how do I…". Each recipe links to the page with the full detail. The TypeScript on this page is typechecked in CI: it is copied from ../examples/recipes/recipes.ts.

I want toRecipe
Wait for a personWait for an approval, with a deadline
Approve from an emailSend an approval link
Make an agent ask before actingApprove a tool call
Limit what an agent spendsCap an agent's spend
Limit what a pipeline spendsStop a pipeline when it has spent too much
Call a flaky model or APIRetry a flaky call
Run something for hoursRun a task for hours
Process many itemsFan out, narrow or wide
Keep going when one step failsTolerate a failure
Show progress in a UIStream progress to a UI
Avoid starting the same work twiceStart a run idempotently
Wait, then continueSleep or wait until a time
Write a worker in Python or GoWrite a worker in another language
Run a worker behind a firewallPull tasks instead of receiving pushes
Keep a run going for monthsCompact a very long run
Stop a runCancel a run

Wait for an approval, with a deadline ​

ts
export const approve = signal("order.approve", z.object({ by: z.string() }));

export const order = sequence([
  step(prepare),
  waitFor(approve, { timeoutMs: 86_400_000, onTimeout: "fail" }),
  step(ship, (state) => ({ approvedBy: signalOf(state, approve).by })),
]);

Deliver the approval with client.signal(runId, approve, { by: "dana" }). The run holds no compute while it waits. Use onTimeout: "continue" to carry on without an answer; the signal value is then null. See signals-and-humans.md.

ts
const link = await client.signalLink(runId, approve, { expiresInMs: 86_400_000 });
await sendEmail({ to: reviewer, body: `Approve here: ${link.url}` });

Opening the link shows a confirmation form and changes nothing, so mail scanners cannot approve by accident. A browser form sends no payload, so give a signal meant for links a schema that accepts an empty one (optional fields, or z.unknown()). See signals-and-humans.md.

Approve a tool call ​

Name the tools that need a person's yes:

ts
export const careful = agentLoop({
  decide,
  tools,
  budget: { tokens: 50_000 },
  approval: { tools: ["search"], timeoutMs: 86_400_000 },
});

When the model picks one, the run waits and holds no compute. Each call waits on its own signal, named after its iteration; the wait's step id is approve#<i>. Answer it either way:

ts
await client.signal(runId, approvalSignal(3), { approved: true, by: "dana" });
await client.signal(runId, approvalSignal(3), { approved: false, reason: "wrong audience" });

A rejection becomes an observation and the model decides again. To approve from an email, mint a link for that one call:

ts
const link = await client.signalLink(runId, approvalSignal(3));
await sendEmail({ to: reviewer, body: `The agent wants to search. Approve: ${link.url}` });

See agents.md.

Cap an agent's spend ​

ts
export const capped = agentLoop({
  decide,
  tools,
  budget: { tokens: 50_000, costMicroUsd: 2_000_000, steps: 25 },
  topup,
});

Report usage from each worker with ctx.reportUsage({ tokens, costMicroUsd }). When a ceiling is reached the run parks on awaiting_signal and keeps its work. Resume it with client.signal(runId, topup, { tokens: 20_000 }). See agents.md.

Stop a pipeline when it has spent too much ​

Every event carries the run's banked usage, and sequence keeps it in state.usage. Skip an expensive step once a ceiling is passed:

ts
export const frugal = step(polish, undefined, {
  when: (state) => (state.usage?.tokens ?? 0) < 100_000,
});

In a hand-written reducer, read event.usage_total and decide. See observability.md.

Retry a flaky call ​

Retries are part of the activity, not your handler:

ts
export const summarise = activity("doc.summarise", {
  input: z.object({ text: z.string(), compact: z.boolean().optional() }),
  output: z.object({ summary: z.string() }),
  retry: { maxAttempts: 5, backoff: "exponential", initialMs: 2_000, maxMs: 60_000 },
  timeoutMs: 120_000,
});

Throw from the handler and the policy decides. If repeating the same request would fail the same way, change the input on later attempts. Here callModel is your handler:

ts
export const summariser = defineWorker({
  "doc.summarise": withRetryInput(callModel, (input, attempt) => ({ ...input, compact: attempt > 1 })),
});

See activities-and-workers.md.

Run a task for hours ​

Leave timeoutMs unset. A task is held by a lease that the SDK worker renews automatically, so there is no duration ceiling. If the worker dies, the lease lapses and the retry policy takes over. Pass ctx.signal to long calls so they stop when the lease is lost. See activities-and-workers.md.

Fan out, narrow or wide ​

For up to a few dozen items, fan out tasks inside the run:

ts
export const narrow = fanOut(
  resize,
  (state) => resultOf(state, list).images.map((image) => ({ image })),
  { concurrency: 4 },
);

For hundreds or thousands, fan out child runs. One run with very many tasks slows down sharply; child runs do not have that problem.

ts
export const wide = fanOutRuns(
  "process-image",
  (state) => resultOf(state, list).images.map((image) => ({ image })),
  { concurrency: 20 },
);

Read results with fanOutResultsOf and fanOutRunsResultsOf. See workflows.md and limits.md.

Tolerate a failure ​

ts
step(notify, undefined, { onFailure: "continue" }),
fanOut(fetchSource, () => [{ url: "https://example.com" }], { concurrency: 4, onFailure: "continue" }),

A failed step stores null instead of failing the run, so narrow the value when you read it.

Stream progress to a UI ​

Durable progress is the journal. Follow it over SSE:

ts
for await (const message of client.stream(runId)) {
  if (message.type === "journal") render(message.data.step_id, message.data.status);
  if (message.type === "emit") renderHint(message.data);
  if (message.type === "end") finish(message.data.status);
}

For extra detail that does not need to survive a reconnect, emit it from the reducer with cmd.emit("progress", { percent: 40 }). Emits are not journaled. From a browser, use the SSE form of GET /v1/runs/{id}/events; see http-api.md.

Start a run idempotently ​

Choose the run id yourself. A second start with the same id does not create a second run.

ts
await client.start("weekly-report", { team }, { runId: `weekly_${team}_${isoWeek}` });

client.startFresh joins a live run under that key, and starts a new one (<id>_2, <id>_3, …) once the earlier runs have ended. See http-api.md.

Sleep or wait until a time ​

ts
export const reminder = sequence([step(sendInvite), sleep(3 * 86_400_000), step(sendReminder)]);

In a hand-written reducer use cmd.timer({ ms }) or cmd.timer({ atMs }) and handle on.timer. A sleeping run holds no compute.

Write a worker in another language ​

A worker is an HTTP endpoint. It verifies the signature, answers 202, does the work and calls back.

python
import hashlib, hmac, json, requests

def handle_push(headers, raw_body: bytes):
    task_id, attempt, ts = headers["vlo-task-id"], headers["vlo-attempt"], headers["vlo-timestamp"]
    body_hash = hashlib.sha256(raw_body).hexdigest()
    expected = "v1=" + hmac.new(PUSH_SECRET, f"{task_id}.{attempt}.{ts}.{body_hash}".encode(), hashlib.sha256).hexdigest()
    if not hmac.compare_digest(expected, headers["vlo-signature"]):
        return 401

    task = json.loads(raw_body)
    queue.submit(run_task, task)   # do the work after answering
    return 202

def run_task(task):
    output = predict(task["input"])
    requests.post(
        task["complete_url"] + "/complete",
        headers={"authorization": f"Bearer {VLO_TOKEN}"},
        json={"attempt": task["attempt"], "output": output, "usage": {"tokens": 1200}},
    )

Also reject a vlo-timestamp more than five minutes old, and heartbeat on {complete_url}/heartbeat for work longer than the lease. The full protocol is in activities-and-workers.md.

Pull tasks instead of receiving pushes ​

Set the queue to mode: "pull" and run a poller wherever the worker can reach the API:

ts
await runPullWorker({ baseUrl: "https://vlo.example.com", token: process.env.VLO_TOKEN!, worker: mediaWorker });

See activities-and-workers.md.

Compact a very long run ​

A run that loops for months should not carry its whole history. End it and start a successor with fresh history:

ts
return [state, [cmd.continueAsNew({ cursor: state.cursor })]];

The two runs are linked, and a parent is told once, by the run that finally closes. See workflows.md.

Cancel a run ​

ts
await client.cancel(runId);

The run ends as cancelled. Cancelling a run that has already ended does nothing.