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
Define the prompt and reward
RL needs a reward function that scores each reply the model generates. Training makes higher-scoring replies more likely. Here, the prompt asks the model to choose between Doom and Quake, and the reward function scoresQuake as 1.0 and any other reply as 0.0. With one correct answer, the mean reward shows how often the model is right.
To format the prompt and score replies, append the following code to train_loops.py. The empty thinking block in Qwen’s chat format turns thinking off:
train_loops.py
Train on sampled replies
Each training step samples a group of 16 replies, updates the model from their rewards, and publishes new sampling weights for the next step. The update uses each reply’s advantage: its reward minus the group’s mean reward. When a group has both correct and incorrect replies, correct replies get positive advantages and become more likely, and incorrect replies get negative advantages and become less likely. When every reply scores the same, every advantage is zero, so the script samples a new group without updating the model. It exits after 20 such groups in a row. Each reply becomes oneDatum with target tokens, the sampler’s log probabilities for those tokens, and the advantage. The -100 target excludes prompt positions from the loss.
To run 5 training steps, append the following code 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
Deploy the latest sampler checkpoint to run evaluations or inference on your tuned model. 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.
- Run this task from a config file: Use the Loops cookbook’s alternative to the Python training loop.
- 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.