esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • AI papersPages
  • Interview KitPages
  • Book a callContact
  • Download ResumeContact
Let’s talk

I’ll build the agent for it.

What Is Multi-Head Attention?

How a transformer looks at a text from several angles at once, what it costs, and where it can go wrong, explained for your AI interview.

In short

Multi-head attention runs several attention steps side by side, each with its own learned view of the text, then joins their results. This lets a transformer track several kinds of links between words at the same time.

10 min readIntermediateVery common · estimateFacts checked 26 November 2026

Say it out loud

Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.

Multi-head attention means the model runs several attention steps at the same time, each with its own learned view of the text, then joins the results. One head might link the word refund with account, another might link not with the complaint. The base model in the 2017 paper used eight heads. Each head works on a smaller slice, so the total cost stays close to one big head. I'd still test the head count on my own tasks, because more heads can make quality worse.

What is multi-head attention, in plain words?

Attention is how a transformer decides which words in the text matter for the word it is reading now. Each word gets a weighted mix of the words in the text, itself included, with more weight on the ones that matter for it.

Multi-head attention runs several of these attention steps at the same time. Each step is a head. Each head has its own learned way of comparing words, so it can pick up a different kind of link. The model then joins what all the heads found.

Think of a claims adjuster reading a 40-page policy with three highlighters. One colour marks dates, one marks exclusions, one marks amounts. Nobody tells the highlighters what to mark. At the end the adjuster reads all three colours together. A head is like one highlighter: it learns its own job during training.

Key words

Head
One attention step inside a transformer, with its own learned settings.
Attention
A way for each word to weigh the other words that matter for it.
Query, key and value
Three smaller versions of a word's vector, each made by a learned multiply: the query asks, the key is matched against, the value is passed on.
Vector
A list of numbers that stands for a word inside the model.
Transformer
The model design from the 2017 paper, based on attention with no recurrence or convolutions.

In an AI model the highlighters are heads, and the pieces of text, called tokens, are what each head compares. Take a bank's support assistant reading a customer message: "the refund is still not in my account". One head might link refund with account. Another might link not with the complaint. The model learns those possibilities from its training data.

How does multi-head attention work, step by step?

  1. 1

    Make smaller views

    Each word's vector is projected into a query, a key and a value for every head. In the paper's base model that is 8 heads, each working on 64 numbers.

  2. 2

    Run the heads in parallel

    Each head compares its query with every key, then mixes the values. The heads run side by side, at the same time.

  3. 3

    Join and project

    The outputs of all heads are joined and projected once more, into one vector per word.

One word, several heads, one joined answer

One word's vector

512 numbers in the base model

Heads run in parallel

Head 1

its own learned view

Head 2

its own learned view

Heads 3 to 8

same steps, own weights

Join the heads

concatenate, then project once more

Richer output

each word sees several links

  1. 1The vector for one word is sent to every head.
  2. 2Each head works at the same time as the others and hands on its own result.
  3. 3The joined result goes on to the next step of the model.
One word's vector goes to every head. The heads run side by side, their results are joined, and the model carries one richer vector per word onward. The sizes match the paper's base model.

The cost stays close to one big head. Eight heads of 64 numbers make 512, the same width as the whole vector, so each head is smaller and the total work is similar.

Tip

Asked what a head learns, don't claim you know what each one does in a given model. Say the heads learn different jobs, and that researchers inspect them to find out what those jobs are.

Here is a small hand-made version with two heads and three words from a support ticket: "refund", "not" and "received". Each head takes its own fixed two-number slice of the vector, and the code prints how much each word attends to the others.

Python
# Two heads look at the same three tokens. Real heads learn their own weights; this sketch hand-picks a slice of each vector.
import math

tokens = ["refund", "not", "received"]
vectors = [
    [1.0, 0.0, 0.0, 1.0],  # refund
    [0.0, 1.0, 1.0, 0.0],  # not
    [1.0, 1.0, 0.0, 0.0],  # received
]

def softmax(scores):
    biggest = max(scores)
    exps = [math.exp(s - biggest) for s in scores]
    total = sum(exps)
    return [round(e / total, 2) for e in exps]

def weights(query_index, part):
    q = vectors[query_index][part]
    scores = []
    for v in vectors:
        k = v[part]
        scores.append(sum(a * b for a, b in zip(q, k)) / math.sqrt(2))
    return softmax(scores)

for head, part in [("head 1", slice(0, 2)), ("head 2", slice(2, 4))]:
    for i, word in enumerate(tokens):
        print(head, word, weights(i, part))

Output

head 1 refund [0.4, 0.2, 0.4]
head 1 not [0.2, 0.4, 0.4]
head 1 received [0.25, 0.25, 0.5]
head 2 refund [0.5, 0.25, 0.25]
head 2 not [0.25, 0.5, 0.25]
head 2 received [0.33, 0.33, 0.33]

Read the output. Head 1 gives refund 0.4 on itself and 0.4 on received, so it links the two. Head 2 keeps refund mostly to itself, at 0.5. The same word gets a different view in each head. The numbers are made up and tiny. Real heads learn their numbers from data. This sketch skips the learned projections and the value mixing, so it shows only the attention weights.

Why not use one big attention step?

With one head, averaging blurs the view. The paper says a single head's averaging gets in the way, so several heads let the model attend to different information at once.

  • One head

    One blended view

    Everything is averaged into one view, so detail gets lost.

  • Eight heads

    Several views at once

    Each head learns its own job, and the total cost stays close to one full-size head.

  • Too many heads

    Quality can drop

    In the paper's tests, quality dropped with too many heads, so the count needs testing.

The paper tested its own head counts. Its single-head model scored 0.9 BLEU points worse than the best setting. The paper scores its results with BLEU on translation tasks. A team building a support bot should test its own head count on its own tickets, the same way.

  • “Multi-head attention allows the model to jointly attend to information from different representation subspaces at different positions.”

    Vaswani et al., 2017

  • “We show that uptrained GQA achieves quality close to multi-head attention with comparable speed to MQA.”

    Ainslie et al., 2023

The 2023 grouped-query paper describes a cheaper variant with fewer key-value heads than query heads. After uptraining, it reaches quality close to full multi-head attention, with speed close to a single key-value head.

What does the interviewer want to hear?

A strong answer covers

  • A plain definition: several attention steps run side by side, then joined.
  • Each head has its own learned projections, so it can learn a different link.
  • The heads run in parallel, and the total cost stays close to one big head.
  • Why it helps: one blended view loses detail, and heads keep several views.
  • The trade-off: too many heads can lower quality, and cheaper variants like grouped-query attention exist.
  • Testing the head count on your own tasks, not assuming more is better.

Common traps

  • Trap"It's just several models running together."

    BetterIt's one model where each attention step has its own learned settings, and their outputs are joined.

  • Trap"More heads always means a smarter model."

    BetterThe paper found quality dropped with too many heads. Test the count on your own work.

  • Trap"Each head is a fixed slice of the sentence."

    BetterEach head works on learned projections of the whole vector. Its job is learned during training.

Can you spot the right idea?

Spot the right statement

Which statement about multi-head attention is right?

Quick check

A team says their model has 16 heads, so it must be better than one with 8. What is the best reply?

Can you answer these from memory?

Flash cards · tap a card to flip it

1 / 6

What will they ask next?

  1. Q043What is self-attention?11 min
  2. Q044What are queries, keys and values?11 min
  3. Q045Why scale attention scores by √d_k?12 min
  4. Q047What is positional encoding, and why is it needed?11 min
  5. Q055What is the KV cache, and why does it speed up generation?12 min

Learn it in depth

Last updated on 26 November 2026

Building an AI assistant that reads long documents?

I design and build AI products and agents, including the interface and the system behind them. Book a short call and tell me what yours needs to do.