Ask ChatGPT the same question twice and you might get two differently-worded answers. Is the model confused? No — it's a setting, and you can control it. LLMs come with a small set of knobs — temperature, top-p, max tokens, context window — and knowing what each one really does separates people who use AI from people who build with it. Interviewers test these because they're cheap to ask and instantly expose whether you've actually shipped anything.
Where the randomness comes from
Rewind to lesson 2: at every step the model produces a probability for every possible next token. Then one token must be picked. Always picking the single top token gives you the same answer every time — safe, but often stiff and repetitive. Sampling from the probabilities gives variety — sometimes brilliance, sometimes nonsense. The knobs in this lesson all answer one question: how adventurous should the picking be?
Temperature — the adventure dial
Temperature reshapes the probabilities before picking. Low temperature exaggerates the leader — the top token becomes near-certain. High temperature flattens the field — underdogs get real chances.
Practical ranges: 0–0.3 for anything that must be consistent and correct — extraction, classification, code, support answers, anything automated tests will check. 0.7–1.1 for creative work — marketing copy, brainstorming, story writing. And say this in interviews, because it's the part people get wrong: temperature changes randomness, not intelligence — and temperature 0 makes answers repeatable, not correct. If the model's best guess is wrong, temperature 0 just gives you that wrong answer reliably.
Top-p — trimming the tail
Even at moderate temperature, thousands of terrible tokens keep tiny probabilities — and occasionally one gets sampled, producing that one weird word in an otherwise fine sentence. Top-p (nucleus sampling) fixes this by cutting the tail: sort tokens by probability, keep the smallest set whose probabilities sum to p (say 0.9), sample only within that set.
The elegant part: the set size adapts. When the model is confident ("The capital of India is ___"), two or three tokens already cover 90% — tiny set, focused choice. When the context is open ("Once upon a time, ___"), dozens of tokens share the mass — bigger set, more freedom. Its blunt cousin top-k keeps a fixed count (say the top 50) regardless of confidence. Convention worth quoting: tune temperature OR top-p aggressively, not both — they fight over the same job.
Max tokens — the output guillotine
max_tokens caps how many tokens the model may generate. Hit the cap and generation just stops — mid-list, mid-sentence, mid-word. The API even tells you it happened via the finish reason:
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Explain RAG in detail"}],
max_tokens=40, # deliberately tiny
)
print(response.choices[0].message.content)
print("finish_reason:", response.choices[0].finish_reason)Result
RAG (Retrieval-Augmented Generation) is a technique where a language model first retrieves relevant documents from an external knowledge source and then finish_reason: length
finish_reason: length = truncated by the cap (a healthy completion says stop). Users report this bug as "the bot gives incomplete answers" — now you know the 30-second fix: raise max_tokens, or ask for a more concise format.
Wait — why did the bot forget the start of the conversation?
The context window is the model's working memory: the maximum tokens one request can hold — system prompt + conversation history + retrieved documents + the answer being generated, all together. It is a hard physical limit, not a suggestion. When a long chat outgrows it, something must give: apps silently drop or summarize the oldest messages. The user experiences it as amnesia — "it forgot my name from an hour ago" — but nothing malfunctioned. The model never remembers anything by itself; whatever isn't inside the current context window does not exist for it.
Selection-round radar: "What is a context window and what happens when it overflows?" — the expected answer names the components that share the window (prompt + history + documents + output) and one overflow strategy (sliding window, summarizing old turns, or storing facts externally and re-injecting them). That last one is literally RAG applied to chat memory — a connection worth saying out loud.
Tokens and money — the billing meter
APIs charge per token: one rate for input tokens (everything you send — including, on every single call, the entire conversation history), a higher rate for output tokens (what the model writes). Quick math you should be able to do on a napkin:
# 10,000 requests/day, 1,200 input + 300 output tokens each
# Rs 0.010 per 1K input, Rs 0.030 per 1K output
daily = 10_000 * (1200/1000 * 0.010 + 300/1000 * 0.030)
print(f"per day: Rs {daily:.0f}, per month: Rs {daily*30:,.0f}")Result
per day: Rs 210, per month: Rs 6,300
Notice the input side dominates — 1,200 vs 300. That's typical, because history and instructions ride along on every call. The cost-cutting levers, in order of impact: trim the prompt and history, cache repeated prompt parts, route easy requests to a smaller model, and only then worry about shortening answers.
Common mistakes
- Using high temperature for extraction/automation tasks — then wondering why QA can't reproduce bugs.
- Believing temperature 0 guarantees correct answers — it guarantees repeatable ones.
- Blaming the model for truncated answers instead of checking
finish_reason == length. - Forgetting the answer also consumes context — a full window means no room left to respond.
- Ignoring that chat history is re-sent (and re-billed) on every call — long conversations get linearly more expensive.
- Cranking temperature AND top-p together — two hands fighting over one steering wheel.
Quick recap
| Knob | Job | Remember |
|---|---|---|
| Temperature | randomness dial | low = repeatable, high = varied; never = smarter |
| Top-p | cut the unlikely tail | adaptive set; tune it or temperature, not both |
| max_tokens | output cap | finish_reason "length" = you truncated it |
| Context window | working memory limit | prompt + history + docs + answer share it; overflow = amnesia |
| Token pricing | the bill | input usually dominates; history re-billed every call |
Practice Zone — PYQs from real selection rounds
Six MCQs and three hands-on tasks — configure a real call, map bug reports to parameters, and estimate a monthly bill.
Setting temperature ≈ 0 makes the model:
Asked in

You're building (a) a bot that extracts invoice numbers from emails, and (b) a bot that writes Instagram captions. Sensible temperatures are:
Asked in

Top-p (nucleus) sampling with p = 0.9 means:
Asked in

If max_tokens (the output limit) is set too low, the visible symptom is:
Asked in

A chatbot conversation has grown longer than the model's context window. What actually happens / must happen?
Asked in

LLM APIs are typically priced:
Asked in

Hands-on tasks:
You're building a customer-support bot for an electronics store. Answers must be consistent, short and never cut off mid-sentence. Fill in sensible values for temperature and max_tokens in this (OpenAI-style) call, and justify each.
Asked in

response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are a support agent for TechMart."},
{"role": "user", "content": "My earbuds won't pair. Help?"},
],
temperature=___,
max_tokens=___,
)Three bug reports, one parameter each. (1) "The summary bot gives a differently-worded summary every time for the same document — QA can't write test cases." (2) "Answers stop mid-sentence on long questions." (3) "The bot completely forgot the user's name from 40 messages ago." Name the parameter/limit behind each and the fix.
Asked in

Your bot handles 10,000 requests/day. Each request: ~1,200 input tokens, ~300 output tokens. Price: ₹0.010 per 1K input tokens, ₹0.030 per 1K output tokens. Estimate the monthly (30-day) cost, and name the single most effective lever to cut it.
Asked in

FAQ
Is temperature 0 fully deterministic?
Almost — you'll get the same answer nearly every time, which is what matters in practice. (Fine print: large-scale serving can introduce tiny numeric non-determinism, so "near-deterministic" is the precise word — a nice nuance if an interviewer pushes.)
What values should I use if I'm unsure?
The API defaults (typically temperature ≈ 1, top-p ≈ 1) are fine for exploration. The moment your output feeds code or users, decide deliberately: factual/automated → temperature 0–0.3; creative → 0.7–1.1; and set max_tokens with comfortable headroom.
Do bigger context windows make RAG unnecessary?
No — you'd pay to reprocess your whole document pile on every request, and models attend less sharply to needles in huge haystacks. Retrieval keeps the window filled with only what's relevant. Lesson 9 settles this properly.
Next lesson: the highest-leverage skill per minute spent — Lesson 6: Prompting Basics →


