Back to blog posts

AI Engineering / Attention

Attention

Updated Jul 2026 5 min read Built from handwritten notes

What is attention?

Q) How does LLM understand entire sentences or paragraphs?
Q) How does it understand the context of the text?
Ans) Attention Mechanism

  • Attention is a mechanism that helps an LLM decide: “When I am processing this word or token, what other words or tokens should I pay attention to?”
  • Attention lets each token look at other tokens and decide which ones are important for the LLM to understand the context or predict the next token.
  • Attention mechanism runs on a trained model.
  • Attention is determined by relationships between tokens.

The original Transformer paper,Attention Is All You Need , made attention the central operation for modern language models.

Attention Score

When LLM reads a word (token), it asks: What other related words shall I pay attention to so that I understand the sentence?

Let's take an example.

User Prompt: The capital of India is _____ ?

When looking at the token “India” the attention mechanism calculates a score between “India” and all other words in the sentence.

Now during pre-training, the LLM has seen lots of text like:
- “India's capital, New Delhi, .... ”
- “New Delhi, the capital of India, ....”
- “Red Fort is in New Delhi, India”
- “PM's resides in the captial - New Delhi, India.”

The model has seen the tokens “capital” and “India” together many times. So LLM determines that when looking at token India, the token Capital can help me understand the context of this sentence. So it gives a high attention score to token Capital.

Attention between word 'India' and the words around it

ThecapitalofIndiais_____high attention to “capital”lowerlow
For token “India”, attention withScore
The0.02
capital0.91
of0.10
is0.09

Thus, attention knows that to understand the context of this sentence and to predict the next token, attention should be given to “capital.”

Example 1: Attention when predicting the next token

User Prompt: The capital of India is _____ ?

Now during pre-training, the LLM has seen lots of text like:
- “India's capital, New Delhi, .... ”
- “New Delhi, the capital of India, ....”
- “Red Fort is in New Delhi, India”
- “PM's resides in the captial - New Delhi, India.”

From repeated patterns, it learns that India and New Delhi are related.

TextDimension: like a place
India[0.95]
New Delhi[0.96]
France[0.92]
Coffee shop[0.35]

From the above table you can see that there are other places which are also semantically related to India like France (both are like a place, so vectors are close). So the answer to user's query can be France or New Delhi.
But since the model has seen lots of text referring to India and New Delhi in the same context, it knows that India & New Delhi are more closely related than India & France. Hence when predicting the next token, the model predicts “New Delhi” instead of France.

Example 2: attention by understanding the pattern

The animal did not cross the road because it was tired.

When the LLM looks at the token “it”, it asks: What other tokens should it attend to so that the LLM understands the context?

The LLM asks a query: Who was tired?

  • The model is trained to know that “tired” is an adjective, and some noun in this sentence will tell who was tired.
  • Two nouns appear: animal and road.
  • The model has been trained that only living things can get tired.
  • So it knows the animal was tired and not road.

Hence, the attention score of “it” with “animal” is high. It understands the context by looking at patterns: “tired” belongs to a noun, only living things can be tired, and animal is a living thing while road is not.

Takeaway

Attention is not magic. It is a learned scoring process. For each token, the model asks which other tokens are useful right now, assigns higher scores to the useful tokens, and uses that weighted context to understand meaning and predict what comes next.