# Useful but Little-Known Features of ADK 2.0
🌍 What if you could safely cancel a running agent mid-execution while preserving everything it has already accomplished?
ADK 2.0's cancellation feature uses the AbortController/AbortSignal pattern to gracefully interrupt agent execution. The cancel signal propagates through Runner, LlmAgent, Models, and Tools, while committed events are preserved.
📌 Title: Cancelling Agent Execution
🔗 URL:
🧩 Overview
Agent cancellation follows the AbortController/AbortSignal pattern. When a signal is issued from an AbortController, it propagates through the stack: Runner → LlmAgent → Models → Tools. Crucially, events that have already been committed are preserved, and the process completes gracefully without throwing exceptions. AbortSignal.timeout() enables automatic timeout-based cancellation, and AbortSignal.any() lets you combine multiple signals.
🛠 How to use it
Create an AbortController and pass its signal to the Runner.
```python
from adk import App, AbortController, AbortSignal
app = App(agent=my_agent)
# Manual cancellation
controller = AbortController()
task = "long task", signal=controller.signal)
# Cancel when needed
controller.abort()
# Timeout-based auto-cancel (2 seconds)
signal = AbortSignal.timeout(2000)
result = await "time-limited task", signal=signal)
# Combining multiple signals
combined = AbortSignal.any([
AbortSignal.timeout(5000),
user_cancel_signal
])
result = await "task", signal=combined)
```
🏗 Building it into production
・Wire AbortController to user actions (cancel buttons) to enable UI-driven cancellation
・Use AbortSignal.timeout() to cap maximum execution time for API calls and prevent resource waste
・Combine user cancellation and timeout signals with AbortSignal.any()
・Design workflows to return partial results from committed events after cancellation
💡 Use cases
⏱ Set timeouts on LLM calls to auto-cancel when responses are slow
🖱 Instantly interrupt agent execution when a user clicks a cancel button in the UI
🔀 Run multiple agents in parallel and cancel all but the first to complete
💰 Cut off LLM calls when a cost budget is reached
⚠️ Watch out
Cancellation completes gracefully, meaning the agent does not stop the instant abort() is called. There may be a slight delay while the current operation (LLM inference, tool execution) finishes. Since committed events are preserved, design your workflows with cancellation points in mind to avoid inconsistent intermediate states. Implementing signal-aware early returns inside tools is also an effective practice.
✨ The cancellation feature gives you safe control over the agent execution lifecycle. Combined with timeouts, it dramatically improves predictability in production environments.
#
ADK# #
AIAgent#