Skip to content

Error Handling

Configure how the agent handles gadget errors and LLM failures.

TypeDescription
parseFailed to parse gadget call from LLM output
validationParameters don’t match schema
executionGadget threw an error
// Default: stop on first error
.withStopOnGadgetError(true)
// Continue executing all gadgets
.withStopOnGadgetError(false)
.withErrorHandler((context) => {
const { error, gadgetName, errorType } = context;
if (errorType === 'parse') {
return false; // Stop execution
}
return true; // Continue
})

LLMist includes automatic retry for transient failures:

.withRetry({
retries: 5,
minTimeout: 2000,
maxTimeout: 60000,
onRetry: (error, attempt) => console.log(`Retry ${attempt}`),
})
// Disable retry
.withoutRetry()
.withHooks({
controllers: {
afterLLMError: async (ctx) => {
if (ctx.error.message.includes('rate limit')) {
await sleep(1000);
return { action: 'recover', fallbackResponse: 'Try again.' };
}
return { action: 'rethrow' };
},
},
})
ExceptionPurpose
TaskCompletionSignalGracefully terminate the loop
HumanInputRequiredExceptionPause for user input
AbortExceptionGadget cancelled
// Terminate loop
throw new TaskCompletionSignal('Task completed');
// Request input
throw new HumanInputRequiredException('What is your preference?');

Use throwIfAborted() to check cancellation:

async execute(params, ctx) {
this.throwIfAborted(ctx);
await this.doWork();
this.throwIfAborted(ctx);
return 'done';
}

Register cleanup handlers with onAbort():

async execute(params, ctx) {
const browser = await chromium.launch();
this.onAbort(ctx, () => browser.close());
// ...
}