Vercel Workflows package
Run durable agent loops with host-owned Workflow steps.
Use @yolk-sdk/vercel-workflows when an agent run can outlive one HTTP request and should resume through Vercel Workflows.
Install:
pnpm add @yolk-sdk/vercel-workflows@canary effect@4.2.0 workflow@^5.0.0-beta.42Wrap your Workflow
Your Workflow function stays in your app. Pass host-owned step functions into the package loop.
import { createHook } from 'workflow'
import { runVercelAgentWorkflow } from '@yolk-sdk/vercel-workflows'
export async function runAgentWorkflow(input: { request: unknown; context: unknown }) {
'use workflow'
return await runVercelAgentWorkflow({
input,
runModelStep,
runToolBatchStep,
closeStream,
writeError,
awaitInput: async awaitingInput => {
using hook = createHook<unknown>({ token: awaitingInput.hookToken })
return await hook
}
})
}runModelStep, closeStream, and writeError are app-owned 'use step' functions.
runToolBatchStep can be a step for ordinary tools, or a Workflow-safe orchestration callback
that invokes separate steps and independent child Workflow starts. Keep provider calls, tool IO,
persistence, telemetry, and Effect runtimes inside concrete steps, not orchestration code.
See Independent child orchestration.
When a model result is done: false with no tool calls, the package continues directly to the next
model step without invoking the tool-batch callback.
Use Effect at route boundaries
Host routes can use @yolk-sdk/vercel-workflows/effect to start, replay, resume, and cancel runs with typed Effect errors while keeping Vercel's SDK underneath.
import { Effect } from 'effect'
import { VercelWorkflows } from '@yolk-sdk/vercel-workflows/effect'
const program = Effect.gen(function* () {
const workflows = yield* VercelWorkflows
const run = yield* workflows.start(runAgentWorkflow, [{ request, context }])
const readable = yield* run.getReadable<Uint8Array>()
return new Response(readable, {
headers: { 'x-workflow-run-id': run.runId }
})
}).pipe(Effect.provide(VercelWorkflows.layer))Do not use the Effect API inside a 'use workflow' function. Keep it in routes, actions, services, or tests.
Resume HITL
When runToolBatchStep returns awaitingInput, the package calls awaitInput, waits for the hook result, then reruns the same tool batch with accumulated HITL responses.
Your app owns hook tokens, route auth, response validation, and request matching. Give each pause
its own token, have each answer name its pause (the eventId of its AgentAwaitingInput; a request
id alone is ambiguous), and do not read resumeHook success as "the answer was used": record the
answer durably first (HITL resume).
Emit replay-safe stream events
Use sequenceDurableAgentEvent to add deterministic ids without writing. Use
writeDurableAgentEvent when you want the same sequencing plus an NDJSON write:
${streamId}:${turn}:${eventSequence}
${streamId}:${turn}:${eventSequence}:${executionId}Clients should de-dupe by eventId during resume/reconnect.
Every execution of a step starts from the same eventSequence, and a step can run more than once
(loop retries, platform retries, queue redelivery, Workflow 5.8 speculative execution). Pass an
executionId unique per execution of the step body, generated once at its start, such as
${stepId}:${attempt}:${crypto.randomUUID()} (getStepMetadata()'s stepId and attempt alone
repeat: speculative execution re-runs a discarded body under the same attempt, possibly beside it),
on every event the model call produces, from LLMStreamStart (required) through
AssistantMessage/UsageUpdate. The sequencer appends it to the id and sets it on the event with
the streamId and the step's turn as executionTurn, so a step's turn must be the turn its
model call streams (runModelTurn's turn; a non-negative safe integer, or sequencing throws).
Leave it off host events (a promoted user message, the terminal event written after the commit); a
receipt replay rewrites only that terminal event.
Yolk clients then remove a superseded execution's output, drop its later events and late starts,
and, at AgentEnd or AgentAwaitingInput, show the committed answer and the calls the tool batch
ran for each turn in which a newer execution superseded an older one (limits: a turn retried after a
user message shows both answers; a client that connects mid-turn starts each execution's turn from
its first event (executionTurn), so it cannot tell which of two executions streaming at once
started later and shows the one it saw first as superseded until the terminal event; the committed
answer is matched by value across the run; call ids reused across turns can mix calls in model
events without executionTurn until the client sees a turn start). Tool-batch steps are out of scope (read calls are
not ledgered and can rerun differently). Upgrade clients with the server: older clients show both
executions back to back.
Commit before protocol terminal events
“Terminal” is a logical client boundary. Final callback ownership is:
AgentEnd: durable completion commit -> AgentEnd -> closeStream
AgentError: failure commit/mapping -> AgentError -> writeError-owned close
AgentAwaitingInput: persist pending HITL -> emit pause -> release writer lock, do not close -> resumeUse commitThenWriteTerminalEvent when a step must commit host state before writing the terminal
event. It cannot infer whether an event is Workflow-final and never closes the writer.
The workflow loop calls closeStream only on successful completion. Failure paths call writeError
best-effort; that callback must write the safe run-final error and close, including after a failed
success close.
Next pages
- Step contracts
- Routes
- HITL resume
- Testing
- Cloudflare runtime: run the same Workflow code on Cloudflare Durable Objects