camelAI Documentation

Guides

Deterministic replay workflows in camelRun v2

Run replay-cached JavaScript workflows on a camelRun v2 agent via POST /v2/agents/:idOrKey/workflows or the workflows built-in.

View as Markdown

camelRun v2 workflows execute sandboxed JavaScript inside QuickJS with step-level replay caching persisted in the agent's Durable Object SQLite storage. If a workflow fails halfway through and you re-run it with the same idempotencyKey, already completed steps return their cached results immediately instead of repeating model calls, shell commands, or tool invocations.

Run a workflow from the SDK or API

Call POST /v2/agents/:idOrKey/workflows (or agent.runWorkflow(code, { idempotencyKey })):

ts
const workflow = await agent.runWorkflow(
  `
  const plan = await wf.agent("List the 3 riskiest files in src/ as JSON.", {
    outputSchema: {
      type: "object",
      properties: { files: { type: "array", items: { type: "string" } } },
      required: ["files"],
    },
  });

  const audits = await wf.parallel(
    plan.files.map((file) => () => wf.agent("Audit " + file + " for security issues.")),
  );

  const testResult = await wf.bash("npm test");
  const checkpoint = await wf.commit("post-audit-checkpoint");

  return { files: plan.files, audits, testExitCode: testResult.exitCode, checkpoint };
  `,
  { idempotencyKey: "security-audit-2026-11-21" },
);

console.log(workflow.status, workflow.result);
console.log(`Executed: ${workflow.stepsExecuted}, Replayed from cache: ${workflow.cachedSteps}`);

Over REST

bash
curl -s -X POST "https://run.camelai.com/v2/agents/eng-agent/workflows" \
  -H "Authorization: Bearer $CAMELRUN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "idempotencyKey": "release-check-42",
    "code": "const tests = await wf.bash(\"npm test\"); const summary = await wf.agent(\"Summarize: \" + JSON.stringify(tests)); return summary;"
  }'

Completed workflows return HTTP 200; failed workflows return HTTP 422 with status: "failed" and error: { code: "workflow_failed", message }.

Letting an agent run workflows itself

Enable "workflows" in builtins so the model gets the workflow_run({ code, idempotencyKey? }) tool and can write and execute multi-step replayable scripts on its own:

ts
const agent = await client.agents.upsert("release-manager", {
  instructions: "Use workflow_run when you need to fan out parallel analyses and run tests.",
  builtins: ["files", "bash", "workflows"],
});

The wf runtime API

Inside workflow code, the global wf object provides seven replay-cached primitives:

MethodDescription
await wf.agent(prompt, opts?)Runs a prompt on the agent (supports opts.outputSchema) and returns run.output ?? run.text. Cached by step index and arguments.
await wf.bash(command, opts?)Runs the bash tool inside the agent's session VM and returns its result.
await wf.commit(message?)Calls repo_checkpoint to create a named checkpoint ref on the agent's repository.
await wf.tool(name, args?)Invokes any tool configured on the agent (built-in, local WebSocket, webhook, or MCP) by name.
await wf.step(name, async () => ...)Memoizes any custom async JavaScript block under name so replays skip recomputation.
await wf.parallel(tasks)Runs an array of tasks concurrently via Promise.all. Each element can be a function (idx) => Promise, a prompt string (runs wf.agent(str)), or { prompt, ...opts }.
await wf.sleep(ms)Sleeps inside the workflow (clamped to at most 10,000 ms, capped at 2,000 ms per invocation) and caches completion.

Response fields

json
{
  "workflowId": "wf_...",
  "scope": "wf:release-check-42",
  "status": "completed",
  "result": { "...": "..." },
  "output": { "...": "..." },
  "logs": [],
  "stepsExecuted": 4,
  "cachedSteps": 0,
  "error": null
}
  • timeoutMs for a workflow execution is 120 seconds inside QuickJS.
  • Reuse the same idempotencyKey on retry to replay cached steps instead of re-executing already completed work.