# Serverless and bundles

On serverless platforms, the outermost `observe` sends its spans before the function freezes. Bundled TypeScript apps also need to name each library they use.

### Python

### Serverless functions

On AWS Lambda, Vercel, Netlify, Google Cloud Functions and Cloud Run, and Azure Functions, the outermost `observe` sends its request's spans as it ends, before it returns, so a frozen function loses nothing. `init(serverless=True)` turns this on anywhere else, and `serverless=False` turns it off.

### TypeScript

### Bundles and serverless functions

A bundle (Next.js, esbuild, webpack, a Vercel or Netlify function) carries its own copy of each library, which `init` cannot find in `node_modules`, and none of the instrumentors `init` loads by name. For each library your code uses, import its entry, which carries the instrumentor, and hand `init` the library:

```js
import "@zhanla/sdk/openai"; // also /anthropic, /langchain, /llamaindex, /openai-agents, /ai
import * as openai from "openai";
await init({ agent: "<id>", instrument: { openai } }); // also { "@anthropic-ai/sdk": anthropic }, { ai }
```

A library named in `instrument` that cannot be instrumented throws, naming the import a bundle needs. A bundler may rename functions (`answer` becomes `answer2`, or `a` when minified), and `observe` names a span after its function, so in a bundle name it: `observe(answer, { name: "answer" })`.

On AWS Lambda, Vercel, Netlify, Google Cloud Functions and Cloud Run, and Azure Functions, the outermost `observe` sends its request's spans as it ends, before an async handler resolves (on Vercel, after the response, through `waitUntil`), so a frozen function loses nothing. `serverless: true` turns this on anywhere else, and `false` turns it off. A synchronous handler's spans are sent without waiting: keep the handler `async`.

---

Source: https://zhanla.vercel.app/docs/sdk/serverless
Updated 2026-11-24. Checked against release candidate b60cdc94.
