Blog ·
Choosing between local and cloud LLM inference often starts with two assumptions: local execution avoids network delay, and cloud APIs have effectively unlimited capacity. Both assumptions break down once you measure time-to-first-token (TTFT), inter-token latency (ITL), and KV cache memory consumption under realistic context budgets.
1. Two Phases, Two Hardware Bottlenecks
Inference splits into two distinct phases with different resource demands:
- Prefill: Processes all input tokens in parallel to build attention states and populate the key-value (KV) cache. Because matrix-matrix operations can saturate compute units, long prompts primarily increase TTFT and stress compute throughput.
- Decode: Generates output tokens sequentially, one step at a time. Each step reads growing KV cache state across transformer layers to produce the next token, making single-stream decode heavily dependent on memory bandwidth rather than peak compute.
| Dimension | Local Inference | Cloud Inference |
|---|---|---|
| Network RTT | None (~0 ms over IPC/loopback) |
Adds connection, TLS, and routing overhead on every request |
| Memory Ceiling | Fixed by local unified memory or VRAM | Backed by larger accelerator pools and tiered/offloaded KV caches |
| Queueing & Batching | Controlled locally; single-user or small local queue | Shared infrastructure; batching improves throughput but can add jitter |
| Prefix Cache Reuse | Local to the host/engine instance | Shared across requests when prompt prefixes match on the provider |
| Marginal Cost | Power and hardware depreciation; zero per-token fee | Per-token / per-request billing; scales directly with context length |
2. Context Budgets Are Memory Budgets
A model’s maximum supported context window is an upper bound on token positions, not a zero-cost budget. During generation, the KV cache stores keys and values across layers for active tokens. Its memory footprint grows with:
- Prompt and output token count (larger context windows expand state linearly per token).
- Model architecture parameters (number of layers, KV heads, and head dimension).
- Tensor precision (for example, FP16/BF16 versus quantized KV cache formats).
- Concurrent sequences (parallel streams multiply active KV memory).
On a local machine with fixed unified memory or VRAM, an oversized system prompt, large tool schemas, and unpruned conversation history crowd out model weights or force aggressive context truncation. Even if a local model fits in memory, long prefills delay the first token and long contexts increase memory reads on every decode step.
3. Architectural Trade-Offs
- Short context, low latency sensitivity, or strict data locality: Local inference avoids external network hops and keeps prompts on-device, provided the model and KV cache fit comfortably inside available memory without paging or swapping.
- Long context, variable bursts, or shared system/RAG prefixes: Cloud inference handles larger working sets and can amortize repeated prompt prefixes via caching, at the cost of network round-trip time, external dependency risk, and per-token cost on long inputs.
- Hybrid routing: Keeping latency-sensitive or short-context tasks local while offloading large-context synthesis or heavy bursts to cloud endpoints works only when context serialization and fallback timeouts are explicitly budgeted.
4. Failure Modes
- Unbounded context growth: Appending full tool outputs and chat turns without compaction inflates prefill time, shrinks available memory headroom, and degrades relevance when relevant facts sit mid-context.
- KV cache exhaustion: Exceeding memory limits triggers eviction, recomputation, or out-of-memory failures—turning a working prototype into high-latency stalls under load.
- Ignoring tail latency: Evaluating only average response time hides spikes caused by cold caches, large prompt prefills, or network and queueing variance.
Companion Video and Related Reading
- Companion Video: Watch the full walkthrough on the CyberBodhak YouTube channel (
youtubeId: PENDING_YOUTUBE_ID). - Related CyberBodhak Posts: Explore more architectural notes at https://cyberbodhak.com/posts/.