Skip to Content
Runlane
Recipes

Human-in-the-loop

Generate content, pause for approval, then publish or reject it.

Use a wait token when generated content needs an external decision. Runlane stores the token and resumes linked runs. Your application owns the review inbox, notification channel, authentication, authorization, and business audit.

Request content approval

The Runlane token and step calls below are real. The marked generation, notification, and publishing calls are pseudocode for your application and providers:

src/tasks/approve-content.ts
import { ErrorCode, RunlaneError, task, waitToken } from '@runlane/core'
import * as z from 'zod'

const approvalSchema = z.object({ approved: z.boolean() })
const contentSchema = z.object({ body: z.string(), title: z.string() })
const approvalResultSchema = z.object({ status: z.enum(['published', 'rejected', 'timed_out']) })

export const contentApproval = waitToken({ id: 'content-approval', output: approvalSchema })

export const approveContent = task({
  id: 'content.approve',
  schema: z.object({ draftId: z.string() }),
  output: approvalResultSchema,
  async run({ draftId }, context) {
    const content = await context.step.run('generate-content', { output: contentSchema }, async ({ token }) => {
      //////////////////////////////////
      // MAKE YOUR API CALLS HERE
      //////////////////////////////////
      const result = await generateContentDraft({ draftId, idempotencyKey: token, signal: context.signal })

      return contentSchema.parse(result)
    })

    const created = await context.wait.createToken({ meta: { draftId }, timeout: '24h' })
    const tokenId = created.token.id

    await context.step.run('request-approval', { output: z.null() }, async ({ token }) => {
      //////////////////////////////////
      // MAKE YOUR API CALLS HERE
      //////////////////////////////////
      await notifyContentReviewers({ content, idempotencyKey: token, tokenId })

      return null
    })

    const approval = await context.wait.forToken(tokenId, {
      ...contentApproval,
      reason: 'content_approval_pending',
    })
    if (!approval.ok) {
      if (approval.error.code !== ErrorCode.WaitTokenExpired) throw new RunlaneError(approval.error)
      return { status: 'timed_out' }
    }
    if (!approval.output.approved) return { status: 'rejected' }

    await context.step.run(
      'publish-content',
      { output: z.object({ published: z.literal(true) }) },
      async ({ token }) => {
        //////////////////////////////////
        // MAKE YOUR API CALLS HERE
        //////////////////////////////////
        await publishContent({ content, idempotencyKey: token, signal: context.signal })

        return { published: true }
      },
    )

    return { status: 'published' }
  },
})

Keep generation, notification, and publishing in saved steps. Make notification and publishing idempotent: a remote action can succeed before its step is saved. Use the step's token as the provider key where supported. See make side effects safe to repeat.

The approval token stays the same across attempts. After the wait resolves, saved generation and notification results replay before the handler checks the reviewer's choice. See the full human-in-the-loop guide for token expiry, reminder windows, authorization, and reconciliation.

Complete it from your application

Use an authenticated state-changing route to record the reviewer’s choice:

src/approvals/complete-content-approval.ts
import { ActorType, type RunlaneRuntime } from '@runlane/core'
import { contentApproval } from '../tasks/approve-content.js'

interface PendingContentApproval {
  readonly tokenId: string
}

interface Reviewer {
  readonly id: string
}

export async function completeContentApproval(
  runlane: RunlaneRuntime,
  approval: PendingContentApproval,
  reviewer: Reviewer,
  approved: boolean,
) {
  return runlane.wait.completeToken(
    contentApproval,
    approval.tokenId,
    { approved },
    {
      actor: { id: reviewer.id, type: ActorType.Operator },
    },
  )
}

The first terminal write wins. A repeated or competing completion returns the stored winner. Reconcile application state from that returned token, not from the submitted body.

For production controls and daily reminders, follow build a human-in-the-loop flow. Keep maintenance running for timeouts and resume recovery.

On this page