Skip to main content
Use the Python SDK to train a low-rank adaptation (LoRA) adapter for Qwen3.6-35B-A3B with reinforcement learning (RL) on serverless Loops. Sample from the tuned weights, save resumable training state, and deploy the model for inference. Training bills for token usage, and deployment bills for GPU usage while the deployment is live.
To have your coding agent run this quickstart, see Run a quickstart with your agent.

Prerequisites

Optionally, install the Baseten CLI to list and deploy checkpoints and deactivate the run from your terminal.

Install the SDK

Create a uv project and install baseten-loops:
Export your workspace API key as BASETEN_API_KEY. Replace YOUR_API_KEY with your key:
Save the following code as train_loops.py, then verify the installation by running uv run python train_loops.py:
train_loops.py
Output

Create a training run

Create a ServiceClient, 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 scores Quake 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 one Datum 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
The 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 last save_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
You’ve now trained a LoRA adapter and generated a reply from its published weights. This confirms that sampling works; it doesn’t evaluate fine-tuning quality. To sample from newer weights, create a new sampling client. Baseten pins each serverless sampling client to the checkpoint used to create it.

Save resumable training state

Call save_state() to save the model and optimizer state. Append to train_loops.py:
train_loops.py
Use the returned weights/ path to resume training.

Inspect the results

Call list_checkpoints() to list the run’s checkpoints, then close the local clients. Append to train_loops.py:
train_loops.py
Run the script from your project directory:
The output has the following shape. Values and checkpoint rows vary:
Output
The script prints the session and run IDs before the first sampling request. That request can take several minutes while serverless capacity starts.
Check that the script finishes and compare the mean rewards across its 5 training steps. Each printed reward is the fraction of correct replies in a group of 16. Sampled rewards can vary between runs. Use the returned checkpoint paths, names, and IDs directly. Serverless checkpoint names can include a unique suffix. To list the run’s checkpoints after the script exits: Replace <run_id> with the printed run ID:

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:
Saved checkpoints remain available after deactivation.

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 an hf_access_token in workspace secrets and bills for its GPU while it’s live.
To deploy a checkpoint:Replace <id> with a sampler checkpoint’s id, the first column of the listing above, then run:
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:
Output
Use the returned model and deployment IDs to run inference on your endpoint. When you’re done, delete the deployment to stop GPU billing. Deactivating the Loops run doesn’t delete the deployment.

Next steps