Self-attention, Q/K/V, multi-head attention, and next-token prediction — the architecture behind every modern LLM, with healthcare admin scenarios.
To understand a sentence, AI needs to figure out which words refer to which. In healthcare admin documents, this is everywhere:
What does it refer to? The vendor? The equipment? Last week? A human reader instantly knows it's "the equipment" — that's the only thing that can fail an inspection. The LLM has to figure out the same thing, but using only math.
Older AI architectures (RNNs, LSTMs) processed words one at a time, left to right, with limited memory of what came before. They struggled with these reference questions, especially over long distances. Attention is the breakthrough that fixed this — and is what makes modern LLMs work.
The transformer reads the entire input at once. No one-word-at-a-time memory bottleneck.
For each word, decide how much every other word matters. "it" should pay attention to "equipment" — much less to "last".
Each word's representation is updated by blending in the words it's paying attention to. Now "it" knows it means "equipment".
The whole process repeats dozens of times, each layer building richer context. By the end, every token "knows" the whole sentence.
For attention to work, each word/token needs to play three roles simultaneously. Think of it like a hospital filing system:
"What am I looking for?" The word's question — what kind of context would help me understand myself?
"it" Q-vector says: "Looking for whatever can fail inspection."
"What do I represent?" The word's identity tag — what kinds of queries should match me?
"equipment" K-vector says: "I am a physical thing that can be inspected."
"What's my actual content?" The information the word delivers if attention chooses it.
"equipment" V-vector carries: "[the actual meaning of equipment]" — context to mix in.
One attention pass captures one type of relationship. But sentences have many kinds of relationships happening at once:
So transformers use multiple attention heads in parallel — typically 8 to 64 heads, each learning a different relationship type. They run independently then concatenate. Think of it as 16 different highlighters, each tracking one kind of connection.
This is the actual attention pattern for a healthcare admin sentence. Each row is a query word ("I'm looking…"). Each column is a key word ("…at this"). The cell colour shows how strongly the row attends to the column.
A modern LLM stacks 32 to 96 of these layers — each one performs the same set of operations on the previous layer\'s output. Here\'s what one layer does:
Each layer captures different patterns. Studies of trained models show:
For a healthcare query like "summarise this vendor proposal and flag risks", early layers identify the verbs and nouns; middle layers connect "proposal" to all the semantic content; late layers actually reason about what counts as a risk.
Attention is powerful but expensive. The math: each token attends to every other token. So compute scales as N² where N is the number of tokens.
| Tokens in Prompt | Attention Operations | Healthcare Equivalent |
|---|---|---|
| 1,000 | 1 million | ~3 pages of text |
| 10,000 | 100 million | ~30-page vendor proposal |
| 100,000 | 10 billion | Entire MOH circular library |
| 1,000,000 | 1 trillion | 10 years of meeting minutes |
This is why context windows exist — the model literally can\'t fit unlimited text into one attention computation. Modern models offer 128K–1M tokens; AgentSea\'s practical limit is more modest.
After all those layers of attention, the transformer\'s job is shockingly simple: predict the most likely next token. Then run the whole stack again to predict the token after that. And again. And again.
For the next token after "requesting", the model produces a probability distribution over the entire vocabulary (~100,000 tokens). The top candidates might look like:
Sampling picks one token from this distribution — often the highest probability, sometimes randomised slightly. That picked token is appended to the sequence, and the whole process repeats for the next token.
The transformer can think, but only about what\'s in its prompt. How do we get our hospital\'s actual policies into that prompt? That\'s what RAG solves — the next explainer.
🔍 Continue to RAG →