NylorunDocsBeta
Get startedBuildRunDeployReferenceMore

Loop

Run, verify, and retry workflow work until a decision returns output.

Loop repeats three durable stages: run, verify, and decide. The decision either returns final output or supplies the next input.

import { Loop } from "@nylorun/agents/define";

export const polish = Loop({
  id: "polish",
  run: coder,
  verify: ({ output }) => {
    const text = output.answer ?? "";
    if (text.trim().length >= 8) return { pass: true };
    return {
      pass: false,
      feedback: "Answer must be at least eight characters. Try again.",
    };
  },
  decide: ({ output, verdict, iteration }) => {
    if (verdict.pass) return { output };
    if (iteration >= 3) throw new Error("Still too short after 3 tries");
    return { input: verdict.feedback };
  },
});

run may be an agent, tool, workflow, or slot. verify may be a function, tool, or verifier agent. A verifier agent must declare an output schema that accepts both verdict shapes:

{ pass: true }
{ pass: false, feedback: "What to improve" }

decide receives the current output, verdict, iteration, original input, and prior history. Always define a terminal condition: return an output, cap attempts, or throw a useful error. Runtime checkpoints each iteration and Studio shows its node status and history.

See the shipped loop example or return to the Workflows overview to register and run the composed workflow.