# OpenAI usage export
Source: https://greenpixie.com/docs/guides/ai-usage-data/openai

How to pull token counts and cost from the OpenAI Usage and Costs APIs.

OpenAI reports API usage in the Usage Dashboard and through the organisation Usage and Costs APIs. The APIs give token counts per model, project, user and key at up to one-minute resolution, so use them where you can. The dashboard is the fallback and has an export of its own.

## Usage API and Costs API [#usage-api-and-costs-api]

You need an Admin API key for the organisation, created by an organisation owner in the organisation settings. Usage endpoints exist for each capability; completions covers the Responses and Chat Completions traffic that most AI workloads produce.

1. Create an Admin API key and store it in your secrets manager.
2. Call `GET https://api.openai.com/v1/organization/usage/completions` with `start_time` and `end_time` as Unix seconds, `bucket_width=1d`, and `group_by[]=model`. Add `project_id`, `user_id` or `api_key_id` to the grouping if you want attribution.
3. Set `limit` to the number of buckets you want, up to 31 at daily width, and page with `next_page`.
4. Repeat for the other usage endpoints you use, then call `GET https://api.openai.com/v1/organization/costs` for spend.

```bash
curl "https://api.openai.com/v1/organization/usage/completions?\
start_time=1790812800&end_time=1793491200&bucket_width=1d&group_by[]=model&limit=31" \
  -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \
  -H "Content-Type: application/json"
```

Each bucket's results carry `input_tokens`, `input_cached_tokens`, `input_uncached_tokens`, `output_tokens`, audio and image token counts, `num_model_requests`, and the `model`, `project_id`, `user_id`, `api_key_id`, `batch` and `service_tier` you grouped by. The other usage endpoints are `embeddings`, `images`, `audio_speeches`, `audio_transcriptions`, `moderations`, `vector_stores`, `code_interpreter_sessions`, `file_search_calls` and `web_search_calls`, under the same `/v1/organization/usage/` path. The Costs API returns `amount.value` in US dollars per `line_item`, daily only, up to 180 buckets per call.

## Usage Dashboard export [#usage-dashboard-export]

You must be an organisation owner, or hold the Usage Dashboard permission, to open the dashboard. All data is shown in UTC.

1. Open the [Usage Dashboard](https://platform.openai.com/usage) and select the organisation. Sub-organisations are reported separately, so repeat for each.
2. Use the dashboard's own project selector to choose projects, or clear it to see the whole organisation.
3. Choose the reporting period. There is no limit on the export date range, though long periods may arrive as several files.
4. Export the usage data. Use the cost data for billed amounts, and treat any negative activity rows as adjustments to compare by unit and date rather than as credits.

Under **API capabilities**, **Responses and Chat Completions** lets you filter by user and drill down to one-minute intervals. If Scale Tier is enabled, its subscription cost sits at organisation level under **Spend categories** rather than against a project.

## Check the export worked [#check-the-export-worked]

You should have:

* One bucket per day per model for the full date range from the completions endpoint, with `input_tokens`, `input_cached_tokens` and `output_tokens` populated.
* The same for any other usage endpoints your workloads use, such as embeddings or images.
* A daily costs file for the same range, one per organisation if you run sub-organisations.

## Next steps [#next-steps]

Once you have the export, send it to Greenpixie for enrichment with energy, carbon and water metrics, or enrich it yourself using the API.

- [Enriching your usage data using the API](https://greenpixie.com/docs/guides/enrich-api): Pull the rate card and enrich the data yourself, in your own environment.
- [Connect your data for Greenpixie to enrich it](https://greenpixie.com/docs/guides/connect-data): Grant Greenpixie access to your bucket or container for managed enrichment.

## References [#references]

* [Reviewing API usage and costs](https://help.openai.com/en/articles/10478918-reviewing-api-usage-and-costs)
* [Usage API reference](https://platform.openai.com/docs/api-reference/usage)