🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
feat(evals): block evaluator configs on billing-exhausted and non-401 auth LLM errors (#14943)
Hassieb Pakzadf69e490ec351
diff --git a/packages/shared/prisma/migrations/20260709124500_add_billing_exhausted_evaluator_block_reason/migration.sql b/packages/shared/prisma/migrations/20260709124500_add_billing_exhausted_evaluator_block_reason/migration.sqlnew file mode 100644index 0000000000..2d27dc144f--- /dev/null+++ b/packages/shared/prisma/migrations/20260709124500_add_billing_exhausted_evaluator_block_reason/migration.sql@@ -0,0 +1,3 @@+-- AlterEnum+ALTER TYPE "EvaluatorBlockReason" ADD VALUE 'LLM_CONNECTION_BILLING_EXHAUSTED';+ALTER TYPE "EvaluatorBlockReason" ADD VALUE 'LLM_CONNECTION_ENDPOINT_UNREACHABLE';diff --git a/packages/shared/prisma/schema.prisma b/packages/shared/prisma/schema.prismaindex 78fd859c6a..35c33e03ec 100644--- a/packages/shared/prisma/schema.prisma+++ b/packages/shared/prisma/schema.prisma@@ -1071,6 +1071,8 @@ enum JobConfigState {enum EvaluatorBlockReason {LLM_CONNECTION_AUTH_INVALID+ LLM_CONNECTION_BILLING_EXHAUSTED+ LLM_CONNECTION_ENDPOINT_UNREACHABLELLM_CONNECTION_MISSINGDEFAULT_EVAL_MODEL_MISSINGEVAL_MODEL_CONFIG_INVALIDdiff --git a/packages/shared/src/features/evals/evalConfigBlocking.ts b/packages/shared/src/features/evals/evalConfigBlocking.tsindex 224bc4bddc..6208b9d05e 100644--- a/packages/shared/src/features/evals/evalConfigBlocking.ts+++ b/packages/shared/src/features/evals/evalConfigBlocking.ts@@ -65,6 +65,16 @@ export const EVALUATOR_BLOCK_METADATA: Record<"Evaluator paused: LLM authentication failed. Update the LLM connection used by this evaluator and then reactivate it.",shortLabel: "Authentication failed",},+ LLM_CONNECTION_BILLING_EXHAUSTED: {+ message:+ "Evaluator paused: the LLM provider reported exhausted credits or an exceeded spend budget. Add credits or raise the limit in your provider account, then reactivate the evaluator.",+ shortLabel: "Provider credits exhausted",+ },+ LLM_CONNECTION_ENDPOINT_UNREACHABLE: {+ message:+ "Evaluator paused: the LLM connection's endpoint hostname could not be resolved. Fix the base URL of the LLM connection used by this evaluator, then reactivate it.",+ shortLabel: "Endpoint unreachable",+ },LLM_CONNECTION_MISSING: {message:"Evaluator paused: no LLM connection found for the provider used by this evaluator. Add or restore the LLM connection, then reactivate it.",@@ -107,6 +117,8 @@ export function getEvaluatorBlockResolutionPath(params: {if (blockReason === EvaluatorBlockReason.LLM_CONNECTION_AUTH_INVALID ||+ blockReason === EvaluatorBlockReason.LLM_CONNECTION_BILLING_EXHAUSTED ||+ blockReason === EvaluatorBlockReason.LLM_CONNECTION_ENDPOINT_UNREACHABLE ||blockReason === EvaluatorBlockReason.LLM_CONNECTION_MISSING) {return `/project/${projectId}/settings/llm-connections`;diff --git a/packages/shared/src/server/llm/completionErrorMapping.ts b/packages/shared/src/server/llm/completionErrorMapping.tsindex 7d6ef36449..166df92a15 100644--- a/packages/shared/src/server/llm/completionErrorMapping.ts+++ b/packages/shared/src/server/llm/completionErrorMapping.ts@@ -16,6 +16,7 @@ const NON_RETRYABLE_LLM_ERROR_PATTERNS = ["Only HTTPS base URLs are allowed","Blocked hostname detected","Blocked IP address detected",+ "DNS lookup failed","Redirect validation failed","Maximum redirects","Circular redirect detected",diff --git a/packages/shared/src/server/llm/errors.test.ts b/packages/shared/src/server/llm/errors.test.tsnew file mode 100644index 0000000000..58b5d81902--- /dev/null+++ b/packages/shared/src/server/llm/errors.test.ts@@ -0,0 +1,248 @@+import { EvaluatorBlockReason } from "@prisma/client";+import { describe, expect, it } from "vitest";++import { mapToLLMCompletionError } from "./completionErrorMapping";+import { inferLLMCompletionBlockReason, LLMCompletionError } from "./errors";++function errorWithStatus(message: string, status?: number): Error {+ const error = new Error(message);+ if (status !== undefined) {+ (error as any).status = status;+ }+ return error;+}++describe("inferLLMCompletionBlockReason", () => {+ it("maps 401 to LLM_CONNECTION_AUTH_INVALID", () => {+ expect(+ inferLLMCompletionBlockReason({+ responseStatusCode: 401,+ message: "Unauthorized",+ }),+ ).toBe(EvaluatorBlockReason.LLM_CONNECTION_AUTH_INVALID);+ });++ it("maps 404 to EVAL_MODEL_UNAVAILABLE", () => {+ expect(+ inferLLMCompletionBlockReason({+ responseStatusCode: 404,+ message: "Model not found",+ }),+ ).toBe(EvaluatorBlockReason.EVAL_MODEL_UNAVAILABLE);+ });++ it("maps 402 to LLM_CONNECTION_BILLING_EXHAUSTED", () => {+ expect(+ inferLLMCompletionBlockReason({+ responseStatusCode: 402,+ message: "Payment required",+ }),+ ).toBe(EvaluatorBlockReason.LLM_CONNECTION_BILLING_EXHAUSTED);+ });++ it.each([+ // OpenRouter (402, but the status is often only embedded in the message)+ "402 Insufficient credits. Add more using https://openrouter.ai/settings/credits",+ // Anthropic (400 invalid_request_error)+ '400 {"type":"error","error":{"type":"invalid_request_error","message":"Your credit balance is too low to access the Anthropic API."}}',+ // Google AI Studio prepaid billing+ "Your prepayment credits are depleted. Please go to AI Studio at https://ai.studio/projects to manage your project and billing.",+ // LiteLLM proxy budgets (surfaces as 400 or 429)+ "400 Budget has been exceeded! Team=abc Current cost: 20.003, Max budget: 20.0",+ // OpenAI insufficient_quota (surfaces as 429)+ "429 You exceeded your current quota, please check your plan and billing details.",+ ])(+ "maps billing exhaustion message %s to LLM_CONNECTION_BILLING_EXHAUSTED",+ (message) => {+ expect(+ inferLLMCompletionBlockReason({ responseStatusCode: 400, message }),+ ).toBe(EvaluatorBlockReason.LLM_CONNECTION_BILLING_EXHAUSTED);+ },+ );++ it("maps revoked GCP service account grants to LLM_CONNECTION_AUTH_INVALID", () => {+ expect(+ inferLLMCompletionBlockReason({+ responseStatusCode: 500,+ message: "invalid_grant: Invalid grant: account not found",+ }),+ ).toBe(EvaluatorBlockReason.LLM_CONNECTION_AUTH_INVALID);+ });++ it("maps OpenRouter monthly key limit exhaustion to LLM_CONNECTION_BILLING_EXHAUSTED", () => {+ expect(+ inferLLMCompletionBlockReason({+ responseStatusCode: 402,+ message:+ "This request requires more credits, or fewer max_tokens. You requested up to 65536 tokens, but can only afford 62702.",+ }),+ ).toBe(EvaluatorBlockReason.LLM_CONNECTION_BILLING_EXHAUSTED);+ });++ it("maps unresolvable custom endpoint hostnames to LLM_CONNECTION_ENDPOINT_UNREACHABLE", () => {+ expect(+ inferLLMCompletionBlockReason({+ responseStatusCode: 500,+ message: "DNS lookup failed for litellm.internal.example.com",+ }),+ ).toBe(EvaluatorBlockReason.LLM_CONNECTION_ENDPOINT_UNREACHABLE);+ });++ it("maps Google's non-401 invalid API key message to LLM_CONNECTION_AUTH_INVALID", () => {+ expect(+ inferLLMCompletionBlockReason({+ responseStatusCode: 400,+ message: "API key not valid. Please pass a valid API key.",+ }),+ ).toBe(EvaluatorBlockReason.LLM_CONNECTION_AUTH_INVALID);+ });++ it("returns null for generic client errors", () => {+ expect(+ inferLLMCompletionBlockReason({+ responseStatusCode: 400,+ message: "messages: at least one message is required",+ }),+ ).toBeNull();+ });+});++describe("LLMCompletionError retryability for block-worthy errors", () => {+ it("forces isRetryable=false when a block reason is inferred", () => {+ const error = new LLMCompletionError({+ message: "429 Budget has been exceeded! Current cost: 10.0",+ responseStatusCode: 429,+ isRetryable: true,+ });++ expect(error.getEvaluatorBlockReason()).toBe(+ EvaluatorBlockReason.LLM_CONNECTION_BILLING_EXHAUSTED,+ );+ expect(error.isRetryable).toBe(false);+ });++ it("keeps caller-provided retryability when no block reason applies", () => {+ const error = new LLMCompletionError({+ message: "429 Rate limit reached for gpt-4o-mini, try again in 356ms",+ responseStatusCode: 429,+ isRetryable: true,+ });++ expect(error.getEvaluatorBlockReason()).toBeNull();+ expect(error.isRetryable).toBe(true);+ });+});++describe("mapToLLMCompletionError billing classification", () => {+ it("blocks and does not retry OpenAI insufficient_quota 429s", () => {+ const cause = errorWithStatus(+ "429 You exceeded your current quota, please check your plan and billing details.",+ 429,+ );+ cause.name = "InsufficientQuotaError";++ const mapped = mapToLLMCompletionError(cause);++ expect(mapped.responseStatusCode).toBe(429);+ expect(mapped.isRetryable).toBe(false);+ expect(mapped.getEvaluatorBlockReason()).toBe(+ EvaluatorBlockReason.LLM_CONNECTION_BILLING_EXHAUSTED,+ );+ });++ it("blocks OpenRouter 402 insufficient credits", () => {+ const mapped = mapToLLMCompletionError(+ errorWithStatus(+ "402 Insufficient credits. Add more using https://openrouter.ai/settings/credits",+ 402,+ ),+ );++ expect(mapped.isRetryable).toBe(false);+ expect(mapped.getEvaluatorBlockReason()).toBe(+ EvaluatorBlockReason.LLM_CONNECTION_BILLING_EXHAUSTED,+ );+ });++ it("blocks Anthropic low-credit 400s", () => {+ const mapped = mapToLLMCompletionError(+ errorWithStatus(+ '400 {"type":"error","error":{"type":"invalid_request_error","message":"Your credit balance is too low to access the Anthropic API. Please go to Plans & Billing to upgrade or purchase credits."}}',+ 400,+ ),+ );++ expect(mapped.isRetryable).toBe(false);+ expect(mapped.getEvaluatorBlockReason()).toBe(+ EvaluatorBlockReason.LLM_CONNECTION_BILLING_EXHAUSTED,+ );+ });++ it("blocks Google invalid-API-key 400s as auth failures", () => {+ const mapped = mapToLLMCompletionError(+ errorWithStatus("API key not valid. Please pass a valid API key.", 400),+ );++ expect(mapped.isRetryable).toBe(false);+ expect(mapped.getEvaluatorBlockReason()).toBe(+ EvaluatorBlockReason.LLM_CONNECTION_AUTH_INVALID,+ );+ });++ it("surfaces DNS failures hidden behind SDK connection-error wrappers and blocks", () => {+ // Anthropic/OpenAI SDKs wrap secureLlmFetch failures as+ // `APIConnectionError { message: "Connection error.", cause }`.+ const mapped = mapToLLMCompletionError(+ new Error("Connection error.", {+ cause: new Error("DNS lookup failed for litellm.internal.example.com"),+ }),+ );++ expect(mapped.message).toBe(+ "DNS lookup failed for litellm.internal.example.com",+ );+ expect(mapped.isRetryable).toBe(false);+ expect(mapped.getEvaluatorBlockReason()).toBe(+ EvaluatorBlockReason.LLM_CONNECTION_ENDPOINT_UNREACHABLE,+ );+ });++ it("does not retry status-less invalid_grant auth failures", () => {+ const mapped = mapToLLMCompletionError(+ new Error("invalid_grant: Invalid grant: account not found"),+ );++ expect(mapped.isRetryable).toBe(false);+ expect(mapped.getEvaluatorBlockReason()).toBe(+ EvaluatorBlockReason.LLM_CONNECTION_AUTH_INVALID,+ );+ });++ it("keeps generic connection errors retryable and unblocked", () => {+ const mapped = mapToLLMCompletionError(new Error("Connection error."));++ expect(mapped.isRetryable).toBe(true);+ expect(mapped.getEvaluatorBlockReason()).toBeNull();+ });++ it("keeps plain rate-limit 429s retryable and unblocked", () => {+ const mapped = mapToLLMCompletionError(+ errorWithStatus(+ "429 Your requests to gpt-5-mini in eastus have exceeded rate limit.",+ 429,+ ),+ );++ expect(mapped.isRetryable).toBe(true);+ expect(mapped.getEvaluatorBlockReason()).toBeNull();+ });++ it("keeps 5xx errors retryable and unblocked", () => {+ const mapped = mapToLLMCompletionError(+ errorWithStatus("Request failed with status code 502", 502),+ );++ expect(mapped.isRetryable).toBe(true);+ expect(mapped.getEvaluatorBlockReason()).toBeNull();+ });+});diff --git a/packages/shared/src/server/llm/errors.ts b/packages/shared/src/server/llm/errors.tsindex 015f76c60c..9c1bdafebd 100644--- a/packages/shared/src/server/llm/errors.ts+++ b/packages/shared/src/server/llm/errors.ts@@ -7,6 +7,56 @@ const BLOCK_REASON_PATTERNS = [pattern: "Model use case details have not been submitted for this account",blockReason: EvaluatorBlockReason.PROVIDER_ACCOUNT_NOT_READY,},+ // Google AI Studio rejects invalid API keys with a 400, so this cannot rely+ // on the 401 status check above.+ {+ pattern: "API key not valid",+ blockReason: EvaluatorBlockReason.LLM_CONNECTION_AUTH_INVALID,+ },+ // google-auth-library reports deleted/revoked Vertex service accounts as a+ // status-less OAuth error, not a 401.+ {+ pattern: "invalid_grant",+ blockReason: EvaluatorBlockReason.LLM_CONNECTION_AUTH_INVALID,+ },+ // secureLlmFetch DNS validation: the custom base URL's hostname resolved to+ // no address via any strategy. Terminal until the connection's endpoint is+ // fixed; often hidden behind SDK "Connection error." wrappers, so the+ // matching non-retryable pattern must also exist in completionErrorMapping+ // for the cause-chain message extraction to surface it here.+ {+ pattern: "DNS lookup failed",+ blockReason: EvaluatorBlockReason.LLM_CONNECTION_ENDPOINT_UNREACHABLE,+ },+ // Exhausted credits or spend budgets are terminal until a human tops up the+ // provider account, but providers report them with inconsistent status+ // codes (OpenRouter 402, Anthropic 400, LiteLLM 400/429, OpenAI 429), so+ // they must be matched by message.+ {+ pattern: "Insufficient credits",+ blockReason: EvaluatorBlockReason.LLM_CONNECTION_BILLING_EXHAUSTED,+ },+ {+ pattern: "credit balance is too low",+ blockReason: EvaluatorBlockReason.LLM_CONNECTION_BILLING_EXHAUSTED,+ },+ {+ pattern: "prepayment credits are depleted",+ blockReason: EvaluatorBlockReason.LLM_CONNECTION_BILLING_EXHAUSTED,+ },+ {+ pattern: "Budget has been exceeded",+ blockReason: EvaluatorBlockReason.LLM_CONNECTION_BILLING_EXHAUSTED,+ },+ {+ pattern: "exceeded your current quota",+ blockReason: EvaluatorBlockReason.LLM_CONNECTION_BILLING_EXHAUSTED,+ },+ // OpenRouter monthly key limits near exhaustion.+ {+ pattern: "requires more credits",+ blockReason: EvaluatorBlockReason.LLM_CONNECTION_BILLING_EXHAUSTED,+ },] as const;export function inferLLMCompletionBlockReason(params: {@@ -17,6 +67,10 @@ export function inferLLMCompletionBlockReason(params: {return EvaluatorBlockReason.LLM_CONNECTION_AUTH_INVALID;}+ if (params.responseStatusCode === 402) {+ return EvaluatorBlockReason.LLM_CONNECTION_BILLING_EXHAUSTED;+ }+if (params.responseStatusCode === 404) {return EvaluatorBlockReason.EVAL_MODEL_UNAVAILABLE;}@@ -43,11 +97,15 @@ export class LLMCompletionError extends Error {this.name = LLMCompletionErrorName;this.responseStatusCode = params.responseStatusCode ?? 500;- this.isRetryable = params.isRetryable ?? false; // Default to false - be explicit about retryabilitythis.blockReason = inferLLMCompletionBlockReason({responseStatusCode: this.responseStatusCode,message: this.message,});+ // A block-worthy error is terminal by definition: retrying would burn the+ // eval retry budget on a config that is being paused anyway. This matters+ // for billing errors that arrive as otherwise-retryable 429s.+ this.isRetryable =+ this.blockReason !== null ? false : (params.isRetryable ?? false); // Default to false - be explicit about retryabilityif (Error.captureStackTrace) {Error.captureStackTrace(this);diff --git a/packages/shared/src/server/services/email/evaluatorBlocked/EvaluatorBlockedEmailTemplate.tsx b/packages/shared/src/server/services/email/evaluatorBlocked/EvaluatorBlockedEmailTemplate.tsxindex 76fa5a2347..4619f53359 100644--- a/packages/shared/src/server/services/email/evaluatorBlocked/EvaluatorBlockedEmailTemplate.tsx+++ b/packages/shared/src/server/services/email/evaluatorBlocked/EvaluatorBlockedEmailTemplate.tsx@@ -28,6 +28,10 @@ const getReasonSummary = (blockReason: EvaluatorBlockReason) => {switch (blockReason) {case EvaluatorBlockReason.LLM_CONNECTION_AUTH_INVALID:return "LLM authentication failed";+ case EvaluatorBlockReason.LLM_CONNECTION_BILLING_EXHAUSTED:+ return "LLM provider credits exhausted";+ case EvaluatorBlockReason.LLM_CONNECTION_ENDPOINT_UNREACHABLE:+ return "LLM connection endpoint unreachable";case EvaluatorBlockReason.LLM_CONNECTION_MISSING:return "LLM connection missing";case EvaluatorBlockReason.DEFAULT_EVAL_MODEL_MISSING:@@ -54,6 +58,26 @@ const getResolutionSteps = (blockReason: EvaluatorBlockReason) => {<br />• Reactivate the paused evaluator</>);+ case EvaluatorBlockReason.LLM_CONNECTION_BILLING_EXHAUSTED:+ return (+ <>+ • Add credits or raise the spend limit in your LLM provider account+ <br />+ • Check which connection is affected in Project Settings → LLM+ Connections+ <br />• Reactivate the paused evaluator+ </>+ );+ case EvaluatorBlockReason.LLM_CONNECTION_ENDPOINT_UNREACHABLE:+ return (+ <>+ • The endpoint hostname of the LLM connection could not be resolved+ <br />+ • Fix the connection's base URL in Project Settings → LLM+ Connections+ <br />• Reactivate the paused evaluator+ </>+ );case EvaluatorBlockReason.LLM_CONNECTION_MISSING:return (<>