> ## Documentation Index
> Fetch the complete documentation index at: https://docs.baseten.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Sample from a run

> Sample from the weights a Loops training run publishes, and see how its paired sampler picks up new weights in each availability mode.

A sampler generates completions from a base model or a published checkpoint. Each run can have a paired sampler that serves the weights the run publishes, for RL rollouts or evaluation during training. To sample without a trainer, such as after a run ends, [create a standalone sampler](https://docs.baseten.co/loops/standalone-samplers.md).

## Sample published weights

Request a sampling client from a training client with [`save_weights_and_get_sampling_client()`](https://docs.baseten.co/reference/sdk/loops/training-client). It publishes the current weights and returns a client for them.

To sample from weights you saved with [`save_weights_for_sampler()`](https://docs.baseten.co/reference/sdk/loops/training-client), pass the returned path to [`TrainingClient.create_sampling_client()`](https://docs.baseten.co/reference/sdk/loops/training-client). Then call [`sample()`](https://docs.baseten.co/reference/sdk/loops/sampling-client) with a tokenized prompt. To generate one completion from the saved weights and print it, run:

```python theme={"system"}
from baseten.loops import ModelInput, SamplingParams

weights = training_client.save_weights_for_sampler(name="final").result(timeout=600.0)
sampling_client = training_client.create_sampling_client(model_path=weights.path)

tokenizer = training_client.get_tokenizer()
prompt = tokenizer.apply_chat_template(
    [{"role": "user", "content": "How do I learn Python?"}],
    tokenize=False,
    add_generation_prompt=True,
)
response = sampling_client.sample(
    prompt=ModelInput.from_ints(tokenizer.encode(prompt, add_special_tokens=False)),
    num_samples=1,
    sampling_params=SamplingParams(max_tokens=48),
)
print(tokenizer.decode(response.sequences[0].tokens))
```

The script prints the generated reply. For a complete script and its output, see [Sample from the checkpoint](https://docs.baseten.co/examples/sft-sdk-loop.md#sample-from-the-checkpoint).

By default, the run creates its paired sampler at the first sampling request. To have the sampler ready for that request, set `with_sampler=True` in [`create_lora_training_client()`](https://docs.baseten.co/reference/sdk/loops/service-client), and the run creates its paired sampler with the trainer. Leave it off for runs that only train, so the run doesn't provision a sampler.

## Weight sync

Publishing sampler weights makes a checkpoint available to the run's paired sampler. What a sampling client serves afterward depends on the [availability mode](https://docs.baseten.co/loops/availability-modes/overview.md):

* **Serverless:** Loops pins the client to the published checkpoint. Publish again and use the returned client to sample from later weights. Each publish needs a name the run hasn't used; see [Save a checkpoint](https://docs.baseten.co/loops/save-checkpoints.md).
* **Dedicated:** The client serves the published weights or later ones. The paired sampler loads newly published weights without restarting, so the client doesn't stay on an exact snapshot.

In the dedicated mode, [`TrainingClient.policy_version`](https://docs.baseten.co/reference/sdk/loops/training-client) reports the trainer's optimizer-step count, which is the policy version that a client tracks. The serverless mode doesn't support it.

A paired sampler accepts checkpoints with the run's LoRA configuration. To sample from a checkpoint with a different LoRA configuration, [create a standalone sampler](https://docs.baseten.co/loops/standalone-samplers.md).

## Next steps

* [Set sampling parameters](https://docs.baseten.co/loops/sampling-settings.md): Control temperature, cut-offs, length, and stop sequences.
* [Router replay](https://docs.baseten.co/loops/router-replay.md): Record the experts that a mixture-of-experts sampler chose so the trainer can replay them.
* [Save a checkpoint](https://docs.baseten.co/loops/save-checkpoints.md): Save the weights that a sampler serves.
