Recover from tool errors
SetOnError: "return-to-model" on a tool when the model can reasonably recover from the failure. The SDK converts the error into a tool result and allows the next turn to choose another action. Leave the default fail-run behavior for unsafe or unrecoverable failures.
Respect cancellation
Pass the request context toRun, Continue, or Stream. Cancelling it stops model streaming and prevents new tool work from starting.