How language models predict text
زبان کے ماڈل متن کا اندازہ کیسے لگاتے ہیں
35 min read
Three ways to see it
A large language model, an LLM, does one thing during inference: given a sequence of tokens, predict the most likely next token. That is the whole loop. A token is roughly a word or part of a word. The English sentence 'I want chai and biscuit' is around seven tokens. The Urdu transliteration 'mujhe chai chahiye' is also a few tokens, with some sub-word splits. The model produces one token at a time, then feeds the new token back as input, and predicts the next one, and the next, until it decides to stop or the user runs out of patience.
Before prediction, words become numbers. Each token is mapped to a vector, called an embedding. The embedding is a long list of numbers, often a thousand or more, and it captures something about meaning. 'Chai' and 'tea' end up near each other. 'Chai' and 'briefcase' end up far apart. The embeddings are not handwritten; they are learned from billions of examples of language. Once words are numbers, the entire machine becomes a matter of arithmetic.
The transformer architecture lets the model pay attention to relevant earlier tokens when predicting the next one. If the sentence is 'Bilal opened the file Saad sent him last week, and the most striking thing about it was its' the model has to look back many tokens, possibly back to 'Saad' or 'file', to decide what comes next. Attention is the mechanism that lets every token, in effect, ask every other token how relevant they are. This is why the same model can write an Urdu poem in the morning and a draft of an FBR notice in the afternoon. The attention pattern is different; the machine is the same.
Quick check
Quick check: what makes modern AI different from a rule-based program?
The why-tree
Why-tree level one: why predict the next token at all? Because almost any task in language can be re-framed as completing text. Translation is completing the next-language version. Summary is completing the words 'in short:'. Answering a question is completing the question with an answer. One prediction engine, many uses.
Try this with Claude
AI-edge prompt: 'Walk me through, in plain Urdu and English, exactly how you produced the previous answer. Tell me what you attended to, where you were guessing, and where I should double-check. Be specific.' This forces a kind of self-explanation that, while imperfect, often reveals weak spots before they cost you.
Sources
Sources and further reading. Vaswani et al., 'Attention Is All You Need' (arxiv.org/abs/1706.03762). Anthropic docs, 'How Claude works' and 'Prompt engineering overview' (docs.anthropic.com). Jay Alammar, 'The Illustrated Transformer' (jalammar.github.io). Stanford CS 224n, course notes on language modelling. OpenAI Cookbook, 'How to count tokens' and 'How sampling works'.