How an AI chat really works
Five ideas explain almost everything: messages, tokens, the context window, tools, and agents. Learn these and the AI stops being magic. It becomes a strange but powerful tool that works best when you know its limits.
The whole trip in one picture
Messages, and the stage whisper
A chat is a stack of messages: yours, then the AI’s, then yours again. But before the show starts, someone slips the actor a note the audience never sees — the system prompt. It sets the tone, the rules, and the job, and when your message argues with that note, the note usually wins. Some models also mutter to themselves first: a little thinking… before the answer.
Under the hood
Conversations are structured as user and
assistant messages, with a system message at the
start acting as a highly influential meta-instruction — it steers
behaviour even over conflicting user instructions, though jailbreaks can
sometimes override it. Reasoning models emit intermediate
“thinking” tokens as part of the assistant message, and a
single message can hold multiple parts: text, files, tool calls and tool
results.
Tokens: the words are really numbers
The AI never sees your words. A tokenizer chops the text into chunks and gives each chunk a number. Common chunks get one number each; a rare word shatters into many little pieces. You pay for every chunk — the ones you send in and the ones that come back out.
Under the hood
Tokens are a compression of text into sub-word chunks: encode on the way in, decode on the way out, billed both directions. Vocabulary size is the trade-off — one token per character would compress nothing, one per string would be unlookupable, so tokenizers keep common character groupings plus single characters as a fallback. That fallback is why unusual words fragment into many tokens, and why the cost of a call is genuinely hard to estimate up front: you cannot know exactly how your text will be chopped.
The context window: one desk, fixed size
The model works at a desk of a fixed size, and everything must fit on it at once: the stage whisper, the whole conversation so far, the tool menu, and the space for its answer. The size is a hard limit. Overfill the desk and the call fails, or the answer gets cut off mid-sentence.
Under the hood
The context window is the combined input and output token budget, hard-coded per model. Input includes the system prompt, the full message history and tool definitions; output is whatever the assistant generates. Exceeding the limit causes API errors or truncated generations, so long conversations creep toward the ceiling turn by turn.
Lost in the middle
Here is the strange part: a bigger pile on the desk makes the AI worse, not better. It pays most attention to the start and the end of what it reads; the middle goes blurry. So the winning move is not stuffing the desk — it is the smallest set of things that really matter. And when a chat gets long, start a fresh one.
Under the hood
“Lost in the middle” is the measured tendency of LLMs to weight early and late context more heavily than the middle, which is why output quality degrades as the window fills. The craft of fighting it has a name: context engineering — curating the optimal set of tokens at inference time. Given a finite attention budget, good context engineering means finding the smallest possible set of high-signal tokens that maximise the likelihood of the outcome you want.
Tools: the AI writes a note, your code does the job
The AI cannot touch the world. What it can do is write a very tidy request note: “please run this tool with these details”. Your code reads the note, does the actual job, and passes the result back. That little loop is the whole trick — and it is enough to build really powerful things. One warning: hand the AI a menu of too many tools and it gets slower and dumber, like anything else crowding the desk.
Under the hood
Tool definitions (name, description, JSON-schema parameters) are given to the model up front. The model responds with a tool call message carrying an id and arguments — nothing has executed yet. Your application runs the tool locally and returns a result message (success or error, tied to the same id), and the model continues with the full history. Error handling is essential. And because every definition sits in the context window, tool count is a real cost: trouble can start around 12 tools, and many frameworks recommend a maximum of about 6.
Agents and workflows: who decides when to stop
Both are ways of chaining many AI calls together, and one question tells them apart. A workflow is a recipe: your code runs fixed steps and your code decides when it is done. An agent is the AI with tools in a loop, deciding for itself when the job is finished — more flexible, less predictable. And a single call? That is just an API call. Not everything needs to be an agent.
Under the hood
The split comes from Anthropic’s “Building Effective Agents”: workflows are predetermined, code-defined sequences of LLM calls where program logic controls termination; agents are given tools and choose their own next actions and stopping point. Workflows shine at decomposing hard tasks into smaller ones LLMs are better at, at repeatable and parallelisable processes, and at inserting human review (HITL) between steps. Agents suit ambiguous tasks that need improvisation.