Skip to content

Chapter 3 of 6

Sampling, Decoding, and API Controls

Several API-level parameters shape how the model converts probabilities into text. Temperature scales the logits before sampling: zero makes the model greedy and deterministic, picking the highest-probability next token, while higher values flatten the distribution for more creative output. Top-p, or nucleus sampling, restricts the token pool to the smallest set whose cumulative probability exceeds p, dynamically limiting candidates regardless of temperature. The two are often combined for fine-grained control. Other knobs include stop sequences that halt generation when a specified string appears, logit bias that adds a constant to selected tokens' logits to encourage or suppress them, max_tokens that caps output length, and repetition, frequency, or presence penalties that discourage verbatim reuse.

These controls matter because reproducibility with LLMs is hard. Sampling randomness, version updates, and non-deterministic infrastructure can change outputs across runs, so fixing seed and version is necessary to reproduce a response exactly. The OpenAI API exposes a system fingerprint identifying the backend configuration; comparing fingerprints across responses helps detect silent behavior changes.

For complex tasks, iterative prompt refinement—drafting a prompt, testing it on diverse inputs, diagnosing failures, and editing in cycles—adds constraints, examples, or clearer instructions until quality, format, and edge cases behave as desired. Prompt chaining sequences multiple LLM calls where each call's output becomes the next call's input, breaking complex workflows into modular, testable steps with intermediate validation. Skeleton-of-thought prompting first outlines a skeleton answer and then expands each point in parallel, while chain-of-density iteratively rewrites a summary to be more information-dense while keeping length fixed.

All chapters
  1. 1Foundations of Prompting
  2. 2Prompting Patterns and Techniques
  3. 3Sampling, Decoding, and API Controls
  4. 4Retrieval-Augmented Generation and Grounding
  5. 5Agents, Tools, and Program-Aided Reasoning
  6. 6Safety, Reliability, and Evaluation

Drill it

Reading is not remembering. These come from the Chatgpt Prompting Essentials deck:

Q

What is a prompt in the context of ChatGPT?

A prompt is the natural-language input—question, instruction, or context—a user provides to a language model to elicit a response. It frames the task and guides...

Q

What does the role pattern in a prompt do?

The role pattern assigns the model a persona or function (e.g., 'You are a senior copyeditor'). It conditions tone, vocabulary, and reasoning style, often impro...

Q

How does few-shot prompting work?

Few-shot prompting supplies a small number of input–output examples within the prompt before the new query, demonstrating the desired pattern, format, or reason...

Q

What is zero-shot prompting?

Zero-shot prompting asks the model to perform a task using only the instructions in the prompt, with no examples. It relies on the model's pretrained knowledge...