Bill for AI tokens by model
This guide sets up per-token prices for each AI model and kind of token, then turns one provider response into usage events. At the end you know what the invoice will charge. It is for a developer whose app calls Anthropic or OpenAI and resells that access.
Using a LiteLLM proxy? It sends the events for you: follow LiteLLM integration after step 1.
Words on this page
Section titled “Words on this page”| Word | Meaning |
|---|---|
| Recipe | A ready-made pricing setup that one API call creates: a meter, a plan and its prices. |
token_ |
The kind of token: input, output, cache_, cache_ or cache_. Each has its own price. |
| Cache read and cache write | Prompt caching: tokens the provider reads from its cache cost less than input, and tokens it writes to the cache cost more. |
Before you start
Section titled “Before you start”You need Hardcount running, with its address in HARDCOUNT and a test
secret key in HARDCOUNT_, as in Send usage and get your first
invoice. That page’s steps 2 and 3
also create a customer and its subscription (see the end of step 1 below).
This guide uses the CUSTOMER_ (the customer’s external ID) and
CUSTOMER_ they set.
1. Load ready-made prices
Section titled “1. Load ready-made prices”Apply the recipe for your provider. anthropic_ prices every Claude
model; openai_ prices the GPT, o-series and embedding models;
replicate_ prices GPU seconds.
PLAN_ID=$(curl -s -X POST "$HARDCOUNT/v1/recipes/anthropic_style/instantiate" \ -H "Authorization: Bearer $HARDCOUNT_KEY" \ -H "Content-Type: application/json" \ -d '{}' | jq -r '.created_objects.plan_ids[0]')echo "$PLAN_ID"You see: the plan ID, such as hc_.
The recipe creates a tokens meter, if you have none, and a monthly plan
named “AI API” with no base fee. The plan has one price for each model and
kind of token, at the provider’s list prices. Applying it again returns the
same setup. The models, and how to set the plan’s name and currency:
Ready-made recipes.
A customer must be on the plan before their usage is billed. The first
invoice page, steps 2 and 3, creates the
customer and the subscription. Its step 3 uses the PLAN_ID saved above. Those steps set CUSTOMER_ and CUSTOMER_,
which the steps below use.
2. Turn a provider’s token counts into events
Section titled “2. Turn a provider’s token counts into events”Send one usage event for each kind of token in a call. Each event names the
model and the kind of token in its dimensions, so the plan’s prices can
pick it. Give each event its own idempotency key, made from your call’s ID
and the kind of token, so a resend counts once.
Providers report their counts differently, so map them with care:
| Provider field | token_ |
Note |
|---|---|---|
Anthropic usage. |
input |
Excludes cache reads and writes. |
Anthropic usage. |
cache_ |
|
Anthropic usage. |
cache_ |
Writes kept for 5 minutes. |
Anthropic usage. |
cache_ |
Writes kept for 1 hour. |
Anthropic usage. |
output |
|
OpenAI usage. minus usage. |
input |
prompt_ includes the cached tokens, so take them out. |
OpenAI usage. |
cache_ |
|
OpenAI usage. |
output |
Send the model name exactly as the recipe prices it, such as
claude-sonnet-4. or gpt-4o, not the dated API name.
This shell function sends one event. It needs CUSTOMER_ from the first
invoice page:
send_tokens() { # send_tokens <call id> <token type> <quantity> jq -n --arg ref "$CUSTOMER_REF" --arg call "$1" --arg type "$2" --arg qty "$3" \ '{event_name: "tokens", external_customer_id: $ref, quantity: $qty, idempotency_key: ($call + ":" + $type), dimensions: {provider: "anthropic", model: "claude-sonnet-4.5", token_type: $type}}' \ | curl -s -X POST "$HARDCOUNT/v1/usage-events" \ -H "Authorization: Bearer $HARDCOUNT_KEY" \ -H "Content-Type: application/json" \ -d @- | jq -c '{quantity, token_type: .dimensions.token_type}'}A Claude Sonnet 4.5 call answered with this usage:
"usage": { "input_tokens": 2000, "cache_read_input_tokens": 50000, "cache_creation_input_tokens": 10000, "cache_creation": {"ephemeral_5m_input_tokens": 10000, "ephemeral_1h_input_tokens": 0}, "output_tokens": 1500}becomes four events. A kind with 0 tokens needs no event.
send_tokens call-1 input 2000send_tokens call-1 cache_read 50000send_tokens call-1 cache_write_5m 10000send_tokens call-1 output 1500You see: one line for each event:
{"quantity":"2000","token_type":"input"}{"quantity":"50000","token_type":"cache_read"}{"quantity":"10000","token_type":"cache_write_5m"}{"quantity":"1500","token_type":"output"}provider is not used by the recipe’s prices, but your margin needs it to
find your cost (Margin).
3. Claude Haiku 5.5: the prompt-length price
Section titled “3. Claude Haiku 5.5: the prompt-length price”Claude Haiku 5.5 has two prices. A call whose prompt is over 100,000 tokens pays the higher one, for every token of the call, output included. The prompt counts input, cache reads and cache writes.
The recipe’s Haiku 5.5 prices pick events by one more label,
context_. Work it out once per call, and put it on every event of
that call:
- prompt = input + cache reads + cache writes;
- over 100,000:
"context_;tier": "prompt_ over_ 100k" - 100,000 or less:
"context_.tier": "prompt_ up_ to_ 100k"
For example, a call with 90,000 input tokens and 20,000 cache reads has a
prompt of 110,000 tokens. All its events, its output too, carry
prompt_. A Haiku 5.5 event without context_ fits no
recipe price, so it is not billed.
A LiteLLM proxy adds context_ for you.
4. What the invoice charges
Section titled “4. What the invoice charges”At the end of the period, each model and kind of token becomes one invoice line, such as “Tokens (claude-sonnet-4.5 · input)”. Its quantity is the period’s total for that kind; the amount is rounded to whole cents once.
Say the customer made 1,000 calls like the one in step 2 in June. With the recipe’s Claude Sonnet 4.5 prices:
| Line | Quantity | Price per million | Amount |
|---|---|---|---|
| input | 2,000,000 | $3.00 | 2,000,000 × 0.0003 cents = $6.00 |
| cache_read | 50,000,000 | $0.30 | 50,000,000 × 0.00003 cents = $15.00 |
| cache_write_5m | 10,000,000 | $3.75 | 10,000,000 × 0.000375 cents = $37.50 |
| output | 1,500,000 | $15.00 | 1,500,000 × 0.0015 cents = $22.50 |
The invoice total is $6.00 + $15.00 + $37.50 + $22.50 = $81.00, before tax and credit. Until the period ends, the preview shows it, as in first invoice step 5; for a test customer, steps 6 and 7 issue it now.
5. Price a model the recipe does not cover
Section titled “5. Price a model the recipe does not cover”Usage of a model with no price is stored and not billed: the event’s answer
carries billing_. Add a price for it to the plan with
POST /, one for each kind of token. It applies from
each subscriber’s next billing period.
Every call: Price a model a recipe does not cover.
If something goes wrong
Section titled “If something goes wrong”| What you see | What to do |
|---|---|
meter "tokens" not found |
Step 1 did not run. Apply the recipe. |
customer "…" not found |
CUSTOMER_ is not a customer’s external_. Create the customer first. |
billing_ on an event |
No price takes its model and token_. Check the model name against the recipe, or add a price (step 5). |
billing_ |
The customer’s plan has no price on the tokens meter. Put the customer on the recipe’s plan. |
billing_ |
The customer has no subscription that bills the event’s time. |
Next: see what the usage cost you: Margin. More: How Hardcount picks the price for an event.
Stuck, or found a bug? Ask in Discussions · Open an issue