Streaming, latency, limits and degradation when the provider fails
Beyond the happy path, check the stream and the behaviour when the provider fails. In a stream that means a partial answer, cancellation (Stop closes the connection and stops token spending), a cut connection and parallel requests. For 429 and 5xx the right reaction is a retry with exponential backoff and jitter, respect for Retry-After and limited attempts. Degradation and the kill switch are checked with fault injection on a stub.
AI for QA · AI features
A user pressed "Stop", but the request to the model keeps spending tokens. How do you check that?
An AI feature depends on an external service that is slow, expensive and sometimes unavailable. A happy-path test will not show this: the answer arrives and the text is nice. But in production the provider returns 429, cuts a stream or stays silent for 30 seconds, and the user sees it first. QA checks product behaviour outside the happy path.
How it works and why. The answer is usually streamed: the server sends tokens in parts (Server-Sent Events, text/event-stream) and the client draws them as they come. This gives a fast first impression but adds states a normal request does not have: a partly finished answer, a cut in the middle, cancellation, parallel requests. Each state is its own test.
What to check in a stream.
- A partial answer shows without flicker and broken markup (unpaired
**, an open code block). - Cancellation: "Stop" halts the output, closes the connection, the request to the model stops spending tokens, and a new question can be asked.
- A connection cut in the middle: it is clear the answer is incomplete and there is a "try again".
- Repeated clicks and parallel requests do not duplicate messages.
// cancelling a stream from the client
const ctrl = new AbortController();
const res = await fetch('/api/chat', { method: 'POST', body, signal: ctrl.signal });
const reader = res.body!.getReader();
stopButton.onclick = () => ctrl.abort(); // closes the connection
// in the test: after "Stop" the server stops sending tokens
await page.getByRole('button', { name: 'Stop' }).click();
await expect.poll(() => upstreamStub.activeStreams).toBe(0);
await page.getByRole('textbox').fill('next question');
Latency and cost. Measure time to the first token and total time, as percentiles (p50, p95) rather than an average: a long tail spoils the impression most. Check limits: the maximum input and output length, trimming of the dialogue history, behaviour when context is exceeded, timeouts, the token budget per user.
Provider limits. 429 (rate limit) and 5xx are normal events, not exceptions. The right reaction: a retry with exponential backoff and jitter, respecting the Retry-After header, a limited number of attempts and a clear message to the user. A retry with no pause only worsens overload.
How to retry a request
Retry with no pause
After a 429 all clients send the request again at once. The provider is overloaded even more and the limit does not lift.
Exponential backoff with jitter
The pause grows with each attempt, random jitter spreads clients in time, Retry-After takes priority, attempts are few.
async function callWithRetry(call: () => Promise<Response>, max = 3) {
for (let attempt = 0; ; attempt++) {
const res = await call();
if (res.status !== 429 && res.status < 500) return res;
if (attempt >= max) throw new Error(`provider unavailable: ${res.status}`);
const retryAfter = Number(res.headers.get('retry-after')); // seconds
const base = Number.isFinite(retryAfter) && retryAfter > 0
? retryAfter * 1000
: 500 * 2 ** attempt;
await sleep(base + Math.random() * 250); // jitter
}
}
// test on a stub: 429 then 200; check the attempt count and the pause
test('respects Retry-After', async () => {
const stub = sequence([{ status: 429, headers: { 'retry-after': '2' } }, { status: 200 }]);
const res = await callWithRetry(stub.call);
expect(res.status).toBe(200);
expect(stub.calls).toBe(2);
expect(stub.delaysMs[0]).toBeGreaterThanOrEqual(2000);
});
Graceful degradation. When the provider is unavailable the main scenario must keep working. Degradation is planned in tiers, and each tier has its own test.
Degradation tiers
This is verified with fault injection: a provider stub returns 429, 500, a timeout and a cut stream. Test the kill switch separately: the feature can be turned off by a flag without a release, and after switching off the UI shows the fallback.
Limits and alternatives. A stub does not reproduce real provider behaviour: real 429s have their own headers and nuances, so a few checks against the real provider (with a limited budget) remain. A fallback model may answer worse, so it also goes through the golden set. A retry is dangerous for actions with side effects (payment, sending mail): an idempotency key is needed there. SSE is one-way; for two-way scenarios use WebSocket.
Typical mistakes.
- Testing only a successful stream and never a cut or cancellation.
- "Stop" hides the text but the request to the model continues and costs money.
- Looking at average latency instead of p95.
- Retrying without a pause and without an attempt limit.
- Not testing the fallback path until the provider goes down.
Check yourself
Why does a retry after 429 with no pause do harm?
Answer
All clients retry at once, the provider is loaded even more and the limit does not lift. You need exponential backoff with jitter, respect for Retry-After and a cap on attempts.
How do you check that "Stop" really stops token spending?
Answer
After clicking, check that the connection is closed, the provider stub no longer emits tokens or the number of active streams is zero, and a new question can be asked.
What is a kill switch for and how is it tested?
Answer
To turn the AI feature off with a flag without a release during an outage or incident. Test: switch the flag off and check the UI shows the fallback and no requests go to the provider.