Scrawn LogoScrawn Docs
SDK ReferenceFunctionsAI Token Billing

Overview

Meter AI token usage by streaming events to the backend

Stream token usage events directly with aiTokenStreamConsumer, or use the first-class Vercel AI SDK wrapper that auto-instruments every LLM call.

Stream Token Events

aiTokenStreamConsumer consumes an async iterable of AITokenUsagePayload objects and streams them to the backend for billing.

import { biller } from "../scrawn/biller";

async function* generateUsage() {
  yield {
    userId: "user-123",
    model: "gpt-4",
    inputTokens: 100,
    outputTokens: 50,
    inputDebit: 3,
    outputDebit: 6,
  };
}

const response = await biller.aiTokenStreamConsumer(generateUsage());

Fork mode

Return a forked stream to simultaneously bill and stream tokens to the user:

import { biller } from "../scrawn/biller";

async function* tokenStream() {
  yield {
    userId: "user-123",
    model: "gpt-4",
    inputTokens: 100,
    outputTokens: 50,
    inputDebit: biller.tag("GPT_INPUT"),
    outputDebit: biller.expr("COMPLEX_FEE"),
  };
}

const { response, stream } = await biller.aiTokenStreamConsumer(
  tokenStream(),
  { return: true }
);

// Stream tokens to user while billing runs in background
for await (const token of stream) {
  process.stdout.write(token.outputTokens.toString());
}

const result = await response;

Configuration

Prop

Type

AITokenUsagePayload

Each payload in the stream represents a single token usage event:

Prop

Type