lemma.flush() to the runtime’s waitUntil(), call it inside after(), or await it last, so delivery finishes first. On Cloudflare Workers, the SDK also needs Node.js compatibility and credentials from the Worker’s bindings. When a Durable Object run outlives its request, build its trace when the run ends and send it with ingest().
Finish delivery with lemma.flush()
lemma.flush() waits until every trace the client has started sending is delivered or dropped, and it never rejects. It also waits for a callback trace held open by its returned stream, a plain ReadableStream or a native async generator, so it covers a streamed response. It doesn’t wait for:
- A trace handle you haven’t ended
- A callback trace whose callback is still running
ctx.waitUntil(lemma.flush()) before trace.end() waits for nothing. Call it after the trace ends, or inside after(), which runs once the response is sent.
The flush also covers the whole client. On a client shared across requests, it also waits for other requests’ sends and streams, and a returned stream that nobody reads or cancels holds it until the runtime’s own limit. A client built per request, as in the Workers example below, scopes the flush to that request.
Pass it to the runtime where the response is sent:
On Workers, a callback that returns the agent’s stream as a plain
ReadableStream keeps its trace open until the response body is sent, and ctx.waitUntil(lemma.flush()) keeps the Worker running until that trace is delivered:
lemmaFor() builds the client from the Worker’s bindings, as Pass credentials from bindings shows. Return the stream lemma.trace() gives back, not the one your callback built: the SDK ends the trace when that one is read to the end, cancelled, or fails. Other stream types, such as a LangChain or OpenAI SDK stream, end the trace when the callback returns; Serverless or streaming handlers lists them.
In a Next.js route, end the trace, also when the agent throws, then schedule the flush with after(), which runs once the response is sent:
lemma.flush() last. With response streaming, await the pipe into responseStream first, then await lemma.flush(). Awaiting the flush before the stream is read would wait for it until the function times out.
Flush a framework integration
ThevercelAI(), openAIAgents(), and langChain() integrations track their own runs, so each has its own flush: lemmaTelemetry.flush(), processor.forceFlush(), and lemmaHandler.flush(). Call an integration’s flush once the response is complete: each waits only for runs that have already finished, so a flush called earlier finds nothing to wait for. shutdown() also sends the runs still in progress, for a process that is about to exit.
Where to call the integration’s flush, shown here for lemmaTelemetry:
- Next.js:
after(() => lemmaTelemetry.flush()) - AWS Lambda:
await lemmaTelemetry.flush()after the run, before the handler returns - Cloudflare Workers or Vercel Functions, response not streamed: pass the integration’s flush to
ctx.waitUntil()orwaitUntil()after the run - Cloudflare Workers or Vercel Functions, streamed response: flush once the body is sent, as below
TransformStream and flush when the pipe settles, also when the client disconnects:
Set up Cloudflare Workers
The SDK runs on Cloudflare Workers and in Durable Objects once the Worker has Node.js compatibility and builds its client from its bindings.Turn on Node.js compatibility
The SDK’s entry point importsnode:crypto, so the Worker needs Node.js compatibility, or it fails to start. Use a compatibility date of 2024-09-23 or later. With a compatibility date before 2026-08-04, also add the nodejs_compat flag to your Wrangler config; from that compatibility date on, it’s on by default:
wrangler.jsonc, the same settings are "compatibility_date" and "compatibility_flags".
Pass credentials from bindings
Store the ingest key and the project ID as Wrangler secrets, with the commands the setup command prints:npx wrangler versions secret put, then deploy the version with npx wrangler versions deploy. For a named Wrangler environment, add --env <name> to each.
Don’t also list them under vars: wrangler refuses a secret whose name a deployed var uses, and deploying a var deletes the secret of the same name. LEMMA_RELEASE isn’t a secret, so it can go under vars.
Wrangler reads the .env files only while there’s no .dev.vars. For wrangler dev, put LEMMA_API_KEY and LEMMA_PROJECT_ID in .dev.vars when it exists or the app has no .env file, and in .env otherwise. For a Worker, the setup command writes the same file. When a .dev.vars.<name> file exists, wrangler dev --env <name> reads it in place of either file, so add the names there too.
When the Wrangler config declares secrets, wrangler loads LEMMA_API_KEY and LEMMA_PROJECT_ID from that file only when secrets.required lists them. Add both to it.
Build the client from the handler’s env, so it doesn’t depend on whether the runtime fills process.env:
fetch, so you don’t pass one. Up to @uselemma/tracing 7.14.0, pass fetch: (input, init) => fetch(input, init) as well: those versions call the Worker’s fetch in a way the runtime rejects, and the SDK drops every trace.
Send the first trace from the Worker
Bindings,ctx.waitUntil(), and Node.js compatibility behave as they do in production only inside the Workers runtime, so a Node test or script can pass while the deployed Worker sends nothing. Send the first trace from the Worker itself:
1
Run the Worker with debug mode on
Append For a Pages project, append
--var LEMMA_DEBUG:1 to the command that starts your Worker, keeping its other flags such as --env <name>, and send it a request that runs the agent:--binding LEMMA_DEBUG=1 to its wrangler pages dev command. Either flag sets LEMMA_DEBUG for this run only. It overrides any value in .dev.vars or .env, even when secrets.required leaves it out, and no deploy includes it. lemmaFor() above turns on debug mode when it’s 1.The Cloudflare Vite plugin takes no --var, so replace the if in lemmaFor() with a bare enableDebugMode() call for the run, and restore it afterwards.Don’t put LEMMA_DEBUG under vars or secrets.required: under vars, the next deploy turns on debug mode in production; under secrets.required, every deploy fails until you set that secret.2
Check the log
Look for
trace sent in the dev server’s output. Debug mode explains the rest of the log.Trace a Durable Object run that outlives its request
A Durable Object can keep an agent run going across requests and alarms, after the request that started it has ended. Store the run in the object’s storage as it happens, then build one trace when it finishes and send it withingest(). The stored run holds:
- Identity: a stable
id, theinput, and thethreadId - Steps: one record per tool call, with its
name, a stable spanid,input,output,startedAt, andendedAt - Outcome: the
answer, or theerrorwhen the run failed - Timing:
startedAtandendedAtas ISO strings
ingest() sends once and throws when the send fails, so keep the stored run until it succeeds. Await sendRun() in the alarm handler that finishes the run: Cloudflare retries an alarm that throws. When the run’s endedAt is already stored, have the alarm call only sendRun(). The retry then resends the same payload from storage, and Lemma skips spans it already stored.