Tulip Logo IconTulip
Recipes

Scheduled Agents

Run an agent on a schedule, such as a weekly digest, with a cron route.

Scheduled work, such as a weekly digest of the active initiatives, runs from a cron route. The agent runs as itself with read-only grants, and the route sends its result.

The agent

export const digestAgent = defineAgent({
  name: "initiative_digest",
  title: "Weekoverzicht",
  model,
  instructions: `Write the weekly overview of the initiatives in the prompt. Get each one's health
with get_initiative_health, name the risky ones first with the reason in numbers, and invent
nothing. Name initiatives by title, never by id.`,
  tools: { get_initiative_health: getInitiativeHealth },
  // One health check per initiative, up to 20, and the digest itself.
  maxSteps: 25,
  identity: { grants: { initiative: ["view"], task: ["view"], time: ["view"] } },
});

Its tools need no approval, so it needs no approvers.

Run it and send the result

export async function sendInitiativeDigest() {
  const active = await db
    .select({ id: initiatives.id, title: initiatives.title })
    .from(initiatives)
    .where(eq(initiatives.status, "active"))
    .limit(20);
  if (active.length === 0) return null;

  const result = await agents.run("initiative_digest", {
    as: "agent",
    trigger: { type: "schedule", schedule: "weekly" },
    prompt: active.map((initiative) => `- ${initiative.title} (id ${initiative.id})`).join("\n"),
  });
  if (!result?.text) return null;

  await notifications.send(initiativeDigestNotification, {
    to: await allUserIds(),
    data: { digest: result.text },
    dedupeKey: `initiative-digest:${result.runId}`,
  });

  return result.runId;
}

agents.run waits for the text, which is fine in a scheduled route. Start runs with agents.start instead when the result needs no follow-up in the same request.

The cron route

app/api/cron/[schedule]/route.ts
export const maxDuration = 300;

export async function GET(request: Request, { params }: { params: Promise<{ schedule: string }> }) {
  if (!env.CRON_SECRET || request.headers.get("authorization") !== `Bearer ${env.CRON_SECRET}`) {
    return new Response("Unauthorized", { status: 401 });
  }

  const { schedule } = await params;

  if (schedule === "daily") return Response.json({ expired: await agents.approvals.expire() });
  if (schedule === "weekly") return Response.json({ runId: await sendInitiativeDigest() });

  return new Response("Not found", { status: 404 });
}
vercel.json
{
  "crons": [
    { "path": "/api/cron/daily", "schedule": "0 5 * * *" },
    { "path": "/api/cron/weekly", "schedule": "0 6 * * 1" }
  ]
}

Vercel sends CRON_SECRET as a bearer token with every cron request. Set it in the project, and refuse every call without it. The daily job also expires approvals nobody decided in time.

Guidance

  • Give scheduled agents read-only grants unless they really must change data.
  • Bound what one run covers, and raise maxSteps to match.
  • Key the notification on the run id, so a retried cron request does not send twice.

On this page