Looper Documentation
Looper Cookbook

RL with the cookbook

This page takes you from a file of your own problems to a model trained with reinforcement learning (RL), using the cookbook. It's the place to start if you want to run RL without writing the training loop yourself.

In RL, you don't give the model example answers. You give it problems, and a way to check its answers. The model writes several answers to each problem, each answer gets a score, called its reward, and the model learns to write answers like the ones that scored well. How training works explains this in more detail.

There are two ways to run it, and both use the same loop underneath:

This page covers:

Before you start

Choose your problems

This matters more than any setting. RL learns by comparing the answers the model writes to the same problem. Answers that scored better than the others become more likely, and answers that scored worse become less likely.

So a problem only teaches the model something when its answers get different scores:

Before you train, check how the model does on your problems. The script below measures the model on some held-out problems before it trains, so its first line of output tells you. If the model already gets nearly everything right, or nearly everything wrong, change your problems before you spend time training. Make them harder or easier, or mix in problems of different difficulty.

While it trains, the loop reports how many groups of answers had different scores (groups_trained). If that number stays near 0, the model isn't learning from your problems.

Get your data ready

The ready-made script and the script below both take a JSONL file, which is a text file with one JSON object per line. Each line is one problem, with its question and its answer, which is a number:

{"question": "A shop sells pens at 3 for $2. How much do 12 pens cost?", "answer": "8"}

The model is asked each question with an instruction to finish with #### and the number. Its answer scores 1 if the number after #### is right, and 0 if not.

If your answers aren't numbers, you need a different way to score them, called a verifier. Verifiers explains how verifiers work, and how to write your own. Using your verifier with the cookbook shows where it goes in the script below.

Keep some problems out of training, so you can measure the model on problems it hasn't seen. 50 to 100 gives you a rough score, and more gives you a steadier one. Put them in a separate file for the ready-made script, or at the end of your file for the script below.

Run the ready-made script

From the root of your clone of the cookbook, with LOOPER_API_KEY set, run:

python -m looper_cookbook.recipes.rl_basic --data my_problems.jsonl --eval-data my_held_out.jsonl --project my-project

It trains a LoRA of Qwen/Qwen3.5-9B for 50 steps, then saves a checkpoint. Starting the run and its sampler can take a few minutes while Looper finds GPUs. It measures the held-out problems before the first step and after the last, and prints the share it got right each time, such as before: held out 0.420 ± 0.050; after: held out 0.610 ± 0.049. At the end, it prints the checkpoint's name.

The options you're most likely to change:

Option Default What it sets
--model Qwen/Qwen3.5-9B The model to train. See Available models.
--steps 50 How many training steps to run.
--batch-size 32 How many problems each step uses.
--group-size 16 How many answers the model writes to each problem.
--max-turn-tokens 512 The longest answer the model can write, in tokens. An answer that runs out of room scores 0.
--learning-rate 8e-5 for LoRA How big each update is.
--eval-every Only before and after How often to measure the held-out problems.
--thinking Off Let the model reason before it answers.

Choosing settings explains when to change each one. Recipes lists every option.

Run your own script

This script does the same training as rl_basic, and you can change any part of it. Save it as rl.py, set LOOPER_API_KEY and LOOPER_PROJECT, and run it with python rl.py. It holds out the last HELD_OUT problems in your file:

import asyncio
import json
import os
from uuid import uuid4

from transformers import AutoTokenizer

from looper_cookbook import train_rl
from looper_cookbook.datasets.tasks import TaskDataset
from looper_cookbook.episode_sessions.agent_session import AgentSession
from looper_cookbook.message_formatters.models import message_formatter
from looper_cookbook.verifiers.final_number import FinalNumber
from looper_sdk import AdamW, Lora, SamplingParams, ServiceClient, TrainingConfig

MODEL = "Qwen/Qwen3.5-9B"
DATA = "my_problems.jsonl"
HELD_OUT = 50
LORA_RANK = 32
LEARNING_RATE = 8e-5
MAX_TURN_TOKENS = 512
BATCH_SIZE = 32
GROUP_SIZE = 16
STEPS = 50
INSTRUCTION = "Solve it step by step, then give the final answer as a number after '####'."

tokenizer = AutoTokenizer.from_pretrained(MODEL)
formatter = message_formatter(MODEL, tokenizer, enable_thinking=False)
with open(DATA, encoding="utf-8") as lines:
    problems = [json.loads(line) for line in lines if line.strip()]


def asked(problem):
    return [{"role": "user", "content": f"{problem['question']}\n{INSTRUCTION}"}]


# The most tokens one attempt can use: the longest question, and the longest answer.
episode_tokens = (
    max(len(formatter.build_generation_prompt(asked(problem))) for problem in problems)
    + MAX_TURN_TOKENS
)


def task(problem):
    return AgentSession.Config(
        messages=asked(problem),
        verifier=FinalNumber.Config(answer=str(problem["answer"])),
        max_episode_tokens=episode_tokens,
    )


tasks = [task(problem) for problem in problems]
train_tasks, held_out = tasks[:-HELD_OUT], tasks[-HELD_OUT:]
dataset = TaskDataset(train_tasks, batch_size=BATCH_SIZE, seed=0)
print(f"{len(train_tasks)} problems to train on, {len(held_out)} held out")

training = TrainingConfig(
    model=MODEL,
    kind=Lora(rank=LORA_RANK),
    tokens_per_step=BATCH_SIZE * GROUP_SIZE * episode_tokens,
    max_seq_len=episode_tokens,
    optimizer=AdamW(learning_rate=LEARNING_RATE, betas=(0.9, 0.95), max_grad_norm=1.0),
)
loop_config = train_rl.Config(
    params=SamplingParams(max_tokens=MAX_TURN_TOKENS, temperature=1.0),
    group_size=GROUP_SIZE,
    eval_every=10,
)


def report(step):
    if step.result is None:
        print(f"before training: held out {step.metrics['eval/reward']:.3f}")
        return
    line = (
        f"step {step.step}/{STEPS}: reward {step.metrics['reward']:.3f}, "
        f"{step.metrics['groups_trained']:.0f} of {step.metrics['groups']:.0f} groups trained"
    )
    if "eval/reward" in step.metrics:
        line += f", held out {step.metrics['eval/reward']:.3f}"
    print(line)


async def main():
    config = ServiceClient.Config(
        project=os.environ["LOOPER_PROJECT"],
        display_name="rl",
    )
    async with await config.build() as service:
        print("starting the run and its sampler, which can take a few minutes...")
        training_client = await service.create_training_client(training, timeout_s=20 * 60)
        sampler = await training_client.create_sampling_client(
            max_seq_len=episode_tokens,
            max_concurrent_sequences=min(192, BATCH_SIZE * GROUP_SIZE),
            timeout_s=20 * 60,
        )
        await train_rl.train(
            training_client,
            sampler,
            dataset,
            loop_config,
            formatter=formatter,
            steps=STEPS,
            eval_tasks=held_out,
            on_step=report,
        )

        name = f"rl-{uuid4().hex[:8]}"
        saved = await (await training_client.save_state(name)).result(timeout_s=300)
        await service.wait_checkpoint(saved.id, timeout_s=15 * 60)
        print("saved the trained model as", name)


asyncio.run(main())

What each part does:

To use your own way of scoring, replace FinalNumber.Config(...) in task with your verifier's config. Reinforcement learning explains the loop in full, and The RL loop walks through it step by step.

To try the trained model, use try.py from Try the trained model, with the same instruction after your question.

Is it working?

The script prints three numbers to watch:

With a small held-out set, the score can change by a few points by chance. Reading the numbers explains how to tell a real improvement from noise.

Keep your program running

Your program has to keep running for the whole training run. The training happens on Looper, but your program sends it each step, and it keeps the session open.

So for a long run, run your script on a machine that stays on, such as a server or a cloud VM, rather than a laptop.

Next steps