LancelotDocs
Node.js SDK

Send usage

Node.js SDK

`@lancelotwatch/node` wraps the official OpenAI and Anthropic clients. No dependencies. Never throws into your app.

npm i @lancelotwatch/node

Create a client

import { lancelot } from "@lancelotwatch/node";

const lc = lancelot(); // reads LANCELOT_API_KEY

Create it once and reuse it. It batches events in the background and sends them every two seconds, or every 100 events.

Wrap OpenAI and Anthropic

Bind each client to the customer you're serving. Wrapping is cheap, so do it per request.

import OpenAI from "openai";
import Anthropic from "@anthropic-ai/sdk";

const openai = lc.wrapOpenAI(new OpenAI(), { customerId: user.id, feature: "chat" });
await openai.chat.completions.create({ model: "gpt-5-mini", messages });

const anthropic = lc.wrapAnthropic(new Anthropic(), { customerId: user.id, feature: "summarize" });
await anthropic.messages.create({ model: "claude-haiku-5-5", max_tokens: 500, messages });

customerId and feature can also be functions that read the call's params, so you can wrap once at startup:

const openai = lc.wrapOpenAI(new OpenAI(), { customerId: (params) => params.user });

If you already pass user (OpenAI) or metadata.user_id (Anthropic), the SDK uses it and you can skip customerId.

Streams

Streams work. For OpenAI chat streams, the SDK sets stream_options.include_usage: true for you, so the final chunk carries token counts. Anthropic messages.stream() and OpenAI responses.stream() report when the stream finishes.

Report usage yourself

For other providers, or calls the wrappers don't cover:

lc.track({
  customerId: "cus_123",
  provider: "openai",
  model: "gpt-5-mini",
  inputTokens: 1200,          // the whole prompt, cache reads and writes included
  outputTokens: 300,
  cachedInputTokens: 800,     // optional: cache reads
  cacheWriteTokens: 0,        // optional: Anthropic 5-minute cache writes
  cacheWrite1hTokens: 0,      // optional: Anthropic 1-hour cache writes
  feature: "search",          // optional
  idempotencyKey: response.id // optional: dedupes retries
});

Serverless

Flush before the function ends, or the last events can be lost when the instance freezes:

import { after } from "next/server";

after(() => lc.flush()); // Next.js on Vercel
// elsewhere: await lc.flush();

Letting Lancelot act

const lc = lancelot({ enableFlags: true });

With enableFlags, the SDK polls each customer's flags every 60 seconds and applies them before each wrapped call. Without it, Lancelot can only suggest fixes. See Fixes and autonomy and Emergency stop and budgets.

Blocked calls throw LancelotBlockedError, with a reason of "throttled", "paused", "runaway" or "budget":

import { LancelotBlockedError } from "@lancelotwatch/node";

try {
  return await openai.chat.completions.create({ model: "gpt-5", messages });
} catch (e) {
  if (e instanceof LancelotBlockedError) return "You've hit your plan's limit for now.";
  throw e;
}

To check without calling a model: lc.getFlags(customerId) returns { throttled, paused, throttleCpm, downgrade }, and lc.modelFor(customerId, model) returns the model to call.

Options

OptionDefault
apiKeyprocess.env.LANCELOT_API_KEYlc_live_... or lc_test_...
baseUrlhttps://api.lancelot.watch
flushIntervalMs2000How long to buffer events
maxBatchSize100Events per request, up to 500
maxQueueSize10000Events held while the API is unreachable; oldest dropped first
enableFlagsfalseApply fixes and the emergency stop
flagsIntervalMs60000How often flags are refreshed
breakerfrom your dashboardOverride the emergency stop, or false to turn it off
onErrorwarns once a minuteCalled on delivery problems; never throws
disabledfalseNo network at all, for tests
fetchglobal fetchFor runtimes without one