A large language model is a neural network trained on one deceptively simple objective: given a sequence of tokens, predict the next token. Everything else — answering questions, writing code, summarising — is an emergent consequence of doing that extremely well at scale.
Training happens in stages: - Pre-training: self-supervised next-token prediction over trillions of tokens. - Supervised fine-tuning (SFT): curated instruction/response pairs teach the model to follow requests. - Preference optimisation (RLHF / DPO): human or AI preference data aligns tone, refusals, and helpfulness.
At inference the model outputs a probability distribution over its whole vocabulary for the next position, and a *sampler* picks one token. The chosen token is appended and the process repeats — this loop is called autoregressive decoding.
python
# Conceptually, generation is a loop over a probability distribution
tokens = tokenizer.encode("The capital of France is")
for _ in range(10):
logits = model(tokens)[-1] # scores for every vocabulary entry
next_id = sample(logits, temperature=0.7)
tokens.append(next_id)
print(tokenizer.decode(tokens))Gotcha: the model has no built-in notion of truth. It optimises plausibility, which is why grounding (retrieval, tools, citations) matters so much in production systems.