Wrap the client you already use.

Per-customer AI cost in a few lines of code, for OpenAI and Anthropic in Node or Python.

Install in three lines.

Wrap the official OpenAI or Anthropic client.

NodeNode 18+
$ npm i @lancelotwatch/node
import { lancelot } from "@lancelotwatch/node";
const lc = lancelot({ enableFlags: true }); // reads LANCELOT_API_KEY
const openai = lc.wrapOpenAI(new OpenAI(), {
  customerId: user.id,
  feature: "chat",
});

@lancelotwatch/node on npm

PythonPython 3.9+
$ pip install lancelotwatch
from lancelotwatch import lancelot
lc = lancelot(enable_flags=True)  # reads LANCELOT_API_KEY
openai = lc.wrap_openai(
    OpenAI(), customer_id=user.id, feature="chat"
)

lancelotwatch on PyPI · sync and async clients

OpenAI and Anthropic

Wraps the official clients. In Python, AsyncOpenAI and AsyncAnthropic too.

Your calls don’t change

Same methods, same responses.

Already passing a user ID?

If you pass user (OpenAI) or metadata.user_id (Anthropic), you can skip the customer ID.

Matched to revenue

Use the ID you store in Stripe (metadata.customer_id or metadata.user_id), or the customer’s email. RevenueCat app user IDs work the same way.

What it sends. What it never sends.

Token counts only, never prompts.

Sent
Never sent
Who and what
Customer ID, provider and model.
Prompts.
How much
Input, output and cached tokens, and cache writes (5-minute and 1-hour).
Completions.
Context
An optional feature tag, and the time.
Anything your users type or see.

Costs are worked out on our side from token counts, with each model’s real rates. Cache reads, cache writes and long-context pricing are each priced separately.

Built to stay out of your way.

Off the hot path

Events are queued and sent in the background, every 2 seconds or every 100 events.

Never throws into your app

Delivery problems go to an error callback, or a quiet log line at most once a minute.

Serverless

Call flush() before returning. On Vercel, after(() => lc.flush()).

Streams work

stream: true and the .stream() helpers both work. For OpenAI chat streams, the SDK asks for usage in the final chunk for you.

Python details

Fork-safe for gunicorn and Celery workers. A disabled switch turns off all network calls for tests.

Retries don’t double count

Pass an idempotency key, and duplicate events are dropped.

Letting Lancelot act.

Turn on enableFlags. Without it, Lancelot can only suggest fixes.

What it can applyChecked every 60s
  • Throttle

    A set number of calls a minute. Beyond that, calls throw LancelotBlockedError with reason throttled.

  • Pause

    That customer’s calls throw LancelotBlockedError with reason paused.

  • Cheaper model

    If you approved a swap, like Sonnet to Haiku, that customer’s requests for the expensive model use the cheaper one.

  • Spend hold

    A daily budget at what their plan normally costs.

  • Fails open

    If Lancelot is unreachable, every call is allowed.

Changes come from Lancelot’s agent or from you in the dashboard, and apply before each wrapped call.

Handle a blockNode
import { LancelotBlockedError } from "@lancelotwatch/node";
try {
  await openai.chat.completions.create(req);
} catch (err) {
  if (err instanceof LancelotBlockedError) {
    // err.reason: "throttled" | "paused" | "runaway" | "budget"
    return showYourOwnMessage(err.reason);
  }
  throw err;
}
// Check it yourself
lc.getFlags(customerId);
lc.modelFor(customerId, model);

An outage on our side never blocks your product.

An emergency stop, inside your app.

  • 600

    calls a minute trips the stop, by default.

  • $50

    spent in 5 minutes trips it too.

  • 5m

    cooldown before calls go through again.

Counts are kept per process, so across many servers they’re approximate. Lancelot’s own detectors catch what one server can’t see.

Stop and budgetsLocal
  • Speed

    Under a microsecond per call. No network request.

  • Emergency stop

    Too many calls or spending too fast: stopped for a cooldown, with reason runaway.

  • Budgets

    Hourly or daily, per customer or for every free-plan customer. Then reason budget until the period resets.

  • Notified

    Lancelot hears at once. You’re notified within a minute and can make the limit lasting.

  • Your call

    Set limits in the dashboard. Override them, or turn the stop off, in code.

Keys and environments.

Live and test, kept apart for each product.

Live and test keys

lc_live_ keys send to live data. lc_test_ keys send to the product’s test data, which never sets off alerts.

One product per key

Keys belong to a product, so each product’s usage stays separate.

Who manages keys

Members and above create keys. Owners and admins revoke them.

Revoked fast, always logged

Revoked keys stop working within a minute. Every key created or revoked is in the audit log.

The API.

For other languages, other providers, or usage from a queue or warehouse.

https://api.lancelot.watchBearer lc_live_… or lc_test_…
Endpoint
What it does
POST /api/v1/events
Send usage, up to 500 events per request. Any provider and model name.
GET /api/v1/flags
The current throttles, pauses, swaps and budgets. What the SDK polls.
POST /api/v1/signals
Report an emergency stop or a reached budget.
DELETE /api/v1/customers/{id}
Erase a customer’s personal data, from your own account-deletion flow. Costs stay under a pseudonym.
GET / POST /api/v1/stripe/connection
Check or set the Stripe connection.
POST /api/v1/stripe/sync
Run a Stripe sync now.
Rate limitsPer key, per minute
  • /events

    600 requests

  • Events

    60,000

  • /flags

    1,200 polls

  • /signals

    120 calls

  • Over

    429, with when to retry.

Send usagePOST /events
POST https://api.lancelot.watch/api/v1/events
Authorization: Bearer lc_live_...
{ "events": [{
  "customer_id": "cus_123",
  "provider": "openai",
  "model": "gpt-5-mini",
  "input_tokens": 1200,
  "output_tokens": 300,
  "feature": "search"
}] }

Up to 500 events per request.

Questions.

Will it slow down our AI calls?

No. Events are queued and sent in the background, every 2 seconds or every 100 events. Local limits take under a microsecond and make no network request.

Every call is allowed. The SDK fails open, so an outage on our side never blocks your product.

No. The SDK sends customer ID, provider, model, token counts, an optional feature tag and the time. Never prompts or completions.

SDKs for Node 18+ and Python 3.9+, wrapping the official OpenAI and Anthropic clients. Anything else, like Gemini or Mistral, goes through the API.

Call flush() before returning. On Vercel, after(() => lc.flush()).

Only with enableFlags on, and only at the level you set for each action. Blocked calls throw LancelotBlockedError with a reason, so you decide what your users see.

Pass an idempotency key with each event. Duplicates are dropped, so retries don’t double count.

Yes. POST to /api/v1/events, up to 500 events per request, from any language, queue or warehouse.

Use an lc_test_ key. Its usage goes to the product’s test data, which never sets off alerts. In Python, a disabled switch turns off all network calls for tests.