How to measure LLM cost per customer

Attribute every OpenAI, Anthropic and Gemini call to the customer that caused it, in Python, Node.js, the Vercel AI SDK and LangChain.

By Keyveve, Inc. · Updated 2026-10-22

Your provider bill tells you what you spent. It can't tell you that one customer on a $49 plan is costing you $70 a month. To know that, every model call needs to carry the customer it was for.

The three parts

  1. Usage per call. Every major API returns token counts in the response: input, output, cached input, cache writes. Streams report them at the end.
  2. An exact price. Multiply by the right rates: cached input, batch, priority and long-context tiers all change the price.
  3. The customer. Your code knows which customer a request is for; the provider doesn't. You have to attach it.

Doing this by hand means touching every call site. The approach that scales is to set the customer once per request and let a wrapper do the rest.

Python

import veqen
from openai import OpenAI

client = veqen.wrap(OpenAI(), feature="assistant")

@app.middleware("http")
async def attribute(request, call_next):
    with veqen.tags(customer=request.state.org_id, user=request.state.user_id):
        return await call_next(request)

Node.js

import { wrap, withTags } from "@veqen/sdk";

const client = wrap(new OpenAI(), { feature: "assistant" });
app.use((req, res, next) => withTags({ customer: req.org.id, user: req.user.id }, next));

Vercel AI SDK

import { wrapLanguageModel } from "ai";
import { veqen } from "@veqen/sdk/ai-sdk";

const model = wrapLanguageModel({ model: anthropic("claude-sonnet-4-5"), middleware: veqen({ feature: "assistant" }) });

LangChain

# pip install veqen[langchain]
import veqen
from veqen.integrations.langchain import VeqenCallbackHandler

with veqen.tags(customer=org_id):
    chain.invoke(inputs, config={"callbacks": [VeqenCallbackHandler(feature="rag")]})

From cost to margin

Cost per customer becomes useful next to revenue per customer. Connect Stripe (Pro or Team) and, where your customer tag matches the Stripe customer's id or metadata, you get margin per customer, with the unprofitable ones first. Two fixes are common once you see the list: a per-customer budget for the plan that's losing money, and moving the heaviest feature to a cheaper model after checking quality. The dashboard's What-if page re-prices your real usage on another model or host, and the public simulator does the same for any mix of models.

Without an account

All of the above works offline: set ledger_file and run npx @veqen/sdk report to see cost per customer, feature and model on your machine.

Veqen records what every AI call cost and who it was for, reconciles it to your provider bills and produces the journal entries. Start free or read the quickstart.