Skip to content

    AI Architect Case Files

    Case: In-Product Assistant Latency, Streaming and Prefill

    A SaaS assistant felt slow because of a long prompt and unstreamed replies. A latency budget, streaming, a shorter prefix and a smaller model fixed the tail.

    By Published 7 min read

    • ai-architect
    • case-file
    • industry-saas-b2b
    • pattern-streaming-prefill
    • failure-latency-tail
    • kind-composite
    Share

    Composite scenario, not a real company. Assembled from common industry patterns; every number is illustrative and its assumptions are listed at the end.

    This case belongs to Models, routing and inference for AI Architects.

    Context

    • A B2B project-management product adds an assistant panel to every page: summarise the board, explain a field, draft a status update.
    • Each message sends instructions (2,000 tokens), 12 tool descriptions (3,000) and the page (up to 9,000).
    • The reply returns in one piece, with a spinner until then. Support tickets say the assistant "hangs".

    Problem statement

    Profile Input tokens Answer tokens Wait, nothing on screen
    Typical 8,000 400 15.63 s
    Long (stands in for the slow tail) 14,000 800 30.17 s

    Goal: first visible text within 2 seconds for both profiles, and a typical complete answer within 10 seconds.

    Constraints

    • Page context can be trimmed, not dropped; quality cannot be traded for speed without a check.
    • Replies used to be checked in full before display; the team must decide which intents may stream.
    • Only the Orchestrator and Model gateway can change, not the provider.

    Why the traditional approach fell short

    Approach Where it breaks
    Help centre search and field tooltips Near-instant, which sets the expectation for every help feature, but the same article for everyone and blind to the board
    Report templates A fixed status update that cannot adapt to what changed this week

    A 15 to 30 second wait loses against a search box.

    Options considered

    Option Verdict Reason
    A. Non-AI help: tooltips, help search, a status template Kept for definitional questions Instant, no model risk; cannot see the board
    B. Stream the reply, change nothing else Rejected as the whole answer First text at 2.33 s typical but 3.53 s long, and the full answer is as slow as before
    C. Stream, shrink prefix and context, cache the prefix, shorten answers, small model for first-turn intents Accepted Targets first text and the tail

    Default choice: C. B improves only how the wait feels; the tail comes from input size and output length.

    Solution architecture

    Figure 1. The original request, as a timeline of one non-streamed call

    sequenceDiagram accTitle: Original non-streamed assistant request accDescr: The Client app sends a message. The Orchestrator assembles the prompt from instructions, all tool descriptions and the page context, then waits for the whole model reply before returning anything. The user sees only a spinner until the full reply arrives. participant C as Client app participant O as Orchestrator participant M as Model gateway participant L as LLM provider C->>O: message and page id Note over O: 0.7 s network and context assembly O->>M: 8,000 input tokens M->>L: request Note over L: 1.6 s prefill, then 13.33 s to generate 400 tokens L-->>M: complete reply M-->>O: complete reply O-->>C: complete reply at 15.63 s

    How to read this: the notes carry the time budget, and the only message the user sees is the last arrow.

    Figure 2. Components after the redesign

    Assistant components after the redesignClient app to API gateway; API gateway to Client app (stream); API gateway to Orchestrator; Orchestrator to Retriever; Orchestrator to Model gateway; Model gateway to LLM provider; Model gateway to LLM provider; Orchestrator to Trace storeClient appClient appAPI gateway: streamsAPI gatewaystreamsOrchestrator: intent, tool selectionOrchestratorintent, toolselectionRetriever: page context, cappedRetrieverpage context,cappedModel gateway: stable prefix firstModel gatewaystable prefixfirstLLM provider: small tierLLM providersmall tierLLM provider: large tierLLM providerlarge tierTrace store: time to first textTrace storetime to firsttextstream

    How to read this: the Orchestrator decides what goes into the prompt and which tier answers, and the reply flows back along the same path as a stream.

    Figure 3. Runtime path with streaming and a mid-stream failure

    sequenceDiagram accTitle: Streamed assistant request with failure path accDescr: The Orchestrator sends a trimmed request. The provider streams text in small pieces that the gateway forwards to the client as server-sent events. If an error event arrives after text has been shown, the Orchestrator ends the stream cleanly and the client keeps the partial text and offers a retry. autonumber participant C as Client app participant O as Orchestrator participant M as Model gateway participant L as LLM provider participant T as Trace store C->>O: message and page id O->>M: trimmed request, stable prefix first M->>L: request with streaming on L-->>M: first text piece M-->>C: first text piece, forwarded as an event Note over C: User sees text at about 1.3 s loop until the end of the reply L-->>M: next text piece M-->>C: event end alt error event after partial text L-->>M: error event M-->>C: end of stream with a retry marker Note over C: Partial text stays, retry button shown end O-)T: time to first text, total time, tier

    How to read this: the loop is the stream, and the alt block is the case where the call fails after the user has already seen text.

    How AI solved it, step by step

    Step through the redesigned request, then switch to "Without the fix".

    One assistant request, streamed and trimmed (interactive when JavaScript is on)

    As designed

    1. Classify the intent. A user asks, "Summarise this board." The Orchestrator classifies it as a first-turn summary.
    2. Trim the prompt. It selects only the tool descriptions that intent needs (1,000 tokens, not 3,000) and caps page context at 6,000 tokens.
    3. Stable prefix first. Instructions and tools go first, page and question last, so the 3,000-token prefix can be cached.
    4. Choose the tier. A rule sends first-turn intents of this kind to the small tier, everything else (and unknown intents) to the large tier. The prompt asks for a short answer, about 250 tokens.
    5. Stream the call. Summaries and field explanations of the user's own board are flagged safe to show before the check finishes, so text is forwarded as it arrives, at 0.91 s (typical, small tier).
    6. If it fails midway. An error after partial text ends the stream cleanly; the client keeps the text, marks it incomplete and offers a retry.
    7. Record it. Time to first text and total time are recorded; the content check runs on the finished text. Intents that must be checked first are not streamed.

    Without the fix

    1. Message. 0.7 s goes on network and context assembly.
    2. Everything in the prompt. All 12 tool descriptions and the full page go in: 8,000 tokens.
    3. Prefill. The large tier reads the prompt for 1.6 s before it can write anything.
    4. Decode. It generates 400 tokens one at a time, 13.33 s, sending nothing.
    5. One reply. The complete reply arrives at 15.63 s. The long profile waits 30.17 s.

    Under the hood

    The mechanism is the split between prefill and decode (Under the hood: Prefill and decode).

    • The PagedAttention paper describes the prompt phase as parallelisable over the whole prompt, and the generation phase as producing tokens one at a time, which "severely underutilizes GPU computation" and is "responsible for most portion of the latency of a single request".
    • The DistServe paper names the matching metrics: time to first token for prefill, time per output token for decoding.
    • Anthropic's latency documentation (as of 2026-10-22) recommends a faster model, fewer prompt and output tokens, and streaming.

    The planning model (formulas under Assumptions) is not a documented provider formula, and its cache term is an assumption: OpenAI's documentation says caching reduces processing before the response starts but does not guarantee lower latency. Measure the rates on your own traffic.

    Against the 2 s goal, set Prefill to 1.2 (shrunk prompt, 1.93 s), then 0.6 (cached prefix, 1.33 s).

    Time to first text, typical request, large tier, streamed (interactive when JavaScript is on)
    Overhead0.7 s
    Prefill, 8,000 tokens at 5,000 a second1.6 s
    First decode step0.03 s
    Total2.33 s of 2 s
    Over budget by0.33 s

    What went wrong / failure modes

    Failure What happens Mitigation
    Streaming fails midway An error can arrive as an event inside a stream already started (Anthropic streaming documentation) Keep the partial text, mark it incomplete, offer a retry; this design does not resume
    Cold-cache requests set the tail The first request after the cache lapses pays the full prefill: 2.53 s for the long profile on the large tier, above the 2 s target Measure time to first text by cache state
    Idle connections Anthropic notes delays between events while tool input is produced; the WHATWG standard says legacy proxy servers are known to drop HTTP connections after a short timeout and suggests a comment line every 15 seconds or so Heartbeat comments, a client stall timer
    Unvetted streamed text Text is shown before the check Do not stream replies that need validation first (see guardrails)
    Wrong tier A small-tier summary can be weaker Unknown intents go up; review samples per tier

    Results

    Illustrative targets from the budget model, not measurements. Seconds:

    Step Typical: first text Typical: complete Long: first text Long: complete
    Original, not streamed (user sees the end) 15.63 15.63 30.17 30.17
    Stream 2.33 15.63 3.53 30.17
    Shrink prefix and context, shorter answers 1.93 10.23 2.53 19.17
    Cache the 3,000-token prefix 1.33 9.63 1.93 18.57
    Small tier for first-turn intents 0.91 3.68 1.11 6.66

    With 60% of typical requests on the small tier and 40% on the large, the means are 1.08 s to first text and 6.06 s complete. On the large tier the long profile only just meets the 2 s goal (1.93 s) and still takes 18.57 s to finish.

    Tradeoffs and when NOT to use this approach

    • Streaming improves the feel of the wait, not the total time, and complicates the client.
    • Shorter answers may omit detail; practitioners differ on the trade, so test it with users.
    • Do not stream output that code will parse or that must be validated first.
    • Skip the small tier if the intent classifier is less reliable than the latency problem is costly.
    • Background jobs with no user waiting do not need this.

    Design rules extracted

    1. Budget latency as stages (overhead, prefill, decode) before choosing fixes; measure the tail with a long profile.
    2. Cut input tokens every request pays for: select tools by intent, cap page context.
    3. Put the stable prefix first so it can be cached, and track time to first text by cache state.
    4. Stream user-facing replies, and design the mid-stream failure path on day one.
    5. Keep a non-AI path for fixed answers.

    Assumptions

    All numbers are illustrative. First text = 0.7 + uncached input / prefill rate + 1 / decode rate; complete = 0.7 + uncached input / prefill rate + output / decode rate.

    • Overhead 0.7 s (0.3 network, 0.4 context assembly). Large tier: prefill 5,000 tokens/s, decode 30/s. Small tier: prefill 15,000/s, decode 90/s.
    • Typical: 2,000 + 3,000 + 3,000 page = 8,000 in, 400 out. Long: 5,000 prefix + 9,000 page = 14,000 in, 800 out.
    • Not streamed, typical: 0.7 + 8,000 / 5,000 + 400 / 30 = 15.63 s; long: 0.7 + 2.8 + 26.67 = 30.17 s. Streamed first text: 0.7 + 1.6 + 0.033 = 2.33 s; long 3.53 s.
    • Shrink: tools 3,000 to 1,000, long page 9,000 to 6,000, answers 400 to 250 and 800 to 500. Typical 6,000 in, long 9,000 in.
    • Cache: the 3,000-token prefix adds no prefill time (assumed). Large tier, typical: 0.7 + 3,000 / 5,000 + 0.033 = 1.33 s, complete 0.7 + 0.6 + 8.33 = 9.63 s; long 1.93 s and 18.57 s. Cold cache, long: 0.7 + 1.8 + 0.033 = 2.53 s.
    • Small tier, typical: 0.7 + 0.2 + 0.011 = 0.91 s, complete 3.68 s; long 1.11 s and 6.66 s. Means: 0.6 x 0.91 + 0.4 x 1.33 = 1.08 s; 0.6 x 3.68 + 0.4 x 9.63 = 6.06 s.
    • The split, token counts, rates, product and users are invented.

    Last verified: 2026-10-22

    Ask about this article

    Get answers grounded in this post. AI-generated — based on this article, and may be imperfect.

    Scaled AI Weekly

    Enjoyed this? Get more like it every Monday.

    Real architecture decisions, LLMOps patterns that survive production, and engineering leadership advice — from 12+ years of building at enterprise scale. Free. No spam. Unsubscribe anytime.

    Get new articles by email

    Free. We email a confirmation link first, and you can unsubscribe from any email.

    Join engineers building production AI systems

    Comments