ChatGPT, Claude, Gemini, Llama and other large language models (LLMs) can explain code, write emails, translate, summarise and reason through problems. Under the surface, the core idea is surprisingly simple: predict the next token.
Tokens#
LLMs do not read characters or whole words; they read tokens - frequent chunks of text from a fixed vocabulary (often 30,000-200,000 entries). Common words are one token, rare words are split into pieces:
"Vindhyaedu teaches Python" → ["V", "ind", "hya", "edu", " teaches", " Python"] (illustrative)
A rough rule for English is about 4 characters or ¾ of a word per token. Hindi and other Indic scripts usually need more tokens per word, which is one reason they cost more and work less well in many models.
Next-token prediction#
Given the tokens so far, the model outputs a probability for every token in the vocabulary. Generation is a loop:
- Feed the prompt in.
- Get probabilities for the next token.
- Pick one (sampling), append it.
- Repeat until a stop token or a length limit.
Let us build the smallest possible language model - a bigram model that predicts the next word only from the current word:
after 'students': {'learn': 0.6, 'build': 0.4}
after 'learn' : {'python': 0.33, 'sql': 0.33, 'java': 0.33}
after 'python' : {'.': 0.5, 'at': 0.25, 'and': 0.25}
generated: trainers teach python . students learn pythonA real LLM does the same job, but instead of counting word pairs it uses a neural network with billions of parameters that looks at thousands of previous tokens, trained on trillions of tokens of text and code.
LLM असल में बहुत बड़ा "अगला शब्द बताओ" वाला model है। पिछले शब्दों (tokens) को देखकर वह हर संभव अगले token की probability निकालता है, एक चुनता है, जोड़ता है, और यही दोहराता है। हमारा छोटा bigram model सिर्फ़ एक पिछला शब्द देखता है; ChatGPT या Claude जैसे models हज़ारों पिछले tokens देखते हैं और अरबों parameters से सीखते हैं।
Temperature and sampling#
The model outputs scores called logits; softmax turns them into probabilities. Temperature reshapes them before sampling:
temperature 0.2: python=0.98 java=0.02 sql=0.00 cooking=0.00 temperature 0.7: python=0.69 java=0.22 sql=0.08 cooking=0.00 temperature 1.0: python=0.59 java=0.27 sql=0.13 cooking=0.01 temperature 1.5: python=0.49 java=0.29 sql=0.18 cooking=0.03 10 samples at t=0.2: ['python', 'python', 'python', 'python', 'python', 'python', 'python', 'python', 'python', 'python'] 10 samples at t=1.5: ['sql', 'python', 'sql', 'python', 'java', 'python', 'sql', 'java', 'python', 'python']
- Low temperature (0-0.3) - focused, repeatable answers: code, data extraction, factual Q&A.
- Higher temperature (0.7-1.0+) - more varied, creative text: brainstorming, stories.
- Top-k / top-p (nucleus) sampling - only sample from the most likely tokens, cutting off nonsense.
The Transformer and self-attention#
Modern LLMs are Transformers (introduced in 2017). Their key component is self-attention: for each token, the model computes how much to "attend" to every other token in the context.
In "The trainer explained the bug to Ravi because he found it", attention helps decide that "he" most likely refers to the trainer.
Each token is turned into three vectors - a query (what am I looking for?), a key (what do I contain?) and a value (what information do I pass on?). Attention weights are the softmax of query·key scores:
attention from 'he': Ravi 0.33 he 0.31 careful 0.09 the 0.06 output vector shape: (8, 4)
"he" pays most attention to "Ravi" (and to itself), because their vectors point in similar directions - this is how attention links a pronoun to the person it refers to. Real models learn the query/key/value projections, use many attention heads in parallel, and stack dozens of layers. Because attention looks at all tokens at once, Transformers train efficiently on GPUs - which made today's huge models possible.
How an assistant like ChatGPT or Claude is trained#
| Stage | What happens | Result |
|---|---|---|
| 1. Pre-training | predict the next token on huge amounts of text and code | a "base model" that knows language and facts, but just continues text |
| 2. Instruction fine-tuning | train on examples of instructions and good answers | follows requests ("summarise this", "write a SQL query") |
| 3. Preference training (RLHF and related methods) | people (and AI feedback) rank answers; the model learns to prefer helpful, honest, harmless ones | a helpful, safer assistant |
Context window, hallucinations, RAG and tools#
- Context window - the maximum number of tokens the model can consider at once (the prompt, documents and its own answer). Larger windows let you paste long documents, but cost more.
- Knowledge cut-off - the model only knows what was in its training data, up to some date.
- Hallucination - the model may produce fluent, confident text that is false (invented facts, fake references, wrong numbers), because it generates plausible continuations, not verified ones.
Two techniques make LLMs far more reliable in real applications:
RAG (retrieval-augmented generation) - search your own documents for relevant passages (often with embeddings), put them in the prompt, and instruct the model to answer only from them, with citations:
user question ──► embed ──► search vector database of college documents ──► top 3 passages
│
answer with citations ◄── LLM ◄── prompt = instructions + passages + question
Tool use (function calling) and agents - the model can call a calculator, a database, a code interpreter or a web search, then use the results. An AI agent repeats think → call tool → observe → think until the task is done.
# Sketch of calling an LLM API from Python (not runnable here - needs an API key)
from anthropic import Anthropic
client = Anthropic() # reads the ANTHROPIC_API_KEY environment variable
reply = client.messages.create(
model="claude-sonnet-5-5", # check the provider's docs for current model names
max_tokens=300,
messages=[{"role": "user", "content": "Explain binary search to a first-year student in 5 lines."}],
)
print(reply.content[0].text)
Continue with the Prompt Engineering course on Vindhyaedu to learn how to write prompts that get reliable, well-structured results from these models.
Summary#
- LLMs read tokens and predict the next token; generation repeats that prediction.
- Softmax turns logits into probabilities; temperature and top-p control randomness.
- Transformers use self-attention so every token can use information from every other token.
- Assistants are built by pre-training, instruction fine-tuning and preference training.
- LLMs can hallucinate; RAG, tools and human review make them dependable.