Build a local AI feature
Add timeout and cancellation
Stop work that has exceeded the feature budget or is no longer needed, while distinguishing cancellation from service failure.
A local request can still hang or take too long.
Pass an abort signal to fetch():
const signal = AbortSignal.timeout(8000)
const response = await fetch('http://localhost:11434/api/chat', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify(request),
signal,
})
Eight seconds is an example, not a universal default. Measure the real model and choose a budget that matches the feature.
Cancellation has another use. If the user closes a view or replaces the input, the old result may no longer matter. Abort the pending request so a stale answer cannot update the interface later.
Handle these outcomes separately in logs:
- user or lifecycle cancellation
- timeout
- connection failure
- HTTP error
- invalid model output
They may share the same deterministic fallback, but they need different diagnostics.
Lesson completed