Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

fix(agents): errors from your own AI endpoint no longer fail the run as a system error (#16821)

Mo AbuAboud64456526bfe2
diff --git a/brain/knowledge/ai-intelligence/ai-agents.md b/brain/knowledge/ai-intelligence/ai-agents.md
index 51d9b3ba9f3..67aed13151a 100644
--- a/brain/knowledge/ai-intelligence/ai-agents.md
+++ b/brain/knowledge/ai-intelligence/ai-agents.md
@@ -54,6 +54,7 @@ Three things follow. `run_agent` has two modes: pointed at a saved agent (`agent
- **The managed `activepieces` provider is never user-fault on auth.** It runs on our own OpenRouter key, so a 401 there fails every platform at once and must page.
- **Credit is read from a status or the specific `insufficient_quota` marker, never loose patterns over a response body.** OpenAI signals billing exhaustion as a *retryable* 429 with the marker in the **body**, so credit is checked before the retryable verdict — but scanning a body for `credits`/`402` made a provider 500 whose HTML error page said "credits" complete as a billing failure and hide a real outage.
- **`ENTITY_NOT_FOUND` counts only for an AI-provider `entityType`, and `VALIDATION` counts for nothing.** A bare not-found is our bug; the `VALIDATION` that reaches this surface is the conversation concurrency lock, and a conversation stuck `STREAMING` is a state worth keeping visible. So a user-config refusal thrown from `getAgentConfig` must be `ENTITY_NOT_FOUND`/`AIProvider` with its text as `ActivepiecesError`'s second argument: text left in `params` never crosses the RPC, which is how a step naming a model the Activepieces credits do not serve failed as a bare internal `VALIDATION` on every scheduled run. The billing text heuristic reads provider text only and skips any error carrying our own code, because `isProviderBillingError` matched our own "Activepieces AI credits" wording; our credit exhaustion is always `QUOTA_EXCEEDED`. Those bad models came from `ap_list_ai_models`, which offered the AI builder the whole OpenRouter catalog for Run Agent steps; it now lists only what the resolver accepts, while `listModels` keeps the full catalog because Ask AI and image lookups need it.
+ - **A 400 from the `custom` provider is user-fault.** That endpoint is a gateway the customer chose, and it has its own request rules. For example, OpenCode Go rejects any client but its own with `missing x-opencode-session`. Built-in providers keep 400 internal, so a tool-name bug still pages there (#16821).
A completed job stores no `errorMessage`, so the `warn` log carrying `agentRun.errorClass` is the only remaining record.
- **A tool name is user text on four paths, and only the worker sees all of them.** `createToolName` is applied by the flow-tool dialog and the piece-tool stores, but a knowledge-base name was stored as typed and the AI piece's `toolName` is free `ShortText`. That string becomes the AI-SDK `ToolSet` key verbatim, which is how a name earned a 400 from Anthropic — *our* request, never the user's fault, which is why widening the status allow-list to 400 would have been the wrong fix. `mcpToolNameUtils.toValidToolName` is the guard, applied by `agentToolPolicy.withValidNames` in `execute-agent-run` — the one place the flow-step, chat and eval enqueue paths converge (`agent-conversation-controller` validates tool names not at all; `agent-run-controller` checks only the reserved prefix and duplicates, and does it on the *raw* names, so it cannot see a collision the rewrite creates). Four things it has to get right:
- **The pattern is the intersection of every provider we ship, not the one from the error we happened to see.** Anthropic's `^[a-zA-Z0-9_.-]{1,64}$` is the loosest: OpenAI and Bedrock reject `.`, and Gemini requires a leading letter or underscore. Guarding with Anthropic's rule leaves `handbook.pdf` — the obvious name for a knowledge base file — still failing everywhere else. The guard is `^[a-zA-Z_][a-zA-Z0-9_-]{0,63}$`.
diff --git a/packages/server/worker/src/lib/execute/jobs/ee/agent/run-agent-turn.ts b/packages/server/worker/src/lib/execute/jobs/ee/agent/run-agent-turn.ts
index 3104516c08d..4b7dbaad42c 100644
--- a/packages/server/worker/src/lib/execute/jobs/ee/agent/run-agent-turn.ts
+++ b/packages/server/worker/src/lib/execute/jobs/ee/agent/run-agent-turn.ts
@@ -518,6 +518,10 @@ export function classifyAgentRunError({ error, provider }: { error: unknown, pro
const rejectedWithReason = USER_FACING_ERROR_CODES.has(apError?.code ?? '')
return missingUserConfig || rejectedWithReason ? 'user' : 'internal'
}
+ const rejectedByCustomGateway = apiError.statusCode === 400 && provider === AIProviderName.CUSTOM
+ if (rejectedByCustomGateway) {
+ return 'user'
+ }
if (apiError.statusCode === 400 && provider !== AIProviderName.ACTIVEPIECES && MODEL_UNAVAILABLE_PATTERNS.some((pattern) => pattern.test(message))) {
return 'user'
}
diff --git a/packages/server/worker/test/lib/execute/jobs/ee/agent/run-agent-turn-guards.test.ts b/packages/server/worker/test/lib/execute/jobs/ee/agent/run-agent-turn-guards.test.ts
index 5423c41509a..653b323376e 100644
--- a/packages/server/worker/test/lib/execute/jobs/ee/agent/run-agent-turn-guards.test.ts
+++ b/packages/server/worker/test/lib/execute/jobs/ee/agent/run-agent-turn-guards.test.ts
@@ -91,6 +91,10 @@ describe('classifyAgentRunError', () => {
expect(classify(apiError({ statusCode: 400, message: 'Grok 4.1 Fast is deprecated' }), AIProviderName.OPENROUTER)).toBe('user')
})
+ it('blames the user for a 400 from their own custom endpoint, whose request rules we cannot meet', () => {
+ expect(classify(apiError({ statusCode: 400, message: 'Request is missing x-opencode-session and cannot be routed efficiently.' }), AIProviderName.CUSTOM)).toBe('user')
+ })
+
it('keeps a 400 we caused internal, so an illegal tool name is not laundered as user config', () => {
expect(classify(apiError({ statusCode: 400, message: "tools.0.name: should match pattern '^[a-zA-Z0-9_.-]{1,64}$'" }))).toBe('internal')
})