I’m using the free tier api(5$ limit) and it it not used for a single time, when running my app it gives this error even though I have quota.
OpenAI API response: {
error: {
message: ‘You exceeded your current quota, please check
your plan and billing details. For more information on this
error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors.’,
type: ‘insufficient_quota’,
param: null,
code: ‘insufficient_quota’
}
}
My code:
import { OpenAIApi, Configuration } from “openai-edge”;
const config = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(config);
export async function getEmbeddings(text: string) {
try {
const response = await openai.createEmbedding({
model: “text-embedding-ada-002”,
input: text.replace(/\n/g, " "),
});
const result = await response.json();
console.log("OpenAI API response:", result);
// Check if result.data is defined and has at least one element
if (!result.data || result.data.length === 0) {
throw new Error("No embeddings returned from OpenAI API");
}
return result.data[0].embedding as number[];
} catch (error: any) {
if (error.code === ‘insufficient_quota’) {
console.error(‘Quota exceeded. Please check your plan and billing details.’);
// Optionally, notify the user or admin
} else {
console.error(‘Error calling OpenAI embeddings API:’, error.message);
}
throw error; // Re-throw the error after logging
}
}