CursorでOpenAI APIを使用するために、APIキーを認証しようとしたところ、以下、添付画のエラーが発生しました。
結論、OpenAI API Keyの利用が無料枠のまま使用しており、有料枠への移行と支払い設定が出来ていなかったことが原因でした。
ダイアログに表示されたエラーメッセージ
Invalid OpenAI API Key
Please try again, or run the following command in your terminal to verify that you have not hit your spend limit:
エラーメッセージと同時に示唆されたcurlコマンドを実行してみる。
(API_KEYは実際のものに置き換えます)
curl https://api.openai.com/v1/chat/completions -H "Content-Type: application/json" \
-H "Authorization: Bearer {API_KEY}" -d '{
"messages": [
{
"role": "system",
"content": "You are a test assistant."
},
{
"role": "user",
"content": "Testing. Just say hi and nothing else."
}
],
"model": "gpt-4o-mini"
}'
返却されたレスポンス
{
"error": {
"message": "You exceeded your current quota, please check your plan and billing details.",
"type": "insufficient_quota",
"param": null,
"code": "insufficient_quota"
}
}
レスポンス内容に含まれるエラーから、APIの使用量(クォータ)を超過したため、リクエストが拒否されていることがわかりました。
Open AIアカウントのダッシュボードからLimitsを確認する。
https://platform.openai.com/settings/organization/limits
画像の下半分、Increasing your limitsの箇所より、無料枠アカウントのまま使用していたことがわかりました。
「At least $5 spent on the API since account creation」
の記載より、無料枠から有料枠の利用に移行するためには最低支払い額が5ドル必要で、アカウント作成後にそちらの支払いが確認されない場合、使用可能な機能や利用枠が制限される、もしくは有料機能にアップグレードできない可能性があると解釈し、Buy creditsリンクをクリックして、遷移先のBillingページより、クレジットカードの登録へ進みました。
5ドル分の支払いを追加すると、先ほどのLimitsのページより、現在の使用枠がFree(無料)ではなく、tier 1という有料枠に移行できたことが確認できました。
有料枠への移行が完了したので、もう一度先ほどのcurlを実行してみると以下のようにエラーを含まない有効なレスポンスが返ってきました。
{
"id": "chatcmpl-xxxxxxxxx",
"object": "chat.completion",
"created": 1739262328,
"model": "gpt-4o-mini-2024-07-18",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hi!",
"refusal": null
},
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 26,
"completion_tokens": 3,
"total_tokens": 29,
"prompt_tokens_details": {
"cached_tokens": 0,
"audio_tokens": 0
},
"completion_tokens_details": {
"reasoning_tokens": 0,
"audio_tokens": 0,
"accepted_prediction_tokens": 0,
"rejected_prediction_tokens": 0
}
},
"service_tier": "default",
"system_fingerprint": "fp_1234xxx"
}
Cursorを開き直して、APIキーのVerifyを再度行ったところ無事成功しました。
使用モデルにgpt-4oを選択して回答を出力したところ、CursorアカウントのUsageからPremium modelsのリクエスト回数が期待通りに減っていることも確認ができました。