Skip to content

LLMs: what's actually happening when you hit enter?

DEC 4, 2026 • 6 MIN. READ •

aillmtokenscontext-windowagents

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

LinkTokens

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.

textUnbelievably, the strawberry tasted great!
tokentoken ID
Un2265
bel9880
ievably153516
,11
·the290
·strawberry101830
·tasted88244
·great2212
!0
Text split into tokens (· is a space), each mapped to a token ID. Split with OpenAI's o200k_base tokenizer, used by GPT-4o.

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 in strawberry, the model doesn't see s-t-r-a-w-b-e-r-r-y. In the diagram above, strawberry is 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.

LinkNext-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:

ts

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.

LinkThe 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:

json

[
	{ "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.

start
a while later
near the limit
CLAUDE.md
you
model
summary of older turns
The context window fills up with every turn. Near the limit, older turns get dropped or summarized.

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.

LinkTemperature

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.

LinkTraining 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.

LinkHallucinations

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.

LinkFrom 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:

  1. The app tells the model which tools exist, like read_file, edit_file or run_command.
  2. The model answers with text that says "call read_file with this path".
  3. The program, not the model, actually runs it.
  4. The result is added to the context, and the model is called again.
  5. Repeat until the model answers without asking for a tool.
ts

const messages = [userPrompt]

while (true) {
	const reply = model(messages, tools)
	messages.push(reply)
	if (!reply.toolCall) break
	messages.push(runTool(reply.toolCall))
}
Modelpredicts next tokensTool callread_file(path)Run toolprogram executes itResultadded to contextrepeat untilthe model answerswithout a tool call
An agent: a model, some tools and a loop.

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.

LinkKey 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