Tulip Logo IconTulip
Concepts

Runs

Start agents from a button, an event or a schedule, and see what every run did.

A run is one time an agent did a job. Agents records every run: the chat turn of the assistant, a button in a form, an event listener, a scheduled job. Each run has a principal (whose permissions it used), a trigger (what started it), a status, its output and, if it failed, its error.

Start a run

Three ways, for three needs. Each takes the agent's name and the same input:

const input = {
  as: context.user, // a user, or "agent" to run as the agent itself
  trigger: { type: "action", name: "suggest_tasks" },
  prompt: `Suggest tasks for initiative ${initiative.title}.`,
};
CallUse it forReturns
agents.run(name, input)A result you wait for: text or typed JSON.{ runId, status, text, output }
agents.stream(name, input)Text the user sees appear, such as in a form.{ runId, stream }, a UI message stream
agents.start(name, input)Work in the background, on events and schedules.{ runId } right away

All three return null when the run is skipped: the agent is not active, or the loop guard stops it.

Typed output

Give the agent an output and agents.run returns it typed:

export const plannerAgent = defineAgent({
  name: "task_planner",
  // ...
  output: Output.object({
    schema: z.object({ tasks: z.array(z.object({ title: z.string(), storyPoints: z.number() })) }),
  }),
});

const result = await agents.run("task_planner", input);
result?.output?.tasks; // { title: string; storyPoints: number }[]

Streaming

agents.stream returns an AI SDK UI message stream. Return it from an oRPC procedure with streamToEventIterator, and read the text deltas on the client. A streamed run cannot wait for approvals: agents.stream throws when the agent has a tool whose policy is "ask". Start such agents with run or start.

Triggers

The trigger records what started a run, for the runs list and for history:

TriggerFieldsStarted by
actionnameA user action, such as a button.
eventevent, eventIdA domain event, in an event listener.
schedulescheduleA scheduled job, such as a cron route.
chatchatId, chatTitleA chat message (chat.send).
mcpclientId, clientNameAn MCP app's tool call (no run recorded).

agents.run, agents.stream and agents.start take action, event and schedule.

Background runs

agents.start saves the run as queued and calls enqueueRun(runId). The app hands it to its queue, and the job calls agents.runs.execute(runId):

// server/ai/tasks.ts
export const agentRunTask = defineTask("agents.run", { schema: z.object({ runId: z.string() }) });

// server/ai/handlers.ts
export const agentRunTaskHandler = agentRunTask.handle("execute-agent-run", async ({ payload }) => {
  await agents.runs.execute(payload.runId);
});

execute picks a run up once: it moves it from queued to running in one update, so a job that is delivered twice does nothing the second time. A run is also handed over again when it continues after its approvals, so do not deduplicate queue jobs by run id.

A failed run is recorded as failed and not retried: its tools may already have changed data.

The loop guard

Agents react to events, and their changes cause new events. Pass the run behind the change as causedBy, and Agents stops loops:

  • a run never starts its own agent again;
  • chains of runs stop after three levels.
taskCreatedEvent.listen(async ({ payload, event }) => {
  const via = payload.actor.via;

  await agents.start("task_triage", {
    as: "agent",
    trigger: { type: "event", event: event.name, eventId: event.id },
    prompt: `Estimate task ${payload.code} ${payload.title}.`,
    causedBy: via?.type === "agent" ? via.id : undefined,
  });
});

Statuses

StatusMeaning
queuedStarted in the background, not picked up yet.
runningThe agent is working.
waitingPaused until a tool call it asked for is approved.
completedDone.
failedSomething went wrong; error says what.
canceledStopped, for example when the user stopped the chat.

List runs

const [runs, total] = await Promise.all([
  agents.runs.list({ agent: "task_triage", limit: 20, offset: 0 }),
  agents.runs.count({ agent: "task_triage" }),
]);

Filter by agent, userId (runs for that user), threadId (the chat) and status. Runs are the only place you see what agents did while nobody was watching, such as a background run that failed: show them on the agent's page (see Manage agents).

On this page