What Is a Large Language Model (LLM)? — AI Engineering Explained
LLMs are neural networks that predict the next token using Transformer attention — understanding them unlocks every AI engineering discipline.
What is it?
A Large Language Model (LLM) is a neural network with billions of parameters trained on massive text corpora to predict the next token given a sequence of previous tokens. The core architecture is the Transformer — a stack of attention layers that learn relationships between tokens at any distance. LLMs like GPT-4, Claude, and Gemini are not retrieving stored answers — they compute probability distributions over vocabularies, token by token, generating output one piece at a time.
How it works
Transformer architecture: input tokens are embedded into vectors (dim 512–4096), then passed through N layers of Multi-Head Self-Attention (MHSA) + Feed-Forward Networks (FFN). MHSA computes: Attention(Q,K,V) = softmax(QKᵀ / sqrt(d_k)) × V. This operation is O(n²) in sequence length — the fundamental scaling bottleneck.
Training: next-token prediction on a corpus (15 trillion tokens for Llama 3). Loss = cross-entropy between predicted and actual next token. Gradient descent updates all ~70B parameters per step. Training a frontier model costs $50–100M in compute.
Inference: autoregressive — one token per forward pass. A 4K-token response = 4K forward passes. Each pass through a 70B model requires reading ~140GB of weights from GPU memory — inference latency is dominated by memory bandwidth, not raw compute.
Mixture of Experts (MoE): instead of one dense FFN per layer, MoE uses N expert FFNs and routes each token to K of them (K=2 of N=8 is common). GPT-4 is believed to be a ~1.8T parameter MoE — only ~200B parameters activate per token, giving frontier capability at dense-200B inference cost.
Code example
import anthropic client = anthropic.Anthropic() response = client.messages.create( model="claude-opus-4-5", max_tokens=1024, messages=[{"role": "user", "content": "Explain attention in 2 sentences"}] ) print(response.content[0].text) # Cost: 1024 output tokens × $15/1M = $0.015 per call
Key concepts
Common mistakes
Using max_tokens too high for simple tasks — if you want a yes/no classification but set max_tokens=4096, you waste inference budget and slow responses. Set max_tokens to 10–20 for classification tasks.
Ignoring token counting when building prompts — 128K context sounds huge but 500 source files easily exceed it. Always measure token usage before assuming content fits.
Treating temperature=0 as fully deterministic — floating point operations across different hardware can produce slightly different results even at temp=0.
Not using prompt caching for repeated system prompts — if your system prompt is 2000 tokens and you make 100K calls/day, that is 200M tokens/day. Anthropic and OpenAI offer 80–90% cost reduction on cached prefixes.
Using a frontier model for every task — Llama 3.1 8B handles classification, extraction, and simple generation at 1/100th the cost of GPT-4. Benchmark your specific task on smaller models first.
Interview questions on this topic
Explain the difference between Dense Transformers and Sparse MoE models. How does routing affect inference throughput?
Practice answering these with structured learning → Start on CrackLab
Continue learning — explore the full AI Engineering curriculum
30+ structured topics from Python fundamentals to deploying production LLM systems. Free to start.
Related free guides
RAG grounds LLM answers in your own knowledge base by retrieving relevant document chunks at query time — making it the go-to architecture for enterprise AI.
Prompt engineering is the highest-leverage LLM skill — the right technique can improve accuracy by 30–40% with zero additional compute during training.
AI agents extend LLMs with a feedback loop — perceive → reason → act → observe → repeat — enabling them to solve multi-step tasks that a single prompt cannot.