Home » How to Implement Cancellation and Retry Controls for AI Requests in React Native
React • Technology

How to Implement Cancellation and Retry Controls for AI Requests in React Native

Build a Stop button that cancels the current request, a Retry button that preserves the original operation, and a backend policy that limits duplicate AI work.

A user taps Generate, loses their connection, and taps Retry. By then, the backend may have already sent the first request to the model. If the app treats the second tap as a fresh operation, it can generate two responses, incur duplicate model costs, or repeat an action. A related bug appears when the user taps Stop or sends a new prompt, but a slow response from the previous request still updates the screen.

A reliable React Native implementation handles these problems at three points. The app cancels requests and ignores outdated responses. The backend recognizes repeated submissions of the same operation. The model client applies a bounded retry policy for transient failures. React Native provides fetch and AbortController, and React’s effect cleanup gives the screen a place to cancel outstanding work when it unmounts. 

Define what Stop, Retry, and Regenerate do

ControlWhat the app doesWhat the backend needs
StopAborts the current request and prevents it from updating the screen.A way to propagate cancellation upstream or explicitly cancel a durable job.
RetryResubmits the same prompt with the same operation ID after a temporary failure or an uncertain outcome.A way to recognize the existing operation and return its result or report that it is still running.
RegenerateSubmits the prompt with a new operation ID.A new generation, tracked separately from the previous one.

That distinction matters because a timeout gives the app incomplete information. The server may have accepted the operation, completed it, or failed before doing any work. HTTP semantics advise against automatically retrying a non-idempotent request unless the client knows that repeating it is safe or can determine that the original request was never applied. An AI generation endpoint commonly uses POST, so the backend needs to enforce the operation ID for retries to be safe. 

Implement Stop and Retry with AbortController

The following TypeScript hook handles one active, non-streaming generation per screen. It uses a 20-second UI deadline as an example. The backend owns automatic provider retries. The hook treats a network failure, timeout, 408, 429, or selected gateway failures as eligible for manual retry. It assumes an authenticated HTTPS endpoint that returns { "text": "..." } on success and implements the operation ID contract described below.

import { useEffect, useRef, useState } from "react";

type Operation = { id: string; prompt: string };

type GenerationState =
  | { status: "idle" | "loading" | "stopped" }
  | { status: "complete"; text: string }
  | { status: "error"; message: string; retryable: boolean };

const retryableStatuses = new Set([408, 429, 502, 503, 504]);

export function useAiGeneration(endpoint: string, accessToken: string) {
  const active = useRef<AbortController | null>(null);
  const lastOperation = useRef<Operation | null>(null);
  const [state, setState] = useState<GenerationState>({ status: "idle" });

  useEffect(() => {
    return () => {
      const controller = active.current;
      active.current = null;
      controller?.abort();
    };
  }, []);

  async function generate(operation: Operation) {
    active.current?.abort();

    const controller = new AbortController();
    active.current = controller;
    lastOperation.current = operation;
    setState({ status: "loading" });

    let timedOut = false;
    let readingBody = false;
    const deadline = setTimeout(() => {
      timedOut = true;
      controller.abort();
    }, 20_000);

    try {
      const response = await fetch(endpoint, {
        method: "POST",
        headers: {
          Authorization: `Bearer ${accessToken}`,
          "Content-Type": "application/json",
          "Idempotency-Key": operation.id,
        },
        body: JSON.stringify({ prompt: operation.prompt }),
        signal: controller.signal,
      });

      if (active.current !== controller) return;

      if (timedOut) throw new Error("Request timed out");

      if (!response.ok) {
        setState({
          status: "error",
          message: `Generation failed (HTTP ${response.status}).`,
          retryable: retryableStatuses.has(response.status),
        });
        return;
      }

      readingBody = true;
      const result: { text?: unknown } = await response.json();

      if (active.current !== controller) return;
      if (timedOut) throw new Error("Request timed out");

      if (typeof result?.text !== "string") {
        setState({
          status: "error",
          message: "The server returned an invalid response.",
          retryable: false,
        });
        return;
      }

      setState({ status: "complete", text: result.text });
    } catch (error) {
      if (active.current !== controller) return;

      const networkFailure = !readingBody && error instanceof TypeError;
      setState({
        status: "error",
        message: timedOut
          ? "Generation took too long."
          : networkFailure
            ? "Could not reach the server."
            : "Could not read the response.",
        retryable: timedOut || networkFailure,
      });
    } finally {
      clearTimeout(deadline);
      if (active.current === controller) active.current = null;
    }
  }

  function stop() {
    const controller = active.current;
    if (!controller) return;

    active.current = null;
    controller.abort();
    setState({ status: "stopped" });
  }

  function retry() {
    const operation = lastOperation.current;
    if (active.current || state.status !== "error" ||
        !state.retryable || !operation) return;

    void generate(operation);
  }

  return { state, generate, stop, retry };
}

In an Expo screen, call generate({ id: Crypto.randomUUID(), prompt }) using expo-crypto when the user submits a new prompt or taps Regenerate. Wire Stop to stop() while the request is loading and show Retry when state.retryable is true. If the API sends Retry-After, keep the Retry button unavailable until that interval expires. The hook above leaves that countdown to the screen.

The controller serves two purposes. Its signal asks the networking layer to abort the request. Its identity lets the hook check whether a response still belongs to the active generation, including after reading the JSON body. That second check is what prevents a slow response to prompt A from overwriting the result for prompt B.

Make backend retries safe before enabling them

Start by reserving the operation ID atomically under the authenticated user or tenant. Store a fingerprint of the request with it. If the same key arrives with the same input, return the saved result or wait for the existing operation within the request deadline. If the same key arrives with different input, reject it. A backend that plans to return an explicit “still running” response needs a corresponding status or polling flow in the app.

Then decide which layer owns automatic retries. A common arrangement is manual retry in the app and bounded automatic retry in the backend. If the backend implements its own retry loop, configure the provider SDK’s retry behavior deliberately. Three attempts in a backend loop combined with three attempts per SDK call can produce up to nine upstream calls. AWS recommends bounded retries, exponential backoff with jitter, and care when retrying operations with side effects. 

For a transient rate limit or capacity error, honor Retry-After when the service supplies it. Otherwise, use exponential backoff with jitter and stop when the retry budget or request deadline is exhausted. The backoff wait should also be cancellable. Validation errors, authorization failures, and exhausted quota need their own handling. For agents that can send emails, create orders, or change records, protect the individual tool action with its own deduplication mechanism. A generation-level ID alone cannot safely replay a downstream action whose outcome is unknown. 

What changes when the AI response streams?

For streaming chat, cancellation has to travel from the phone through the route handler to the provider request. Vercel’s AI SDK documents a stop() helper for UI streams and shows how to forward an abort signal into the server-side model call. Whether tapping Stop reaches the provider depends on whether the server runtime propagates the client disconnect and the route passes that signal upstream. A persisted background job needs an explicit cancellation mechanism of its own. 

Once tokens are visible, preserve them as a partial response if the user stops or the stream disconnects. If the next action starts a fresh generation, display it separately. This prevents text from two attempts from appearing as a single answer. Test the streaming transport on both iOS and Android. Vercel’s Expo guide uses expo/fetch for its React Native streaming example. 

How five companies put these controls into practice

CompanyWhat its public documentation shows
GeekyAntsIts engineering write-up describes using evaluation reports and run ledgers to track model behavior, latency, costs, and changes in retry frequency. Engineers use those signals to investigate failing request paths and rerun evaluations after a model, prompt, or configuration change.
OpenAIIts JavaScript SDK accepts an AbortSignal and exposes configurable timeouts and maxRetries. The SDK documents two default retries for eligible failures, including connection errors, 429 responses, and server errors. 
AnthropicIts TypeScript SDK documents configurable retries with exponential backoff. Its streaming helper also exposes stream.abort() so a caller can stop consuming a Claude response. 
VercelIts AI SDK documents how to connect a UI Stop control to HTTP cancellation and forward that cancellation to the model call. It also distinguishes cancelling a stream from resuming one after a disconnect.
Amazon Web ServicesIts Bedrock guidance recommends retrying transient throttling and capacity failures within a defined budget, respecting Retry-After, and adding jitter so clients avoid retrying together. 

Test the failure paths users will actually hit

  • Stop, then submit again. Delay the first response and verify that it cannot overwrite the second.
  • Lose connectivity after the server accepts the request. Tap Retry and confirm that the same operation ID resolves to the existing job or saved result.
  • Return 429 with Retry-After. Verify that the app respects the cooldown and that the backend stays within its attempt limit.
  • Stop during a stream or retry delay. Verify that partial output is handled as intended and inspect server traces to confirm whether upstream work stopped.
  • Unmount the screen while a request is running. Verify that a late response cannot update the screen. If the navigator keeps the screen mounted, test the navigation blur behavior separately.

The most useful production traces tie the operation ID to the attempt count, final outcome, and provider latency. Those fields help distinguish a user who intentionally stopped a generation from a backend timeout or a provider failure that caused repeated work.

About the author

admin

Add Comment

Click here to post a comment