> ## Documentation Index
> Fetch the complete documentation index at: https://wb-21fd5541-docs-1778-mysql-updates.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Start or stop a W&B Sweep Agent on one or more machines.

# Start or stop a sweep agent

Start a W\&B Sweep on one or more agents on one or more machines. W\&B Sweep agents query the W\&B server you launched when you initialized a W\&B Sweep (`wandb sweep)` for hyperparameters and use them to run model training.

To start a W\&B Sweep agent, provide the W\&B Sweep ID that was returned when you initialized a W\&B Sweep. The W\&B Sweep ID has the form:

```bash theme={null}
entity/project/sweep_ID
```

Where:

* entity: Your W\&B username or team name.
* project: The name of the project where you want the output of the W\&B Run to be stored. If the project is not specified, the run is put in an "Uncategorized" project.
* sweep\_ID: The pseudo random, unique ID generated by W\&B.

Provide the name of the function the W\&B Sweep will execute if you start a W\&B Sweep agent within a Jupyter Notebook or Python script.

The following code snippets demonstrate how to start an agent with W\&B. We assume you already have a configuration file and you have already initialized a W\&B Sweep. For more information about how to define a configuration file, see [Define sweep configuration](/models/sweeps/define-sweep-configuration/).

<Tabs>
  <Tab title="CLI">
    Use the `wandb agent` command to start a sweep. Provide the sweep ID that was returned when you initialized the sweep. Copy and paste the code snippet below and replace `sweep_id` with your sweep ID:

    ```bash theme={null}
    wandb agent sweep_id
    ```
  </Tab>

  <Tab title="Python script or notebook">
    Use the W\&B Python SDK library to start a sweep. Provide the sweep ID that was returned when you initialized the sweep. In addition, provide the name of the function  the sweep will execute.

    ```python theme={null}
    wandb.agent(sweep_id=sweep_id, function=function_name)
    ```

    <Warning>
      **Multiprocessing**

      You must wrap your `wandb.agent()` and `wandb.sweep()` calls with `if __name__ == '__main__':` if you use Python standard library's `multiprocessing` or PyTorch's `pytorch.multiprocessing` package. For example:

      ```python theme={null}
      if __name__ == '__main__':
          wandb.agent(sweep_id="<sweep_id>", function="<function>", count="<count>")
      ```

      Wrapping your code with this convention ensures that it is only executed when the script is run directly, and not when it is imported as a module in a worker process.

      See [Python standard library `multiprocessing`](https://docs.python.org/3/library/multiprocessing.html#the-spawn-and-forkserver-start-methods) or [PyTorch `multiprocessing`](https://docs.pytorch.org/docs/stable/notes/multiprocessing.html#asynchronous-multiprocess-training-e-g-hogwild) for more information about multiprocessing. See [https://realpython.com/if-name-main-python/](https://realpython.com/if-name-main-python/) for information about the `if __name__ == '__main__':` convention.
    </Warning>
  </Tab>
</Tabs>

### Stop W\&B agent

<Warning>
  Random and Bayesian searches will run forever. You must stop the process from the command line, within your python script, or the [Sweeps UI](./visualize-sweep-results).
</Warning>

Optionally specify the number of W\&B Runs a Sweep agent should try. The following code snippets demonstrate how to set a maximum number of [W\&B Runs](/models/ref/python/experiments/run) with the CLI and within a Jupyter Notebook, Python script.

<Tabs>
  <Tab title="Python script or notebook">
    First, initialize your sweep. For more information, see [Initialize sweeps](./initialize-sweeps).

    ```
    sweep_id = wandb.sweep(sweep_config)
    ```

    Next, start the sweep job. Provide the sweep ID generated from sweep initiation. Pass an integer value to the count parameter to set the maximum number of runs to try.

    ```python theme={null}
    sweep_id, count = "dtzl1o7u", 10
    wandb.agent(sweep_id, count=count)
    ```

    <Warning>
      If you start a new run after the sweep agent has finished, within the same script or notebook, then you should call `wandb.teardown()` before starting the new run.
    </Warning>
  </Tab>

  <Tab title="CLI">
    First, initialize your sweep with the [`wandb sweep`](/models/ref/cli/wandb-sweep) command. For more information, see [Initialize sweeps](./initialize-sweeps).

    ```
    wandb sweep config.yaml
    ```

    Pass an integer value to the count flag to set the maximum number of runs to try.

    ```python theme={null}
    NUM=10
    SWEEPID="dtzl1o7u"
    wandb agent --count $NUM $SWEEPID
    ```
  </Tab>
</Tabs>
