Cockpitify Docs Search the docs⌘K Product Sign in Try the live demo

Track AI cost per user

Updated · Every plan (full detail: Pro, Max and Team) · Phone · Web

Wrap your OpenAI, Anthropic or Vercel AI SDK client with @cockpitify/node so every AI call is recorded with its user, model, tokens and cost.

To see what each user costs you in AI, wrap the AI client on your server with the Cockpitify server SDK, @cockpitify/node (version 0.2.0). Every call is then recorded in your own Supabase project with the user, model, tokens and cost. It works with streaming and doesn’t slow your calls down.

AI calls must run on a server: a Supabase Edge Function or Node. Never wrap calls in your mobile or web app, and never move an AI key into it.

Open the setup screen

  • Phone: on the Money tab, tap Attribute AI cost to users or Tag AI calls (shown while AI cost is missing), or open the app menu at the top → Connections → AI cost.
  • Web: App → Connections, then Set up on the AI cost row.

The screen shows the code to add and a live status that turns to Connected when the first call arrives, with that call’s tokens and cost.

Prefer to let an AI coding assistant do it? Switch from Code to Paste into an AI assistant and copy the ready-made prompt. Let your coding assistant do it gives the command that installs the Cockpitify skills. No app build yet? Send a test call gives a command that writes one test row, so you can see the status change.

Supported clients

  • OpenAI SDK, and OpenAI-compatible APIs through baseURL: DeepSeek, Groq, OpenRouter, and Gemini’s OpenAI-compatible endpoint. Recorded: chat.completions.create (streaming or not), chat.completions.stream() and responses.create.
  • Anthropic SDK: messages.create (streaming or not) and messages.stream(), also under beta.messages.
  • Vercel AI SDK, as middleware.
  • Plain fetch calls, with Ops.wrapFetch: OpenAI-compatible chat, Responses and Anthropic, JSON and streaming.

Wrap the client

Wrap the client per request, where you create it. userId is the Supabase user making the request, taken from the verified token. supabase is a service-role client of the same project:

// supabase/functions/chat/index.ts
import OpenAI from 'npm:openai'
import { Ops } from 'npm:@cockpitify/node'

const ai = Ops.wrap(new OpenAI({ baseURL, apiKey }), {
  userId: user.id,
  supabase, // service-role client
})

// Use `ai` exactly as before, streaming included.

The Anthropic SDK is wrapped the same way. For the Vercel AI SDK, add the middleware to the model:

import { wrapLanguageModel } from 'ai'

const model = wrapLanguageModel({
  model: openai('gpt-4o-mini'),
  middleware: Ops.middleware({ userId: user.id, supabase }),
})

For plain fetch, wrap it and use the result for the AI request:

const f = Ops.wrapFetch(fetch, { userId: user.id, supabase })

On Node, install it with your package manager (npm install @cockpitify/node, pnpm add @cockpitify/node…) and import from @cockpitify/node. Outside Supabase Edge Functions, call await Ops.flush() before the process exits, so the last records are written.

Calls that aren’t priced per token

For speech, images or anything measured by unit, record the call yourself after it succeeds:

Ops.record({
  userId: user.id,
  supabase,
  provider: 'elevenlabs',
  model: 'eleven_multilingual_v2',
  units: 48000,
  unit: 'characters',
  costUsd: 0.36,
})

unit is a lower-case word such as seconds, characters, images or requests. Without costUsd, the call is recorded but stays unpriced.

How cost is calculated

  • Token prices come from a maintained list of about 200 models. Calls to a model that isn’t on the list are recorded as unpriced, not guessed. Margin says how many there are. To price such a model yourself, pass prices (USD per 1M tokens) to Ops.wrap.
  • With streaming, OpenAI-compatible requests ask the provider for usage automatically; the extra usage chunk is hidden from your code. If a stream is cut short, the tokens are estimated and marked as such.
  • The SDK fails open: if recording fails, your AI call still goes through. Errors go to onError, which by default logs one warning.
  • Rows are written in small batches. A failed write is retried once, and a project on ops 0.69.0 or later stores each call only once.

Pass feature to see which feature spends the most:

const ai = Ops.wrap(client, { userId: user.id, supabase, feature: 'quiz' })

Use the snake_case key of the matching feature in Feature usage, so AI cost per feature lines up with usage. Pass appVersion too (for example from an x-app-version header your app sends) to see AI cost per release.

AI cost per day, split by provider, model or feature, is on AI unit economics.

Next

Was this page helpful?YesNo
Ask a question
Questions? Ask me.I’m Murat, the indie developer behind Cockpitify.

I read every message and reply by email, usually within a day.