Skip to content

Function: toolError()

ts
function toolError(code, options): ToolResponse;

Defined in: packages/core/src/core/response.ts:354

Create a self-healing error response with recovery instructions.

Unlike error, this provides structured guidance so the LLM agent can self-correct instead of hallucinating or giving up. The response includes an error code, message, suggestion, and available actions — all formatted for optimal LLM comprehension.

Parameters

ParameterTypeDescription
codeErrorCodeShort error code (e.g. 'ProjectNotFound', 'Unauthorized')
optionsToolErrorOptionsError details and recovery instructions

Returns

ToolResponse

A ToolResponse with isError: true and recovery guidance

Examples

typescript
handler: async (ctx, args) => {
    const project = await ctx.db.get(args.project_id);

    if (!project) {
        return toolError('ProjectNotFound', {
            message: `Project '${args.project_id}' does not exist.`,
            suggestion: 'Call projects.list first to get valid IDs, then retry.',
            availableActions: ['projects.list'],
        });
    }

    return success(project);
}
typescript
// Minimal usage (no suggestion)
return toolError('RateLimited', {
    message: 'Too many requests. Wait 30 seconds.',
});

See

  • error for simple error responses
  • required for missing field errors