Skip to main content

Error Handling

Every failure mode is a distinct, catchable class. All of them extend CustodianError and carry statusCode, endpoint, and payload (also folded into .message).

Exception types

ErrorWhen it is thrown
AuthenticationError401 — API key missing, invalid, or expired. Also thrown locally when no key is configured.
NotFoundError404 — the assistant, app, or team ID does not exist or is not accessible.
ValidationError422 — malformed request (check model, prompt, examples, file, message).
ServerError5xx — backend error, after retries.
APIConnectionErrorNetwork failure or timeout, after retries.
APIResponseErrorAny other non-2xx HTTP response.
CustodianErrorBase class for all of the above.

Catch errors

import {
createCustodian,
AuthenticationError,
NotFoundError,
ValidationError,
ServerError,
APIConnectionError,
CustodianError,
} from "@custodianlabs/sdk";

try {
const app = await createCustodian({ model: "gpt-4o", prompt: "You are helpful." });
const reply = await app.chat("Hello");
console.log(reply?.response);
} catch (err) {
if (err instanceof AuthenticationError) {
console.error("Check your API key configuration.");
} else if (err instanceof NotFoundError) {
console.error("The requested Custodian Agent or app was not found.");
} else if (err instanceof ValidationError) {
console.error(`Bad request: ${err.message}`);
} else if (err instanceof ServerError) {
console.error(`Backend error (${err.statusCode}). Try again later.`);
} else if (err instanceof APIConnectionError) {
console.error("Could not reach the server. Check your network or base URL.");
} else if (err instanceof CustodianError) {
console.error(`Unexpected SDK error: ${err.message}`);
} else {
throw err;
}
}

Error detail

catch (err) {
if (err instanceof CustodianError) {
console.log(err.statusCode); // e.g. 422
console.log(err.endpoint); // e.g. "/assistants"
console.log(err.payload); // the parsed error body
}
}

Retry behavior

The SDK automatically retries network errors and 5xx responses with exponential backoff. It does not retry 401, 404, 422, or other 4xx errors.

SettingDefaultWhere to set
timeout30 secondsCustodian / AgentTeam / createCustodian options
maxRetries2 (up to 3 attempts total)same
retryBackoff0.25 secondsHttpClient (advanced)
const custodian = new Custodian({
model: "gpt-4o",
systemPrompt: "You are helpful.",
timeout: 60,
maxRetries: 4,
});

// Disable retries:
new Custodian({ model: "gpt-4o", systemPrompt: "...", maxRetries: 0 });

Next steps