Flushing the queue

The SDK batches events and sends them asynchronously after track() (or wrapper calls). In long-running servers this is usually fine. In serverless functions, edge workers, tests, or scripts the runtime may freeze or exit before the queue drains—those events can be lost.

When to call flush()

  • At the end of an HTTP handler or background job, after all model calls for that invocation are done.
  • Before process exit in CLI scripts and integration tests that assert on ingestion.
  • When you need a best-effort guarantee that queued events have been submitted before returning a response to the client.

How to use it

await noryen.flush() waits for the current batch pipeline to finish. If init() was never called, it is effectively a no-op.

route.ts
import { noryen } from "@noryen/sdk";
export async function POST(req: Request) {
noryen.init({ apiKey: process.env.NORYEN_API_KEY });
// … run your LLM logic …
await noryen.flush();
return Response.json({ ok: true });
}
Pair this with the patterns in Serverless & edge and Next.js for route handlers and server actions.

You do not need to flush on every single track() in a hot loop inside one long-lived worker; batching is intentional. Use flush() at natural boundaries (request end, job end, shutdown).