Try the cookbook workflow
Run the same task with a config file instead of a Python training loop.
Define the task and reward
When you fine-tune a model with RL, you provide a task and a reward function that scores the model’s replies. The rewards tell the training loop which behaviors to reinforce. For example, if you want a model to classify support tickets, reward it for assigning the correct category. This quickstart uses a task with one correct answer so you can compare rewards across training steps. The script sends this prompt:Quake scores 1.0. Any other reply scores 0.0. Thinking is off, and a reply can have at most 16 tokens.
Each training step samples 16 replies from the current policy. A reply’s advantage is its reward minus the mean reward of the 16, so the step makes correct replies more likely and wrong replies less likely. When all 16 replies score the same, every advantage is zero and the step samples again. The script runs five training steps with LoRA rank 8 and a learning rate of 2e-5, and prints the mean reward for each step.
Prerequisites
- A Baseten workspace with Loops and a workspace API key.
- uv and Python 3.12 or later.
Install the SDK
Create a uv project and installbaseten-loops:
BASETEN_API_KEY. Replace YOUR_API_KEY with your key:
train_loops.py, then verify the installation by running uv run python train_loops.py:
train_loops.py
Output
Create a training run
Create aServiceClient, then call create_lora_training_client() with the ID of the model you want to train. This example uses Qwen/Qwen3.6-35B-A3B. Set scope="serverless" so Baseten runs the training on hardware it selects and configures for you. The method waits until the trainer accepts operations and returns a TrainingClient.
Replace the contents of train_loops.py with:
train_loops.py
Score a reply
Write the question in Qwen’s chat format. The empty thinking block turns thinking off. The reward decodes a reply and compares it with the answer. Append totrain_loops.py:
train_loops.py
Train on sampled replies
Each completed training step samples a usable group of 16 replies, updates the model from that group, and publishes new sampling weights for the next step. For RL, each sampled reply becomes one datum. It carries the reply’s tokens as targets, the sampler’s logprobs for those tokens, and the reply’s advantage. Prompt positions carry-100 as the target, so they don’t train.
Append to train_loops.py:
train_loops.py
forward_backward() call uses the cispo loss and the sampler’s logprobs to correct for any difference between the policy that sampled a reply and the policy that trains on it. For more information, see Loss functions. save_weights_and_get_sampling_client() publishes a sampler checkpoint under sampler_weights/ that you can deploy to inference, and returns a SamplingClient for it. This checkpoint omits optimizer state, so you can’t resume training from it; use save_state() when you need a resumable checkpoint.
Sample from the tuned weights
The lastsave_weights_and_get_sampling_client() call returned a client for the weights from step 5. Generate one reply from it. Append to train_loops.py:
train_loops.py
Save resumable training state
Callsave_state() to save the model and optimizer state. Append to train_loops.py:
train_loops.py
weights/ path to resume training.
Inspect the results
Calllist_checkpoints() to list the run’s checkpoints, then close the local clients. Append to train_loops.py:
train_loops.py
Output
The script prints the session and run IDs before the first sampling request.
That request can take several minutes while serverless capacity starts.
<run_id> with the printed run ID:
- REST API
- Baseten CLI
Deactivate the run
Closing the local clients doesn’t deactivate the run or its paired sampler. The run incurs no token charges while idle, so deactivation is optional. To deactivate the run and its paired sampler: Replace<run_id> with the ID printed by your script:
- Python
- REST API
- Baseten CLI
Deploy the checkpoint
Now that you’ve fine-tuned your model, deploy its latest sampler checkpoint to run evaluations or inference. Baseten serves the checkpoint and your trained adapter as a dedicated inference deployment. The deployment needs anhf_access_token in workspace secrets and bills for its GPU while it’s live.
- Baseten CLI
- REST API
- Dashboard
To deploy a checkpoint:Replace Follow the prompts for the model name, GPU type and count, and Hugging Face secret name. The output includes the IDs needed to call the model:
<id> with a sampler checkpoint’s id, the first column of the listing above, then run:Output
Next steps
- Design RL environments: Create tasks and reward functions for your own RL training.
- Train a vision model to count shapes: Apply RL to image tasks without a sandbox.
- Dedicated Loops: Configure dedicated hardware, replicas, and spot capacity for training.