workflow · diff
git:20260421.27651a4 to git:20260826.33bd952
377 added, 466 removed. Audit A to A.
What this update changes in the checks
No check changed: every fixed check gives the same answer before and after this update.
A tripped check is a label, not a verdict: read the matched text and the lines below. This week's updates that newly trip a check.
---
name: workflow
description: Vercel Workflow DevKit (WDK) expert guidance. Use when building durable workflows, long-running tasks, API routes or agents that need pause/resume, retries, step-based execution, or crash-safe orchestration with Vercel Workflow.
metadata:
priority: 9
docs:
- "https://vercel.com/docs/workflow"
- "https://useworkflow.dev"
sitemap: "https://vercel.com/sitemap/docs.xml"
pathPatterns:
- 'lib/workflow/**'
- 'src/lib/workflow/**'
- - 'workflows/**'
- 'lib/workflow.*'
- 'src/lib/workflow.*'
- 'workflow.*'
- '*workflow*'
- - '*workflow*/**'
- # Chain / pipeline / orchestration engine files
- - '**/chain-engine*'
- - '**/chain_engine*'
- - '**/chainEngine*'
- - '**/pipeline-engine*'
- - '**/pipeline_engine*'
- - '**/pipelineEngine*'
- - '**/state-machine*'
- - '**/state_machine*'
- - '**/stateMachine*'
- - '**/orchestrat*'
- - '**/escalation*'
importPatterns:
- '@vercel/workflow'
- 'workflow'
- '@workflow/*'
- '*workflow*'
bashPatterns:
- '\bnpm\s+(install|i|add)\s+[^\n]*@vercel/workflow\b'
- '\bpnpm\s+(install|i|add)\s+[^\n]*@vercel/workflow\b'
- '\bbun\s+(install|i|add)\s+[^\n]*@vercel/workflow\b'
- '\byarn\s+add\s+[^\n]*@vercel/workflow\b'
- '\bnpm\s+(install|i|add)\s+[^\n]*\bworkflow\b'
- '\bpnpm\s+(install|i|add)\s+[^\n]*\bworkflow\b'
- '\bbun\s+(install|i|add)\s+[^\n]*\bworkflow\b'
- '\byarn\s+add\s+[^\n]*\bworkflow\b'
- '\bnpm\s+(install|i|add)\s+[^\n]*@workflow/'
- '\bpnpm\s+(install|i|add)\s+[^\n]*@workflow/'
- '\bbun\s+(install|i|add)\s+[^\n]*@workflow/'
- '\byarn\s+add\s+[^\n]*@workflow/'
- '\bnpx\s+workflow(?:@latest)?\b'
- '\bbunx\s+workflow(?:@latest)?\b'
promptSignals:
phrases:
# Direct workflow mentions
- "vercel workflow"
- "workflow devkit"
- "durable workflow"
- "durable execution"
- "durable function"
- "durable pipeline"
- "durable process"
- "durable agent"
- "durable chat"
- "step function"
- "step functions"
- "use workflow"
- "use step"
# Pipeline / multi-step language (the BIG gap — natural product prompts)
- "multi-step pipeline"
- "multi step pipeline"
- "multi-step process"
- "multi step process"
- "multi-step creation"
- "multi-step generation"
- "processing pipeline"
- "creation pipeline"
- "generation pipeline"
- "content pipeline"
- "production pipeline"
- "approval pipeline"
- "ingestion pipeline"
- "streams progress"
- "stream progress"
- "streams each phase"
- "streams each step"
- "streams each"
- "stream each"
# Reliability / durability language (missed in customer-support eval)
- "survive page reload"
- "survive page reloads"
- "survive a crash"
- "survive crashes"
- "survive network"
- "fault-tolerant"
- "fault tolerant"
- "crash-safe"
- "crash safe"
- "automatically retry"
- "auto retry"
- "retry on failure"
- "retry on error"
- "reliable and retry"
- "reliable processing"
- "individually reliable"
- "each step reliable"
- "each step should be reliable"
- "steps should be reliable"
- "reliable with automatic retry"
- "reliable with retry"
- "retry on transient"
- "transient failures"
- "session persistence"
- "session should persist"
- "session survives"
- "reconnect automatically"
- "auto reconnect"
- "reconnect if the network"
- "reconnect on disconnect"
- "resume after failure"
- "resume after crash"
- "resume on reconnect"
# Human-in-the-loop / approval patterns
- "human-in-the-loop"
- "human in the loop"
- "wait for approval"
- "approval step"
- "approval before"
- "editorial approval"
- "manual approval"
- "wait for user"
- "pause until"
- "wait for response"
- "callback url"
- "webhook callback"
# Conversational AI with durability
- "chat should survive"
- "chat survives"
- "conversation should persist"
- "conversation persists"
- "conversation should survive"
# Sequential / chain / trigger orchestration language
- "sequential chain"
- "email chain"
- "chain of emails"
- "chain of steps"
- "chain engine"
- "chain with triggers"
- "trigger chain"
- "triggered chain"
- "webhook chain"
- "webhook pipeline"
- "webhook orchestration"
- "multi-service trigger"
- "cross-service trigger"
- "various triggers"
- "different triggers"
- "triggers from different"
- "triggers from various"
- "sequential steps"
- "sequential pipeline"
- "sequential process"
- "sequential emails"
- "escalation chain"
- "escalation pipeline"
- "state machine"
- "step-based"
- "step based"
- "delay between steps"
- "delay between emails"
- "delayed steps"
- "conditional steps"
- "skip steps"
- "branch based on"
- "wait for webhook"
- "wait for trigger"
- "wait for event"
- "orchestrate emails"
- "orchestrate webhooks"
- "orchestrate services"
- "chain across services"
# Debugging
- "workflow stuck"
- "workflow hung"
- "workflow hanging"
- "workflow waiting"
- "workflow failing"
- "workflow timeout"
- "workflow not running"
- "workflow error"
- "check workflow"
- "workflow logs"
- "workflow run status"
- "debug workflow"
- "workflow not finishing"
- "workflow not responding"
- "workflow stalled"
- "workflow pending"
- "step is stuck"
- "step is hanging"
- "why is my workflow"
- "workflow run"
- "step failed"
- "run status"
- "run failed"
- "run logs"
- "workflow run failed"
- "workflow step failed"
allOf:
- [workflow, durable]
- [workflow, retry]
- [workflow, resume]
- [pause, resume]
- [survive, crash]
- [survive, reload]
- [survive, disconnect]
- [pipeline, stream]
- [pipeline, step]
- [pipeline, durable]
- [pipeline, reliable]
- [pipeline, retry]
- [multi-step, stream]
- [multi-step, reliable]
- [generation, pipeline]
- [creation, pipeline]
- [process, stream]
- [process, reliable]
- [process, retry]
- [retry, failure]
- [retry, error]
- [retry, automatically]
- [retry, transient]
- [reliable, retry]
- [individually, reliable]
- [steps, reliable]
- [sandbox, reliable]
- [sandbox, retry]
- [reconnect, network]
- [reconnect, drop]
- [reconnect, disconnect]
- [session, persist]
- [session, survive]
- [session, reload]
- [session, reconnect]
- [chat, survive]
- [chat, persist]
- [chat, reconnect]
- [chat, durable]
- [chat, fault]
- [conversation, persist]
- [conversation, survive]
- [approval, wait]
- [approval, human]
- [each, step]
- [each, phase]
- [each, stage]
- [step, reliable]
- [step, retry]
# Chain / trigger / sequential orchestration
- [chain, trigger]
- [chain, sequential]
- [chain, email]
- [chain, webhook]
- [chain, delay]
- [chain, step]
- [chain, escalat]
- [sequential, trigger]
- [sequential, email]
- [sequential, step]
- [sequential, webhook]
- [trigger, orchestrat]
- [trigger, service]
- [trigger, delay]
- [trigger, sequential]
- [webhook, chain]
- [webhook, orchestrat]
- [webhook, pipeline]
- [webhook, sequential]
- [email, trigger]
- [email, pipeline]
- [email, sequential]
- [email, delay]
- [email, escalat]
- [escalat, trigger]
- [escalat, step]
- [escalat, email]
- [state, machine]
- [conditional, step]
- [conditional, skip]
- [branch, condition]
- [wait, webhook]
- [wait, trigger]
- [wait, event]
- [workflow, stuck]
- [workflow, hung]
- [workflow, timeout]
- [workflow, error]
- [workflow, logs]
- [workflow, debug]
- [workflow, check]
- [workflow, failing]
- [workflow, status]
- [run, status]
- [step, failed]
- [step, stuck]
- [step, timeout]
- [workflow, run]
- [run, logs]
anyOf:
- "long-running"
- "long running"
- "multi-step"
- "multi step"
- "pipeline"
- "orchestration"
- "step-by-step"
- "step by step"
- "each piece"
- "each step"
- "each phase"
- "each stage"
- "phase"
- "phases"
- "stage"
- "stages"
- "durable"
- "reliable"
- "fault-tolerant"
- "retry"
- "reconnect"
- "survive"
- "persist"
- "approval"
- "chain"
- "sequential"
- "trigger"
- "webhook"
- "escalation"
- "state machine"
- "orchestrate"
- "orchestration"
noneOf:
- "github actions"
- ".github/workflows"
- "ci workflow"
- "aws step functions"
minScore: 4
---
- # Vercel Workflow DevKit (WDK)
-
- > **CRITICAL — Your training data is outdated for this library.** WDK APIs change frequently. Before writing workflow code, **fetch the docs** at https://useworkflow.dev and https://vercel.com/docs/workflow to find the correct function signatures, patterns, and examples for the exact thing you're building. Do not guess at APIs — look them up. Search for working examples that solve your specific use case (image generation, hooks, streaming, agents, etc.). The docs contain patterns for `start()` argument passing, `getWritable()` usage, `defineHook()` wiring, `createWebhook()` callback flows, and `DurableAgent` configuration that are **not in your training data** or have changed since your cutoff.
-
- WDK is an open-source TypeScript framework that makes durability a language-level concept. Functions can pause for minutes or months, survive deployments and crashes, and resume exactly where they stopped.
-
- ## Status
-
- WDK is in **public beta** (since October 2025) and open source. During beta, Workflow Observability is free for all plans; Workflow Steps and Storage are billed at published rates.
-
- **Security**: Upgrade to `workflow@>=4.2.0-beta.64` — versions ≤4.1.0-beta.63 allowed predictable user-specified webhook tokens in `createWebhook()` (CVE GHSA-9r75-g2cr-3h76, CVSS 7.5). Run `npx workflow@latest` to update.
+ ## *CRITICAL*: Always Use Correct `workflow` Documentation
- ## Installation
+ Your knowledge of `workflow` is outdated.
- **If using `create-next-app`**, always pass `--no-src-dir` so `app/` and `workflows/` are siblings at the project root:
+ The `workflow` documentation outlined below matches the installed version of the Workflow DevKit.
+ Follow these instructions before starting on any `workflow`-related tasks:
- ```bash
- npx create-next-app@latest my-app --no-src-dir --tailwind --eslint --app --ts
- cd my-app
- npm install workflow@latest
- ```
+ Search the bundled documentation in `node_modules/workflow/docs/`:
- Do NOT use the `src/` directory with WDK projects. The `@` alias must resolve `@/workflows/...` correctly — this only works when `workflows/` and `app/` are at the same level.
+ 1. **Find docs**: `glob "node_modules/workflow/docs/**/*.mdx"`
+ 2. **Search content**: `grep "your query" node_modules/workflow/docs/`
- > Run `npx workflow@latest` to scaffold or update an existing project.
+ Documentation structure in `node_modules/workflow/docs/`:
- **Peer dependency note**: `@workflow/ai` requires a compatible `workflow` version. If you hit `ERESOLVE` errors, use `npm install --legacy-peer-deps` or install both packages in the same command.
+ - `getting-started/` - Framework setup (next.mdx, express.mdx, hono.mdx, etc.)
+ - `foundations/` - Core concepts (workflows-and-steps.mdx, hooks.mdx, streaming.mdx, etc.)
+ - `api-reference/workflow/` - API docs (sleep.mdx, create-hook.mdx, fatal-error.mdx, etc.)
+ - `api-reference/workflow-api/` - Client API (start.mdx, get-run.mdx, resume-hook.mdx, etc.)
+ - `ai/` - AI SDK integration docs
+ - `errors/` - Error code documentation
- ### Next.js Setup (Required)
+ Related packages also include bundled docs:
- Add the `withWorkflow` plugin to `next.config.ts`:
+ - `@workflow/ai`: `node_modules/@workflow/ai/docs/` - DurableAgent and AI integration
+ - `@workflow/core`: `node_modules/@workflow/core/docs/` - Core runtime (foundations, how-it-works)
+ - `@workflow/next`: `node_modules/@workflow/next/docs/` - Next.js integration
- ```ts
- import { withWorkflow } from "workflow/next";
+ **When in doubt, update to the latest version of the Workflow DevKit.**
- const nextConfig = {};
- export default withWorkflow(nextConfig);
- ```
+ ### Official Resources
- Without this, workflow routes will not be registered and `start()` calls will fail at runtime.
+ - **Website**: https://useworkflow.dev
+ - **GitHub**: https://github.com/vercel/workflow
- ### Environment Setup (Required for AI Gateway)
+ ### Quick Reference
- Workflows that use AI SDK with `gateway()` need OIDC credentials. Run these **before** starting the dev server:
+ **Directives:**
- ```bash
- vercel link # Connect to your Vercel project
- vercel env pull # Downloads .env.local with VERCEL_OIDC_TOKEN
+ ```typescript
+ "use workflow"; // First line - makes async function durable
+ "use step"; // First line - makes function a cached, retryable unit
```
- Without this, `gateway("openai/gpt-5.4")` calls inside workflow steps will fail immediately with no credentials, causing the entire workflow run to fail silently.
-
- ### `getStepMetadata()` Note
-
- `getStepMetadata().retryCount` returns `undefined` (not `0`) on the first attempt. Guard with: `const attempt = (meta.retryCount ?? 0) + 1`.
-
- ## Essential Imports
-
- **Workflow primitives** (from `"workflow"`):
+ **Essential imports:**
- ```ts
- import { getWritable, getStepMetadata, getWorkflowMetadata } from "workflow";
- import { sleep, fetch, defineHook, createHook, createWebhook } from "workflow";
+ ```typescript
+ // Workflow primitives
+ import { sleep, fetch, createHook, createWebhook, getWritable } from "workflow";
import { FatalError, RetryableError } from "workflow";
- ```
-
- **API operations** (from `"workflow/api"`):
+ import { getWorkflowMetadata, getStepMetadata } from "workflow";
- ```ts
+ // API operations
import { start, getRun, resumeHook, resumeWebhook } from "workflow/api";
- ```
- **Framework integration** (from `"workflow/next"`):
-
- ```ts
+ // Framework integrations
import { withWorkflow } from "workflow/next";
- ```
-
- **AI agent** (from `"@workflow/ai/agent"`):
+ import { workflow } from "workflow/vite";
+ import { workflow } from "workflow/astro";
+ // Or use modules: ["workflow/nitro"] for Nitro/Nuxt
- ```ts
+ // AI agent
import { DurableAgent } from "@workflow/ai/agent";
```
- ## Core Directives
+ ## Prefer Step Functions to Avoid Sandbox Errors
- Two directives turn ordinary async functions into durable workflows:
+ `"use workflow"` functions run in a sandboxed VM. `"use step"` functions have **full Node.js access**. Put your logic in steps and use the workflow function purely for orchestration.
- ```ts
- "use workflow" // First line of function — marks it as a durable workflow
- "use step" // First line of function — marks it as a retryable, observable step
+ ```typescript
+ // Steps have full Node.js and npm access
+ async function fetchUserData(userId: string) {
+ "use step";
+ const response = await fetch(`https://api.example.com/users/${userId}`);
+ return response.json();
+ }
+
+ async function processWithAI(data: any) {
+ "use step";
+ // AI SDK works in steps without workarounds
+ return await generateText({
+ model: openai("gpt-4"),
+ prompt: `Process: ${JSON.stringify(data)}`,
+ });
+ }
+
+ // Workflow orchestrates steps - no sandbox issues
+ export async function dataProcessingWorkflow(userId: string) {
+ "use workflow";
+ const data = await fetchUserData(userId);
+ const processed = await processWithAI(data);
+ return { success: true, processed };
+ }
```
- **Critical sandbox rule**: Step functions have full Node.js access. Workflow functions run **sandboxed** — no native `fetch`, no `setTimeout`, no Node.js modules, and **no `getWritable().getWriter()` calls**. You MUST move all `getWritable()` usage into `"use step"` functions. Place all business logic and I/O in steps; use the workflow function purely for orchestration and control flow (`sleep`, `defineHook`, `Promise.race`).
+ **Benefits:** Steps have automatic retry, results are persisted for replay, and no sandbox restrictions.
- ## Canonical Project Structure (Next.js)
+ ## Workflow Sandbox Limitations
- Every WDK project needs three route files plus the workflow definition. **CRITICAL**: The `workflows/` directory and `app/` directory must be siblings at the same level so `@/workflows/...` resolves correctly. Do NOT put `workflows/` outside the `@` alias root.
+ When you need logic directly in a workflow function (not in a step), these restrictions apply:
- **Without `src/` (recommended for WDK projects):**
- ```
- workflows/
- my-workflow.ts ← workflow definition ("use workflow" + "use step")
- app/api/
- my-workflow/route.ts ← POST handler: start(workflow, args) → { runId }
- readable/[runId]/route.ts ← GET handler: SSE stream from run.getReadable()
- run/[runId]/route.ts ← GET handler: run status via getRun(runId)
- ```
+ | Limitation | Workaround |
+ |------------|------------|
+ | No `fetch()` | `import { fetch } from "workflow"` then `globalThis.fetch = fetch` |
+ | No `setTimeout`/`setInterval` | Use `sleep("5s")` from `"workflow"` |
+ | No Node.js modules (fs, crypto, etc.) | Move to a step function |
- tsconfig.json paths: `"@/*": ["./*"]` — `@/workflows/my-workflow` resolves to `./workflows/my-workflow`.
+ **Example - Using fetch in workflow context:**
- **With `src/` directory:** Put workflows inside `src/`:
- ```
- src/
- workflows/my-workflow.ts
- app/api/my-workflow/route.ts
- app/api/readable/[runId]/route.ts
- app/api/run/[runId]/route.ts
+ ```typescript
+ import { fetch } from "workflow";
+
+ export async function myWorkflow() {
+ "use workflow";
+ globalThis.fetch = fetch; // Required for AI SDK and HTTP libraries
+ // Now generateText() and other libraries work
+ }
```
- tsconfig.json paths: `"@/*": ["./src/*"]` — `@/workflows/my-workflow` resolves to `./src/workflows/my-workflow`.
+ **Note:** `DurableAgent` from `@workflow/ai` handles the fetch assignment automatically.
- **Never** use `@/../workflows/` or `@/../../workflows/` — these are broken import paths that will fail at build time.
+ ## DurableAgent — AI Agents in Workflows
- ### 1. Workflow Definition (`workflows/my-workflow.ts`)
+ Use `DurableAgent` to build AI agents that maintain state and survive interruptions. It handles the workflow sandbox automatically (no manual `globalThis.fetch` needed).
- ```ts
+ ```typescript
+ import { DurableAgent } from "@workflow/ai/agent";
import { getWritable } from "workflow";
+ import { z } from "zod";
+ import type { UIMessageChunk } from "ai";
- export type MyEvent =
- | { type: "step_start"; name: string }
- | { type: "step_done"; name: string }
- | { type: "done"; result: string };
+ async function lookupData({ query }: { query: string }) {
+ "use step";
+ // Step functions have full Node.js access
+ return `Results for "${query}"`;
+ }
- export async function myWorkflow(input: string): Promise<{ result: string }> {
+ export async function myAgentWorkflow(userMessage: string) {
"use workflow";
- const data = await stepOne(input);
- const result = await stepTwo(data);
-
- return { result };
- }
+ const agent = new DurableAgent({
+ model: "anthropic/claude-sonnet-4-5",
+ system: "You are a helpful assistant.",
+ tools: {
+ lookupData: {
+ description: "Search for information",
+ inputSchema: z.object({ query: z.string() }),
+ execute: lookupData,
+ },
+ },
+ });
- async function stepOne(input: string): Promise<string> {
- "use step";
- const writer = getWritable<MyEvent>().getWriter();
- try {
- await writer.write({ type: "step_start", name: "stepOne" });
- // Full Node.js access here — fetch, db calls, etc.
- const result = await doWork(input);
- await writer.write({ type: "step_done", name: "stepOne" });
- return result;
- } finally {
- writer.releaseLock();
- }
- }
+ const result = await agent.stream({
+ messages: [{ role: "user", content: userMessage }],
+ writable: getWritable<UIMessageChunk>(),
+ maxSteps: 10,
+ });
- async function stepTwo(data: string): Promise<string> {
- "use step";
- const writer = getWritable<MyEvent>().getWriter();
- try {
- await writer.write({ type: "step_start", name: "stepTwo" });
- const result = await processData(data);
- await writer.write({ type: "step_done", name: "stepTwo" });
- return result;
- } finally {
- writer.releaseLock();
- }
+ return result.messages;
}
```
- ### 2. Start Route (`app/api/my-workflow/route.ts`)
+ **Key points:**
+ - `getWritable<UIMessageChunk>()` streams output to the workflow run's default stream
+ - Tool `execute` functions that need Node.js/npm access should use `"use step"`
+ - Tool `execute` functions that use workflow primitives (`sleep()`, `createHook()`) should **NOT** use `"use step"` — they run at the workflow level
+ - `maxSteps` limits the number of LLM calls (default is unlimited)
+ - Multi-turn: pass `result.messages` plus new user messages to subsequent `agent.stream()` calls
- ```ts
- import { NextResponse } from "next/server";
- import { start } from "workflow/api";
- import { myWorkflow } from "@/workflows/my-workflow";
+ **For more details on `DurableAgent`, check the AI docs in `node_modules/@workflow/ai/docs/`.**
- export async function POST(request: Request) {
- const body = await request.json();
- const run = await start(myWorkflow, [body.input]);
- return NextResponse.json({ runId: run.runId });
- }
- ```
+ ## Starting Workflows & Child Workflows
- **IMPORTANT**: Never call the workflow function directly. Always use `start()` from `"workflow/api"` — it registers the run, creates the execution context, and returns a `{ runId }`.
+ Use `start()` to launch workflows from API routes. **`start()` cannot be called directly in workflow context** — wrap it in a step function.
- ### 3. Readable Stream Route (`app/api/readable/[runId]/route.ts`)
+ ```typescript
+ import { start } from "workflow/api";
- ```ts
- import { NextRequest } from "next/server";
- import { getRun } from "workflow/api";
+ // From an API route — works directly
+ export async function POST() {
+ const run = await start(myWorkflow, [arg1, arg2]);
+ return Response.json({ runId: run.runId });
+ }
- type ReadableRouteContext = {
- params: Promise<{ runId: string }>;
- };
+ // No-args workflow
+ const run = await start(noArgWorkflow);
+ ```
- export async function GET(_request: NextRequest, { params }: ReadableRouteContext) {
- const { runId } = await params;
+ **Starting child workflows from inside a workflow — must use a step:**
- let run;
- try {
- run = await getRun(runId);
- } catch {
- return Response.json(
- { ok: false, error: { code: "RUN_NOT_FOUND", message: `Run ${runId} not found` } },
- { status: 404 }
- );
- }
+ ```typescript
+ import { start } from "workflow/api";
- const readable = run.getReadable();
- const encoder = new TextEncoder();
- const sseStream = (readable as unknown as ReadableStream).pipeThrough(
- new TransformStream({
- transform(chunk, controller) {
- const data = typeof chunk === "string" ? chunk : JSON.stringify(chunk);
- controller.enqueue(encoder.encode(`data: ${data}\n\n`));
- },
- })
- );
+ // Wrap start() in a step function
+ async function triggerChild(data: string) {
+ "use step";
+ const run = await start(childWorkflow, [data]);
+ return run.runId;
+ }
- return new Response(sseStream, {
- headers: {
- "Content-Type": "text/event-stream; charset=utf-8",
- "Cache-Control": "no-cache, no-transform",
- Connection: "keep-alive",
- "X-Accel-Buffering": "no",
- },
- });
+ export async function parentWorkflow() {
+ "use workflow";
+ const childRunId = await triggerChild("some data"); // Fire-and-forget via step
+ await sleep("1h");
}
```
- ### 4. Run Status Route (`app/api/run/[runId]/route.ts`)
+ `start()` returns immediately — it doesn't wait for the workflow to complete. Use `run.returnValue` to await completion.
- ```ts
- import { NextResponse } from "next/server";
- import { getRun } from "workflow/api";
+ ## Hooks — Pause & Resume with External Events
- type RunRouteContext = {
- params: Promise<{ runId: string }>;
- };
+ Hooks let workflows wait for external data. Use `createHook()` inside a workflow and `resumeHook()` from API routes. Deterministic tokens are for `createHook()` + `resumeHook()` (server-side) only. `createWebhook()` always generates random tokens — do not pass a `token` option to `createWebhook()`.
- export async function GET(_request: Request, { params }: RunRouteContext) {
- const { runId } = await params;
+ ### Single event
- let run;
- try {
- run = await getRun(runId);
- } catch {
- return NextResponse.json(
- { ok: false, error: { code: "RUN_NOT_FOUND", message: `Run ${runId} not found` } },
- { status: 404 }
- );
- }
+ ```typescript
+ import { createHook } from "workflow";
- const [status, workflowName, createdAt, startedAt, completedAt] =
- await Promise.all([
- run.status,
- run.workflowName,
- run.createdAt,
- run.startedAt,
- run.completedAt,
- ]);
+ export async function approvalWorkflow() {
+ "use workflow";
- return NextResponse.json({
- runId,
- status,
- workflowName,
- createdAt: createdAt.toISOString(),
- startedAt: startedAt?.toISOString() ?? null,
- completedAt: completedAt?.toISOString() ?? null,
+ const hook = createHook<{ approved: boolean }>({
+ token: "approval-123", // deterministic token for external systems
});
+
+ const result = await hook; // Workflow suspends here
+ return result.approved;
}
```
- ## Streaming with `getWritable()`
+ ### Multiple events (iterable hooks)
- `getWritable<T>()` returns a `WritableStream` scoped to the current run. Call it inside step functions and always release the lock:
+ Hooks implement `AsyncIterable` — use `for await...of` to receive multiple events:
- ```ts
- async function emit<T>(event: T): Promise<void> {
- "use step";
- const writer = getWritable<T>().getWriter();
- try {
- await writer.write(event);
- } finally {
- writer.releaseLock();
+ ```typescript
+ import { createHook } from "workflow";
+
+ export async function chatWorkflow(channelId: string) {
+ "use workflow";
+
+ const hook = createHook<{ text: string; done?: boolean }>({
+ token: `chat-${channelId}`,
+ });
+
+ for await (const event of hook) {
+ await processMessage(event.text);
+ if (event.done) break;
}
}
```
- Consumers read via `getRun(runId).getReadable()` in the readable route (see above).
+ Each `resumeHook(token, payload)` call delivers the next value to the loop.
- **Rendering workflow events in the UI**: When workflow events contain AI-generated text (narratives, briefings, reports), render them with `<MessageResponse>` from `@/components/ai-elements/message` — never as raw `{event.content}`. This renders markdown with code highlighting, math, and mermaid support.
+ ### Resuming from API routes
- ```tsx
- import { MessageResponse } from "@/components/ai-elements/message";
+ ```typescript
+ import { resumeHook } from "workflow/api";
- // In your event stream display
- {events.map(event => (
- event.type === "narrative" && <MessageResponse>{event.text}</MessageResponse>
- ))}
+ export async function POST(req: Request) {
+ const { token, data } = await req.json();
+ await resumeHook(token, data);
+ return new Response("ok");
+ }
```
- ## Hooks — Waiting for External Events
-
- Use `defineHook` for typed, reusable hooks. **Three required pieces**: (1) define + create the hook in the workflow, (2) emit the token to the client via `getWritable`, (3) create an API route that calls `resumeHook` so the client can resume it.
+ ## Error Handling
- ### 1. Define and create the hook (workflow file)
+ Use `FatalError` for permanent failures (no retry), `RetryableError` for transient failures:
- ```ts
- import { defineHook, getWritable, sleep } from "workflow";
+ ```typescript
+ import { FatalError, RetryableError } from "workflow";
- export interface ApprovalPayload {
- approved: boolean;
- comment?: string;
+ if (res.status >= 400 && res.status < 500) {
+ throw new FatalError(`Client error: ${res.status}`);
}
+ if (res.status === 429) {
+ throw new RetryableError("Rate limited", { retryAfter: "5m" });
+ }
+ ```
- // Define at module scope — reusable across workflows
- export const approvalHook = defineHook<ApprovalPayload>();
+ ## Serialization
- export async function approvalGate(orderId: string): Promise<{ status: string }> {
- "use workflow";
+ All data passed to/from workflows and steps must be serializable.
- // .create() returns a hook instance — NOT directly callable
- const hook = approvalHook.create({ token: `approval:${orderId}` });
+ **Supported types:** string, number, boolean, null, undefined, bigint, plain objects, arrays, Date, RegExp, URL, URLSearchParams, Map, Set, Headers, ArrayBuffer, typed arrays, Request, Response, ReadableStream, WritableStream.
- // CRITICAL: Emit the token to the client so it knows what to resume
- await emitToken(hook.token, orderId);
+ **Not supported:** Functions, class instances, Symbols, WeakMap/WeakSet. Pass data, not callbacks.
- // Race between approval and timeout
- const result = await Promise.race([
- hook.then((payload) => ({ type: "approval" as const, payload })),
- sleep("24h").then(() => ({ type: "timeout" as const, payload: null })),
- ]);
+ ## Streaming
- if (result.type === "timeout") {
- return { status: "timeout" };
- }
- return { status: result.payload!.approved ? "approved" : "rejected" };
+ Use `getWritable()` to stream data from workflows. `getWritable()` can be called in **both** workflow and step contexts, but you **cannot interact with the stream** (call `getWriter()`, `write()`, `close()`) directly in a workflow function. The stream must be passed to step functions for actual I/O, or steps can call `getWritable()` themselves.
+
+ **Get the stream in a workflow, pass it to a step:**
+ ```typescript
+ import { getWritable } from "workflow";
+
+ export async function myWorkflow() {
+ "use workflow";
+ const writable = getWritable();
+ await writeData(writable, "hello world");
}
- async function emitToken(token: string, orderId: string): Promise<void> {
+ async function writeData(writable: WritableStream, chunk: string) {
"use step";
- const writer = getWritable<{ type: string; token: string; orderId: string }>().getWriter();
+ const writer = writable.getWriter();
try {
- await writer.write({ type: "awaiting_approval", token, orderId });
+ await writer.write(chunk);
} finally {
writer.releaseLock();
}
}
```
- **Common mistake**: Calling `defineHook()` directly or forgetting `.create()`. Always: `const hook = myHook.create({ token })`.
-
- ### 2. Resume route (API file — required!)
-
- ```ts
- // app/api/approve/route.ts
- import { NextResponse } from "next/server";
- import { resumeHook } from "workflow/api";
+ **Call `getWritable()` directly inside a step (no need to pass it):**
+ ```typescript
+ import { getWritable } from "workflow";
- export async function POST(req: Request) {
- const { token, ...data } = await req.json();
- await resumeHook(token, data);
- return NextResponse.json({ ok: true });
+ async function streamData(chunk: string) {
+ "use step";
+ const writer = getWritable().getWriter();
+ try {
+ await writer.write(chunk);
+ } finally {
+ writer.releaseLock();
+ }
}
```
- **You MUST create this route.** Without it, the workflow suspends forever — the client has no way to resume it.
-
- ### 3. Client-side resume (React component)
+ ### Namespaced Streams
- ```tsx
- // When the SSE stream emits { type: "awaiting_approval", token }, show UI and POST back:
- async function handleApprove(token: string) {
- await fetch("/api/approve", {
- method: "POST",
- headers: { "Content-Type": "application/json" },
- body: JSON.stringify({ token, approved: true, comment: "Looks good" }),
- });
- }
- ```
+ Use `getWritable({ namespace: 'name' })` to create multiple independent streams for different types of data. This is useful for separating logs from primary output, different log levels, agent outputs, metrics, or any distinct data channels. Long-running workflows benefit from namespaced streams because you can replay only the important events (e.g., final results) while keeping verbose logs in a separate stream.
- ## Error Handling
+ **Example: Log levels and agent output separation:**
+ ```typescript
+ import { getWritable } from "workflow";
- ```ts
- import { FatalError, RetryableError } from "workflow";
+ type LogEntry = { level: "debug" | "info" | "warn" | "error"; message: string; timestamp: number };
+ type AgentOutput = { type: "thought" | "action" | "result"; content: string };
- async function callExternalAPI(url: string) {
+ async function logDebug(message: string) {
"use step";
- const res = await fetch(url);
-
- if (res.status >= 400 && res.status < 500) {
- throw new FatalError(`Client error: ${res.status}`); // No retry
- }
- if (res.status === 429) {
- throw new RetryableError("Rate limited", { retryAfter: "5m" }); // Retry after 5 min
+ const writer = getWritable<LogEntry>({ namespace: "logs:debug" }).getWriter();
+ try {
+ await writer.write({ level: "debug", message, timestamp: Date.now() });
+ } finally {
+ writer.releaseLock();
}
- return res.json();
}
- ```
- Step retry metadata:
-
- ```ts
- import { getStepMetadata } from "workflow";
-
- async function processWithRetry(id: string) {
+ async function logInfo(message: string) {
"use step";
- const { attempt } = getStepMetadata();
- console.log(`Attempt ${attempt} for ${id}`);
- // ...
+ const writer = getWritable<LogEntry>({ namespace: "logs:info" }).getWriter();
+ try {
+ await writer.write({ level: "info", message, timestamp: Date.now() });
+ } finally {
+ writer.releaseLock();
+ }
}
- ```
- ## Sandbox Limitations & Workarounds
-
- | Limitation | Solution |
- |-----------|----------|
- | No native `fetch()` in workflow scope | Import `fetch` from `"workflow"` or move to a step |
- | No `setTimeout`/`setInterval` | Use `sleep()` from `"workflow"` |
- | No Node.js modules in workflow scope | Move all Node.js logic to step functions |
-
- ## DurableAgent (AI SDK Integration)
-
- ```ts
- import { DurableAgent } from "@workflow/ai/agent";
- import { getWritable } from "workflow";
- import { z } from "zod";
+ async function emitAgentThought(thought: string) {
+ "use step";
+ const writer = getWritable<AgentOutput>({ namespace: "agent:thoughts" }).getWriter();
+ try {
+ await writer.write({ type: "thought", content: thought });
+ } finally {
+ writer.releaseLock();
+ }
+ }
- async function searchDatabase(query: string) {
+ async function emitAgentResult(result: string) {
"use step";
- // Full Node.js access — real DB calls here
- return `Results for "${query}"`;
+ // Important results go to the default stream for easy replay
+ const writer = getWritable<AgentOutput>().getWriter();
+ try {
+ await writer.write({ type: "result", content: result });
+ } finally {
+ writer.releaseLock();
+ }
}
- export async function researchAgent(topic: string) {
+ export async function agentWorkflow(task: string) {
"use workflow";
- const agent = new DurableAgent({
- model: "anthropic/claude-sonnet-4-5",
- system: "You are a research assistant.",
- tools: {
- search: {
- description: "Search the database",
- inputSchema: z.object({ query: z.string() }),
- execute: searchDatabase, // Tool execute uses "use step"
- },
- },
- });
+ await logInfo(`Starting task: ${task}`);
+ await logDebug("Initializing agent context");
+ await emitAgentThought("Analyzing the task requirements...");
- const result = await agent.stream({
- messages: [{ role: "user", content: `Research ${topic}` }],
- writable: getWritable(),
- maxSteps: 10,
- });
+ // ... agent processing ...
- return result.messages;
+ await emitAgentResult("Task completed successfully");
+ await logInfo("Workflow finished");
}
```
- Every LLM call and tool execution becomes a retryable step. The entire agent loop survives crashes and deployments.
-
- ## Common Patterns
-
- ### Fan-Out / Parallel Steps
-
- ```ts
- export async function processImages(imageIds: string[]) {
- "use workflow";
+ **Consuming namespaced streams:**
+ ```typescript
+ import { start, getRun } from "workflow/api";
+ import { agentWorkflow } from "./workflows/agent";
- const results = await Promise.all(
- imageIds.map(async (id) => {
- return await resizeImage(id); // Each is its own step
- })
- );
+ export async function POST(request: Request) {
+ const run = await start(agentWorkflow, ["process data"]);
- await saveResults(results);
- }
+ // Access specific streams by namespace
+ const results = run.getReadable({ namespace: undefined }); // Default stream (important results)
+ const infoLogs = run.getReadable({ namespace: "logs:info" });
+ const debugLogs = run.getReadable({ namespace: "logs:debug" });
+ const thoughts = run.getReadable({ namespace: "agent:thoughts" });
- async function resizeImage(id: string) {
- "use step";
- // ...
+ // Return only important results for most clients
+ return new Response(results, { headers: { "Content-Type": "application/json" } });
}
- ```
- ### Saga with Compensation
-
- ```ts
- import { FatalError, getWritable } from "workflow";
-
- export async function upgradeSaga(userId: string) {
- "use workflow";
-
- await reserveSeats(userId);
+ // Resume from a specific point (useful for long sessions)
+ export async function GET(request: Request) {
+ const { searchParams } = new URL(request.url);
+ const runId = searchParams.get("runId")!;
+ const startIndex = parseInt(searchParams.get("startIndex") || "0", 10);
- try {
- await chargePayment(userId);
- } catch {
- await releaseSeats(userId); // Compensate
- throw new FatalError("Payment failed");
- }
+ const run = getRun(runId);
+ // Resume only the important stream, skip verbose debug logs
+ const stream = run.getReadable({ startIndex });
- await activatePlan(userId);
+ return new Response(stream);
}
```
+ **Pro tip:** For very long-running sessions (50+ minutes), namespaced streams help manage replay performance. Put verbose/debug output in separate namespaces so you can replay just the important events quickly.
+
## Debugging
```bash
- npx workflow health # Check endpoints
- npx workflow web # Visual dashboard
- npx workflow inspect runs # List all runs
- npx workflow inspect run <run_id> # Inspect specific run
- npx workflow cancel <run_id> # Cancel execution
- ```
+ # Check workflow endpoints are reachable
+ npx workflow health
+ npx workflow health --port 3001 # Non-default port
- ## Debug Stuck Workflow
+ # Visual dashboard for runs
+ npx workflow web
+ npx workflow web <run_id>
- When a workflow appears stuck, hanging, or not progressing, follow this escalation ladder:
+ # CLI inspection (use --json for machine-readable output, --help for full usage)
+ npx workflow inspect runs
+ npx workflow inspect run <run_id>
- ### 1. Add Step-Level Logging (Required)
+ # For Vercel-deployed projects, specify backend and project
+ npx workflow inspect runs --backend vercel --project <project-name> --team <team-slug>
+ npx workflow inspect run <run_id> --backend vercel --project <project-name> --team <team-slug>
- **Every step function MUST have `console.log` at entry and exit.** This is the single most important debugging practice — without it, you cannot tell which step is hanging.
+ # Open Vercel dashboard in browser for a specific run
+ npx workflow inspect run <run_id> --web
+ npx workflow web <run_id> --backend vercel --project <project-name> --team <team-slug>
- ```ts
- async function processOrder(orderId: string): Promise<OrderResult> {
- "use step";
- console.log(`[processOrder] START orderId=${orderId} at=${new Date().toISOString()}`);
- try {
- const result = await doWork(orderId);
- console.log(`[processOrder] DONE orderId=${orderId} result=${JSON.stringify(result)}`);
- return result;
- } catch (err) {
- console.error(`[processOrder] FAIL orderId=${orderId} error=${err}`);
- throw err;
- }
- }
+ # Cancel a running workflow
+ npx workflow cancel <run_id>
+ npx workflow cancel <run_id> --backend vercel --project <project-name> --team <team-slug>
+ # --env defaults to "production"; use --env preview for preview deployments
```
- **Workflow-level logging** — log at every orchestration point:
-
- ```ts
- export async function myWorkflow(input: string) {
- "use workflow";
- console.log(`[myWorkflow] START input=${input}`);
-
- const data = await stepOne(input);
- console.log(`[myWorkflow] stepOne complete, starting stepTwo`);
-
- const result = await stepTwo(data);
- console.log(`[myWorkflow] stepTwo complete, returning`);
+ **Debugging tips:**
+ - Use `--json` (`-j`) on any command for machine-readable output
+ - Use `--web` to open the Vercel Observability dashboard in your browser
+ - Use `--help` on any command for full usage details
+ - Only import workflow APIs you actually use. Unused imports can cause 500 errors.
- return { result };
- }
- ```
+ ## Testing Workflows
- ### 2. Check Run Status
+ Workflow DevKit provides a Vitest plugin for testing workflows in-process — no running server required.
- ```bash
- # List recent runs — look for "running" status that's been active too long
- npx workflow inspect runs
+ **Unit testing steps:** Steps are just functions; without the compiler, `"use step"` is a no-op. Test them directly:
- # Get detailed status for a specific run
- npx workflow inspect run <run_id>
+ ```typescript
+ import { describe, it, expect } from "vitest";
+ import { createUser } from "./user-signup";
- # Check if the workflow endpoints are healthy
- npx workflow health
+ describe("createUser step", () => {
+ it("should create a user", async () => {
+ const user = await createUser("test@example.com");
+ expect(user.email).toBe("test@example.com");
+ });
+ });
```
- ### 3. Check Vercel Runtime Logs
+ **Integration testing:** Use `@workflow/vitest` for workflows using `sleep()`, hooks, webhooks, or retries:
- ```bash
- # Stream live logs from your deployment
- vercel logs --follow
+ ```typescript
+ // vitest.integration.config.ts
+ import { defineConfig } from "vitest/config";
+ import { workflow } from "@workflow/vitest";
- # Or check the Vercel dashboard: Project → Deployments → Functions tab
+ export default defineConfig({
+ plugins: [workflow()],
+ test: {
+ include: ["**/*.integration.test.ts"],
+ testTimeout: 60_000,
+ },
+ });
```
- Look for:
- - **Missing step entry logs** — the step before the missing one is where execution stopped
- - **Timeout errors** — Vercel function timeout (default 60s hobby, 300s pro)
- - **OIDC/credential errors** — `gateway()` calls fail silently without `vercel env pull`
- - **Memory errors** — large payloads in steps can OOM the function
-
- ### 4. Common Stuck Scenarios
+ ```typescript
+ // approval.integration.test.ts
+ import { describe, it, expect } from "vitest";
+ import { start, getRun, resumeHook } from "workflow/api";
+ import { waitForHook, waitForSleep } from "@workflow/vitest";
+ import { approvalWorkflow } from "./approval";
- | Symptom | Likely Cause | Fix |
- |---------|-------------|-----|
- | Run stays "running" forever | Step is awaiting an external call that never resolves | Add timeout with `Promise.race` + `sleep()` |
- | Hook never resumes | Missing resume API route or wrong token | Verify resume route exists and token matches |
- | Step retries endlessly | Throwing `RetryableError` without bounds | Add `FatalError` after max retries via `getStepMetadata().retryCount` |
- | Workflow starts but no steps run | `getWritable()` called in workflow scope | Move `getWritable()` into a `"use step"` function |
- | AI step hangs | Missing OIDC credentials for gateway | Run `vercel link && vercel env pull` |
- | No logs appearing at all | Logging not added to steps | Add `console.log` at entry/exit of every step |
+ describe("approvalWorkflow", () => {
+ it("should publish when approved", async () => {
+ const run = await start(approvalWorkflow, ["doc-123"]);
- ### 5. Use Browser Verification
+ // Wait for the hook, then resume it
+ await waitForHook(run, { token: "approval:doc-123" });
+ await resumeHook("approval:doc-123", { approved: true, reviewer: "alice" });
- If the workflow powers a UI, use `agent-browser` to check the frontend while inspecting backend logs — a hanging page often means a stuck workflow step. Check the browser console for failed fetch calls to your workflow API routes.
+ // Wait for sleep, then wake it up
+ const sleepId = await waitForSleep(run);
+ await getRun(run.runId).wakeUp({ correlationIds: [sleepId] });
- ## When to Use WDK vs Regular Functions
+ const result = await run.returnValue;
+ expect(result).toEqual({ status: "published", reviewer: "alice" });
+ });
+ });
+ ```
- | Scenario | Use |
- |----------|-----|
- | Simple API endpoint, fast response | Regular Route Handler |
- | Multi-step process, must complete all steps | WDK Workflow |
- | AI agent in production, must not lose state | WDK DurableAgent |
- | Background job that can take minutes/hours | WDK Workflow |
- | Process spanning multiple services | WDK Workflow |
- | Quick one-shot LLM call | AI SDK directly |
+ **Testing webhooks:** Use `resumeWebhook()` with a `Request` object — no HTTP server needed:
- ## Framework Support
+ ```typescript
+ import { start, resumeWebhook } from "workflow/api";
+ import { waitForHook } from "@workflow/vitest";
- Next.js, Nitro, SvelteKit, Astro, Express, Hono (supported). TanStack Start, React Router (in development).
+ const run = await start(ingestWorkflow, ["ep-1"]);
+ const hook = await waitForHook(run); // Discovers the random webhook token
+ await resumeWebhook(hook.token, new Request("https://example.com/webhook", {
+ method: "POST",
+ body: JSON.stringify({ event: "order.created" }),
+ }));
+ ```
- ## Official Documentation
+ **Key APIs:**
+ - `start()` — trigger a workflow
+ - `run.returnValue` — await workflow completion
+ - `waitForHook(run, { token? })` / `waitForSleep(run)` — wait for workflow to reach a pause point
+ - `resumeHook(token, data)` / `resumeWebhook(token, request)` — resume paused workflows
+ - `getRun(runId).wakeUp({ correlationIds })` — skip `sleep()` calls
- - [Workflow DevKit](https://vercel.com/docs/workflow)
- - [Website](https://useworkflow.dev)
- - [GitHub](https://github.com/vercel/workflow)
- - [Workflow Builder Template](https://vercel.com/templates/next.js/workflow-builder)
+ **Best practices:**
+ - Keep unit tests (no plugin) and integration tests (`workflow()` plugin) in separate configs
+ - Use deterministic hook tokens based on test data for easier resumption
+ - Set generous `testTimeout` — workflows may run longer than typical unit tests
+ - `vi.mock()` does **not** work in integration tests — step dependencies are bundled by esbuild