Usage accounting
Account for what your requests cost, in DADAThe Algerian dinar, the one currency the docs show.More on Balance and charges →: request by request, then model by model.
- Log the charge of each request.
- Read a receipt again.
- Total a period by model.
- Check against your balance.
Where each figure is read
| Figure | Read from | Key |
|---|---|---|
| one request's charge, in DA | usage.cost, in the answer | API keyAPI keyThe key you create and send with every request that calls a model.More on API keys → |
| one request's receipt | GET /v1/generation, with the answer's X-Generation-Id | API key |
| a day's spend, per model and provider, in DA | GET /v1/activity | 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 → |
| your balanceBalanceWhat you can spend, in DA.More on Balance and charges →, as what came in and what requests spent | GET /v1/credits | management key |
The docs name no rate: every figure here is a charge already made. How a request is charged is in Balance and charges.
1. Log the charge of each request
Every answer carries its charge in usage.cost, in DA, and its receipt handle in the X-Generation-Id header. Yacine logs both for every Souk Bot request, one JSON line each, in usage.jsonl.
import json
import os
from datetime import datetime, timezone
from openai import OpenAI
API_KEY = os.environ["OPENDUNES_API_KEY"]
BASE_URL = "https://opendunes.com/v1"
MODEL = "anthropic/claude-sonnet-4.5"
LOG = "usage.jsonl"
client = OpenAI(base_url=BASE_URL, api_key=API_KEY)
raw = client.chat.completions.with_raw_response.create(
model=MODEL,
messages=[
{"role": "user", "content": "Two lines to welcome a shop in Oran."},
],
)
reply = raw.parse()
line = {
"at": datetime.now(timezone.utc).isoformat(),
"generation_id": raw.headers["X-Generation-Id"],
"model": reply.model,
"prompt_tokens": reply.usage.prompt_tokens,
"completion_tokens": reply.usage.completion_tokens,
"cost": reply.usage.cost,
}
with open(LOG, "a") as log:
log.write(json.dumps(line) + "\n")
print(line)One line of the log, illustrative:
{
"at": "2026-09-28T09:14:02+00:00",
"generation_id": "gen-3bhGkxlo4XFrqiabUM7NDtwDzWwG",
"model": "anthropic/claude-sonnet-4.5",
"prompt_tokens": 14,
"completion_tokens": 38,
"cost": 0.198288
}cost is the charge taken from your balance, in DA, to six decimals: the smallest request never reads as free. Add the lines' cost and you have what those requests spent.
A streamed answer carries its usage.cost in the closing event: Streaming shows where to read it.
2. Read a receipt again
The generation_id in your log opens the request's receipt with GET /v1/generation. Its total_cost is the charge, in DA, with the model that served the request, its tokens and how long it took. Read it when a line of your log is questioned.
You read only your own account's receipts, with any of its API keys. Reading a receipt is free.
3. Total a period by model
For a day or a month, you need no log: GET /v1/activity answers what your account spent per day, model and provider, over the last 30 days. It takes a management key: set OPENDUNES_MANAGEMENT_KEY to it for this call. An API key gets a 401.
Each sample adds the rows up per model and prints each model's requests and spend, in DA.
import json
import os
import urllib.request
from collections import defaultdict
MANAGEMENT_KEY = os.environ["OPENDUNES_MANAGEMENT_KEY"]
ADDRESS = "https://opendunes.com/v1/activity"
request = urllib.request.Request(
ADDRESS,
headers={"Authorization": "Bearer " + MANAGEMENT_KEY},
)
with urllib.request.urlopen(request) as response:
rows = json.load(response)["data"]
requests = defaultdict(int)
spent = defaultdict(float)
for row in rows:
requests[row["model"]] += row["requests"]
spent[row["model"]] += row["usage"]
for model in sorted(spent, key=spent.get, reverse=True):
print(model, requests[model], "requests", round(spent[model], 6), "DA")For one day only, add ?date= and the day, YYYY-MM-DD, in UTC, to the address. To keep the history past 30 days, export it every day: Activity export.
4. Check against your balance
GET /v1/credits answers two totals, in DA: total_credits, every depositDepositMoney entering your balance, by CIB or Edahabia card.More on Balance and charges →, refund and adjustment, and total_usage, everything requests spent. Your balance is the first minus the second.
| Total | Runs from |
|---|---|
your log's cost lines | the first request you logged |
GET /v1/activity | 30 days back, per day |
total_usage | your account's first request |
Compare two totals over the same span only. total_usage counts every request since the account opened; the usage history counts 30 days.
In the app, Activity lists every charge, one line each, to check a single line by eye.
Last modified on