Sub topic 1
How a Language Model Turns a Prompt into Text
Prerequisites
This chapter assumes basic Python, JSON, and HTTP API knowledge. You should also know what a chatbot or AI assistant is.
From prompt to prediction
A large language model (LLM) processes text as tokens, which are small pieces of words, punctuation, or spaces. It reads the input tokens and predicts a probability for the next token. It then selects a token, adds it to the context, and repeats the process until it reaches a stopping condition.
The model does not retrieve a guaranteed answer from a database. It generates a likely continuation based on patterns learned during training. This explains both its flexibility and its mistakes.
What you can build
The same prediction mechanism supports several application patterns:
| Pattern | Typical result | Main control |
|---|---|---|
| Summarization | Shorter version of a document | Length and focus instructions |
| Classification | One label for each input | Allowed labels and validation |
| Extraction | Fields such as dates or totals | Schema and parser |
| Retrieval-augmented generation | Answer based on selected documents | Search quality and citations |
| Tool use | API call or other external action | Allow-list and argument checks |
An application combines the model with ordinary software. Your code supplies context, checks the response, and decides what actions are allowed.
What models get wrong
Models can produce fluent but false statements, often called hallucinations. They can also misunderstand ambiguous instructions, exceed a context limit, or vary between identical requests. A confident tone does not prove correctness.
Treat generated text as untrusted output. Validate important fields, constrain actions, and use retrieval when the answer must come from current or private information.
> callout warning > Do not let raw model output trigger payments, deletes, permission changes, or messages. Put validation and authorization in your code.
Writing reliable prompts
A useful prompt states the task, supplies relevant context, defines the output format, and gives boundaries.
def make_summary_prompt(text: str) -> str:
return (
"Summarize the text in three bullet points. "
"Use only information in the text. "
"Text:\n" + text
)
print(make_summary_prompt("The service launched in June and serves 2,000 users."))For repeated work, test prompts against representative examples rather than judging one impressive response.
Common text tasks
Summarizing compresses information. Rewriting changes tone or structure while preserving meaning. Translation changes language while aiming to preserve meaning and intent. Drafting creates a first version from requirements. Each task benefits from examples, explicit constraints, and a review step.
Exercise
Why should an application validate an LLM response?
Show answer
Because fluent generated text can be incorrect, incomplete, or outside the allowed format.