# How to track API usage and cost by API key?

**URL:** <https://community.openai.com/t/how-to-track-api-usage-and-cost-by-api-key/174145>\
**Category:** API\
**Tags:** api-keys, api-usage, cost\
**Created:** [April 23, 2023, 9:44am UTC](https://community.openai.com/t/how-to-track-api-usage-and-cost-by-api-key/174145 "2023-04-23T09:44:05Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![bzhang](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/bzhang/32/115603_2.png) [@bzhang](https://community.openai.com/u/bzhang)\
**Post date:** [April 23, 2023, 9:44am UTC](https://community.openai.com/t/how-to-track-api-usage-and-cost-by-api-key/174145/1 "2023-04-23T09:44:05Z")

</div>

My company will use different API key for different AI-enabled applications/features/engineering teams. We’d like to track usage and cost by API key. Is that a way to track API usage and cost by API keys?

It seems right now the usage tracking is by organization only.

It would be fine if we need to track it as middleware when hitting OpenAI API. Just want to make sure I am not missing a hidden feature.

---

<div class="post-metadata">

**Author:** ![caracusack](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/caracusack/32/117164_2.png) [@caracusack](https://community.openai.com/u/caracusack)\
**Post date:** [April 25, 2023, 6:36pm UTC](https://community.openai.com/t/how-to-track-api-usage-and-cost-by-api-key/174145/2 "2023-04-25T18:36:21Z")

</div>

Following, I am looking for the same information.

---

<div class="post-metadata">

**Author:** ![omk](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/omk/32/128963_2.png) [@omk](https://community.openai.com/u/omk)\
**Post date:** [May 12, 2023, 9:56am UTC](https://community.openai.com/t/how-to-track-api-usage-and-cost-by-api-key/174145/3 "2023-05-12T09:56:03Z")

</div>

I have the same requirement. There are multiple applications that are using the key and tracking usage per key would help identify utilization across apps.

---

<div class="post-metadata">

**Author:** ![jason17](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/jason17/32/131628_2.png) [@jason17](https://community.openai.com/u/jason17)\
**Post date:** [May 15, 2023, 12:56pm UTC](https://community.openai.com/t/how-to-track-api-usage-and-cost-by-api-key/174145/4 "2023-05-15T12:56:01Z")

</div>

I am looking for the same info. Did anyone get an answer?

---

<div class="post-metadata">

**Author:** ![jiheewon0101](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/jiheewon0101/32/133986_2.png) [@jiheewon0101](https://community.openai.com/u/jiheewon0101)\
**Post date:** [May 18, 2023, 2:07am UTC](https://community.openai.com/t/how-to-track-api-usage-and-cost-by-api-key/174145/5 "2023-05-18T02:07:56Z")

</div>

Same question. #OpenAITEAM

---

<div class="post-metadata">

**Author:** ![FP](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/fp/32/45026_2.png) [@FP](https://community.openai.com/u/FP)\
**Post date:** [May 26, 2023, 2:34am UTC](https://community.openai.com/t/how-to-track-api-usage-and-cost-by-api-key/174145/6 "2023-05-26T02:34:43Z")

</div>

Same need, same question.

What is the material benefit or use case of having different api keys per account if usage for each can’t be tracked?

---

<div class="post-metadata">

**Author:** ![juan\_olano](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/juan_olano/32/449154_2.png) [@juan\_olano](https://community.openai.com/u/juan_olano)\
**Post date:** [May 28, 2023, 1:11pm UTC](https://community.openai.com/t/how-to-track-api-usage-and-cost-by-api-key/174145/7 "2023-05-28T13:11:23Z")

</div>

I have the same need - see usage by API Key.

---

<div class="post-metadata">

**Author:** ![jwatte](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/jwatte/32/127705_2.png) [@jwatte](https://community.openai.com/u/jwatte)\
**Post date:** [May 28, 2023, 4:45pm UTC](https://community.openai.com/t/how-to-track-api-usage-and-cost-by-api-key/174145/8 "2023-05-28T16:45:52Z")

</div>

What we do is log a token identifier, and the response model name and tokens used, for each API request, into our log/metrics/tracing/observability system.  
We then sum this up in a worksheet with a simple query.  
Observe Inc has a simple OpenAI integration: [What's New: OpenAI App!](https://www.observeinc.com/blog/whats-new-openai-app/)  
However, you’ll still need to add instrumentation to your own code to log out the model/token counts, and write a query to parse that back in and sum it up.  
(Observe is an enterprise software suite; there’s no free trial or open source version; if your volumes are low, doing the summary in Python or SQL or somesuch might be tractable as an alternative.)

---

<div class="post-metadata">

**Author:** ![Robertsmania](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/robertsmania/32/42181_2.png) [@Robertsmania](https://community.openai.com/u/Robertsmania)\
**Post date:** [May 28, 2023, 5:03pm UTC](https://community.openai.com/t/how-to-track-api-usage-and-cost-by-api-key/174145/9 "2023-05-28T17:03:32Z")

</div>

Our application uses the stream response method, for which the client does not get token counts in the reply.

---

<div class="post-metadata">

**Author:** ![nguyenanhdon.qn](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/nguyenanhdon.qn/32/45805_2.png) [@nguyenanhdon.qn](https://community.openai.com/u/nguyenanhdon.qn)\
**Post date:** [May 30, 2023, 3:28am UTC](https://community.openai.com/t/how-to-track-api-usage-and-cost-by-api-key/174145/10 "2023-05-30T03:28:01Z")

</div>

You can use the command req Get to  
`https://api.openai.com/v1/dashboard/billing/usage?start_date=2023-05-01&end_date=2023-05-31`

The result returned at the variable `total_usage/100` is the cost of credit you have used from start\_date to end\_date

 ![image](https://us1.discourse-cdn.com/openai1/original/3X/8/b/8bd877606b54fb813114569d8cc66cc7c089b966.png)

---

<div class="post-metadata">

**Author:** ![bp\_tpg](https://avatars.discourse-cdn.com/v4/letter/b/90ced4/32.png) [@bp\_tpg](https://community.openai.com/u/bp_tpg)\
**Post date:** [June 7, 2023, 11:30am UTC](https://community.openai.com/t/how-to-track-api-usage-and-cost-by-api-key/174145/11 "2023-06-07T11:30:48Z")

</div>

Same question here, would be really usefull.

---

<div class="post-metadata">

**Author:** ![rsnr](https://avatars.discourse-cdn.com/v4/letter/r/7c8e57/32.png) [@rsnr](https://community.openai.com/u/rsnr)\
**Post date:** [June 12, 2023, 4:52am UTC](https://community.openai.com/t/how-to-track-api-usage-and-cost-by-api-key/174145/12 "2023-06-12T04:52:59Z")

</div>

Would love to have usage by API key too.

---

<div class="post-metadata">

**Author:** ![SomebodySysop](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/somebodysysop/32/17613_2.png) [@SomebodySysop](https://community.openai.com/u/SomebodySysop)\
**Post date:** [June 12, 2023, 9:41am UTC](https://community.openai.com/t/how-to-track-api-usage-and-cost-by-api-key/174145/13 "2023-06-12T09:41:46Z")

</div>

> [@jwatte](#):
>
> What we do is log a token identifier, and the response model name and tokens used, for each API request, into our log/metrics/tracing/observability system.

Just finished doing the same exact thing, using SQL. OpenAI sends you the token usage on every query, so I just record it, along with user, model, date, IP address and a couple other parameters. This best and easiest way to do it, IMHO.

---

<div class="post-metadata">

**Author:** ![davide.giannuzzi](https://avatars.discourse-cdn.com/v4/letter/d/b38774/32.png) [@davide.giannuzzi](https://community.openai.com/u/davide.giannuzzi)\
**Post date:** [June 12, 2023, 11:20am UTC](https://community.openai.com/t/how-to-track-api-usage-and-cost-by-api-key/174145/14 "2023-06-12T11:20:35Z")

</div>

Same problem here. A tracker by api-key would be very useful, otherwise we are losing the sense of having multiple api-keys for the same organization

---

<div class="post-metadata">

**Author:** ![Enrico](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/enrico/32/1785_2.png) [@Enrico](https://community.openai.com/u/Enrico)\
**Post date:** [June 12, 2023, 11:43am UTC](https://community.openai.com/t/how-to-track-api-usage-and-cost-by-api-key/174145/15 "2023-06-12T11:43:45Z")

</div>

+1 for tokens/cost breakdown per API key (which sums up to the total).

Especially important for chat streaming responses which don’t include a token count, and puts the burden to all downstream apps to re-tokenize the responses and maintain databases for what’s already an upstream state.

---

<div class="post-metadata">

**Author:** ![AI-Roguelite](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/ai-roguelite/32/146592_2.png) [@AI-Roguelite](https://community.openai.com/u/AI-Roguelite)\
**Post date:** [June 15, 2023, 5:41am UTC](https://community.openai.com/t/how-to-track-api-usage-and-cost-by-api-key/174145/16 "2023-06-15T05:41:49Z")

</div>

Since they don’t have this feature, is the only easy way to make a separate account?

---

<div class="post-metadata">

**Author:** ![AI-Roguelite](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/ai-roguelite/32/146592_2.png) [@AI-Roguelite](https://community.openai.com/u/AI-Roguelite)\
**Post date:** [June 15, 2023, 5:44am UTC](https://community.openai.com/t/how-to-track-api-usage-and-cost-by-api-key/174145/17 "2023-06-15T05:44:12Z")

</div>

How to view result only for a specific api key?

---

<div class="post-metadata">

**Author:** ![nguyenanhdon.qn](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/nguyenanhdon.qn/32/45805_2.png) [@nguyenanhdon.qn](https://community.openai.com/u/nguyenanhdon.qn)\
**Post date:** [June 15, 2023, 11:17am UTC](https://community.openai.com/t/how-to-track-api-usage-and-cost-by-api-key/174145/18 "2023-06-15T11:17:20Z")

</div>

> [@AI-Roguelite](#):
>
> Since they don’t have this feature, is the only easy way to make a separate account?

In the header there is an Authorization variable which is Bearer sk-xxx with sk-xxx as APIKEY

---

<div class="post-metadata">

**Author:** ![AI-Roguelite](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/ai-roguelite/32/146592_2.png) [@AI-Roguelite](https://community.openai.com/u/AI-Roguelite)\
**Post date:** [June 15, 2023, 3:58pm UTC](https://community.openai.com/t/how-to-track-api-usage-and-cost-by-api-key/174145/19 "2023-06-15T15:58:58Z")

</div>

Ah I didn’t see that. That will probably work well enough for my case. Thanks!

---

<div class="post-metadata">

**Author:** ![jochenschultz](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/jochenschultz/32/772274_2.png) [@jochenschultz](https://community.openai.com/u/jochenschultz)\
**Post date:** [June 29, 2023, 7:40pm UTC](https://community.openai.com/t/how-to-track-api-usage-and-cost-by-api-key/174145/21 "2023-06-29T19:40:43Z")

</div>

If that’s your real API key, you should delete it asap. Hope that’s not too late.

[Next page](https://community.openai.com/t/how-to-track-api-usage-and-cost-by-api-key/174145.md?page=2)
