openai
azure_bootstrap.openai
¶
SDK-agnostic Azure OpenAI / Anthropic usage tracker.
Records tokens + cost per deployment in three sliding windows (60s, 60m,
24h). Optional soft TPM cap via acquire(). Threshold-based cost alerts
via check_thresholds_and_alert(). Pricing table includes both OpenAI
and Anthropic defaults; apps override per-deployment via env vars or
register_pricing(...).
Classes:
| Name | Description |
|---|---|
AiUsageTracker |
Class facade around the module-level singleton. |
Functions:
| Name | Description |
|---|---|
record_usage |
Record one call. Best-effort, never raises. |
record_rate_limit_event |
Mark a rate-limit-hit event + fire an ERROR alert (deduped). |
acquire |
Soft TPM cap. No-op when |
usage_snapshot |
Returns by_deployment + totals with windowed metrics. |
check_thresholds_and_alert |
Compare windowed metrics against env thresholds; fire alerts on breach. |
reset_state |
Test-only. Refuses unless AZURE_BOOTSTRAP_ALLOW_RESET=1. |
AiUsageTracker
¶
Class facade around the module-level singleton.
Mostly here for callers that prefer to inject a tracker instance; the module-level functions are the canonical entry points.
record_usage
¶
Record one call. Best-effort, never raises.
Source code in azure_bootstrap/openai/__init__.py
record_rate_limit_event
¶
Mark a rate-limit-hit event + fire an ERROR alert (deduped).
Source code in azure_bootstrap/openai/__init__.py
acquire
¶
Soft TPM cap. No-op when AI_TPM_LIMIT is unset/0.
On max-wait reached: fires CRITICAL alert and lets the call through — a slow, billed call beats a stranded customer.
Source code in azure_bootstrap/openai/__init__.py
usage_snapshot
¶
Returns by_deployment + totals with windowed metrics.
Source code in azure_bootstrap/openai/__init__.py
check_thresholds_and_alert
¶
Compare windowed metrics against env thresholds; fire alerts on breach.
Designed to run every 10 minutes via APScheduler.
Source code in azure_bootstrap/openai/__init__.py
reset_state
¶
Test-only. Refuses unless AZURE_BOOTSTRAP_ALLOW_RESET=1.