OpenAI API Keys: Setup, Billing, Spending Limits, and Leak Prevention
Create a project-scoped key, add credits, and verify your balance. Use budget alerts for notification and an application-side cutoff to control spending. If a key leaks, revoke it immediately.
ok589 Editorial Team4 min read

1. Select the right project before creating a key
- Sign in to the OpenAI API platform with your own account and check the organization and project. Use separate projects for testing and production so a debugging script cannot use production credentials. If you cannot create a key or access billing, ask the organization owner to check your permissions. Do not borrow someone else’s key.
- In the target project, open API keys and select Create new secret key. Name the key for its purpose and environment. Select Restricted and grant access only to the endpoints you need. Save the key in a password manager immediately; you cannot view the full key again.
- Set
OPENAI_API_KEYas a server-side environment variable. Follow the official documentation to send a short test request. Confirm it succeeds before starting a batch job. Do not paste the key into an online testing tool.

2. Check the organization before and after adding credits
- ChatGPT subscriptions and API usage are billed separately. Subscription payments do not give you API credits. Open Billing in the selected organization and check its name, payment method, and balance. Do not use the ChatGPT subscription page to add API credits.
- If prepaid billing is available, select the option to buy credits. Check the minimum purchase, currency, and credit expiration shown on the page before paying. For a first purchase, you can leave auto-recharge off. If you later enable it to keep jobs running, check the balance that triggers a recharge, the amount added each time, and the monthly auto-recharge limit.
- After payment, check both the transaction record and the available balance. If you were charged but the balance has not updated, allow time for the payment to post rather than paying again. If the discrepancy persists, contact the official Help Center with the transaction time, amount, and receipt. Do not send your card number or API key.

3. Distinguish budget alerts from spending cutoffs
| Control | What happens at the limit |
|---|---|
| Monthly project budget | Triggers a notification. Requests can still succeed after the budget is exceeded. |
| Monthly auto-recharge limit | Caps automatic credit purchases, not spending from the existing balance. Manual purchases do not count toward this limit. |
| Application-side spending cutoff | Blocks new requests routed through the cutoff. It does not cancel requests already in progress. |
- Under Limits in project settings, enter a monthly budget and set alerts for both an early warning and the budget threshold. Confirm the person on duty receives the emails. The project budget is not a hard spending cap. Do not rely on a zero balance for a precise cutoff either: usage charges can post late.
- To enforce a monthly budget, route every job through one backend and maintain a monthly spending ledger. Before sending a request, reserve its estimated cost using the model’s rates, input size, and maximum output size. Reject the request if the remaining budget cannot cover it. Make each reservation atomic so concurrent requests cannot claim the same funds. Reconcile the reservation with actual usage afterward, and run every retry through the same check.

4. Prevent leaks and revoke exposed keys immediately
- Do not embed keys in web frontends, mobile apps, or browser extensions. Only the backend should read the key. Disable request-header logging, exclude local configuration files from version control, and inspect staged changes before committing.
- If you expose a key or spot unfamiliar usage, revoke the key immediately. Deleting a post or removing a file from a repository does not invalidate the key. Find and fix the source of the leak, create a replacement key, and update the deployment. Review usage and billing for the affected project and time period.
- Enable two-factor authentication and store backup recovery codes offline. See two-factor authentication tools. Two-factor authentication protects account sign-in; it does not stop someone from using a leaked API key.
5. Pre-launch checklist
- Verify the project, key permissions, available credit balance, and auto-recharge settings.
- Set a low test threshold in the application and confirm an over-budget request is rejected. An alert email alone does not prove the cutoff works.
- Confirm someone receives the alerts, old keys have been revoked, and logs contain no credentials.