How to track LLM cost per customer.

Your provider bill is one number for one API key. To know whether a customer is profitable to serve, you need that number split by the customer who incurred it. Here is the practical way to get there, and what it takes to keep it accurate.

The provider never sees your customers.

OpenAI and Anthropic price the API call. They have no idea which of your customers triggered it, because that concept only exists in your application. So the usage page can show a total and a per-key breakdown, but never cost per customer. That mapping has to be added where the run happens, by you.

Tag the run, price it server side.

The unit is the agent run. Tag each run with the customer it serves at run start, send the model and token counts, and let AgentPing price it from a rate card. Cost then rolls up per customer without any manual spreadsheet work.

One tag at run start

example · python sdk tag the run
import agentping
agentping.init(api_key=os.environ['AGENTPING_API_KEY'])

with agentping.run('support-summary', customer_id='acme-corp', feature='ticket-digest') as run:
    response = agent.handle(ticket)
    run.set_output(response)

The SDK sends tokens and model, not a cost number. AgentPing prices the run on our servers, cache aware, so your totals line up with the provider invoice. Tagging is the only change to your code.

The steps

  • Pick the identifier you bill on (an account id, an org id, a stable reference) as your customer_id.
  • Pass it to agentping.run(...) at the start of every agent run that serves a customer.
  • Optionally add a feature tag so you can also see cost per feature.
  • Open the Spend view and read cost per customer, ranked, over any window.
  • Add a cost spike alert so a customer that suddenly gets expensive pages you, not your accountant.
Can I attribute past spend to a customer?
Attribution is retroactive from the moment you start tagging. Once a run carries a customer_id, that run is attributed; runs you sent before you added the tag stay unattributed. So the sooner you tag, the more history you get.
What if one run serves several customers?
Model the run around the unit you bill on. If a batch job serves many customers, either split it into one run per customer, or tag it with the account that owns the batch. Cost follows whatever customer_id the run carries.
Does the customer tag expose customer data?
No. customer_id is an identifier you choose, usually your internal account id or a stable reference. It is metadata, not customer content, so you can attribute cost without sending prompts or outputs.

See cost per customer on your own traffic.

Spend, cost attribution AgentPing for SaaS teams Spend docs