Skip to main content

Overview

calado batches in memory and flushes every 5 seconds on a timer. Serverless runtimes freeze between invocations, so that timer often never fires and events get stranded in a dead queue. The fix is to call calado.flush() before your handler returns. How you do that depends on the runtime.

Next.js App Router

Use Next’s after() helper. It runs after the response is sent, so the user doesn’t wait on the flush.
import { after } from "next/server";
import { calado } from "calado";

export async function POST(req: Request) {
  // ...handle the request, make LLM calls...

  after(() => calado.flush());

  return Response.json({ ok: true });
}

Vercel Edge / Next.js Edge runtime

Use ctx.waitUntil so the platform keeps the worker alive until the flush resolves.
import { calado } from "calado";

export default async function handler(
  req: Request,
  ctx: { waitUntil: (p: Promise<unknown>) => void }
) {
  // ...handle the request...

  ctx.waitUntil(calado.flush());

  return new Response("ok");
}

AWS Lambda

Flush in a finally block so it runs on both success and error paths.
import { calado } from "calado";

export async function handler(event: unknown) {
  try {
    // ...handle the request...
    return { statusCode: 200 };
  } finally {
    await calado.flush();
  }
}

Cloudflare Workers

Same pattern as Vercel Edge. Workers provide ctx.waitUntil on the request handler’s second argument.
export default {
  async fetch(req: Request, env: Env, ctx: ExecutionContext) {
    // ...handle the request...

    ctx.waitUntil(calado.flush());

    return new Response("ok");
  },
};

Long-running Node processes

If your server stays warm between requests (Express, Fastify, a worker process), the default 5-second flush timer handles everything. You don’t need to call flush() manually. On graceful shutdown, call await calado.shutdown() to flush and clear timers.
process.on("SIGTERM", async () => {
  await calado.shutdown();
  process.exit(0);
});

Tuning batch size and flush interval

If you’re sending many events and want lower latency, lower the batch size or flush interval.
calado.init(process.env.CALADO_API_KEY!, {
  batchSize: 5,
  flushInterval: 1_000,
});
Set flushInterval: 0 to disable the timer entirely. Use this in serverless setups where you always flush manually. See Node.js SDK → Init options for the full list.