How to track API usage and cost by API key?

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.

43 Likes

Following, I am looking for the same information.

7 Likes

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.

5 Likes

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

4 Likes

Same question. #OpenAITEAM

5 Likes

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?

4 Likes

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

3 Likes

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!
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.)

4 Likes

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

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

3 Likes

Same question here, would be really usefull.

Would love to have usage by API key too.

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.

3 Likes

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

+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.

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

How to view result only for a specific api key?

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

2 Likes

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

1 Like

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

1 Like