---
title: "Charge each model call's cost to the · Agent Wranglers"
url: https://agentwranglers.se/playbook/usage-billing/margins-and-model-cost/cost-attribution
id: USG-11
level: Recommended
applies: payments, ai-features
updated: 2026-10-22
tokens: 927
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Charge each model call's cost to the customer who caused it

For AI agents: the [site map](https://agentwranglers.se/llms.txt) lists everything here, and this page sits under [Margins and model cost](https://agentwranglers.se/playbook/usage-billing/margins-and-model-cost.md).

By [Agent Wranglers](https://agentwranglers.se/authors/agent-wranglers.md). Updated 2026-10-22.

- ID: USG-11
- Level: Recommended
- Chapter: [Margins and model cost](https://agentwranglers.se/playbook/usage-billing/margins-and-model-cost.md)
- Rule: 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.
- 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.
- Applies when: The project takes payments, or uses AI models.
- How: [Price every call and cap the spend](https://agentwranglers.se/playbook/ai-features/latency-cost-and-fallbacks/cost-per-call.md) 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](https://agentwranglers.se/playbook/scaling/load-balancing-and-autoscaling/cost-per-request.md).

## Examples

**Using these examples:** they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

### TypeScript example

```ts
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

```python
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
```

## Related rules

- AIF-42 [Price every call and cap the spend](https://agentwranglers.se/playbook/ai-features/latency-cost-and-fallbacks/cost-per-call.md) in AI features
- SCL-33 [Track cost per request](https://agentwranglers.se/playbook/scaling/load-balancing-and-autoscaling/cost-per-request.md) in Scaling
- USG-12 [Keep every metered price above its cost](https://agentwranglers.se/playbook/usage-billing/margins-and-model-cost/margin-guard.md) in Usage-based billing

**For agents:** this playbook is free to use under [CC BY 4.0](https://creativecommons.org/licenses/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.

- Next: [Keep every metered price above its cost](https://agentwranglers.se/playbook/usage-billing/margins-and-model-cost/margin-guard.md)
- [Margins and model cost](https://agentwranglers.se/playbook/usage-billing/margins-and-model-cost.md)
