LLMs: what's actually happening when you hit enter?
DEC 4, 2026 • 6 MIN. READ •
I started with plain chat, asking questions and copying code back and forth. Then I ran models locally with Ollama on my desktop. At work, Cursor and Claude came along, and I started using Claude Code and Codex on my hobby projects too. Now I use them every day: I type a prompt, hit enter, and code shows up.
Most of the time, anyway. Deep into a long session, the agent ignored a rule I'd given it at the start. It confidently called a function that doesn't exist. I ran the same prompt twice and got two different answers. Each time I shrugged and tried again, until I realized I was treating tools I rely on like magic.
I like learning how the tools and libraries I use work under the hood, so I dug in. It turns out that once you know a handful of concepts, most of the "weird" behavior stops being weird. Here they are:
- Tokens: what the model actually reads
- Next-token prediction: the only thing the model does
- The context window: the model's working memory
- Temperature: why answers vary
- Training vs prompting: what it knows vs what you tell it
- Hallucinations: why it makes things up
- Agents: how a chat model ends up editing your code
Tokens
A model doesn't read text the way we do, letter by letter or word by word. Before anything else, the text is split into tokens: chunks that can be a whole word, part of a word, a space plus a word, or a single character. Each token is then mapped to a number, and those numbers are what the model sees.
The exact split depends on the model's tokenizer, but common words are usually one token, while rare or long words get split into pieces. A rough rule of thumb for English is about 4 characters per token. You can paste text into OpenAI's tokenizer to see it for yourself.
This explains a few things:
- Pricing and limits are in tokens, not words or characters.
- Counting letters is hard for a model. If you ask how many
rs are instrawberry, the model doesn't sees-t-r-a-w-b-e-r-r-y. In the diagram above,strawberryis a single token: one number. The letters are hidden inside the token. - Code and non-English text often cost more tokens, because they're split into smaller pieces.
Next-token prediction
Here's the part that surprised me the most: at its core, an LLM does one thing. Given a sequence of tokens, it predicts which token comes next.
More precisely, it outputs a probability for every token in its vocabulary.
For The cat sat on the, mat might get a high probability, floor a bit less, banana almost none.
One token is picked, appended to the input, and the whole thing runs again:
let tokens = tokenize(prompt)
while (true) {
const probabilities = model(tokens)
const next = pick(probabilities)
if (next === END) break
tokens.push(next)
}That's it. Every answer, every function it writes, every explanation is generated one token at a time, each one based on everything before it. This is also why responses stream in: they're produced token by token.
The context window
The model doesn't remember your previous message. To keep a conversation going, the app sends the earlier messages along with your new one:
[
{ "role": "user", "content": "What's a CSS rem?" },
{ "role": "assistant", "content": "A rem is relative to the root font size..." },
{ "role": "user", "content": "And em?" }
]The model reads all of it and predicts the next answer. The maximum number of tokens it can work with at once (input plus output) is the context window.
This explains why long sessions go wrong. Once the conversation gets close to the limit, the tool has to drop or summarize older messages. Your instruction from an hour ago may simply not be there anymore. And even within the limit, a long and noisy context makes it harder for the model to focus on what matters.
That's why tools like Claude Code have project instruction files (CLAUDE.md):
they're added to the context of every session, so the important stuff is always there.
Temperature
Remember the pick(probabilities) step? There are different ways to pick. Always taking the most likely token
is predictable, but often repetitive and bland. Sampling according to the probabilities gives more variety:
sometimes more creative, sometimes wrong.
Temperature controls this. A low temperature sharpens the probabilities, so the top tokens win almost every time. A high temperature flattens them, so less likely tokens get picked more often. That's why the same prompt can give two different answers: a different token picked early on leads to a different path for everything that follows.
Training vs prompting
So where does the knowledge come from? From training. A model is first trained on a huge amount of text to get good at predicting the next token. Then it's fine-tuned to follow instructions and behave like an assistant. Everything it learned is stored in its weights, and those weights are frozen once training is done.
That has two consequences. First, it has a knowledge cutoff: it doesn't know about anything after its training data was collected, like a library version released last month.
Second, your prompt doesn't change its weights. Some apps save "memories", but that just means adding them back into the context later. That's why giving it the right docs, code and examples matters so much: it's how it gets information it didn't learn in training.
Hallucinations
Put next-token prediction and training together, and hallucinations make sense. The model isn't looking up facts in a database. It's generating a plausible continuation. Most of the time, the plausible answer is also the correct one. But when it isn't sure, a plausible-looking function name or URL is still a plausible continuation, so that's what comes out, with the same confidence as everything else.
This is why I always check what it writes: run the tests, read the diff, open the link.
From chat to agent
So how does a model that only predicts text end up editing files and running tests on my machine?
The model can't do anything by itself. What tools like Claude Code add is a loop with tools:
- The app tells the model which tools exist, like
read_file,edit_fileorrun_command. - The model answers with text that says "call
read_filewith this path". - The program, not the model, actually runs it.
- The result is added to the context, and the model is called again.
- Repeat until the model answers without asking for a tool.
const messages = [userPrompt]
while (true) {
const reply = model(messages, tools)
messages.push(reply)
if (!reply.toolCall) break
messages.push(runTool(reply.toolCall))
}That's what an agent is: a model, some tools and a loop. And now the earlier sections apply here too: every file it reads fills up the context window, every step is next-token prediction, and a wrong guess early in the loop can carry through.
Key takeaways
- Tokens: the model reads chunks of text, not letters or words.
- Next-token prediction: every answer is generated one token at a time.
- Context window: the model's working memory, so keep sessions focused.
- Temperature: tokens are sampled, so the same prompt can give different answers.
- Training vs prompting: give it the docs and code it didn't learn in training.
- Hallucinations: it generates plausible text, so verify what it writes.
- Agents: a model, some tools and a loop, all sharing one context window.
So that's it: no magic, just next-token prediction in a loop, with a context window to manage!
Learn More
- [1hr Talk] Intro to Large Language Models
https://www.youtube.com/watch?v=zjkBMFhNj_g · Andrej Karpathy
- Large Language Models explained briefly
https://www.youtube.com/watch?v=LPZh9BOjkQs · 3Blue1Brown
- Let's build the GPT Tokenizer
https://www.youtube.com/watch?v=zduSFxRajkE · Andrej Karpathy
- Tokenizer
https://platform.openai.com/tokenizer · see how text is split into tokens
- Building effective AI agents
https://www.anthropic.com/engineering/building-effective-agents
- Attention Is All You Need
https://arxiv.org/abs/1706.03762 · the transformer paper