USG-11Recommended
Charge each model call's cost to the customer who caused it
Store the cost of every model call, retry, embedding and tool call with the customer and the metered action it served. Then cost and revenue sit side by side.
How to do it
Price every call and cap the spend computes each call's cost. This rule links that cost to a bill. Pass the customer ID and the usage event key down to the model gateway, and write both on the cost row. Count retries, failed calls and background work too, since you pay for them. Work that serves no single customer, such as a nightly index, goes to a shared cost line. Then cost per customer is one query, as in Track cost per request.
Examples
TypeScript example
export async function callModel(input: ModelInput, ctx: { customerId: string; usageEventKey: string }) {
const response = await gateway.generate(input)
await costs.insert({
customerId: ctx.customerId,
usageEventKey: ctx.usageEventKey,
model: response.model,
costMicroUsd: callCostMicroUsd(response.model, response.usage),
})
return response
}
Python example
def call_model(model_input, customer_id: str, usage_event_key: str):
response = gateway.generate(model_input)
costs.insert(
customer_id=customer_id,
usage_event_key=usage_event_key,
model=response.model,
cost_micro_usd=call_cost_micro_usd(response.model, response.usage),
)
return response
How to check
A query over last month's cost rows finds none without a customer ID or a shared cost label. A test makes a call that fails and is retried once, and both attempts appear under the same usage event key.
Related rules
- AIF-42 Price every call and cap the spend in AI features
- SCL-33 Track cost per request in Scaling
- USG-12 Keep every metered price above its cost in Usage-based billing
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/usage-billing/margins-and-model-cost/cost-attribution">Agent Wranglers usage-based billing playbook</a>. In code you write, add a comment next to it with the same link.