Local LLM Inference
A practical explanation of local LLM inference, covering the inference process, prefill and decoding, KV cache, hardware, batching, latency, throughput, and serving.
Local LLM inference is the process of running a large language model on hardware you control and using it to generate predictions without sending the model execution to a remote AI provider. It is the core process behind local AI assistants, coding tools, private chatbots, document processing systems, and other applications that use locally hosted language models.
Running a model locally is only the first step. Once the model is installed, developers need to understand how inference works, how memory is used, why some models are much faster than others, and how factors such as context length, batching, quantization, and hardware affect performance.
This article focuses specifically on the inference process rather than general local LLM installation. Understanding these fundamentals makes it easier to choose an inference runtime, diagnose performance problems, and design applications around local models.
What Is LLM Inference?
Inference is the process of using a trained model to produce an output from an input. For a language model, the input is usually a sequence of tokens and the output is generated one token at a time.
Training changes the model's parameters so that it learns patterns from large datasets. Inference does not normally change those parameters. Instead, the trained weights are loaded into memory and used to calculate the next token.
Input text
↓
Tokenization
↓
Model computation
↓
Next-token probabilities
↓
Token selection
↓
Next token
↓
Repeat
↓
Generated textThis distinction is important because inference is generally much less computationally expensive than training the same model, although large models can still require substantial hardware.
How Local LLM Inference Works
A local inference system consists of several components working together. The model weights provide the learned parameters, the inference runtime executes the mathematical operations, and the hardware performs those operations.
Application
↓
Inference API
↓
Inference Runtime
↓
Model Weights + KV Cache
↓
CPU / GPU
↓
Generated TokensThe runtime is responsible for much more than simply loading a model. It manages tensors, memory, token generation, attention computation, caching, hardware acceleration, batching, and other operations required to generate responses efficiently.
The Two Main Phases of LLM Inference
Modern autoregressive LLM inference can be understood as two major phases: prefill and decoding.
Prefill
During prefill, the model processes the existing input context. If a user sends a prompt containing hundreds or thousands of tokens, the model processes that sequence before generating the answer.
The model performs attention and other computations across the input and constructs the internal state needed for subsequent token generation.
Decoding
During decoding, the model generates new tokens sequentially. After generating one token, the model uses it as part of the growing sequence and predicts the next token.
Prompt
↓
Prefill
↓
Token 1
↓
Token 2
↓
Token 3
↓
Token 4
↓
...
↓
End of responseDecoding is inherently sequential, which is one reason generating a long response can take significantly more time than processing a short prompt.
Time to First Token vs Generation Speed
Two different performance metrics are particularly useful when evaluating LLM inference: time to first token and token generation speed.
Time to first token, often abbreviated TTFT, measures how long the system takes to produce the first generated token after receiving a request. It includes request processing and the initial model computation.
Generation speed is commonly expressed in tokens per second. It describes how quickly the model produces additional tokens during decoding.
| Metric | What it measures | Important factors |
|---|---|---|
| TTFT | Time until first generated token | Prompt length, prefill speed, hardware |
| Tokens per second | Generation speed | Model size, memory bandwidth, decoding |
| End-to-end latency | Total request duration | Prompt, generation length, queueing, network |
| Throughput | Total tokens processed over time | Batching, concurrency, hardware |
What Is the KV Cache?
The key-value cache, commonly called the KV cache, is one of the most important concepts in transformer inference.
During attention computation, the model produces key and value representations for tokens. During autoregressive generation, previously calculated information can be reused instead of being recomputed from scratch for every new token.
The KV cache stores this information so that subsequent decoding steps can access it efficiently.
Prompt tokens
↓
Key / Value calculations
↓
KV Cache
↓
Reuse during decoding
↓
Faster generationThe benefit is substantial, but the cache consumes memory. Longer contexts and larger models generally require more KV-cache memory.
Why Context Length Affects Inference
The context window determines how much input and previous conversation the model can process at once. Increasing context length can increase both computation and memory requirements.
A model configured with a short context may use significantly less memory than the same model configured for a very long context. This is particularly important when running locally on hardware with limited VRAM.
Model Weights and Memory
The model weights are usually the largest fixed memory component of an LLM inference workload. A model with more parameters generally requires more memory, although the exact requirement depends heavily on numerical precision and quantization.
For example, a model stored using lower-precision representations can require substantially less memory than the same model stored using higher precision.
Inference also requires additional memory for activations, runtime structures, temporary tensors, and the KV cache.
Total inference memory
≈ Model weights
+ KV cache
+ Runtime overhead
+ Temporary tensors
+ Other application memoryVRAM vs System RAM
Local inference can use both GPU memory and system RAM. The exact distribution depends on the inference runtime and configuration.
When a model fits comfortably into GPU memory, GPU execution can provide high performance. If the model is larger than available VRAM, some runtimes can place part of the workload in system RAM.
This can make larger models runnable on limited GPUs, but transferring data between CPU memory and GPU memory can reduce performance.
GPU Acceleration in Local Inference
GPUs are well suited to LLM inference because many model operations can be executed in parallel. Modern inference runtimes can use GPU acceleration to process matrix operations and other tensor computations efficiently.
However, having a powerful GPU does not automatically guarantee high inference speed. Model size, quantization, memory bandwidth, runtime implementation, context length, and workload concurrency all matter.
CPU Inference
LLMs can also be executed entirely or partially on a CPU. CPU inference is useful when no suitable GPU is available or when the workload does not require high generation speed.
Modern CPU inference can be surprisingly capable with smaller quantized models. The main limitation is usually throughput and latency compared with an appropriately configured GPU.
Quantization and Inference
Quantization reduces the numerical precision used to represent model weights. This can significantly reduce memory requirements and can make a model practical on hardware with limited RAM or VRAM.
Common local model variants include different quantization levels, with lower-bit formats requiring less memory but potentially introducing larger quality differences.
| Approach | Memory usage | Potential trade-off |
|---|---|---|
| Higher precision | Higher | Better preservation of original weights |
| 8-bit quantization | Lower | Usually relatively small quality trade-off |
| 4-bit quantization | Much lower | Greater potential quality loss |
| Very low-bit quantization | Very low | Higher quality-performance trade-off |
Quantization is particularly useful for local inference because memory capacity is often the limiting factor on consumer hardware.
Batching
Batching means processing multiple requests or sequences together instead of handling every request independently.
A batch allows the hardware to perform more parallel computation. This can significantly improve overall throughput when multiple users or requests are active at the same time.
However, batching can increase individual request latency if a request has to wait for other work to be scheduled or if the batch becomes too large.
| Workload | Priority | Typical goal |
|---|---|---|
| Interactive chatbot | Low latency | Fast response for each user |
| Batch document processing | Throughput | Process as many tokens as possible |
| Multi-user API | Balanced | Good latency and high throughput |
Continuous Batching
Continuous batching is a serving technique that dynamically manages active requests rather than waiting for an entire static batch to finish.
As one request finishes or progresses, another can be added to the workload. This can keep the hardware busy and improve utilization in multi-user inference systems.
Continuous batching is especially useful for production services where requests arrive at different times and have different prompt and generation lengths.
Throughput vs Latency
Inference systems often have to balance latency and throughput. Latency describes how quickly one request is completed, while throughput describes how much total work the system can process over time.
Optimizing exclusively for one can hurt the other. For example, aggressive batching may increase total tokens processed per second while making individual requests wait longer.
Memory Bandwidth and LLM Inference
LLM inference is often heavily influenced by memory movement. During decoding, the system repeatedly accesses model weights and cached attention information.
This means that memory bandwidth can be just as important as raw compute performance for certain workloads. Two GPUs with similar compute specifications can behave differently when running the same local model if their memory systems differ significantly.
This is one reason why simply comparing the number of compute units or theoretical operations is not enough to predict real-world LLM performance.
Inference Runtimes
An inference runtime is the software layer that executes the model. It loads model weights, manages tensors and memory, performs model operations, and coordinates CPU or GPU execution.
Different runtimes are optimized for different environments and use cases.
- llama.cpp for efficient local inference across CPUs and GPUs
- Ollama for simplified local model management and API access
- vLLM for high-throughput model serving
- Transformers-based runtimes for flexible Python workflows and experimentation
The best runtime depends on whether the goal is simple desktop experimentation, application development, or high-throughput production serving.
Local LLM APIs
A local inference runtime can expose a model through an HTTP API. This allows applications to communicate with the model as a separate service.
Frontend / Client
↓
Application Backend
↓
Local HTTP API
↓
Inference Server
↓
Model
↓
Generated ResponseThis architecture is useful because the application does not need to know exactly how the model is executed. The inference service can potentially be replaced with another runtime or model without changing the entire application.
Streaming Responses
Streaming allows generated tokens to be returned to the client as they are produced instead of waiting for the complete response.
Streaming does not necessarily make the underlying model generate tokens faster. Its main benefit is perceived and interactive latency because the user can see the response immediately after the first token arrives.
Request
↓
Inference
↓
Token 1 → Client
↓
Token 2 → Client
↓
Token 3 → Client
↓
Token 4 → Client
↓
DoneSpeculative Decoding
Speculative decoding is an inference technique that uses a smaller draft model to propose multiple tokens and a larger model to verify them.
When the larger model accepts several proposed tokens, multiple generation steps can effectively be completed with fewer expensive large-model iterations.
The technique can improve generation speed in suitable configurations, but its effectiveness depends on the relationship between the draft and target models and the workload.
Context Processing and Prompt Length
Long prompts can increase the amount of work required during prefill. This is especially noticeable when applications repeatedly send large system instructions, conversation histories, or retrieved documents.
For applications with large recurring prefixes, techniques such as prompt or context caching can reduce repeated computation when supported by the inference system.
Reducing unnecessary context is also one of the simplest ways to improve inference efficiency.
Inference with RAG
Local LLM inference can be combined with retrieval-augmented generation. In that architecture, relevant documents are retrieved first and then included in the model's context.
User Query
↓
Retriever
↓
Relevant Documents
↓
Prompt Construction
↓
Local LLM
↓
ResponseThe retrieved documents increase the input context, which can increase prefill work and KV-cache usage. Efficient retrieval is therefore important not only for answer quality but also for inference performance.
Multi-User Local Inference
Running a model for one user is much simpler than serving many users simultaneously. With multiple requests, the inference server has to manage queues, memory, batching, concurrency, and potentially different context lengths.
A workstation that feels fast for a single developer may not provide acceptable performance when exposed as a public API.
How to Measure Local LLM Performance
A useful benchmark should measure more than one number.
- Time to first token
- Prompt processing speed
- Generation tokens per second
- End-to-end response latency
- Maximum concurrent requests
- Peak RAM usage
- Peak VRAM usage
- Total throughput
- Failure rate under load
The benchmark should use representative prompts and response lengths. A tiny prompt can make a system appear much faster than it will be when processing the application's real workload.
Why the Same Model Can Run at Different Speeds
The model itself is only one part of the performance equation. The same model can have very different inference speeds depending on the environment.
- CPU or GPU hardware
- Available VRAM
- Memory bandwidth
- Quantization format
- Inference runtime
- Context length
- KV-cache configuration
- Batch size
- Number of concurrent users
- CPU and GPU offloading
- Driver and software configuration
This is why benchmark results from another computer should be treated as a reference rather than a guarantee of the performance you will get locally.
Common Local Inference Problems
The Model Does Not Fit in Memory
If the model and runtime require more memory than the system has available, loading can fail or the operating system can begin using much slower storage as virtual memory.
The most direct solutions are choosing a smaller model, using stronger quantization, reducing context size, or adding hardware with more memory.
Generation Is Too Slow
Slow generation can result from an oversized model, CPU-only execution, GPU memory limitations, excessive offloading, an unsuitable quantization format, or inefficient runtime configuration.
Before changing everything at once, measure where the bottleneck is. A smaller model with good hardware utilization can often provide a much better experience than a larger model that barely fits.
VRAM Is Full but the GPU Is Not Fully Utilized
High VRAM usage does not necessarily mean that the GPU is performing useful computation at maximum capacity. The workload can be limited by memory bandwidth, CPU processing, data transfers, synchronization, or sequential decoding.
GPU utilization should therefore be interpreted together with token generation speed, memory usage, and the rest of the inference pipeline.
Long Context Makes the Model Much Slower
A larger context increases the amount of information the model must process. Long prompts can increase prefill time, while a large KV cache can increase memory pressure during decoding.
Reducing irrelevant context and retrieving only the information needed for a request can often improve both performance and answer quality.
How to Choose an Inference Setup
The right local inference setup depends on the workload rather than simply the largest model your hardware can run.
| Use case | Practical priority | Typical approach |
|---|---|---|
| Personal assistant | Simplicity and latency | Small or medium quantized model |
| Coding assistant | Quality and context | Coding-focused model with suitable context |
| Private documents | Privacy and memory | Local model with retrieval pipeline |
| Batch processing | Throughput | Larger batches and optimized serving |
| Public API | Concurrency and reliability | Dedicated inference server |
Local Inference for Development
For development, simplicity is usually more valuable than squeezing out every possible token per second. A straightforward local runtime can provide a stable environment for testing prompts, application logic, structured outputs, and AI features.
The local model can also serve as a development substitute for a cloud model, although developers should remember that differences in model behavior can affect production results.
Local Inference for Production
Production inference requires more than getting a model to generate text. The serving system needs predictable resource usage, monitoring, request limits, authentication, health checks, error handling, and capacity planning.
For production workloads, dedicated inference servers and specialized serving runtimes can provide better concurrency and throughput than a simple desktop-oriented setup.
The architecture should also separate the application from the inference service so that the model or runtime can be replaced without rewriting the entire product.
Best Practices for Local LLM Inference
- Choose the smallest model that meets the application's quality requirements.
- Use quantization when it provides a useful memory-performance trade-off.
- Leave memory headroom for the KV cache and runtime overhead.
- Use realistic context sizes.
- Measure TTFT and generation speed separately.
- Benchmark with realistic prompts and response lengths.
- Test concurrent requests before exposing a model as a service.
- Use batching when throughput matters.
- Use streaming for interactive applications.
- Keep the application layer separate from the inference runtime.
- Monitor RAM, VRAM, latency, throughput, and failures.
- Optimize only after identifying the actual bottleneck.
Frequently Asked Questions
What is local LLM inference?
Local LLM inference is the process of running a trained language model on hardware you control and using it to generate responses locally instead of sending inference requests to a remote AI provider.
What is the difference between prefill and decoding?
Prefill processes the existing input context before generation begins. Decoding generates new tokens sequentially. Prefill is strongly affected by prompt length, while decoding determines much of the perceived response generation speed.
Why does the KV cache use so much memory?
The KV cache stores attention-related information from previous tokens so that the model does not need to recompute it during every decoding step. Larger models, longer contexts, and some configurations can therefore require substantial cache memory.
Is GPU required for local LLM inference?
No. LLMs can run on CPUs, especially smaller and quantized models. GPUs are generally preferred when higher generation speed or throughput is required.
How can I make local LLM inference faster?
Common approaches include choosing a smaller model, using appropriate quantization, reducing unnecessary context, using GPU acceleration, optimizing batching, and selecting an inference runtime suited to the workload. The correct optimization depends on the actual bottleneck.
Conclusion
Local LLM inference is more than simply loading a model and generating text. The inference process involves prompt processing, token decoding, attention, KV-cache management, memory movement, hardware acceleration, and request scheduling.
The most important performance factors include model size, quantization, available RAM and VRAM, memory bandwidth, context length, inference runtime, batching, and concurrency. Understanding these factors makes it easier to select hardware and configure a local model appropriately.
For personal use, a relatively small quantized model and a simple runtime may be all that is needed. For production applications, inference becomes an infrastructure problem involving throughput, latency, concurrency, monitoring, and reliability.
The practical approach is to measure real workloads, identify the actual bottleneck, and optimize the part of the inference pipeline that limits performance rather than assuming that a larger model or more powerful hardware will automatically produce a better system.