ML Systems: From Weights to Work. Lesson 3. What attention actually computes.

This educational session uses fictional healthcare operations examples. This session uses AI narration.

From an identifier to a contextual representation

An embedding maps a token identifier into a vector of learned numbers. A token is not necessarily a word, and its vector is not a row in a verified fact database. The same token can contribute differently in different contexts because later layers transform and combine its representation with other tokens. Position information also matters: a sequence should distinguish a reversal, a date correction and a quoted example even when they contain many of the same words.

Imagine a fictional account note: the clinician is approved for one location, but the second location is still pending. A useful representation must preserve the contrast, the entities and the relationship between approval and location. It is not enough to retrieve the word approved. The downstream application still needs authoritative records because a learned representation can encode an incorrect interpretation just as easily as a correct one.

Queries, keys and values

In scaled dot-product attention, the model projects representations into queries, keys and values. A query is compared with keys through dot products, scaled by the square root of the key dimension. A mask excludes positions that are not allowed, and a softmax turns the remaining scores into weights. The output is a weighted combination of value vectors. Different heads can learn different projections; the familiar formula is softmax of QK transpose divided by square root of d, plus a mask, multiplied by V.

The word attention can mislead. The weights are numerical routing coefficients inside a learned computation. They are not a statement that a source is trustworthy, that an entity is authorized, or that a sentence is a faithful explanation. A high weight on the wrong date can help produce a confident wrong answer. A permission check belongs in application code even when the relevant permission document is in context.

Causality and the two inference phases

A causal language model prevents a position from using future positions when predicting the sequence. During prompt processing, many prompt positions can be computed in parallel while respecting that causal structure. During ordinary autoregressive generation, the next token depends on the sequence generated so far. These different shapes of work help explain why prompt processing and token-by-token decoding have different performance characteristics.

A key-value cache stores earlier key and value representations so decoding does not recompute them from scratch for every new token. It spends memory to save work. Grouped-query attention shares key-value heads among multiple query heads, reducing cache requirements relative to using one key-value head per query head. The cache contains model activations for a session; it is not a durable patient record or an approval ledger.

Context design is an information-selection problem

Increasing the context window changes how much information can be supplied. It does not establish that the right evidence will be used correctly. A compact packet with record identifiers, source times and an explicit conflict can be easier to evaluate than an unstructured transcript containing repeated stale assertions. Compression has its own risk: a summary can omit a limitation that changes the decision.

For a healthcare worker, preserve the exact fields that determine the task: practice, clinician, payer, location, effective date and evidence status when applicable. Do not treat embedding similarity as an exact join. Retrieval can nominate candidate records; the application must verify that the returned record belongs to the right scope and matches the requested identifiers. Evaluate both retrieval recall and whether the model's final answer remains supported by the selected evidence.

Your decision exercise. A fictional retrieval test returns the correct enrollment document in 98 of 100 cases. In 12 of those 98, the answer uses a similarly named clinician or an outdated location. An attention visualization highlights the word approved. The current system passes all retrieved snippets directly to the model without checking record identifiers. Which change most directly targets the observed failure while preserving a useful test?

Option 1. Keep retrieval recall as one metric, add exact scope and entity checks, and separately score supported answers against the current record.

Option 2. Increase the number of retrieved snippets until the approval phrase receives more attention.

Option 3. Replace the exact clinician identifier with its embedding so semantic similarity controls the join.

Pause here and commit to a choice. Name the decisive evidence and one limitation before continuing.

Reference analysis. Retrieval recall and answer correctness are different stages. Here the right document is usually present, yet entity and temporal binding still fail. Enforce the structured scope and identifier checks before presenting a compact evidence packet, then measure both retrieval and supported-answer performance on the same held-out cases. This does not guarantee the remaining interpretation is correct; preserve conflicts and test whether the model cites the relevant field. An attention heatmap does not validate identity, currency or authority, and adding more snippets can increase ambiguity.

Now change one fact. Now the correct record is missing from retrieval in 30 percent of cases, while entity checks are perfect on returned records. Explain why the highest-priority intervention changes, and what a valid unknown response should contain.

Draft a six-field evidence packet aloud. Include the requested entity, the source identity, the observation time, the exact supporting value, a conflict or missing-data field, and the action boundary. Then explain why a key-value cache, a vector index and an authoritative workflow record cannot be used interchangeably, even though all three may help a model answer a question.
