Open Source vs Cloud LLMs

Generative & Agentic AI · GenAI on the Cloud · Advanced · 15 min

Run Llama on your laptop or call Bedrock/Vertex/Azure, and size the hardware.

A moving van: the model's weights are the furniture and the GPU is the truck. Billions of parameters times bytes each tells you the truck size, and quantizing is packing the furniture flat.

The idea in everyday words

Cloud LLM APIs are like taking a taxi: pay per ride, no maintenance, but you don't own the car. Open source models are like buying your own car: upfront cost (GPUs), you control everything, and long-distance it can be cheaper.

Open source / open weights

  • Models: Llama, Mistral, Qwen, DeepSeek, Gemma, Phi.
  • Run locally: Ollama, LM Studio, llama.cpp. Serve at scale: vLLM, TGI.
  • Hub: Hugging Face (models, datasets, Transformers library).
  • Pros: privacy, control, no per-token fee. Cons: GPUs, ops, usually slightly behind top closed models.

Cloud platforms

Cloud Service Models
AWS Bedrock, SageMaker Claude, Llama, Mistral, Titan
GCP Vertex AI Gemini, Claude, Llama
Azure Azure OpenAI / AI Foundry GPT, Llama, Mistral
Direct OpenAI, Anthropic, Google APIs Their own

How much GPU memory?

Memory (GB) ≈ parameters (billions) × bytes per parameter

  • FP16 = 2 bytes → 7B model ≈ 14 GB
  • 8-bit = 1 byte → 7 GB
  • 4-bit = 0.5 bytes → 3.5 GB (fits a laptop!) Add ~20% for context/KV cache. This is quantization: smaller numbers, tiny quality loss.

Break-even: when does self-hosting pay?

Per-token APIs cost more as traffic grows; a GPU costs the same per hour whether it is busy or idle. Find the crossover with your own numbers (the ones below are illustrative only, check the AWS pricing pages):

api_price_per_m = 3.00        # $ per 1M tokens, blended input + output
gpu_hourly = 5.00             # $ per hour for one GPU instance
gpu_tokens_per_sec = 1500     # throughput YOU measured with vLLM
hours = 730                   # one month, always on

gpu_capacity_m = gpu_tokens_per_sec * 3600 * hours / 1_000_000
for monthly_m in [100, 1000, 3000]:
    api = monthly_m * api_price_per_m
    gpus = int(-(-monthly_m // gpu_capacity_m))      # round up
    selfhost = gpus * gpu_hourly * hours
    print(monthly_m, "M tokens/month: API 
quot;, round(api), "| self-host
quot;, round(selfhost), "with", gpus, "GPU")

Below roughly 1.2 billion tokens a month the API wins here; above it the GPU starts to pay off, if it stays busy and you count the engineering time to run it.

Go deeper (engineer level): what really limits self-hosted serving

Memory is weights + KV cache + activations. The weights are fixed. The KV cache grows with tokens × concurrent sequences: a Llama-3-8B-like model (32 layers, 8 KV heads, head size 128) needs 2 × 32 × 8 × 128 × 2 bytes = 128 KiB per token in fp16, so 32 users with 8k-token contexts need about 34 GB (exactly 32 GiB) of cache on top of 16 GB of weights. That is why a model that "fits" on a GPU still runs out of memory under load.

Prefill vs decode. Prefill (reading the prompt) is compute-bound and parallel. Decode (one token at a time) is memory-bandwidth-bound: every step reads all the weights. A rough ceiling for one stream is bandwidth ÷ weight bytes, for example about 600 GB/s ÷ 16 GB ≈ 37 tokens per second on an A10G-class GPU. Batching many users shares each weight read, which is why throughput rises with batch size while per-user latency gets a little worse. Continuous batching and PagedAttention (vLLM) keep the GPU full without wasting cache memory.

Splitting big models. Tensor parallelism splits each layer across GPUs in one node (needs fast links such as NVLink); pipeline parallelism splits layers across stages and adds bubbles. Quantization formats (GPTQ, AWQ, GGUF, FP8) trade a little quality for memory and speed: run your own eval set on the quantized model, because the loss is uneven across tasks (maths and code often suffer first).

Failure modes to plan for: out-of-memory errors only at peak concurrency, one very long prompt slowing every user in the batch, cold starts that take minutes because the weights load from S3 (keep minimum capacity warm), and a self-hosted model drifting behind the managed one you compared it with. Check the SageMaker AI and vLLM docs for current settings.

Step by step

  1. Install:

    Download Ollama.

  2. Pull:

    ollama pull llama3 (4-bit, ~4.7GB).

  3. Run:

    ollama run llama3 → chat locally, offline.

  4. API:

    POST localhost:11434/api/generate from your app.

Code example

BYTES = {"fp16": 2, "int8": 1, "int4": 0.5}

def vram_gb(params_billion, precision):
    return params_billion * BYTES[precision] * 1.2   # +20% overhead

for size in [7, 13, 70]:
    print(size, "B model:", {p: round(vram_gb(size, p), 1) for p in BYTES}, "GB")

# Calling a local Ollama model (works on your machine, not in this browser):
# import requests
# requests.post("http://localhost:11434/api/generate", json={"model": "llama3", "prompt": "Hi"})

Interview tips

  • Know quantization and why 4-bit models run on laptops.

  • Trade-offs: cost, latency, privacy, quality, control.

  • vLLM uses PagedAttention for high-throughput serving.

Challenge: GPU Memory Estimator

Return the estimated memory in GB: params_billion × bytes_per_param, where precision is "fp16" (2), "int8" (1) or "int4" (0.5). No overhead.

Any other precision (a typo like "fp61", or one we did not list) must raise an error (JavaScript: throw), not return a guess.

Example: vram(7, "fp16") → 14

Solve it in the browser with tests, hints and an AI coach.

Practice problems

More in GenAI on the Cloud

Previous: Guardrails & Responsible AI on Bedrock · Next: RAG: Chat With Your Own Documents