Cortex · Tools and gates

Tool errors

An error thrown by a tool’s execute function is returned to the model as the tool result, and the run continues to its output. The tool-end event carries an observation of kind error with the error message. The model’s turns are scripted; no model is called.

Tool and agent
const refund = defineTool({
  id: 'refund',
  name: 'refund',
  description: 'Refund a booking to the card it was paid with',
  input: z.object({ booking: z.string() }),
  execute: () => {
    throw new Error('The card service did not answer.');
  },
});

const desk = defineAgent({
  id: 'desk',
  instructions: 'Refund a booking when the member asks for it.',
  tools: [refund],
});

const result = await desk.run('Please refund my two seats for Saturday. Grace', { llm }).result;
Scripted model turns
1  calls refund {"booking":"B-2041"}
2  writes {"response":"I could not refund that just now. Please ask again later."}
Output
{
  observation: {
    kind: 'error',
    toolId: 'refund',
    args: { booking: 'B-2041' },
    error: 'The card service did not answer.',
  },
  toolResult: 'error: The card service did not answer.',
  result: {
    ok: true,
    output: {
      response: 'I could not refund that just now. Please ask again later.',
    },
  },
}
Output matches the expected value

Runs in this page on the installed package. The input and the expected output are defined by the example.