AI Engineer Roadmap

Core LLM Elements

Tokens, context windows, temperature, Top-K, Top-P, max tokens, and repetition penalties. What they are, why they matter, and how they connect.

Large language models come with a set of controls that shape how they behave: how creative or precise they are, how much they can remember within a conversation, and how they decide what to say next. These controls are not complicated, but they do build on each other. This guide walks through the core elements one by one: what they are, how they work, and how they connect.

Tokens: The Building Blocks of Language

Tokens are the smallest units of language that an LLM can process. For humans, the smallest unit of language is a single character. But for LLMs, a token can represent a character, part of a word, a whole word, or even a short phrase.

For example, take the word amoral. The "a" at the beginning is a prefix that changes the meaning of the word "moral." So "amoral" might be split into two tokens: a and moral. In contrast, the word "cat" is a single token because the letters do not carry independent meaning.

A good rule of thumb is that an average English word is about 1.5 tokens. So, if you write 100 words, you’re likely generating around 150 tokens. The tool that converts text into tokens is called a tokenizer. Different tokenizers might split the same text into tokens differently, but the general principle remains the same.

INPUT "tokenization is great" tokenizer TOKENS token ization is great
"tokenization" splits into "token" + "ization". One word does not always equal one token.

This matters for two reasons you will care about immediately:

One more thing worth knowing: tokenization behaves very differently across languages. The same sentence in Arabic will use significantly more tokens than in English, because most models are trained predominantly on English text and their tokenizers are optimized for it. This is directly relevant if you are building bilingual products for the region.

See it for yourself: Go to platform.openai.com/tokenizer, paste any text, and watch it get split into tokens in real time. Try the same sentence in English and then in Arabic. The difference will make this click immediately.

Context Window: The Model's Working Memory

A context window represents the “working memory” of an LLM. It determines how much information from a conversation or input the model can retain and use to generate subsequent responses. When you send a message, the model does not just read your new message in isolation, it reads the entire conversation from the beginning, your messages, its responses, any instructions you gave it upfront, all of it, every single time. All of that has to fit inside the context window.

Imagine there is a box that represents the other person's working memory. If the entire conversation fits inside that box, they can remember everything you have said and respond appropriately. But if the conversation grows too long and overflows the box, they start to forget the earlier parts.

The context window is measured in tokens, not words. This means that a long conversation can fill up the context window quickly, even if it does not seem like a lot of text. For example, a conversation with 10,000 tokens would fill up a context window of 8,000 tokens, leaving no room for the model to generate a response.

Context windows have grown significantly over time. Early LLMs had context windows of around 2,000 tokens. Today, many models handle 128,000 tokens or more. At first glance, 128,000 tokens might seem like overkill - after all, how often do you need to process a conversation or document that long? But context windows aren’t just for conversations. They also store:

System prompts
Conversation history
Attached documents
Source code

A few long documents or snippets of code can quickly fill up even a large context window.

Watch out: The moment the conversation exceeds the limit, the model starts losing the earlier parts. It does not warn you, it just quietly forgets. That is why very long conversations start to feel like the model is losing the thread. It literally is.

Understanding context windows is crucial if you want to get the most out of LLMs. Whether you’re building a chatbot, analyzing documents, or generating code, you need to know how much information the model can handle at once.

If you exceed the context window

The model will start to forget earlier parts of the conversation or input, and its responses will become less reliable.

If you stay within the context window

The model can maintain a coherent and accurate dialogue.

So, the next time you interact with an LLM, think about the context window. It’s the invisible box that determines how much the model can remember, and how well it can respond.

Sampling Parameters: How the Model Chooses the Next Word

Before reading this section: If you have not watched the 3Blue1Brown videos yet, now is a good time. The videos on Transformers, the tech behind LLMs and Attention in transformers, step-by-step show exactly how the model processes tokens and arrives at these probability scores visually. You do not need to understand all the math, just the general picture of how a Transformer takes in tokens and produces a ranked list of candidates for the next one. That picture will make everything below much easier to follow.

Here is the connection. A Transformer processes your entire input - every token you sent - through layers of attention and feed-forward operations. At the very end of that process, it outputs a single thing: a score for every token in its vocabulary, indicating how likely each one is to come next. Those raw scores are called logits. A function called softmax then converts them into probabilities that sum to 1.

So when an LLM generates a response, it is not "knowing" the answer the way a human does. At every single step, it produces a probability distribution over its entire vocabulary, often 100,000+ possible next tokens. Given the prompt "The sky is…", the model might calculate that "blue" has an 80% probability, "cloudy" has 15%, "green" has 0.1%, and so on. It picks one token based on those odds, adds it to the sequence, runs the whole Transformer forward pass again, and repeats until the response is done.

NEXT TOKEN PROBABILITY · "…there was a unicorn that lived in a magical…" 45% forest 22% land 15% realm 8% valley 10% others…
At each step, the Transformer produces a probability score for every token in its vocabulary. "Forest" leads here, but the sampling parameters below decide whether it always wins or whether the others get a chance too.

Once you understand that the model is picking from a probability distribution at every step, the next question is: what controls that picking? That is exactly what sampling parameters are. They are the settings you configure - either through an API call or a model playground - to shape how the model behaves when it generates output. They do not change the model's knowledge or training. They change the rules for how it samples from the distribution it already computed.

Every sampling parameter ultimately answers one of four practical questions:

01
How random should the output be?
Temperature, Top-K, Top-P, Min-P
02
How long should the output be?
Max Tokens, Stop Sequences
03
How do I stop the model from repeating itself?
Frequency Penalty, Presence Penalty, Repetition Penalty
04
How do I get the same output every time?
Logit Bias, Seed

This guide covers the most important ones: Temperature, Top-K, Top-P, max tokens, and Repetition Penalties. These are the parameters you will actually tune day-to-day. If you want a full breakdown of every parameter, the article LLM Parameters: Temperature, Top-P, Top-K Guide by Amit Ray covers all of them in detail.

Temperature

Temperature is the most important sampling parameter you will work with. It controls the randomness of the model's output by scaling the logits before the softmax function converts them into probabilities.

A lower temperature sharpens the distribution, the top token gets most of the probability mass and the model behaves predictably. A higher temperature flattens it, lower-probability tokens get more of a chance and the output becomes more varied and creative.

LOW TEMP (0.2) Predictable forest land realm … DEFAULT (1.0) Balanced forest land realm … HIGH TEMP (1.5) Creative forest land realm …
Temperature reshapes the entire distribution. Low values make the top token dominate. High values give lower-probability tokens a real chance.

The scale runs from 0 to 2 in most APIs, with a default of 1. Here is what each range actually does and when to use it:

Temperature range reference

Range Behaviour Best for
0.0 Always picks the single highest-probability token. Fully deterministic. Factual Q&A, structured data extraction, classification, math
0.1 - 0.3 Very low randomness. Top token strongly dominates. Little variation but still highly focused RAG, code generation, summarization, translation
0.4 - 0.7 Balanced randomness. Good mix of coherence and variety. General chat, email drafting, content writing (good default)
0.8 - 1.0 Moderate to high randomnesss. More creative and diverse output Creative writing, brainstorming, idea generation
1.2 - 2.0 Very high randomness. Outputs become increasingly unpredictable and often incoherent Experimental creative tasks, data augmentation, exploration

In practice, you will rarely need to go above 1.2. Start at 0.7 for most tasks and adjust from there, lower if the output feels too loose, higher if it feels too repetitive or formulaic.

Top-K and Top-P

Temperature reshapes the probability distribution. Top-K and Top-P are filters that cut it down before sampling even begins, preventing the model from ever picking genuinely unlikely or nonsensical tokens, regardless of what temperature says.

Top-K (Hard Token Cutoff)

Top-K is the simpler of the two. Setting top_k = 4 means the model only considers the four most probable tokens. Everything else is discarded before sampling. An additional softmax can be applied to the surviving tokens so their probabilities sum to 1.

TOP-K = 4 · ONLY THE 4 HIGHEST-PROBABILITY TOKENS SURVIVE 40% forest 25% land 18% realm 10% valley Cut-off 5% grove ✕ out 2% others… ✕ out
Top-K = 4: only the four most probable tokens survive. "Grove" and everything below the cut-off are discarded before the model samples.

Top-K quick reference

Top-K Value Behavior
1 Greedy decoding (equivalent to temperature = 0)
10 - 20 Very focused, good for factual tasks
40 - 50 Balanced (Gemini's default range)
100+ Wide pool, more creative but may include unlikely tokens

Top-P (Nucleus Sampling)

Top-P works differently from Top-K. Instead of fixing a hard number of tokens to keep, you set a probability threshold between 0 and 1. The model then walks down its sorted list of tokens - starting from the most probable - and keeps adding them to the candidate pool until their combined probability reaches that threshold. Everything after that point is discarded.

For example, setting top_p = 0.8 means: "keep adding the most likely tokens until together they account for 80% of the probability mass, then stop." If the model is very confident about the next word, just 2 or 3 tokens might already cover 80%, so the pool stays small. If the model is uncertain and probability is spread across many tokens, it might need 30 or 40 tokens to reach 80%, so the pool grows automatically. This adaptive behaviour is exactly what makes Top-P more flexible than Top-K.

TOP-P = 0.80 · TOKENS KEPT UNTIL CUMULATIVE PROBABILITY = 80% 35% forest 25% land 20% realm 0.8 SUM 10% valley ✕ out 10% others… ✕ out 0.35 + 0.25 + 0.20 = 0.80 Tokens kept progressively form the Nucleus block
Top-P = 0.8: tokens are added by descending probability until their cumulative sum hits 0.8. Here three tokens make the cut. Everything beyond the threshold is discarded.

In practice, Top-P is generally preferred over Top-K because it adapts to the model's own level of confidence at each step. Most APIs use it as their default.

Top-P quick reference

Top-P Value Behavior Best For
0.1 - 0.3 Very narrow nucleus. Only the most probable tokens survive. Factual, precise outputs
0.5 - 0.7 Moderate filtering. Removes unlikely tokens while preserving variety. Balanced content generation
0.9 - 0.95 Wide nucleus. Most reasonable tokens are included. Creative writing, brainstorming
1.0 No filtering. All tokens considered (default). When relying on temperature alone

The general recommendation: adjust temperature or top-p, but not both at the same time. Tweaking both makes the behavior hard to reason about. Most experts set one to its default and tune the other.

Temperature vs Top-P vs Top-K: Head-to-Head

This is one of the most commonly asked questions about LLM sampling parameters. Here is a side-by-side comparison to make the distinctions concrete:

Feature Temperature Top-P Top-K
What it controls Shape of the probability distribution Cumulative probability threshold Fixed number of candidate tokens
Adaptive? No, always reshapes the full distribution Yes, pool size varies with model confidence No, always keeps exactly K tokens
When applied Before softmax After softmax After softmax
Can produce deterministic output? Yes (at 0) Approximately (at very low values) Yes (at K = 1)
OpenAI support Yes Yes No
Claude support Yes Yes Yes (top_k)
Gemini support Yes Yes Yes
DeepSeek support Yes Yes Yes (via API)
Best used with Top-P or Top-K (not both) Temperature (as alternative to Top-K) Temperature (common in open-source)

See it in action: The best way to truly understand how temperature, Top-K, and Top-P interact is to play with them live. Open the LLM Temperature & Sampling Visualizer and move the sliders -watch how the probability distribution changes in real time as you adjust each parameter. Five minutes of playing with this will solidify everything in this section better than reading it twice.

Max Tokens

What It Does

Max tokens (also max_tokens, max_output_tokens, or max_completion_tokens depending on the API) sets the maximum number of tokens the model will generate in its response. It is a hard upper limit - the model may stop earlier at a natural ending or stop sequence, but it will never exceed this count.

Important Distinctions by Provider (April 2026)

Provider / Model Parameter Name Max Output Limit Context Window
OpenAI GPT-4o max_completion_tokens 16,384 tokens 128K
OpenAI o3 max_completion_tokens 100,000 tokens 200K
Claude 4 Opus max_tokens 32,768 tokens 200K
Claude 4 Sonnet max_tokens 64,000 tokens 200K
Gemini 2.5 Pro max_output_tokens 65,536 tokens 1M+
DeepSeek-V3 max_tokens 8,192 tokens (default) 128K
Llama 4 Maverick max_tokens (via API/runtime) Configurable 1M

Cost Implication: You are billed for actual tokens generated, not max_tokens. But setting max_tokens too high can lead to unnecessarily verbose responses and higher costs. Set it to the maximum you actually need, not the maximum the model supports.

Repetition Penalties

Left to itself, a model will sometimes repeat itself, the same word three times in a paragraph, the same phrase it already used, or in extreme cases the same sentence indefinitely. This happens because once a pattern appears in the output, the model's probability distribution makes that pattern statistically attractive again.

Repetition penalties fix this by reducing the probability of tokens the model has already generated. There are two variants you will encounter across different APIs:

FREQUENCY PENALTY 1st use of "dog" 2nd use of "dog" 3rd use of "dog" Penalty grows proportionally PRESENCE PENALTY 1st use of "dog" 2nd use of "dog" 3rd use of "dog" Same flat penalty on every repeat
Frequency penalty scales, the more times a token appears, the bigger the hit. Presence penalty is flat, any token that has appeared at all gets the same penalty each time.

The frequency penalty specifically targets tokens that are used repeatedly, lowering their score each time they appear. The more often a token is used, the less likely the model is to use it again. The presence penalty applies immediately - as soon as a token has appeared at all, it is penalized, pushing the model toward introducing new vocabulary and new ideas.

In practice, you will reach for the frequency penalty when your model starts echoing itself, the same word appearing three times in a paragraph, or a phrase used earlier creeping back in. It is most noticeable in long outputs like reports or summaries. A small value around 0.3 is usually enough to clean this up. You rarely need to go higher.

The presence penalty solves a different problem. Use it when the model keeps circling the same idea without moving forward, rephrasing the same point in slightly different words instead of covering new ground. It is useful for creative tasks, brainstorming, or any situation where you want the output to explore rather than consolidate.

Most of the time you will not need either. When you do, start low, adjust one at a time, and stop as soon as the output feels natural again.

Putting It All Together

These parameters do not work in isolation. Every time the model generates a single token, they run in order:

THE GENERATION PIPELINE · Applied sequentially to every generated token STEP 1 RAW LOGITS 100K+ Vocab Scores STEP 2 PENALTIES Repetition Fixes STEP 3 TEMPERATURE Confidence Scaling STEP 4 TOP-K / TOP-P Tail Filtering STEP 5 SAMPLE Pick 1 Final Token Appended to context window - Pipeline repeats for next token
The full pipeline runs for every single token the model generates. Penalties run first, then temperature reshapes what's left, then Top-K or Top-P filters the candidates, then one token is sampled and the cycle repeats.

Everything covered in this guide: tokens, context windows, sampling parameters, and output controls, forms the vocabulary you will use every day as an AI engineer. You will not always be tuning all of these at once. Most of the time you are adjusting temperature and occasionally Top-P. But the moment a model behaves unexpectedly, this knowledge is what tells you whether the problem is in how you are sampling, how much context you are feeding in, or how the output length is being constrained. Understanding these elements is what separates someone who uses AI tools from someone who can actually reason about and control them.

Sources & Further Reading

Tahir, Understanding LLM Context Windows, Tokens, Attention and Challenges - context windows, tokenization, memory behavior
Amit Duwal, Mastering LLM Parameters: Temperature, Top-K, and Top-P - sampling parameter deep dive
Amit Ray, LLM Temperature, Top-P, Top-K & Core Parameters Guide - full parameter reference including repetition penalties, max tokens, logit bias, and seed
3Blue1Brown, Large Language Models explained briefly - visual intro to LLMs
3Blue1Brown, Transformers, the tech behind LLMs (Ch. 5) - how the transformer architecture works
3Blue1Brown, Attention in transformers, step-by-step (Ch. 6) - attention mechanism explained visually
OpenAI, Tokenizer Tool - interactive tokenization playground
Louis-7, LLM Temperature & Sampling Visualizer - interactive sampling parameter explorer