AbortSignal is a lightweight cancellation mechanism for stopping streaming output, cancelling tool calls in progress, and ending an Agent turn in a controlled way.
Typical scenarios:
- interrupting a long response from the user interface
- enforcing a timeout on the server side
- stopping an old turn before starting a new one with different context
User-initiated interrupt
Stop a long-running answer and immediately switch to a new request.
Server-side timeout
Cap the runtime of a single turn so requests do not block forever.
Context switching
Abort the current turn cleanly before launching the next one.
Core Concepts
AbortSignal: a signal object that can be shared across coroutines_abort_signal: the special call parameter used to pass the signalabort(reason): trigger cancellation with an optional reason string
_abort_signal is runtime-only. It is not included in the prompt and is not passed to the model or tools as normal input.Basic Usage
Pass it through _abort_signal
Prefer the
ABORT_SIGNAL_PARAM constant instead of hard-coding the parameter name.llm_chat example
llm_function example
Runtime Behavior
- Streaming output stops yielding new chunks after cancellation
- In-flight tool calls are cancelled when possible
- In event mode,
ReactEndEvent.extraincludesaborted: true - If you provide a reason, the event metadata may also include
abort_reason - In non-event mode, the generator ends early without yielding a final event object
Abort is cooperative. If a tool is not cancellable or is blocked in a non-interruptible state, actual shutdown may be delayed.
Built-in TUI Behavior
When using@tui, sending a new message while the Agent is still responding will automatically trigger an abort:
- the current turn is stopped
- the new message enters the queue
- an interruption prompt is added automatically so the model can wrap up quickly
AbortSignal explicitly.