Looper Documentation
Looper Cookbook

What you build

How training works explains what happens in a training run. This page explains who does each part: what Looper runs for you, what the cookbook's ready-made code can do, and what you write yourself. You can use as much or as little of the cookbook as you like.

Three layers

Training with Looper involves three layers:

The cookbook only uses calls the SDK has, so anything the cookbook does, your own code can do too. It saves you from writing the common parts yourself.

Who does what in an RL run

Using the cookbook's async RL loop, train_rl_async, as an example:

Looper does The cookbook does You do
Runs the trainer and the sampler on GPUs Runs the loop: sample, group the answers, train, publish, repeat Create the training run and its sampler: the model, LoRA or full, sizes and optimizer
Generates the answers Decides when to sample and publish, so the trainer and sampler both stay busy Provide the tasks: the prompts to train on, and held-out ones to evaluate on
Computes the loss and gradients, and updates the weights Drops answers that are older than max_policy_lag Decide how answers are scored: pick one of the cookbook's verifiers, or write your own
Copies the new weights to the sampler Formats prompts in each model's chat format Save the model when training ends, and record the metrics you want
Stores checkpoints Runs multi-turn attempts with tool calls, in a shell or a Docker container

For SFT, the cookbook's train_sft loop works the same way: you provide the run, your examples and the final save, and the loop does the rest.

Three ways to use the cookbook

Use a loop, and swap in your own pieces

The cookbook splits an RL task into pieces, and you can replace any piece without touching the others:

Piece What it does The cookbook has To use your own
Verifier Scores an answer, giving its reward FinalNumber checks a final number. CodeTestsVerifier runs code against tests Subclass Verifier
Dataset Holds the tasks, and picks a batch for each step TaskDataset, over a list of tasks Write a class with __len__ and batch
Environment Runs the model's tool calls, for tasks that use tools A shell, and a code checker Subclass the environment class
Episode session Runs one attempt at a task, turn by turn AgentSession Write your own session

This is the most common way to use the cookbook. For example, you can write your own scoring and keep everything else, as in the example below.

Copy a loop and change it

The loops are short files, meant to be copied: looper_cookbook/train_sft.py, train_rl.py and train_rl_async.py. Copy one into your project and change it when the loop itself needs to work differently, for example to train on two kinds of tasks with different settings.

Use the SDK alone

Write the whole loop yourself with the SDK's calls. You decide when to sample, when to publish, and which answers to train on. Training and Sampling cover every call you need. You can still borrow single helpers from the cookbook, such as its message formatters, which put conversations into each model's chat format.

Example: your own scoring with the cookbook's loop

This example shows how to use only some parts of the cookbook. It trains a model to answer capital-city questions, in three parts:

  1. Your own verifier, which you write yourself. It scores the model's answers.
  2. Your tasks, the questions to train on, built with the cookbook's task and dataset classes.
  3. The cookbook's async loop, which runs the training.

1. Write your own verifier

This first part is all code you write yourself. It defines a verifier called NamesCity, which gives an answer a reward of 1 if it names the right city, and 0 if not.

From the cookbook, it imports only three small helpers:

Everything else in this code block is yours:

import dataclasses

from looper_cookbook.verifiers.base import Score, Verifier, last_reply


# Everything below is your own code.
class NamesCity(Verifier):
    # The verifier's settings: here, just the right answer.
    # Each task gets its own Config, such as NamesCity.Config(city="Paris").
    @dataclasses.dataclass(frozen=True, kw_only=True)
    class Config:
        city: str

        # The cookbook calls build() when it runs an attempt, to turn the settings into a verifier.
        async def build(self) -> "NamesCity":
            return NamesCity(self)

    def __init__(self, config):
        self._config = config

    # The scoring itself: 1.0 if the model's answer names the city, 0.0 if not.
    async def verify(self, messages, environment) -> Score:
        answer = last_reply(messages).lower()
        return Score(reward=float(self._config.city.lower() in answer))

2. Build the tasks

Each task is a question, plus the verifier that scores answers to it. Here, every task uses your NamesCity verifier, with that question's right answer. AgentSession.Config and TaskDataset come from the cookbook: they hold the tasks and pick a batch of them for each training step.

from looper_cookbook.datasets.tasks import TaskDataset
from looper_cookbook.episode_sessions.agent_session import AgentSession

capitals = [("France", "Paris"), ("Japan", "Tokyo"), ("Kenya", "Nairobi"), ("Peru", "Lima")]
tasks = [
    AgentSession.Config(
        messages=[{"role": "user", "content": f"What is the capital of {country}?"}],
        verifier=NamesCity.Config(city=city),
        max_episode_tokens=1024,
    )
    for country, city in capitals
]
dataset = TaskDataset(tasks, batch_size=2, seed=0)

3. Train with the cookbook's loop

The cookbook's train_rl_async runs the training: it samples answers, has your verifier score them, trains on the scores and publishes the new weights. You don't write any of that.

This assumes you've already created training_client, sampling_client and formatter, as Reinforcement learning shows:

from looper_cookbook import train_rl_async
from looper_sdk import SamplingParams

config = train_rl_async.Config(
    params=SamplingParams(max_tokens=512), group_size=8, max_policy_lag=1
)
reports = await train_rl_async.train(
    training_client, sampling_client, dataset, config, formatter=formatter, steps=20
)

To score a different way, you change only the verifier. To train on different questions, you change only the tasks. The loop stays the same.

Next steps