The Neural Maze

The Neural Maze

KTO for Real-World LLM Alignment - A Hands-On Guide

Finetuning Sessions · Lab 5 / 8

Miguel Otero Pedrido's avatar
Antonio Zarauz Moreno's avatar
Miguel Otero Pedrido and Antonio Zarauz Moreno
Mar 13, 2026
∙ Paid

Welcome to Lab 5 of the Finetuning Sessions!

In today's lab, we're moving from theory to the terminal. We are stepping onto the production alignment frontier with a hands-on Kahneman-Tversky Optimization (KTO) experiment.

If you haven't read our deep dive on the RLHF Landscape, make sure to review it before going forward!

Understanding the theoretical foundations of PPO and DPO is essential to mastering the code we are about to run—and understanding exactly why we are leaving them behind for this lab.

The RLHF Landscape - Aligning LLMs Beyond SFT

The RLHF Landscape - Aligning LLMs Beyond SFT

Miguel Otero Pedrido and Antonio Zarauz Moreno
·
Mar 11
Read full story

The shift from preference-based methods like PPO and DPO to KTO isn't just a minor algorithmic tweak, it's a fundamental rethinking of how we collect and use human feedback in the real world.

While PPO and DPO taught us how to align models, they rely on a glaring assumption: that you have access to expensive, perfectly curated pairwise preferences (e.g., Output A is better than Output B).

KTO attacks the data acquisition bottleneck of production environments, allowing us to align LLMs directly from the messy, abundant, binary "thumbs-up / thumbs-down" feedback generated by actual users.

Today, we are going deep into the implementation.

We will walk through a live training job, analyzing how to navigate extreme data imbalances—like having far more "thumbs-down" than "thumbs-up" examples—while maintaining stable gradient updates and actually matching or beating DPO performance.

By the end of this lab you'll have a first-principles understanding of how to orchestrate high-fidelity alignment using the real-world, binary user feedback that actually scales in production.


The Mechanics of KTO

KTO operates by treating model alignment as prospect theoretic optimization, a departure from the traditional maximum likelihood estimation used in earlier reinforcement learning from human feedback (RLHF) methods.

Kahneman and Tversky's Prospect Theory posits that humans do not perceive value in a linear or purely objective fashion; instead, we perceive random variables through a biased, psychologically defined lens—most notably, we are inherently loss-averse.

While methods like Direct Preference Optimization (DPO) implicitly incorporate some of these human biases by comparing pairs, KTO elevates this principle to an explicit objective function.

It aims to directly maximize the utility of model generations as perceived by a human-like observer, rather than simply maximizing the probability of a "chosen" response over a "rejected" one.

To achieve this, KTO abandons the single preference-based reward model in favor of a loss function that separates positive and negative signals. It replaces the traditional, monolithic loss aversion coefficient with two distinct hyperparameters:

  • λ_D → Desirable weight

  • λ_U → Undesirable weight

These represent the relative importance the model assigns to positive and negative feedback, respectively.

By decoupling these weights, KTO can be finely tuned to reflect the human tendency to weight losses more heavily than equivalent gains.

This mathematical shift allows the model to define its "reference point"—the threshold between a good and bad output—internally, based on the distribution of the data rather than a fixed comparison between two specific strings.

The most significant "superpower" resulting from this architecture is KTO's unparalleled ability to handle extreme data imbalances.

In real-world production environments, gathering perfectly paired "chosen" and "rejected" data is a bottleneck; it is far more common to have a massive stream of binary feedback where "thumbs-down" events significantly outnumber "thumbs-up" events.

Because KTO does not require 1:1 pairs, it can digest these asymmetric signals without collapsing. It manages this flawlessly by adjusting the λ weights to compensate for the scarcity of positive examples, effectively extracting a strong alignment signal from a sea of negative feedback.

Ultimately, KTO proves that the "geometry" of alignment data matters less than the utility function used to process it.

Research shows that KTO can match or even exceed DPO's performance while discarding up to 90% of desirable data, provided the remaining data is labeled accurately. This makes KTO the ideal choice for developers working with "messy" telemetry or crowd-sourced labels.

It transforms the alignment process from a rigid data-curation task into a flexible, theoretically grounded optimization problem that thrives on the raw, asymmetric signals found in the wild.


The KTO dataset geometry

To truly master KTO, we have to look at the geometry of our data. Traditional RLHF and DPO methods are mathematically shackled to paired preference data: for every prompt, they demand a perfectly curated "chosen" response and a "rejected" response.

But KTO datasets fundamentally break this constraint!

KTO requires nothing more than a binary signal indicating whether a specific output is desirable or undesirable. In practice, a KTO dataset (like the ones found in modern fine-tuning libraries) simply consists of a prompt, a single completion, and a boolean label (thumbs-up or thumbs-down).

So, how do we conceptualize the conversion of a legacy, paired RLHF dataset (like Anthropic-HH or OpenAssistant) into a KTO-ready format?

The conversion is brutally simple: we break the pairs apart.

We take the preferred response (y_w​) and assume it is drawn from the desirable distribution, giving it a positive label. Then, we take the dispreferred response (y_l​), assume it belongs to the undesirable distribution, and give it a negative label. By breaking up n preference pairs, we instantly generate 2n independent examples for KTO.

If you want to be rigorously aggressive and remove any hidden trace of paired biases, you can apply a "one-y-per-x" hack. This involves subsampling exactly one output per prompt, completely obliterating the paired structure.

The true magic of this dataset structure emerges when dealing with the messy reality of production data.

Unlike DPO, which demands a strict 1:1 balance of winners and losers, KTO handles extreme data imbalances effortlessly. You can randomly discard up to 90% of your desirable examples—leaving your dataset wildly imbalanced with up to 10 negative examples for every 1 positive example—and KTO will adapt by simply shifting its loss and risk aversion hyperparameters (λ_D​ and λ_U​) to match or exceed DPO's performance.

This proves that KTO doesn't secretly rely on paired data; it thrives on raw, asymmetric, real-world signals.

User's avatar

Continue reading this post for free, courtesy of Miguel Otero Pedrido.

Or purchase a paid subscription.
© 2026 Miguel Otero Pedrido · Privacy ∙ Terms ∙ Collection notice
Start your SubstackGet the app
Substack is the home for great culture