Activity export
Write your usage history to a CSV file you keep: one row per day, model and provider, with its spend in DADAThe Algerian dinar, the one currency the docs show.More on Balance and charges →.
Before you start
| You need | Where |
|---|---|
| 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 → | Settings, under Management keys |
| Python 3.9+, Node 18+ or Go 1.22+ | your machine |
jq, for the cURL tab | your machine |
1. Set your management key
GET /v1/activity takes a management key. An API keyAPI keyThe key you create and send with every request that calls a model.More on API keys → gets a 401. API keys says what each kind of key does.
export OPENDUNES_MANAGEMENT_KEY="…"2. Export the last 30 days
Yacine keeps a file of what his account spends, for his accountant. Each sample appends the rows to activity.csv and writes the header on the first run.
| Input | Set to |
|---|---|
DAY | empty: the last 30 days · a day, YYYY-MM-DD, in UTC: that day only |
FILE | the file the rows are added to |
import csv
import json
import os
import urllib.request
from datetime import datetime, timezone
MANAGEMENT_KEY = os.environ["OPENDUNES_MANAGEMENT_KEY"]
ADDRESS = "https://opendunes.com/v1/activity"
DAY = os.environ.get("DAY", "")
FILE = "activity.csv"
COLUMNS = [
"date",
"model",
"provider_name",
"endpoint_id",
"requests",
"prompt_tokens",
"completion_tokens",
"usage",
]
url = ADDRESS + "?date=" + DAY if DAY else ADDRESS
request = urllib.request.Request(
url,
headers={"Authorization": "Bearer " + MANAGEMENT_KEY},
)
with urllib.request.urlopen(request) as response:
rows = json.load(response)["data"]
today = datetime.now(timezone.utc).strftime("%Y-%m-%d")
rows = [row for row in rows if row["date"] < today]
new_file = not os.path.exists(FILE)
with open(FILE, "a", newline="") as out:
writer = csv.DictWriter(out, fieldnames=COLUMNS, extrasaction="ignore")
if new_file:
writer.writeheader()
writer.writerows(rows)
print(len(rows), "rows written to", FILE)Each column is a field of the route, under its own name, and usage is the spend in DA. Usage history says what every field holds.
The samples leave out today's rows. A day's totals grow until it ends in UTC, so the file keeps finished days only.
3. Keep a record past 30 days
The usage history reaches 30 days back. To keep more, run the export once a day, after midnight UTC, with DAY set to yesterday: the file grows by one day each run.
On a Linux server whose clock is set to UTC, one crontab line runs the Python sample, saved as export.py, at 00:05:
5 0 * * * cd ~/export && DAY=$(date -u -d yesterday +\%F) python3 export.pyA day exported twice is written twice. Run the first export with DAY empty, then only the daily run.
Reading your usage history is free: the daily run costs nothing. What each row's requests cost, and how to read one request's charge, is in Usage accounting.
Last modified on