Short answer
Tag every model call with the customer or team it was for, taken from your own login or API key and never from the request. Record the cost from the token usage the provider returns, check that customer's total for the month before each call, and refuse the call once their budget is used. For hard separation, give each customer or team its own key with its own limit.
If you charge customers a flat price and your agent calls a model for them, every customer has a cost you can't see. Most are cheap. One heavy user can cost more than they pay. The same goes inside a company: five departments share one key and nobody knows who spent what. This guide shows how to record cost per customer or team, stop one at its budget, and read the numbers.
Why your provider can't tell you
OpenAI and Anthropic split cost by project or workspace, key and model. They don't know which of your customers a call was for. Unless each customer has its own key or project, you have to record that yourself, at the moment you make the call.
Two ways to split cost
- One key per customer or team. Each gets its own key with its own limit. Simple to read and the limit is enforced in one place, but you manage many keys.
- One shared key, tagged calls. Every call records who it was for. One key to manage, but the tagging and the limit are your code's job.
Most teams start with tagging. The code below does that.
The fix: tag, record, check
Record the real token usage the provider returns after each call, and check the customer's monthly total before the next one:
import sqlite3, time
# Prices per 1M tokens (input, output). Check your provider's price page; these change.
PRICES = {"gpt-4o-mini": (0.15, 0.60)}
db = sqlite3.connect("spend.db", timeout=10) # use your main database in production
db.execute("""CREATE TABLE IF NOT EXISTS usage (
customer TEXT, month TEXT, model TEXT,
input_tokens INT, output_tokens INT, cost REAL, at REAL)""")
class OverBudget(Exception):
pass
def month():
return time.strftime("%Y-%m")
def spent(customer):
row = db.execute("SELECT COALESCE(SUM(cost), 0) FROM usage WHERE customer = ? AND month = ?",
(customer, month())).fetchone()
return row[0]
def check_budget(customer, monthly_budget):
"""Call before every model call. customer must come from your login, never from the request body."""
if spent(customer) >= monthly_budget:
raise OverBudget(f"{customer} has used its ${monthly_budget:.2f} for {month()}")
def record(customer, model, input_tokens, output_tokens):
"""Call after every model call, with the usage the provider returned."""
price_in, price_out = PRICES[model]
cost = (input_tokens * price_in + output_tokens * price_out) / 1_000_000
with db:
db.execute("INSERT INTO usage VALUES (?, ?, ?, ?, ?, ?, ?)",
(customer, month(), model, input_tokens, output_tokens, cost, time.time()))
return costUse it around each model call:
def handle_request(user, messages):
customer = user.account_id # from your auth, not from the client
check_budget(customer, monthly_budget=plan_budget(customer))
reply = client.chat.completions.create(model="gpt-4o-mini", messages=messages)
record(customer, "gpt-4o-mini",
reply.usage.prompt_tokens, reply.usage.completion_tokens)
return replyAnd the report you will actually look at:
SELECT customer, month, ROUND(SUM(cost), 2) AS cost, COUNT(*) AS calls FROM usage GROUP BY customer, month ORDER BY cost DESC;
We tested this on 5 December 2026 with two customers and a budget of $0.01. The first customer was refused on its fourth call, having spent $0.0126. The second customer, with nothing spent, went through. The report showed each one's cost and number of calls.
Notice the first customer ended a little over budget. Checking the total before a call can't know what that call will cost. If a customer must never go over, refuse a call when the remaining budget is less than your worst-case cost for one call, as in the per-run budget guide.
Three rules that keep the numbers honest
- Take the customer from your login. Never from a field the client sends. Otherwise one customer can charge their usage to another.
- Use the provider's usage, not your estimate. Count tokens from the response. Retries and failed calls that were billed count too.
- Tag the run as well as the customer. An agent makes many calls for one task. A run ID lets you see the cost of a task, not just of a call.
Per department works the same way
If the question is "we need to cap what each department can spend on agent runs", treat each department as a customer. Tag the call with the department from the user's account, give each a monthly budget, and check it before each call. Or give each department its own key with its own limit, below.
What this simple version does not cover
- Streaming. A long streamed answer is only recorded when it ends. Ask the provider for usage at the end of a stream; OpenAI needs
stream_options={"include_usage": True}. - Calls at the same moment. Two calls for one customer can both pass the check before either is recorded.
- Prices. The price table is yours to keep current, including cached input tokens, which are priced differently.
- Calls you didn't wrap. A framework or tool that calls the model on its own skips your code. A proxy in front of the model sees every call.
Doing it with SteerPlane
SteerPlane's gateway sits in front of the model, so it sees every call made with a SteerPlane key, works out the cost on the server and checks the key's limits before each call. Give each customer or department its own key and monthly budget:
pip install "steerplane[cli]" steerplane keys create --name acme-corp --max-cost 50 # $50 a month for this customer steerplane keys create --name finance-team --max-cost 200 # or one key per department
Point the agent's model client at the gateway with that key, and calls are refused once the key passes its monthly budget. Each key can also have a cost limit per run, set on the dashboard's API Keys page, and the dashboard shows the cost of each run. Setup is on the setup page.
One thing to know. This gives you a budget per key, so it is a fit when you are happy to have one key per customer or team. If you need one key shared by thousands of end users, keep the tagging above in your own code.
A checklist before you charge for an agent
- Every model call is tied to a customer or team, taken from your login.
- Cost comes from the usage the provider returns.
- Each customer has a monthly budget that is checked before each call.
- You can see cost per customer and per task each month.
- Your provider's own project or workspace limit is switched on as a last line.
Questions people ask
Can OpenAI or Anthropic show cost per customer?
Not by themselves. Their dashboards split cost by project or workspace, key and model. If each customer has its own project or key you can read it there; otherwise you need to tag and record each call yourself.
How do I cap what each department can spend on agent runs?
Treat each department like a customer: tag every call with it, record the cost, and check the department's monthly total before each call. Or give each department its own key with a monthly budget, so the limit is enforced in one place.
Should I use one API key per customer?
It is the simplest way to separate cost and set a hard limit for each customer. It means managing many keys. With one shared key, tag each call and keep the totals yourself.