User tracking
Know what each of your apps, customer groups and end users spends, in DADAThe Algerian dinar, the one currency the docs show.More on Balance and charges →.
Two levels
| Level | Told apart by | Spend read from |
|---|---|---|
| an app or a customer group | its own API keyAPI keyThe key you create and send with every request that calls a model.More on API keys → | the key: GET /v1/key, Activity in the app |
| one end user | your own id for them, in your log | your log of usage.cost |
1. Give each group its own key
Yacine runs Souk Bot for shops on the web and in a phone app. He gives each its own key, named for it: Souk Bot web, Souk Bot mobile. In the app, Activity then shows which key spent what.
Set a cap on each key, in DA, reset daily, weekly or monthly. A spent cap stops that key with a 402; the other keys run. API keys sets out each setting.
An account holds at most 5 keys, paused keys counted, unless raised. A key tells apart an app or a group, never each of your end users: step 2 does that.
From a script
With a management keyManagement keyThe key that runs your account through the API: the balance, the usage history, your keys, guardrails, observability. It never calls a model.More on API keys →, POST /v1/keys creates a key with its name and cap in one call, and answers its secret once. The body for Souk Bot mobile, capped at 2 000 DA a month, illustrative:
{
"name": "Souk Bot mobile",
"limit": 2000,
"limit_reset": "monthly"
}Keys has the call in four languages.
2. Log each end user's charge
Your code knows which of your users sent a request; OpenDunes knows its charge. After each answer, log your user's id beside usage.cost, the charge in DA.
The sample sends one question for CUSTOMER_ID and adds a line to spend.jsonl. Run it with OPENDUNES_API_KEY set to the key of that customer's group.
import json
import os
from openai import OpenAI
API_KEY = os.environ["OPENDUNES_API_KEY"]
BASE_URL = "https://opendunes.com/v1"
MODEL = "anthropic/claude-sonnet-4.5"
CUSTOMER_ID = "customer-4521"
LOG = "spend.jsonl"
client = OpenAI(base_url=BASE_URL, api_key=API_KEY)
reply = client.chat.completions.create(
model=MODEL,
messages=[
{"role": "user", "content": "Is the red jacket still in size M?"},
],
)
line = {"customer": CUSTOMER_ID, "cost": reply.usage.cost}
with open(LOG, "a") as log:
log.write(json.dumps(line) + "\n")
print(reply.choices[0].message.content)
print(line)Each customer's spend, in DA, from the log:
jq -s 'group_by(.customer)
| map({customer: .[0].customer, spent: (map(.cost) | add)})' spend.jsonlLog a name, an email address or a phone number as the id. The log then holds personal data it has no need of.
Log an id of your own that means nothing outside your app. The table from that id to a person stays in your user records.
To keep the receipt handle, the model and the tokens on each line too, Usage accounting logs them all.
3. Read each key's spend
GET /v1/key answers the key that sends it: what it has spent this UTC day, week and month, and what is left of its cap. Run the sample once per key, with OPENDUNES_API_KEY set to it.
import json
import os
import urllib.request
API_KEY = os.environ["OPENDUNES_API_KEY"]
ADDRESS = "https://opendunes.com/v1/key"
request = urllib.request.Request(
ADDRESS,
headers={"Authorization": "Bearer " + API_KEY},
)
with urllib.request.urlopen(request) as response:
key = json.load(response)["data"]
print("today:", key["usage_daily"], "this month:", key["usage_monthly"])
print("left of the cap:", key["limit_remaining"])limit_remaining is null on a key with no cap. Reading a key is free.
The usage history, GET /v1/activity, counts per day, model and provider, not per key. Per key, read the key itself, or Activity in the app.
With a management key, GET /v1/keys lists every key of your account in one call, each with its name, usage_daily, usage_weekly, usage_monthly and limit_remaining, in DA.
Last modified on