Skip to main content

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.

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.

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
No explicit rules are programmed in. The model learns by observing patterns in how words appear together in context, then uses those patterns to predict the most likely continuation of any new input it receives.
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:
  1. Tokenizes the input into a sequence of sub-word pieces.
  2. Computes a probability distribution over every possible next token.
  3. Samples (or picks the highest-probability) token from that distribution.
  4. Appends the chosen token to the sequence and repeats from step 2.
This loop continues until the model produces a stop token or reaches the configured output length. Because every token conditions the next, the model’s outputs are coherent and context-aware.

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.
1

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:
Extract the name of the author from the quotation below.

"Some humans theorize that intelligent species go extinct before they can
expand into outer space. If they're correct, then the hush of the night sky
is the silence of the graveyard."
― Ted Chiang, Exhalation
Output:
Ted Chiang
Instruction prompting is the most direct approach and works well for clear, well-scoped tasks.
2

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:
"Some humans theorize that intelligent species go extinct before they can
expand into outer space. If they're correct, then the hush of the night sky
is the silence of the graveyard."
― Ted Chiang, Exhalation

The author of this quote is
Output:
Ted Chiang
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.
3

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:
Your role is to extract the name of the author from any given text.

"Some humans theorize that intelligent species go extinct before they can
expand into outer space. If they're correct, then the hush of the night sky
is the silence of the graveyard."
― Ted Chiang, Exhalation
Output:
Ted Chiang
Scenarios are especially effective for open-ended tasks where you want the model to adopt a consistent persona or perspective across a long interaction.
4

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:
Quote:
"When the reasoning mind is forced to confront the impossible again and
again, it has no choice but to adapt."
― N.K. Jemisin, The Fifth Season
Author: N.K. Jemisin

Quote:
"Some humans theorize that intelligent species go extinct before they can
expand into outer space. If they're correct, then the hush of the night sky
is the silence of the graveyard."
― Ted Chiang, Exhalation
Author:
Output:
Ted Chiang
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.
When using an LLM for coding tasks, tell it the language, framework, and any constraints up front. The more specific your instructions, the fewer corrections you will need to make.

General tips for better prompts

Regardless of which prompting style you use, these practices reliably improve output quality:
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.
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.
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.
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.
"Some humans theorize that intelligent species go extinct before they can
expand into outer space. If they're correct, then the hush of the night sky
is the silence of the graveyard."
― Ted Chiang, Exhalation

###


Fine-tuning is more expensive and time-consuming than prompt engineering, but it can unlock higher performance on specialized tasks that require consistent formatting or domain-specific behavior.

Build docs developers (and LLMs) love