OpenAI Usage API api_key_id: Reconcile Tokens and Costs by Key
OpenAI Usage API api_key_id grouping solves a practical reporting gap: I can see which API key produced completion-token activity and which key accumulated cost. The tricky part is not making the two requests. It is joining their daily buckets without dropping unattributed or unmatched data. I want a reconciliation report to expose gaps, not smooth them over. A missing cost row, a cost-only row,…
The OpenAI Usage API now provides API-key filtering and grouping capabilities for the usage and cost APIs, which helps reconcile token usage and costs. However, the two responses are not identical datasets, as the completions usage endpoint can have a null api_key_id while the costs endpoint returns monetary amounts with a nullable API-key dimension.
To reconcile the data accurately, a full-outer join is recommended, keyed by (start_time, end_time, api_key_id). This ensures that all rows are retained, even if there is a mismatch between usage and cost data for a specific API key. The implementation loads all pages from both APIs, creates separate indexes for usage and cost, unions their keys, and assigns a status to every row based on whether it is matched, usage-only, or cost-only.
This approach prevents the hiding of unattributed or unmatched data and provides a transparent reconciliation report.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.