Lesson 4 · Building LLM applications
Local and open-source models
Ollama, LM Studio, Hugging Face, Llama, Mistral, Gemma, Qwen — when and how to self-host models.
Both sit behind an OpenAI-compatible API, so the application code is the same either way. Most teams use both.
Local and open-source models
Open-source models (Llama, Mistral, Gemma, Qwen, Phi) can be downloaded and run locally or on your own infrastructure. Tools like Ollama (one-command local model runner), LM Studio (GUI), vLLM (high-throughput serving), and Hugging Face Transformers (programmatic) make this accessible.
The trade-off: open-weight models have tended to trail the most capable closed models on capability (a gap often put at months rather than years; it is a rule of thumb that shifts with each release, so check current benchmarks and your own evaluation), but offer: data privacy (no data leaves your env), cost predictability (no per-token fees), customisation (fine-tuning), and air-gapped deployment. The gap has narrowed over time: when Meta released Llama 3.1 405B in July 2024, it reported results competitive with the leading closed models of the time on many benchmarks.
Why local models matter
Three scenarios demand local models: (1) Compliance — HIPAA, FedRAMP, EU data residency may prohibit sending data to OpenAI. (2) Cost — at very high, steady volume, fixed GPU costs can beat per-token prices; the experiment below shows how high that volume is. (3) Latency/autonomy — local models have no network dependency and no rate limits. The downside: you operate the infra (GPU provisioning, model serving, updates, monitoring).
Local model stack
Model weights (Hugging Face) → serving runtime (vLLM / TGI / Ollama) → OpenAI-compatible API → your app. vLLM is the production serving choice (PagedAttention, continuous batching, high throughput). Ollama is the dev/laptop choice. TGI (Text Generation Inference, by Hugging Face) is the middle ground. All expose OpenAI-compatible APIs so your app code doesn't change.
Running local models
# Ollama — one command to run locally
# Model names change often; check your provider's model list (as of 28 October 2026). Replace <...-model> with a model id from that list.
# Terminal: ollama run <ollama-model-tag> (pick a tag from Ollama's model library)
# Then use the OpenAI-compatible endpoint:
from openai import OpenAI
client = OpenAI(base_url="http://localhost:11434/v1", api_key="ollama")
r = client.chat.completions.create(
model="<ollama-model-tag>",
messages=[{"role": "user", "content": "What is RAG?"}],
)
print(r.choices[0].message.content)
# Works great for dev — but Ollama isn't built for production throughputExperiment: local vs cloud model choice
See when local wins and when cloud wins. Cloud costs assume 75% input and 25% output tokens over a 30-day month. Prices as of 26 October 2026 (USD per million tokens, from our model catalog); check /pricing for current prices. OpenAI has announced it will remove gpt-4o and gpt-4o-mini from its API on 14 January 2027, so treat these as worked examples, not model advice. GPU costs are illustrative: $1,000 per GPU per month for rented GPUs, before the people who run them. Get your own quotes.
What to observe
Local wins on: data privacy, air-gapped, latency, and cost only at very high, steady volume (at the prices and illustrative GPU costs above, about 30M tokens/day before a 70B model on 4 GPUs beats gpt-4o, and far more against gpt-4o-mini). Cloud wins on: capability (the most capable models are usually closed; as a rule of thumb open-weight models trail by months, but check current benchmarks), cost at low-medium volume, and zero ops. Most production systems use BOTH — local for privacy/latency-sensitive traffic, cloud for complex reasoning. The OpenAI-compatible API means your code is identical.
Production local deployment
Production local: vLLM with continuous batching, GPU autoscaling, model versioning (pin weights), monitoring (throughput, latency, GPU utilisation), OpenAI-compatible API for code portability, and a cloud fallback for reliability. Expect some capability gap against the most capable closed models — evaluate the open-weight model you plan to run (e.g. a 70B-parameter model) against your eval set before committing.
Challenge
Your startup needs to process 2M tokens/day of customer support transcripts (75% input, 25% output). That costs about $0.52/day on gpt-4o-mini or $8.75/day on gpt-4o. Is self-hosting an open-weight 70B-parameter model on 4 rented GPUs (illustrative: $4,000/month) cheaper? At what daily volume does it break even against each model, and which hidden costs move that line? (Hint: ops time + eval gap + fallback.) Prices as of 26 October 2026 (USD per million tokens, from our model catalog); check /pricing for current prices. OpenAI has announced it will remove gpt-4o and gpt-4o-mini from its API on 14 January 2027, so treat these as worked examples, not model advice.
Production checklist
Production checklist
0 of 10 checked
Knowledge check
When is self-hosting an open-weight 70B-parameter model cheaper than a cloud model?
Complete
You can now choose and deploy local/open-source models. Next: fine-tuning fundamentals — when fine-tuning beats prompting and retrieval.
Mark this chapter as complete
Track your progress and unlock the next chapter.