---
title: How do you track OpenAI API costs by project with the Usage and Costs APIs?
url: https://opencontroller.lyzr.com/blog/track-openai-api-costs-by-project
published: 2026-11-13
updated: 2026-11-13
author: Lyzr
---

# How do you track OpenAI API costs by project with the Usage and Costs APIs?

**Call OpenAI's organisation Costs API with an admin key to get daily spend grouped by project, API key and line item, and the Usage API to get token counts by project, key and model. Give each agent its own project so the project becomes the cost boundary. The Costs API is the one to reconcile against the invoice; the Usage API is for detail.**

Finance wants a monthly number per team. Platform teams want to know which agent burned through tokens last Tuesday. OpenAI's organisation endpoints can answer both, if your projects and keys are set up with attribution in mind.

## Which endpoints do you need?

| Endpoint | Returns | Time buckets | Group by |
|---|---|---|---|
| `GET /v1/organization/costs` | Billed amounts with currency | `1d` only | `project_id`, `user_id`, `line_item`, `api_key_id`, `api_source` |
| `GET /v1/organization/usage/completions` | Input, output and cached tokens, request counts | `1m`, `1h`, `1d` | `project_id`, `user_id`, `api_key_id`, `model`, `batch`, `service_tier`, `api_source` |
| Other usage endpoints | Embeddings, images, audio speech and transcription, vector stores, code interpreter, and more | Varies | Similar keys |

Both families require an **admin API key**. Normal project keys cannot read organisation-wide usage. Keep the admin key in a secrets manager, restrict it to the reporting job, and rotate it on a schedule.

## How do you pull costs by project?

A daily job is enough for most teams:

1. Call `/v1/organization/costs` with `start_time` (Unix seconds), `bucket_width=1d`, `group_by=project_id,line_item`.
2. Page through results with `next_page` while `has_more` is true. `limit` accepts 1 to 180 buckets; the default is 7.
3. Store each row: date, project ID, line item, amount, currency.
4. Join project IDs to your registry to add agent name, owner and cost centre.
5. Publish a daily and month-to-date view per team.

Filters narrow the pull: `project_ids` for specific projects, `api_key_ids` for specific keys, and `line_items` for exact line item names.

## How do you get token-level detail per agent?

The Usage API answers questions the Costs API cannot:

- **Which model?** Group completions usage by `model` to see whether an agent moved to a more expensive one.
- **Which key?** Group by `api_key_id` when several agents share a project.
- **Cache effectiveness.** Compare `input_cached_tokens` with `input_tokens`. A falling ratio after a prompt change usually means higher cost.
- **Batch versus synchronous.** Group by `batch` to confirm overnight work really goes through the cheaper batch path.
- **Hourly spikes.** Use `bucket_width=1h` or `1m` to find runaway loops; the Costs API cannot show this.

To turn tokens into money, multiply by your contracted prices per model and token type. Expect small differences from the Costs API, which reflects billed amounts.

## How should you structure projects and keys for attribution?

| Pattern | Attribution quality | When to use |
|---|---|---|
| One project per agent | Best: cost maps directly to the agent | Production agents with clear owners |
| One project per team, one key per agent | Good: costs by project, detail by key | Teams running many small agents |
| One shared project and key | Poor: no split without a gateway | Avoid for production |

A short checklist:

1. Name projects with the registry ID, not a free-text label.
2. Never share an API key between two agents.
3. Set a budget and alert on each project; our guide to [budgets and alerts for agent spend](/blog/set-budgets-and-alerts-for-ai-agent-spend) has thresholds.
4. Revoke keys with no usage for 30 days.
5. Reconcile monthly: the sum of daily Costs API amounts should match the invoice; investigate any gap.

## Where does this approach fall short?

- **Daily grain for money.** Costs come only in daily buckets.
- **No task context.** OpenAI sees requests, not business tasks. To report [cost per task](/blog/measure-cost-per-task-for-ai-agents), you need your own request tagging.
- **One provider.** Agents that also call Anthropic, Bedrock, Azure OpenAI or Vertex AI need the same exercise on each, then a common schema. Our guide to [attributing LLM costs to agents](/blog/how-to-attribute-llm-costs-to-ai-agents) covers the cross-provider model.
- **Key sprawl.** Keys created outside your process do not map to an owner until someone finds them.

## How Opencontroller does this

Opencontroller auto-discovers agents, models and API keys across AWS, Azure and Google Cloud and connects to OpenAI Enterprise workspaces, so unknown keys surface with an owner to assign. Its LLM Gateway (Lyzr Gateway) exposes OpenAI-compatible endpoints and attributes cost per agent and per version across providers, with routing, fallback and retry, and caching. In an October 2026 benchmark under stated conditions, it added under 0.7 ms p95 per in-gateway routing method; see the [benchmark](/llm-gateway-benchmark) and the [gateway comparison pillar](/blog/llm-gateway-vs-agent-gateway-vs-control-plane), or [book a demo](https://www.lyzr.ai/book-demo/).

## Frequently asked questions

**Why don't Usage API totals match the Costs API exactly?**
The Usage API reports tokens, and you apply prices yourself. The Costs API reports billed amounts, which is what the invoice reflects.

**Can I group costs by API key?**
Yes. The Costs API accepts `api_key_id` in `group_by` and `api_key_ids` as a filter.

**How far back can one request go?**
Up to 180 daily buckets per request with the Costs API; page with `next_page` for more.

## Key facts

OpenAI's organisation Costs API (GET /v1/organization/costs) returns daily cost buckets (bucket_width 1d only, limit 1-180, default 7) groupable by project_id, user_id, line_item, api_key_id and api_source, filterable by project_ids, api_key_ids and line_items. The Usage API (for example GET /v1/organization/usage/completions) returns tokens and request counts in 1m, 1h or 1d buckets, groupable by project_id, user_id, api_key_id, model, batch, service_tier and api_source. Both require an admin API key. Giving each agent its own project makes the project the cost boundary. Opencontroller by Lyzr discovers models and API keys and its LLM Gateway attributes cost per agent and per version. Pricing starts from $250K per deployment per year.

## Related

- [How do you attribute LLM costs to individual AI agents?](https://opencontroller.lyzr.com/blog/how-to-attribute-llm-costs-to-ai-agents.md)
- [How do you set budgets and alerts for AI agent spend?](https://opencontroller.lyzr.com/blog/set-budgets-and-alerts-for-ai-agent-spend.md)
- [How do you measure cost per task for an AI agent?](https://opencontroller.lyzr.com/blog/measure-cost-per-task-for-ai-agents.md)
- [More on Gateways and cost](https://opencontroller.lyzr.com/blog/cluster/gateways-and-cost)
