By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Model inference takes seconds or minutes. Your API needs to decide how to handle that latency: block the client, stream tokens back as they arrive, or hand off to a background worker. Each pattern has tradeoffs, and picking the wrong one will either frustrate users or waste infrastructure.
A) SYNC B) STREAMING C) ASYNC JOB
client ─▶ server client ─▶ server client ─▶ server ─▶ queue
...wait 8 s... ◀ token ◀ token ◀ ... client ◀ 202 + job id
client ◀ answer (words appear live) worker runs the task
client polls or gets a webhook
simple, bad UX best for chat best for minutes-long work
ok under ~2 s TTFT is what counts survives a closed tabSynchronous is the simplest: client sends a request, server waits for the model, returns the full answer. It works fine for calls under a couple of seconds—think classification or a one-line summary. Beyond that, users see a frozen screen.
Streaming sends tokens back as the model generates them. The client sees words appear live. This is standard for chat and writing tasks. Time to first token (TTFT) becomes the metric that matters most.
Async jobs are for work that takes 30 seconds or longer, or anything that must survive a browser tab closing. The server returns a job ID immediately (202 status), the client polls for results or listens for a webhook, and a worker pool handles the actual inference.
Proxies and load balancers between your code and the client can buffer the entire response before sending it. You'll think you're streaming, but the client waits for everything. Trace your actual network path to confirm tokens flow through.
Model calls that go silent for 30-60 seconds will hit default idle timeouts. Set generous timeouts on your proxies and load balancers. Send periodic heartbeats during long tool calls to keep the connection alive.
When a client disconnects mid-stream, cancel the upstream model call. You stop paying for tokens nobody will read. Handle this explicitly—it's not automatic.
If you have guardrails that validate output, decide whether to validate as tokens arrive or buffer a few sentences first. Streaming token-by-token validation adds latency and complexity.
API ─▶ enqueue job ─▶ [ QUEUE ] ─▶ workers (autoscale on queue depth)
│ │
▼ ▼
job table ◀──── status/result ───── store results
(id, state, progress)
▲
└── client polls GET /jobs/{id} or receives webhook / pushThe API enqueues a job and returns its ID. A pool of workers pulls jobs from the queue and scales based on queue depth. Results go into a database. The client polls or receives a webhook when done.
Make workers idempotent. A retry must not charge a card twice or send a notification twice. Store progress so the UI can show 'searching... summarizing...' instead of a blank loading screen. Add a dead-letter queue for jobs that fail repeatedly.
Set a maximum runtime and token budget per job. Without limits, a stuck worker or runaway agent can drain your account.
Perceived latency is a feature. Showing partial progress and streaming text can make a 10-second answer feel faster than a silent 4-second one.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.