Sub topic 1
How a language model turns a prompt into text
Prerequisites
This chapter assumes you can read basic Python, know what JSON and an HTTP API are, and have used a chatbot. You don't need any machine learning background.
The working model
A large language model (LLM) is a program that predicts the next piece of text. It does not look up answers or reason in a separate step. It reads the text so far and produces a probability for every possible next piece. Then one piece is chosen and added to the text, and the process repeats.
The pieces are called tokens. A token is a chunk of text, often a short word or part of a word. "Unbelievable" may become two or three tokens. Models read and write tokens, not characters or words, and providers bill by token.
The generation loop
- Your prompt is split into tokens.
- The model scores every token in its vocabulary as a candidate for the next position.
- One token is picked from those scores.
- The token is appended to the text, and the loop returns to step 2.
- The loop stops at a special end token or when a length limit is reached.
Picking the next token
Step 3 is where settings matter. With temperature near 0, the model nearly always picks the highest-scoring token, so output is stable. With a higher temperature, lower-scoring tokens get picked more often, so output is more varied. The table shows the usual trade-off.
| Setting | Effect | Good for |
|---|---|---|
| Low temperature (0 to 0.3) | Repeatable, conservative | Classification, extraction |
| Higher temperature (0.7 and up) | Varied, more surprising | Brainstorming, drafting |
Even at temperature 0, some providers do not guarantee identical output on every call. Plan for small differences.
The context window
The model only sees what fits in its context window, the maximum number of tokens for the prompt and the reply combined. Anything outside the window does not exist for the model. It has no memory between calls. A chat app creates the feeling of memory by sending the earlier messages again each time.
Why this shapes what you build
The model produces likely text, not verified facts. That makes it strong at tasks where fluent, pattern-based text is the goal, such as summarizing, rewriting, and classifying. It is weaker where exactness matters, such as arithmetic or citing sources from memory. Later chapters cover how prompts, retrieval, and tools address those weak spots.
A minimal call
Most APIs wrap the loop in one function. The complete function below stands in for whichever provider you use. It is a stub here so the code runs as shown.
def complete(prompt: str, temperature: float = 0.0) -> str:
# Stub: a real version would send an HTTP request to a model API
# and return the generated text.
return "billing"
reply = complete("Classify this ticket: 'I was charged twice.'", temperature=0.0)
print(reply)Exercise
A user asks your chat app a follow-up question, and the model answers as if the earlier conversation never happened. Which property of the model explains this, and what is the usual fix?
Show answer
The model has no memory between calls and only sees the tokens in the current context window. The fix is to send the earlier messages again with each request, trimming them if they approach the window limit.
Exercise
You are building a feature that labels support tickets. Should you use a low or high temperature, and why?
Show answer
Low. Labeling needs repeatable, conservative choices, and a low temperature makes the model nearly always pick the highest-scoring token.