Large language models (LLMs) are functions that map text to text. Given an input string, the model predicts the text that should come next — one token at a time. That deceptively simple objective, applied at massive scale across enormous quantities of text, causes the model to internalize a surprisingly broad range of knowledge and skills.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/openai/openai-cookbook/llms.txt
Use this file to discover all available pages before exploring further.
What LLMs learn during training
An LLM is trained by minimizing prediction error: for every position in a document, it tries to predict the next word (or sub-word token). To get good at this task across billions of examples, the model is forced to generalize. Along the way it picks up:- Spelling and grammar across many languages
- How to paraphrase, summarize, and translate text
- How to answer questions and sustain a conversation
- How to write and reason about code
- Domain knowledge drawn from the breadth of its training corpus
LLMs do not “know” facts the way a database does. They encode statistical patterns from training data. This is why they can sometimes produce confident-sounding but incorrect answers — a phenomenon called hallucination.
How text generation works
When you send a prompt to an LLM, the model:- Tokenizes the input into a sequence of sub-word pieces.
- Computes a probability distribution over every possible next token.
- Samples (or picks the highest-probability) token from that distribution.
- Appends the chosen token to the sequence and repeats from step 2.
Four ways to control a large language model
The most powerful lever you have is the text prompt. There are four main prompting patterns, each with different trade-offs.Instruction prompting
Tell the model directly what you want. Instructions can be detailed — don’t be afraid to write a full paragraph — but keep the total token count within the model’s context window.Prompt:Output:Instruction prompting is the most direct approach and works well for clear, well-scoped tasks.
Completion prompting
Begin a sentence or pattern that the model is likely to complete in the way you want. This takes advantage of the model’s core training objective — predicting what comes next.Prompt:Output:Completion-style prompts require more experimentation and you often need a stop sequence, because the model won’t always know when to stop on its own.
Scenario prompting
Give the model a role or situation to play out. This is useful for complex queries where framing the model as an expert or a character improves the quality of its responses.Prompt:Output:Scenarios are especially effective for open-ended tasks where you want the model to adopt a consistent persona or perspective across a long interaction.
Few-shot (demonstration) prompting
Show the model examples of the input-output format you want before presenting the actual query. This is sometimes called few-shot learning.Prompt:Output:In most cases, fewer than eight examples are enough to get strong gains from few-shot prompting. The examples teach the model both the task and the desired output format.
Code capabilities
LLMs are not limited to natural language. Models like GPT-4o can read, write, explain, and refactor code across dozens of programming languages. The same prompting strategies apply — clear instructions, useful context, and concrete examples all improve code quality.General tips for better prompts
Regardless of which prompting style you use, these practices reliably improve output quality:Be specific
Be specific
Vague instructions produce vague outputs. If you want a comma-separated list, say so. If you want the model to respond “I don’t know” when it lacks information, tell it that explicitly. Every constraint you state is one the model can follow.
Provide context
Provide context
Help the model understand the bigger picture: what the output is for, who will read it, and what background knowledge is relevant. Context shifts the model’s probability distribution toward outputs that are actually useful.
Ask for expert-quality output
Ask for expert-quality output
Explicitly prompting for high-quality or expert-level responses — phrases like “explain in detail” or “as a senior engineer would write it” — tends to raise the quality bar. The model has seen expert writing in its training data and can draw on it when directed.
Ask the model to show its reasoning
Ask the model to show its reasoning
For complex tasks, adding a line like “Let’s think step by step” before the answer often dramatically improves accuracy. This is especially powerful for math, logic, and multi-step tasks. See the prompt engineering guide for a deeper look at this technique.
Fine-tuning as an alternative to prompting
When you have hundreds or thousands of examples of the behavior you want, you can fine-tune a custom model. Fine-tuned models can learn the task from the training data and may not need extensive prompt instructions. A separator sequence (such as###) between input and output helps the model recognize where to start generating.