> ## 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.

# Choose where to run the Loops SDK

> Run your Loops training script on your laptop to start, then on Baseten with baseten loops exec for long or unattended runs.

Run a Loops script on your laptop to start. Your laptop runs the Python loop, which submits training operations, waits for results, and requests samples. Baseten runs the trainer and sampler.

For long or unattended runs, use `baseten loops exec` to run the same script on Baseten. You don't need to change the SDK client when you move the script.

Start with the project and `train_loops.py` from the [Loops quickstart](https://docs.baseten.co/loops/quickstart.md). Keep the same [`ServiceClient`](https://docs.baseten.co/reference/sdk/loops/service-client) setup in the script for either location:

```python train_loops.py theme={"system"}
from baseten.loops import ServiceClient

service_client = ServiceClient()
```

`ServiceClient()` reads `BASETEN_API_KEY` and connects to `https://api.baseten.co` by default.

## Run the Python script on your own machine

**To run the script locally**:

1. From your project directory, set your [workspace API key](https://docs.baseten.co/organization/api-keys.md). Replace `<YOUR_API_KEY>` with the key:

   ```bash theme={"system"}
   export BASETEN_API_KEY="<YOUR_API_KEY>"
   ```

2. Run your training script:

   ```bash theme={"system"}
   uv run python train_loops.py
   ```

   The quickstart script prints the session and run IDs, training loss, sampled text, and checkpoint paths. Compare its output with [Inspect the results](https://docs.baseten.co/loops/quickstart.md#list-checkpoints).

Keep your laptop or third-party host awake while the script runs. Its proxy, VPN, firewall, DNS, and idle-connection policies must permit sustained traffic to Baseten.

A sandbox or hosted notebook can restrict outbound traffic even when the process stays running. Check DNS resolution and outbound HTTPS access from the environment that runs your script, not only from your laptop. Also check whether the host suspends idle processes or its proxy closes long-lived connections. A successful first API request doesn't show that the environment can sustain a training run.

The SDK retries transient connection failures, but it can't keep a run going from a host or network that's persistently unstable.

Closing the training client doesn't deactivate the Loops run. See [Deactivate a run](https://docs.baseten.co/loops/deactivate-a-run.md) to end it and its paired sampler, and to stop a dedicated run's GPU billing.

## Run the Python script on Baseten

`baseten loops exec` packages a project directory and runs your script as a [Training Job](https://docs.baseten.co/training/overview.md), using your active [Baseten CLI profile](https://docs.baseten.co/reference/cli/baseten/overview.md). The job supplies `BASETEN_API_KEY` from a team secret, which Truss creates on first use unless you supply your own credential. Use a CLI profile for the same workspace as your local API key.

**To run the script on Baseten**:

From your project directory, run the same `train_loops.py` with its uv dependencies and stream its logs:

```bash theme={"system"}
baseten loops exec --dir . --tail --with-uv --no-ssh -- uv run python train_loops.py
```

By default, the job opens an SSH session you can connect to, which requires [Training SSH](https://docs.baseten.co/training/ssh.md#prerequisites) for your workspace. This command disables SSH with `--no-ssh`. Omit it if you want an SSH session and your workspace has Training SSH enabled.

Always pass `--dir`. Use `.` for the current project directory or pass another project path. The remote command runs from the uploaded directory.

With `--tail`, the command writes the submission result to stdout and streams logs to stderr until the job finishes. It returns a nonzero exit code if the job fails. Press Ctrl+C to stop watching; the remote job keeps running.

**To submit the script and check it later**:

1. Run the following command to return a structured result after submission:

   ```bash theme={"system"}
   baseten loops exec --dir . --with-uv --no-ssh --output json -- uv run python train_loops.py
   ```

2. Use `job_id` from the result to follow the job's logs:

   ```bash theme={"system"}
   baseten train job logs --job-id "<job_id>" --tail
   ```

The Training Job runs only your script and bills as a Training Job. The Loops run bills separately, by its [availability mode](https://docs.baseten.co/loops/availability-modes/overview.md). `baseten loops exec` has no Loops API equivalent, because it uploads local source; see [`baseten loops exec`](https://docs.baseten.co/reference/cli/baseten/loops#exec) for every option.
