AbortSignal / Timeouts
Overview
Abort controllers allow you to cancel ongoing LLM operations, which is essential for:
- User-initiated cancellations (e.g., “Stop generating” buttons)
- Implementing timeouts for long-running operations
- Cleaning up resources when components unmount
- Managing multiple parallel requests
Quick Start
TypeScript
Python
Go
Basic Examples
Implementing Timeouts
Automatically cancel operations that take too long:
TypeScript
Python
Go
User-Initiated Cancellation
Build responsive backend services that allow users to cancel long-running operations:
TypeScript (Express)
Python (FastAPI)
Streaming with Abort Controllers
Abort controllers work seamlessly with streaming responses:
TypeScript
Python
Go
Error Handling
Properly handle abort errors to distinguish cancellations from other failures:
TypeScript
Python
Go
Best Practices
When to Use Each Pattern
TypeScript
Key Benefits
- AbortSignal.timeout(): Cleaner code for simple timeout scenarios
- Manual AbortController: More control over cancellation logic and reasons
- Better Error Handling: Clear distinction between timeouts and user cancellations
- Standards Compliance: Uses modern web standards that work across different environments
Advanced Patterns
For more advanced abort controller patterns including:
- Cancelling parallel operations - Cancel multiple concurrent calls at once or individually
- Fastest request wins - Race multiple LLM providers and cancel slower ones
- Implementing timeouts for parallel operations - Set automatic timeouts for batches of operations
- Batching with cancellation support - Process items in batches with cancellation
See the Concurrent Calls guide for detailed examples and implementations.
Related Topics
- Error Handling - Learn about all error types including BamlAbortError
- Streaming - Stream responses with cancellation support
- Concurrent Calls - Advanced cancellation patterns for parallel operations
- API Reference - Detailed API documentation