Stealing Part of a Production Language Model

Nicholas Carlini, Daniel Paleka, Krishnamurthy Dj Dvijotham, Thomas Steinke, Jonathan Hayase, A. Feder Cooper, Katherine Lee, Matthew Jagielski, Milad Nasr, Arthur Conmy, Itay Yona, Eric Wallace, David Rolnick, Florian Tramèr

TL;DR

This paper introduces the first practical model-stealing attack against production large language models (LLMs) like ChatGPT. By exploiting standard API features like logit bias, the researchers extracted the exact hidden dimension size and the final layer weight matrix of commercial models for under $20 USD. This work shatters the assumption that proprietary models can be kept entirely "black-box" through API-only access.

What Problem Does This Paper Address?

Proprietary LLM providers (like OpenAI and Google) keep their model architectures, parameter counts, and hidden dimensions strictly secret to maintain a competitive advantage and mitigate safety risks. Users can only interact with these models through APIs that return text, log-probabilities (logprobs), or allow "logit biasing" (modifying token probabilities to guide generation).

The authors investigate a fundamental question in AI security: How much precise architectural information can an adversary learn about a production language model simply by querying its API?

Methods

Unlike prior model-stealing attacks that attempt a difficult "bottom-up" reconstruction starting from the input layer, this attack works top-down, targeting the very last layer of the transformer model: the embedding projection layer.

The core insight relies on linear algebra. The final layer maps a relatively small hidden dimension ($h$) to a much larger vocabulary logit vector ($l$). Because $h \ll l$, the matrix of output logits is fundamentally low-rank.

By sending a series of random prompt prefixes to the API, the authors collect a matrix of output logit vectors ($Q$). They then perform a Singular Value Decomposition (SVD) on $Q$. Because the matrix is low-rank, the singular values drop off precipitously after the $h$-th value. The point where this sharp drop occurs reveals the exact hidden dimension $h$.

Algorithm 1: Hidden-Dimension Extraction Attack

To make this work on real-world APIs that restrict output to only the top-K tokens, the authors developed clever differential querying techniques. By applying a large "logit bias" to specific tokens, they force those tokens into the top-K window, allowing them to mathematically reconstruct the full, unconstrained logit vector.

Datasets

The authors validated their attacks using two categories of models:

  1. Open-source models (for ground-truth validation): GPT-2 (Small and XL), Pythia (1.4B and 6.9B), and LLaMA (7B and 65B).
  2. Production black-box models (via OpenAI's API): ada, babbage, babbage-002, gpt-3.5-turbo-instruct, and gpt-3.5-turbo-1106.

No new datasets were introduced, as the attack relies entirely on generating random prompt prefixes to probe the target models.

Key Insights and Ideas

  • The Low-Rank Bottleneck: The final layer of an LLM acts as a massive dimensionality bottleneck. This mathematical property makes it inherently vulnerable to rank-estimation techniques like SVD.
  • Weaponizing Steering Features: API features designed to give users control over text generation (like logit_bias) inadvertently provide the exact mathematical leverage needed to bypass top-K logprob restrictions and reconstruct hidden states.
  • Top-Down Extraction is Easier: Peeling off the final layer bypasses the complex, non-linear activations (like ReLUs or GeLUs) deeper in the network that traditionally stymied model-stealing attempts.
Figure 1. SVD can recover the hidden dimensionality of a model when the final output layer dimension is greater than the hidden dimension. Here we extract the hidden dimension (2048) of the Pythia 1.4B model. We can precisely identify the size by obtaining slightly over 2048 full logit vectors.

Key Results

  • Revealing OpenAI Secrets: The attack successfully uncovered the previously unconfirmed hidden dimensions of OpenAI’s legacy models: ada is 1024, babbage is 2048, and babbage-002 is 1536.
  • Extremely Low Cost: Extracting the entire final weight matrix for ada and babbage cost less than $20 USD per model.
  • Probing GPT-3.5: The authors extracted the exact hidden dimension of gpt-3.5-turbo for under $200 USD. They estimate that stealing its entire final weight matrix would cost less than $2,000 USD.
  • High Precision: The weight matrices were recovered with a near-perfect Root Mean Square Error (RMSE) of around $10^{-4}$ to $10^{-5}$ (up to unavoidable linear symmetries).
  • Real-World Impact: Following responsible disclosure, both OpenAI and Google modified their APIs to implement defenses against this specific vector of attack.

Limitations and Future Work

  • Single-Layer Limit: The attack only recovers the final layer. Due to the non-linearities and normalization layers (like LayerNorm or RMSNorm) present throughout transformers, there is no obvious way to extend this method to deeper layers.
  • Symmetry Invariance: The weight matrix is recovered up to an affine or orthogonal transformation, meaning the exact original weights aren't perfectly isolated, though they are functionally equivalent.
  • Future Directions: The authors suggest that future work could attempt to "break" the remaining orthogonal symmetries by exploiting quantized weights (e.g., models stored in 4-bit or 8-bit precision), which might allow for a bit-for-bit exact recovery of the layer.

Summary updated

Abstract

We introduce the first model-stealing attack that extracts precise, nontrivial information from black-box production language models like OpenAI's ChatGPT or Google's PaLM-2. Specifically, our attack recovers the embedding projection layer (up to symmetries) of a transformer model, given typical API access. For under \$20 USD, our attack extracts the entire projection matrix of OpenAI's Ada and Babbage language models. We thereby confirm, for the first time, that these black-box models have a hidden dimension of 1024 and 2048, respectively. We also recover the exact hidden dimension size of the gpt-3.5-turbo model, and estimate it would cost under $2,000 in queries to recover the entire projection matrix. We conclude with potential defenses and mitigations, and discuss the implications of possible future work that could extend our attack.
BibTeX
@inproceedings{carlini2024stealing,
  author = {Nicholas Carlini and Daniel Paleka and Krishnamurthy Dj Dvijotham and Thomas Steinke and Jonathan Hayase and A. Feder Cooper and Katherine Lee and Matthew Jagielski and Milad Nasr and Arthur Conmy and Itay Yona and Eric Wallace and David Rolnick and Florian Tramèr},
  title = {{Stealing Part of a Production Language Model}},
  booktitle = {International Conference on Machine Learning},
  year = {2024},
  url = {https://openreview.net/forum?id=VE3yWXt3KB},
}
Figure