diff --git a/.changeset/lucky-needles-change.md b/.changeset/lucky-needles-change.md new file mode 100644 index 00000000..f50167b7 --- /dev/null +++ b/.changeset/lucky-needles-change.md @@ -0,0 +1,7 @@ +--- +"@empiricalrun/ai": patch +"@empiricalrun/cli": patch +"@empiricalrun/core": patch +--- + +fix: add retry logs for model api calls diff --git a/packages/ai/src/providers/anthropic/index.ts b/packages/ai/src/providers/anthropic/index.ts index af7c319c..a994a3a3 100644 --- a/packages/ai/src/providers/anthropic/index.ts +++ b/packages/ai/src/providers/anthropic/index.ts @@ -69,7 +69,7 @@ const createChatCompletion: ICreateChatCompletion = async (body) => { try { const startedAt = Date.now(); const response = await promiseRetry( - (retry) => { + (retry, attempt) => { return anthropic.messages .create({ model: canonicalModelName(model), @@ -92,6 +92,9 @@ const createChatCompletion: ICreateChatCompletion = async (body) => { err instanceof Anthropic.APIConnectionTimeoutError || err instanceof Anthropic.InternalServerError ) { + console.warn( + `Retrying request for anthropic model: ${body.model}. Retry attempt: ${attempt}`, + ); retry(err); throw err; } diff --git a/packages/ai/src/providers/azure-openai/index.ts b/packages/ai/src/providers/azure-openai/index.ts index 7fa48d69..5f5caab7 100644 --- a/packages/ai/src/providers/azure-openai/index.ts +++ b/packages/ai/src/providers/azure-openai/index.ts @@ -39,8 +39,8 @@ const createChatCompletion: ICreateChatCompletion = async ( maxRetries: 5, shouldRetry: async (resp, retryCount) => { if (resp instanceof Response) { - console.log( - `Retrying request for azure-openai model: ${body.model}. Retry count: ${retryCount}`, + console.warn( + `Retrying request for azure-openai model: ${body.model}. Retry attempt: ${retryCount}`, ); if (resp.status === 429) { requestStartTime = new Date().getTime(); diff --git a/packages/ai/src/providers/fireworks/index.ts b/packages/ai/src/providers/fireworks/index.ts index 02a97e4c..d0cc1f11 100644 --- a/packages/ai/src/providers/fireworks/index.ts +++ b/packages/ai/src/providers/fireworks/index.ts @@ -40,7 +40,7 @@ const createChatCompletion: ICreateChatCompletion = async (body) => { try { const startedAt = Date.now(); const completion = await promiseRetry( - (retry) => { + (retry, attempt) => { return fetch("https://api.fireworks.ai/inference/v1/chat/completions", { method: "POST", headers: { @@ -64,6 +64,9 @@ const createChatCompletion: ICreateChatCompletion = async (body) => { AIErrorEnum.RATE_LIMITED, "Fireworks API rate limit reached", ); + console.warn( + `Retrying request for fireworks model: ${body.model}. Got response status code ${response.status}. Retry attempt: ${attempt}`, + ); retry(err); throw err; } diff --git a/packages/ai/src/providers/google/index.ts b/packages/ai/src/providers/google/index.ts index 51bff46b..bcbdcafb 100644 --- a/packages/ai/src/providers/google/index.ts +++ b/packages/ai/src/providers/google/index.ts @@ -90,7 +90,7 @@ const createChatCompletion: ICreateChatCompletion = async (body) => { try { const startedAt = Date.now(); const completion = await promiseRetry( - (retry) => { + (retry, attempt) => { return modelInstance .startChat({ // @ts-ignore same as above @@ -101,6 +101,9 @@ const createChatCompletion: ICreateChatCompletion = async (body) => { .catch((err: Error) => { // TODO: Replace with instanceof checks when the Gemini SDK exports errors if (err.message.includes("[429 Too Many Requests]")) { + console.warn( + `Retrying request for google model: ${body.model}. Retry attempt: ${attempt}`, + ); retry(err); } throw err; diff --git a/packages/ai/src/providers/openai/index.ts b/packages/ai/src/providers/openai/index.ts index d0af5d78..189d37f3 100644 --- a/packages/ai/src/providers/openai/index.ts +++ b/packages/ai/src/providers/openai/index.ts @@ -33,7 +33,7 @@ const createChatCompletion: ICreateChatCompletion = async (body) => { try { const startedAt = Date.now(); const completions = await promiseRetry( - (retry) => { + (retry, attempt) => { return openai.chat.completions.create(body).catch((err) => { if ( err instanceof OpenAI.RateLimitError && @@ -46,6 +46,9 @@ const createChatCompletion: ICreateChatCompletion = async (body) => { err instanceof OpenAI.APIConnectionTimeoutError || err instanceof OpenAI.InternalServerError ) { + console.warn( + `Retrying request for openai model: ${body.model}. Retry attempt: ${attempt}`, + ); retry(err); throw err; } @@ -158,7 +161,7 @@ const runAssistant: ICreateAndRunAssistantThread = async (body) => { return asstRunResp; })().catch((err: any) => { if ((err.message as string).includes("server_error")) { - console.log( + console.warn( `Retrying request due to server error (attempt ${attempt})`, ); requestStartTime = Date.now();