Table of contents
First introduced in 2017’s “Attention Is All You Need,” Transformers revolutionized AI. They power today’s most advanced language models — including GPT, Claude, and others — by processing entire text sequences at once and using attention mechanisms to find relationships between words anywhere in the context. This makes them far more capable than older models that generated text one token at a time with limited memory.
Text generation works like this: the model see all the words once, remember the full context and predicts the next word, adds it to the sequence, and repeats.
Example:
Write a story → “Once”
Write a story once → “Upon”
Write a story once upon → “A”
Prompt → Look at all words → Predict next word → Add it → Repeat
⚡ Transformer Diagram
The transformer diagram may look scary and make you want to quit GenAI learning—but don’t worry. As developers, we don’t need to go too deep. Let’s explore each part of the transformer at a high level.

🧩 Tokenization: Turning Text into Tokens
What it is: Tokenizer breaks the text into smaller pieces called tokens—like words, parts of words, punctuation, or emojis. GenAI models work with these tokens instead of raw sentences, making text easier for them to understand.
Why it matters: Models understand tokens, not raw text.
Algorithm:
• WordPiece: Splits tricky words into parts.
• BPE: Groups frequent pairs for efficiency.
• SentencePiece: Great for languages with no spaces.
Example:

Tips
• Use the same tokenizer at inference time that was used during training, or embeddings will mismatch.
• Count tokens before sending prompts to control cost limits
function simpleTokenizer(text) {
return text.match(/(\w+|[^\w\s])/gu) || [];
}
const tokens = simpleTokenizer("Write a story once upon a time!");
console.log(tokens);
// ["Write","a","story","once","upon","a","time","!"]
📏 Embeddings: Giving Tokens Numbers
What it does: Each token is mapped to a list of numbers called an embedding vector. These numbers let GenAI models compare meanings and find similarities.
Why it matters: Numbers let the model understand word meaning.
Key properties:
• Distances ≈ semantic similarity. The smaller the distance between two embedding vectors, the more similar the words.
• Vector arithmetic works: king – man + woman ≈ queen.

Code:
import { Configuration, OpenAIApi } from "openai";
const openai = new OpenAIApi(new Configuration({
apiKey: process.env.OPENAI_API_KEY,
}));
async function getEmbedding(text) {
const response = await openai.createEmbedding({
model: "text-embedding-ada-002",
input: text,
});
return response.data.data[0].embedding;
}
(async () => {
const userQuery = "Write a story once upon a time."
const embeddingVector = await getEmbedding(userQuery);
console.log(embeddingVector);
})();
// Each word will be represent in the array of number
// [[ 0.01234567, -0.03456789, ..., 0.00123456], [...]]
🔍 Self-Attention – Focusing on the Right Words
What it does: Each token looks at every other token to decide what’s important for meaning.
Example:
“I put the book on the shelf because it was dusty.”
When the model sees “it”, self‑attention lets it check every other word to see what “it” most likely refers to.
• Highest connection: “book” (because books can be dusty)
• Lower connection: “shelf” (possible but less likely)
• Minimal connection: words like “I”, “put”, “because”
Why it matters: Captures relationships between words, even if they’re far apart in the sentence.
👀 Multi-Head Attention – Seeing Relationships in Different Ways
Why multiple heads: Instead of one “view,” the model uses multiple attention heads. Each head captures a different type of relationship — like grammar, theme, or tone.
Example:
Imagine reading this sentence: “The bank can guarantee deposits will be safe.”
One attention head might focus on financial meanings of bank, another on the action guarantee, while a third looks at deposits. Each “head” understands different parts or meanings simultaneously.
Why it matters: Combining all these focused views gives the model a richer, fuller understanding of the sentence, capturing multiple relationships and nuances at once.
📍 Positional Encoding — Remembering Word Order
Problem: Self-attention alone ignores sequence order. Positional encoding adds small numeric signals telling the model: this is the first token, second token, etc.
Example:
Without order info, “dog bites man” and “man bites dog” might look the same. Positional encoding fixes that.
• “dog bites man” → (dog: position 1), (bites: position 2), (man: position 3)
• “man bites dog” → (man: position 1), (bites: position 2), (dog: position 3)
🧠 Feed-Forward Networks – Thinking Step
Role: After attention figures out context, each token’s info goes through a small neural network that adds complexity and non-linear reasoning.
Why it matters: Turns basic token representations into richer, more abstract concepts.
Example:
Sentence: The cat jumped over the fence.
Step 1: Self-attention collects context for each word:
• cat → “animal”, “subject”, “doing the jumping”
• jumped → “action”, “past tense”
• fence → “object being jumped over”
Step 2: For each word’s gathered clues, the feed‑forward network transforms them into a deeper meaning:
• cat → “small agile animal performing the action”
• jumped → “leap movement over obstacle”
• fence → “barrier between spaces”
⚖️ Layer Normalization – Keeping Learning Stable
Purpose: Like adjusting volume so things don’t get too loud or too quiet during training.
Example:
Imagine adjusting thermostat settings in a building so the temperature stays comfortable and doesn’t spike or drop suddenly.
Layer normalization keeps the data “temperature” steady during training by normalizing values—avoiding overly large or tiny numbers that can confuse neural networks.
Why this matters: By stabilizing values, the model learns faster and more reliably without errors or crashes.
🎯 Softmax – Turning Scores into Probabilities
Function: The model produces raw scores (logits). Softmax converts them to probabilities that add to 1
Example:
If the scores are:
• “cat”: 2.5 → 70% chance
• “dog”: 1.5 → 20% chance
• “hat”: 0.5 → 10% chance
The model picks the most likely token as a next token
Use case: Pick the most likely next token when generating text.
🚀 OpenAI API Text Generation (JavaScript)
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
async function generateText(prompt) {
const response = await openai.chat.completions.create({
model: "gpt-4o-mini", // Choose the model you prefer
messages: [{ role: "user", content: prompt }],
max_tokens: 50,
temperature: 0.7,
});
return response.choices[0].message.content;
}
(async () => {
const prompt = "Once upon a time";
const generatedText = await generateText(prompt);
console.log("Generated text:", generatedText);
})();
💡 Tips for Developers
Use existing APIs: Hugging Face, OpenAI, and others offer easy-to-use SDKs.
Don’t reinvent the wheel: Let mature libraries handle the heavy lifting; focus on prompt design and fine-tuning for your use case.
Monitor tokens: Keep prompts under model token limits (for example, 4,096 for GPT‑3) to avoid errors and control costs..
Experiment: Adjust settings like temperature and top‑k to balance creativity and accuracy in generated text.
✅ Conclusion
Transformers have revolutionized how machines understand and generate language. By breaking down text into tokens, turning those tokens into numbers through embeddings, and applying attention mechanisms paired with thoughtful processing steps like feed-forward networks and normalization, these models achieve remarkable understanding and creativity.
The good news? You don’t need to build these complex models from scratch. With powerful pre-built models and easy-to-use APIs available today, you can harness this technology with just a few lines of code—focusing your energy on crafting great prompts and fine-tuning for your unique applications.
Remember these key takeaways:
• Tokenization bridges raw language and AI understanding.
• Embeddings capture meaning as numbers.
• Attention and transformer blocks make sense of context and relationships.
• Layered processing ensures stable, meaningful output.
Happy coding — and welcome to the future of GenAI! 🚀
