01 How to Calculate LLM VRAM Requirements
Calculating the VRAM required to run a Large Language Model (LLM) is a multi-component problem that trips up even experienced ML engineers. The naive approach — "multiply parameters by 2 bytes for FP16" — only accounts for model weights and ignores KV cache, activations, framework overhead, and batch scaling. Understanding every component is critical for production deployments and hardware purchasing decisions.
The complete VRAM formula: Total VRAM = Model Weights + KV Cache + Activation Memory + Framework Overhead. Model weights: parameters × bytes_per_precision (FP32=4, FP16/BF16=2, INT8=1, INT4=0.5 bytes). Framework overhead: PyTorch/vLLM typically adds 0.5–1 GB. Activation memory during inference is small (a few hundred MB for batch=1), but scales with batch size during training.
GPU VRAM Fit Reference
- RTX 4090 — 24 GB: Llama 3 8B in FP16, 70B in INT4 (2× GPU)
- RTX 4080 — 16 GB: 7B/8B FP16, 13B INT4
- RTX 3090 — 24 GB: Same as 4090 but slower bandwidth
- RTX 4070 Ti — 12 GB: 7B INT4 comfortably, 7B INT8 tight
- H100 SXM5 — 80 GB: 70B FP16, 405B INT4
- A100 SXM4 — 80 GB: 70B FP16, fine-tuning 7B full
- A100 PCIe — 40 GB: 30B FP16, 70B INT4 (2× GPU)
- T4 — 16 GB: 7B FP16 inference only
02 Model Quantization: FP32, BF16, INT8, INT4 Compared
Quantization is the single most impactful lever for making large models fit on available hardware. It reduces numerical precision of model weights, dramatically cutting memory requirements with varying degrees of quality impact. Modern techniques like GPTQ, AWQ, and GGUF have made INT4 quantization nearly lossless for most real-world tasks.
- FP32: Baseline (training precision), no loss
- BF16: ≈0% quality loss, standard inference
- INT8 (LLM.int8): <1% perplexity increase
- GPTQ-4bit: ~3–5% perplexity increase
- AWQ-4bit: ~2–4% perplexity increase (better)
- GGUF Q4_K_M: ~3% perplexity, most popular local format
- GPTQ-3bit: ~8–15% perplexity, significant loss
- GPTQ: GPU-optimised, works with ExLlamaV2/AutoGPTQ
- AWQ: Activation-aware, often better than GPTQ at same bitwidth
- GGUF: CPU+GPU hybrid (llama.cpp), cross-platform
- SpQR: Mixed-precision, outlier-aware (research)
- bitsandbytes: Load-time INT8/INT4, easy Hugging Face integration
- EXL2: Variable bit-width per layer, best quality/size ratio
03 Context Window & KV Cache Memory Explained
The context window defines how much text an LLM can "see" at once — including your input prompt, conversation history, retrieved RAG documents, and the model's own output. Modern models range from 4K tokens (older GPT-3.5) to 1M tokens (Gemini 1.5 Pro). Longer context enables more powerful applications but has significant VRAM and latency implications.
The KV cache formula: KV_GB = 2 × layers × kv_heads × head_dim × seq_len × batch × bytes ÷ 1e9. For Llama 3 8B (32 layers, 8 KV heads with GQA, 128 head_dim) at FP16 with 8K context, batch=1: ≈0.53 GB. At 128K context: ≈8.5 GB — larger than the model itself in INT4. Grouped Query Attention (GQA) reduces KV heads from 32 to 8 in Llama 3, shrinking KV cache by 4× compared to standard MHA.
04 GPU Comparison for LLM Inference: H100 vs A100 vs RTX 4090
Hardware choice is one of the most consequential decisions in LLM deployment. Three key metrics determine suitability: memory capacity (fits the model), memory bandwidth (determines decode speed), and compute (determines prefill speed). These are not always correlated — the RTX 4090 has impressive bandwidth but limited VRAM; the A100-40G has 40 GB but lower bandwidth than H100.
- 989 TFLOPS BF16 (with sparsity: 1979 TFLOPS)
- 3.35 TB/s HBM3 bandwidth
- NVLink 4.0: 900 GB/s inter-GPU
- Best for: 70B+ production serving, training
- ~210 tok/s Llama3-70B INT4
- 312 TFLOPS BF16
- 2.0 TB/s HBM2e bandwidth
- ECC memory, NVLink 3.0
- Best for: Enterprise inference + fine-tuning
- ~125 tok/s Llama3-70B INT4
- 82.6 TFLOPS FP32
- 1.008 TB/s GDDR6X bandwidth
- No ECC, consumer driver limits
- Best for: Local inference, prototyping
- ~100 tok/s Llama3-8B FP16
- Unified memory: CPU + GPU share 192 GB
- 800 GB/s memory bandwidth
- Best for: 70B FP16 local, power efficiency
- ~30–40 tok/s Llama3-70B FP16
- No CUDA — Metal/MLX framework
05 Understanding Tokens: Counting, Cost & Optimization
Tokens are the fundamental unit of text in LLMs — not words or characters, but subword pieces from the model's vocabulary (typically 32K–128K token vocabulary using Byte Pair Encoding or SentencePiece). Understanding tokenisation is critical for cost estimation, prompt engineering, and context window planning.
Rule of thumb: 1 token ≈ 4 characters ≈ 0.75 words in English. However, this varies significantly by content type: code is denser (more tokens per line), non-Latin scripts like Chinese use 1–2 characters per token, and structured data (JSON, XML) is very token-inefficient. A 1,000-word essay is roughly 1,333 tokens; a 10-page PDF might be 3,000–5,000 tokens.
06 Vector Embeddings & RAG Architecture
Embedding models transform text into dense numerical vectors that capture semantic meaning. Similar texts produce vectors close together in high-dimensional space, enabling semantic search, recommendation, and retrieval-augmented generation. The quality of embeddings directly determines the quality of RAG — garbage embeddings mean irrelevant retrieved context, which can make LLM responses worse than no RAG at all.
- text-embedding-3-small: 1536-dim, $0.02/1M — best value
- text-embedding-3-large: 3072-dim, $0.13/1M — highest quality
- Cohere embed-v3: 1024-dim, $0.10/1M — multilingual
- BGE-M3 (local): 1024-dim, free — state-of-art open
- nomic-embed-text: 768-dim, free — 8K context window
- Pinecone Serverless: $0.08/GB/month, managed, easy
- Weaviate Cloud: $25/month starter, GraphQL API
- Qdrant: Self-host free, cloud from $25/month
- pgvector: PostgreSQL extension, free, SQL interface
- Chroma: Local dev, free, SQLite-backed
- Milvus: Enterprise scale, Kubernetes-native
07 Fine-Tuning LLMs: LoRA, QLoRA & Full Fine-Tune
Fine-tuning adapts a pre-trained LLM to your specific domain, style, or task format. The choice of technique has massive implications for hardware requirements, training time, and cost. Full fine-tuning delivers maximum quality but requires enormous GPU resources. Parameter-efficient methods like LoRA and QLoRA achieve near-equivalent results at a fraction of the cost.
- All parameters updated
- VRAM: model × 16–20× (FP32 grads + Adam)
- 7B model: needs 4–8× A100 80GB
- Best quality ceiling
- Risk of catastrophic forgetting
- Cost: $500–$5,000 for 7B model
- Trains low-rank matrices only (r=4–64)
- VRAM: model (FP16) + LoRA adapters + optimizer
- 7B model: 1× A100-40G or 2× RTX 4090
- Within 1% of full fine-tune quality
- Adapters can be merged or swapped
- Cost: $50–$200 for 7B model
- Base model in 4-bit NF4 quantization
- LoRA adapters in BF16
- 7B model: fits on single RTX 4090 (24GB)
- 70B model: fits on single A100-80G
- Within 1–2% of full fine-tune quality
- Cost: $10–$50 for 7B model on cloud GPU
08 Inference Optimization: Batching, Streaming & KV Caching
Inference optimisation is where production LLM engineering lives. Raw model performance (tokens/second at batch=1) is rarely the relevant metric for real applications — you need to maximise throughput across concurrent users while keeping per-request latency acceptable. The key techniques are continuous batching, prompt caching, and speculative decoding.
09 LLM API Providers: Cost & Feature Comparison 2025–2026
The LLM API market has become fiercely competitive, with pricing dropping 90%+ since 2023. Choosing the right provider and model for your use case can mean a 100× difference in cost with similar quality. The key dimensions: context window size, input/output pricing ratio, rate limits, latency, multimodal capabilities, and structured output support.
- GPT-4o: $2.50/$10.00 per 1M in/out, 128K ctx
- GPT-4o-mini: $0.15/$0.60 per 1M, 128K ctx
- o1: $15/$60 per 1M, reasoning model
- o1-mini: $3/$12 per 1M, fast reasoning
- GPT-4o Batch: 50% discount, 24hr window
- Function calling, JSON mode, vision
- Claude 3.5 Sonnet: $3/$15 per 1M, 200K ctx
- Claude 3.5 Haiku: $0.80/$4 per 1M, 200K ctx
- Claude 3 Opus: $15/$75 per 1M, top quality
- Prompt caching: 90% discount on cached tokens
- Best for: long-doc analysis, coding, safety
- Tool use, vision, computer use
- Gemini 1.5 Pro: $1.25/$5 per 1M (≤128K)
- Gemini 1.5 Flash: $0.075/$0.30 per 1M
- Gemini 2.0 Flash: Next-gen, competitive pricing
- 1M token context window (1.5 Pro)
- Best for: long-context, multimodal, cost
- Free tier: 15 RPM on Flash
10 RAG vs Fine-Tuning: When to Use Each
RAG and fine-tuning are complementary techniques, not competitors. They solve different problems: RAG addresses what the model knows (factual knowledge), while fine-tuning addresses how the model behaves (style, format, reasoning patterns). Choosing incorrectly is one of the most common and expensive mistakes in production AI development.
- Knowledge changes frequently (docs, news, policies)
- You need source citations/attribution
- Knowledge base is very large (10K+ documents)
- Multiple knowledge domains with different access controls
- Fast time-to-production (<1 week to deploy)
- Budget is limited — no GPU training cost
- Consistent output format is critical (structured JSON, code)
- Domain-specific vocabulary/jargon (medical, legal, finance)
- Reducing prompt length and API cost at scale
- Teaching new reasoning capabilities
- Proprietary communication style/tone
- Latency reduction (shorter system prompts)
11 AI ROI & Business Case for LLM Implementation
Building a defensible AI business case requires quantifying both the benefits (labour savings, output quality improvements, speed gains) and the total cost of ownership (API costs, infrastructure, engineering time, prompt maintenance, quality assurance). Many organisations underestimate the "hidden costs" of AI: prompt engineering, evaluation pipelines, safety reviews, and ongoing maintenance.
12 Quick Reference: GPU Specs, Token Costs & Key Formulas
Tokens/Second (decode): tok_s = memory_bandwidth_GBs / (model_size_GB / params_B) × batch_size
Fine-Tune FLOPs: FLOPs = 6 × params × training_tokens | Time_hr = FLOPs / (gpu_TFLOPS × 1e12 × util × 3600)
GPT-4o: $2.50/$10 | GPT-4o-mini: $0.15/$0.60 | o1: $15/$60 | Claude 3.5 Sonnet: $3/$15 | Claude 3.5 Haiku: $0.80/$4 | Gemini 1.5 Pro: $1.25/$5 | Gemini Flash: $0.075/$0.30 | Mistral Large: $2/$6 | Mistral Small: $0.20/$0.60 | text-embedding-3-small: $0.02/1M | text-embedding-3-large: $0.13/1M (all per 1M tokens)