SPB Git forge

spb/doc-api

Public
2commits 1branches 0releases
15.7 MBsize
maindefault branch
13 days agolast push
Python 88.3% TypeScript 7.6% Shell 4.1%
1.6 KB · 25 lines shellscript
Raw Blame History
1#!/usr/bin/env bash2# Usage & costs cookbook (Admin API, read-only): daily completions usage grouped by model, and daily costs by line item.3# STATUS: ACCOUNT_RESTRICTED — verified 2026-09-18 with our project key: 403 "Missing scopes: api.usage.read".4# Needs OPENAI_ADMIN_KEY. Times are Unix seconds; bucket_width 1m|1h|1d (usage), 1d only (costs);5# usage limit: <=  31 buckets for 1d, 168 for 1h, 1440 for 1m; paginate with `page` = response.next_page.6set -euo pipefail7source "$(dirname "$0")/../../../scripts/lib.sh"8KEY="${OPENAI_ADMIN_KEY:-$OPENAI_API_KEY}"9START=$(( $(date +%s) - 7*86400 ))1011q() { # q PATH12  local out; out="$(mktemp)"13  local st; st="$(curl -sS -G -o "$out" -w '%{http_code}' "https://api.openai.com$1" -H "Authorization: Bearer $KEY")"14  log_request openai GET "${1%%\?*}" "$st" 0 "admin example usage_costs.sh"15  printf '%s %s\n' "$st" "$1"; mask < "$out" | head -c 800; echo; rm -f "$out"16}17# 1. Completions usage per day, grouped by model (+ service_tier), 7 buckets18q "/v1/organization/usage/completions?start_time=$START&bucket_width=1d&group_by=model&group_by=service_tier&limit=7"19# 2. Same but for a single project and only Batch traffic20# q "/v1/organization/usage/completions?start_time=$START&bucket_width=1d&project_ids=proj_XXX&batch=true&limit=7"21# 3. Embeddings + images + audio usage families exist too:22q "/v1/organization/usage/embeddings?start_time=$START&bucket_width=1d&group_by=model&limit=7"23# 4. Costs (USD) per day grouped by line item and project; only bucket_width=1d is supported24q "/v1/organization/costs?start_time=$START&bucket_width=1d&group_by=line_item&group_by=project_id&limit=7"25