Interrupt the thinking process programmatically

Hello,

I would like to know if there’s a known way to interrupt the thinking process under certain conditions.

In my case, I have a single agent, single task, multiple tools architecture. If any tool returns a string that starts with “ERROR”, I would like to interrupt immediately the thinking process, and return that string as final result.

I tried to edit agent/task descriptions telling to stop if it meets that string, or just raise an exception, but the agent keeps retrying using different tools or the same one.

What is in this case the best approach to implement?