Reverse-Engineering Decoding Strategies Given Blackbox Access to a Language Generation System

Daphne Ippolito, Nicholas Carlini, Katherine Lee, Milad Nasr, Yun William Yu · DOI

Abstract

As Large Language Models (LLMs) become central to modern computing, they are increasingly deployed behind proprietary APIs that hide their internal configurations. One such hidden component is the decoding strategy—the algorithm used to select tokens from the model's output distribution. This paper presents a methodology for reverse-engineering these strategies, specifically focusing on top-$k$ and nucleus (top-$p$) sampling. By applying statistical techniques such as the "coupon collector" problem and utilizing reference models, we demonstrate that these parameters can be estimated with high accuracy. Our analysis reveals that ChatGPT (circa early 2023) likely utilizes nucleus sampling with $p \approx 0.81$, a configuration that inadvertently limits the model's ability to perform truly random tasks.

Introduction

When interacting with a Large Language Model through a web interface or API, users typically receive a completed string of text without any visibility into the underlying generation process. The "decoding strategy" is the set of rules the model follows to choose the next word from a list of possibilities. Common strategies include top-$k$ sampling, which restricts choices to the $k$ most likely tokens, and nucleus (top-$p$) sampling, which selects from a dynamic set of tokens whose cumulative probability exceeds a threshold $p$.

Understanding these hidden settings is not merely a matter of curiosity. It is essential for several critical areas of AI safety and transparency:

  1. AI Detection: Knowing the exact sampling parameters makes it significantly easier to distinguish between human-written and machine-generated text.
  2. Bias and Reliability: Hidden truncation settings can introduce systematic biases, preventing models from generating certain valid outputs.
  3. Accountability: As AI is integrated into high-stakes industries, the lack of transparency regarding these "blackbox" settings poses a challenge for auditing and reproducibility.

Methodology

The core challenge of this research is treating the LLM as a blackbox that only provides a single-token response for a given prompt. The researchers developed distinct approaches for the two primary sampling methods.

Estimating $k$ in Top-$k$ Sampling

To estimate $k$, the authors rely on the intuition of the "Coupon Collector" problem. If a prompt is designed to elicit a nearly uniform distribution of tokens (e.g., "List a random noun"), the model will sample from its top-$k$ vocabulary. By repeatedly querying the API with the same prompt and tracking the number of unique tokens returned, one can observe when the set of outputs saturates. The point at which new tokens stop appearing provides a strong signal for the value of $k$. The researchers refined this using an algorithm that compares results across different prompt categories (e.g., nouns vs. adverbs) to ensure the stability of the estimate.

Estimating $p$ in Nucleus Sampling

Estimating $p$ is more complex because the number of tokens in the "nucleus" changes depending on the model's confidence. To solve this, the authors use a "known" reference model (such as GPT-2) to approximate the probability distribution of the target blackbox. By counting the unique tokens produced by the blackbox for a specific prompt and summing the probabilities of those same tokens in the reference model, they can approximate the $p$ threshold. This "cross-model attack" works effectively even when the reference model is smaller or slightly different from the target.

Distinguishing Strategies

To determine whether a model uses top-$k$ or top-$p$, the researchers analyze the ratio of unique tokens produced by prompts with high-entropy distributions versus low-entropy distributions. In a top-$k$ system, the number of possible tokens is fixed regardless of the distribution's shape. In a top-$p$ system, the number of tokens varies wildly: a "flat" distribution results in many tokens, while a "peaky" distribution results in very few.

Experimental Setup and Datasets

The study evaluated several prominent model families, including GPT-2 (Base and XL), GPT-3 (Davinci), BLOOM (3B), and Pythia (2.7B). Most notably, the researchers applied these techniques to the December 2022 and January 2023 versions of ChatGPT. They utilized custom prompt sets designed to force the models into high-variance states, such as asking for random nouns, adverbs, months, or dates.

Key Results and Insights

The "Broken Dice" Phenomenon

One of the most compelling findings involves ChatGPT's inability to perform simple random tasks. When asked to simulate a 20-sided die roll, ChatGPT was found to be incapable of returning 6 of the 20 possible numbers. This occurs because the truncation settings (top-$p$) remove the "tail" of the probability distribution. If the model's internal probability for the number "17" falls below the threshold, that number becomes impossible for the model to generate, even if it is a valid outcome for the prompt.

Reverse-Engineering ChatGPT

Through their estimation techniques, the authors concluded that ChatGPT likely uses nucleus sampling with a value of $p \approx 0.81$. This specific value explains the truncation observed in the die-rolling experiments and provides a fingerprint for the model's generation style during that period.

Accuracy of Estimation

  • Top-$k$: For values of $k < 100$, the method was highly precise, typically staying within $\pm 5$ of the ground truth.
  • Top-$p$: The researchers estimated $p$ within a 0.05 margin of error for most models, provided the reference model was sufficiently similar to the target.

Limitations and Future Work

While effective, these methods require a high volume of API queries, which can be costly and subject to rate-limiting. Furthermore, the rise of "temperature annealing" (where randomness is scaled dynamically) and frequent model updates present moving targets for reverse-engineering. Future research may focus on reducing the query cost and developing more robust methods for models that employ multiple, layered decoding strategies simultaneously. As proprietary models continue to evolve, these forensic techniques will be vital for maintaining a level of public understanding and oversight of AI systems.

Summary updated

BibTeX
@inproceedings{ippolito2023reverse,
  author = {Daphne Ippolito and Nicholas Carlini and Katherine Lee and Milad Nasr and Yun William Yu},
  title = {{Reverse-Engineering Decoding Strategies Given Blackbox Access to a Language Generation System}},
  booktitle = {INLG},
  year = {2023},
  doi = {10.18653/v1/2023.inlg-main.28},
}
Figure