Sub topic 1
How a language model turns a prompt into a response
Prerequisites
This chapter assumes you have used a chat-based AI assistant and can read short Python snippets. You do not need to know how neural networks work. Later chapters build on the working model introduced here.
The prompt is the only input
A large language model (LLM) is a program that predicts text. It does not look things up, remember your last chat, or know what you meant. Everything it knows about your request is in the text you send, called the prompt. The model also carries patterns learned during training, but it has no access to your files, your team, or your goals unless the prompt includes them.
This explains the first rule of prompting. If a detail is not in the prompt, the model has to guess it. A guess may be fine, or it may be wrong in a way you did not expect.
Tokens: the unit the model reads
The model does not read words. It reads tokens, which are short pieces of text such as a whole common word, part of a rare word, or a punctuation mark. A tokenizer splits your prompt into tokens before the model sees it.
Tokens matter for two practical reasons:
- Models have a context window, which is the maximum number of tokens the prompt and the reply can use together. Text past that limit is cut off or rejected.
- Many services charge and apply limits per token, not per word.
As a rough guide, English text averages a little under one token per word on the short side and about four characters per token. Treat this as an estimate. Each model has its own tokenizer.
Predicting one token at a time
The model generates its reply one token at a time. At each step it reads the prompt plus everything it has written so far. It then produces a probability for every token it could output next. One token is chosen, added to the text, and the process repeats until the model emits a stop signal or reaches a length limit.
Because each token depends on the ones before it, the opening of your prompt and the opening of the reply both steer what follows. A prompt that ends with Sentiment: pushes the model toward a short label. A prompt that ends with an open question invites a longer answer.
Why wording changes the output
The model's probabilities come from patterns in its training text. Your wording selects which patterns apply. "Explain DNS" and "Explain DNS to a ten-year-old in two sentences" point at different kinds of text, so they produce different replies.
Two consequences follow:
- Small wording changes can cause large output changes, and you cannot predict every one in advance. This is why later chapters cover testing prompts.
- The model produces plausible text, not verified facts. A fluent answer can still be wrong.
A worked comparison
The snippet below sends the same task with a vague prompt and a specific prompt. It uses a stand-in function so it runs without any account. Replace call_model with a call to your own provider.
def call_model(prompt):
# Stand-in for a real API call. It only shows what would be sent.
return f"[model receives {len(prompt.split())} words]\n{prompt}"
vague = "Tell me about this ticket."
specific = (
"Summarize the support ticket below for an engineering manager.\n"
"Use at most 3 bullet points. State the customer impact first.\n\n"
'Ticket:\n"""\nLogin fails with error 500 since the 2.4 update.\n"""'
)
for name, prompt in [("vague", vague), ("specific", specific)]:
print(f"--- {name} ---")
print(call_model(prompt))The vague prompt leaves out the ticket text, the audience, the length, and the format. The model would have to invent all four. The specific prompt supplies them, so the space of likely replies is much smaller.
Common misunderstandings
- "The model remembers me." Each request is independent unless the application resends earlier messages.
- "It understood my intent." It continues text in a way that fits your words. Intent you did not write down is not available to it.
- "Asking again gives the same answer." Token choice can involve randomness, so replies may differ between runs. A later chapter covers the settings that control this.
- "A confident tone means correct." Tone reflects the style of the text, not the accuracy of the content.
Exercise
A colleague says, "I told the assistant about our refund policy yesterday, so it will apply it in today's new chat." Using the model of one prompt in and one reply out, explain why this may not work and what to do.
Show answer
The model only uses the text in the current prompt. A new chat usually does not include yesterday's messages, so the policy is not in the input and the model must guess. Include the refund policy text in the prompt, or use an application that resends it with each request.
Exercise
A model writes a reply one token at a time, and each token is chosen using everything written so far. Why does the way you end a prompt, such as with "Sentiment:", affect the reply?
Show answer
The ending of the prompt is the text the model continues from. Ending with "Sentiment:" makes a short label the most likely continuation, so the first generated tokens are steered toward a label and the rest of the reply follows from them.