How does a machine that does not think like a human manage to produce answers that sound so human? Ask ChatGPT a question and a response appears in seconds, often with surprising fluency. Behind the seemingly effortless exchange, mathematics does the heavy lifting.
“First, your text gets converted into numbers—specifically, vectors, which are just lists of coordinates that place your words in a massive ‘map of meaning.’ Then, those numbers pass through hundreds of layers of weighted equations,” explains Mir Samreen, a mathematician and AI research analyst specialising in high-dimensional data training and algorithmic optimisation.
From words to numbers
Models first break sentences into smaller units called tokens. A token could be a complete word, part of a word or punctuation. These tokens are assigned numerical identifiers and mapped to vectors — numerical representations that the model can process.
Samreen compares this to a city map, where every word has an address based on meaning. ‘King’ and ‘Queen’ might be neighbours, while ‘Apple’ belongs in the fruit district. A vector, in this analogy, is the word’s address; linear algebra helps calculate relationships between these numerical representations.
In reality, however, these are not fixed addresses. A token’s representation is learned during training and can change as the model processes the surrounding context.
Order matters, too: ‘Dog bites man’ and ‘man bites dog’ contain the same words but mean very different things. Models therefore use mechanisms to represent token positions.
Where attention enters
The model processes these numerical representations through layers of neural-network computation.
In Transformer-based models, self-attention calculates relationships between tokens and assigns different weights to information in the surrounding context, helping the model build context-sensitive representations.
In the original Transformer architecture, attention uses three sets of representations called queries, keys and values. The model calculates relationships between queries and keys, converts them into weights and uses those weights to combine information from the values.
Matrix multiplication is a major part of neural-network computation, allowing large sets of numerical operations to be performed efficiently, often in parallel.
Several attention heads perform different calculations, while Transformer layers also contain feed-forward networks and other components that further transform information.
The mathematics that makes it learn
Four important areas of mathematics underpin many modern AI systems: linear algebra, calculus, probability and statistics, and optimisation. Linear algebra underpins vectors and matrices; calculus supplies gradients for learning; probability and statistics help model uncertainty; and optimisation adjusts parameters to reduce errors.
Samreen likens training to descending a fog-covered mountain. “Calculus provides the slope of the ground right under your feet. It tells the AI: ‘If you adjust your internal settings slightly in this direction, you will move downhill toward fewer mistakes.’ By taking millions of tiny steps downhill using calculus, the AI gradually gets smarter.”
Technically, a loss function evaluates predictions. Backpropagation calculates gradients, and optimisation algorithms adjust parameters to reduce the loss. This process repeats across vast numbers of training examples; it does not usually mean reaching zero error.
From calculation to response
The scale of computation is difficult to imagine. Samreen estimates that a modern model’s response can involve hundreds of billions to trillions of calculations, mostly additions and multiplications. The actual number varies with the model, its size, the length of the prompt and response, and its architecture. Her figure should therefore be treated as an estimate, not a universal count.
Once trained, an autoregressive language model generates an answer through a process called inference. It produces scores, known as logits, for possible next tokens. These scores are converted into a probability distribution, and a decoding method selects the next token. That token is added to the sequence, and the process repeats.
It is not always the single most likely next word. Models work with tokens, not necessarily whole words, and different decoding methods can produce different outputs. The basic journey is prompt, tokens, numerical representations, neural-network computation and, one token at a time, a response.
Why a convincing answer can be wrong
Probability also helps explain why AI can sound certain and still get things wrong. As Samreen explains, AI does not know facts in the way humans do. Language models learn patterns and relationships from training data and use them to estimate which tokens are likely to follow a given context.
They can encode factual information, but generating a response does not automatically mean checking its claims against reliable evidence.
This is the problem behind AI hallucinations: a response can sound convincing without being factually verified. Additional training, information retrieval and external tools can improve reliability, but statistical plausibility does not guarantee accuracy.