Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

chore(evals/experiments): support retries via bullmq; set 2 minute LLM call completion timeouts (#4709)

marliessophied54c888cd9f6
diff --git a/packages/shared/src/server/llm/fetchLLMCompletion.ts b/packages/shared/src/server/llm/fetchLLMCompletion.ts
index 7c3f994a92..09668e3c53 100644
--- a/packages/shared/src/server/llm/fetchLLMCompletion.ts
+++ b/packages/shared/src/server/llm/fetchLLMCompletion.ts
@@ -163,7 +163,7 @@ export async function fetchLLMCompletion(
maxTokens: modelParams.max_tokens,
topP: modelParams.top_p,
callbacks: finalCallbacks,
- clientOptions: { maxRetries },
+ clientOptions: { maxRetries, timeout: 1000 * 60 * 2 }, // 2 minutes timeout
});
} else if (modelParams.adapter === LLMAdapter.OpenAI) {
chatModel = new ChatOpenAI({
@@ -178,6 +178,7 @@ export async function fetchLLMCompletion(
configuration: {
baseURL,
},
+ timeout: 1000 * 60 * 2, // 2 minutes timeout
});
} else if (modelParams.adapter === LLMAdapter.Azure) {
chatModel = new ChatOpenAI({
@@ -190,6 +191,7 @@ export async function fetchLLMCompletion(
topP: modelParams.top_p,
callbacks: finalCallbacks,
maxRetries,
+ timeout: 1000 * 60 * 2, // 2 minutes timeout
});
} else if (modelParams.adapter === LLMAdapter.Bedrock) {
const { region } = BedrockConfigSchema.parse(config);
@@ -204,10 +206,13 @@ export async function fetchLLMCompletion(
topP: modelParams.top_p,
callbacks: finalCallbacks,
maxRetries,
+ timeout: 1000 * 60 * 2, // 2 minutes timeout
});
} else if (modelParams.adapter === LLMAdapter.VertexAI) {
const credentials = GCPServiceAccountKeySchema.parse(JSON.parse(apiKey));
+ // Requests time out after 60 seconds for both public and private endpoints by default
+ // Reference: https://cloud.google.com/vertex-ai/docs/predictions/get-online-predictions#send-request
chatModel = new ChatVertexAI({
modelName: modelParams.model,
temperature: modelParams.temperature,
@@ -266,6 +271,7 @@ export async function fetchLLMCompletion(
configuration: {
baseURL,
},
+ timeout: 1000 * 60 * 2, // 2 minutes timeout
})
.pipe(new StringOutputParser())
.invoke(
diff --git a/packages/shared/src/server/redis/evalExecutionQueue.ts b/packages/shared/src/server/redis/evalExecutionQueue.ts
index 95eb4c2b8c..3970d95ba9 100644
--- a/packages/shared/src/server/redis/evalExecutionQueue.ts
+++ b/packages/shared/src/server/redis/evalExecutionQueue.ts
@@ -29,7 +29,7 @@ export class EvalExecutionQueue {
attempts: 10,
backoff: {
type: "exponential",
- delay: 5000,
+ delay: 1000,
},
},
},
diff --git a/packages/shared/src/server/redis/experimentCreateQueue.ts b/packages/shared/src/server/redis/experimentCreateQueue.ts
index 9c8ab14ce2..c28b3d5ed4 100644
--- a/packages/shared/src/server/redis/experimentCreateQueue.ts
+++ b/packages/shared/src/server/redis/experimentCreateQueue.ts
@@ -26,10 +26,10 @@ export class ExperimentCreateQueue {
defaultJobOptions: {
removeOnComplete: true,
removeOnFail: 10_000,
- attempts: 2,
+ attempts: 10,
backoff: {
type: "exponential",
- delay: 5000,
+ delay: 1000,
},
},
},
diff --git a/worker/src/ee/evaluation/evalService.ts b/worker/src/ee/evaluation/evalService.ts
index ec51a36b7e..94e83b788c 100644
--- a/worker/src/ee/evaluation/evalService.ts
+++ b/worker/src/ee/evaluation/evalService.ts
@@ -451,7 +451,7 @@ export const evaluate = async ({
evalScoreSchema,
),
{
- numOfAttempts: 5, // turn off retries as Langchain is doing that for us already.
+ numOfAttempts: 1, // turn off retries as Langchain is doing that for us already.
},
);
diff --git a/worker/src/ee/experiments/experimentService.ts b/worker/src/ee/experiments/experimentService.ts
index c1b809fa37..9ccd33506b 100644
--- a/worker/src/ee/experiments/experimentService.ts
+++ b/worker/src/ee/experiments/experimentService.ts
@@ -12,7 +12,6 @@ import { kyselyPrisma, prisma } from "@langfuse/shared/src/db";
import { ExperimentCreateEventSchema } from "@langfuse/shared/src/server";
import {
ForbiddenError,
- InternalServerError,
InvalidRequestError,
LangfuseNotFoundError,
Prisma,
@@ -148,7 +147,7 @@ export const createExperimentJob = async ({
logger.error(
`Prompt content not in expected format ${prompt_id} not found for project ${projectId}`,
);
- throw new InternalServerError(
+ throw new InvalidRequestError(
`Prompt ${prompt_id} not found in expected format for project ${projectId}`,
);
}
@@ -263,7 +262,7 @@ export const createExperimentJob = async ({
traceParams,
),
{
- numOfAttempts: 5, // turn off retries as Langchain is doing that for us already.
+ numOfAttempts: 1, // turn off retries as Langchain is doing that for us already.
},
);
diff --git a/worker/src/features/utilities.ts b/worker/src/features/utilities.ts
index a4155bb1a7..4e4861edbc 100644
--- a/worker/src/features/utilities.ts
+++ b/worker/src/features/utilities.ts
@@ -37,6 +37,7 @@ export async function callStructuredLLM<T extends ZodSchema>(
traceParams: traceParams
? { ...traceParams, tokenCountDelegate: tokenCount }
: undefined,
+ maxRetries: 1,
});
if (traceParams) {
@@ -75,6 +76,7 @@ export async function callLLM(
traceParams: traceParams
? { ...traceParams, tokenCountDelegate: tokenCount }
: undefined,
+ maxRetries: 1,
});
if (traceParams) {
diff --git a/worker/src/queues/evalQueue.ts b/worker/src/queues/evalQueue.ts
index f1c4b01ad9..b1ee44e3f5 100644
--- a/worker/src/queues/evalQueue.ts
+++ b/worker/src/queues/evalQueue.ts
@@ -1,5 +1,5 @@
import { Job } from "bullmq";
-import { ApiError, BaseError } from "@langfuse/shared";
+import { BaseError } from "@langfuse/shared";
import { kyselyPrisma } from "@langfuse/shared/src/db";
import { sql } from "kysely";
import {
@@ -70,8 +70,7 @@ export const evalJobExecutorQueueProcessor = async (
e.message.includes(
"Please ensure the mapped data exists and consider extending the job delay.",
)
- ) &&
- !(e instanceof ApiError) // API errors are expected (e.g. wrong API key or rate limit or invalid return data)
+ )
) {
traceException(e);
logger.error(
diff --git a/worker/src/queues/experimentQueue.ts b/worker/src/queues/experimentQueue.ts
index 55db3d54f3..c0de104e64 100644
--- a/worker/src/queues/experimentQueue.ts
+++ b/worker/src/queues/experimentQueue.ts
@@ -6,6 +6,11 @@ import {
traceException,
} from "@langfuse/shared/src/server";
import { createExperimentJob } from "../ee/experiments/experimentService";
+import {
+ ForbiddenError,
+ InvalidRequestError,
+ LangfuseNotFoundError,
+} from "@langfuse/shared";
export const experimentCreateQueueProcessor = async (
job: Job<TQueueJobTypes[QueueName.ExperimentCreate]>,
@@ -21,6 +26,16 @@ export const experimentCreateQueueProcessor = async (
});
return true;
} catch (e) {
+ if (
+ e instanceof ForbiddenError ||
+ e instanceof InvalidRequestError ||
+ e instanceof LangfuseNotFoundError
+ ) {
+ logger.info("Failed to process experiment create job", e);
+ // LFE-3174: improve error reporting to the user for experiment create job
+ return;
+ }
+
logger.error("Failed to process experiment create job", e);
traceException(e);
throw e;