# Set up the Diagrid Assistant in a self-managed region

Each region runs its own [Diagrid Assistant](https://docs.diagrid.io/concepts/diagrid-assistant). In Diagrid's own regions, Diagrid provides the model key the assistant uses. In a [self-managed BYOC region](https://docs.diagrid.io/operate/hosting/enterprise-self-hosted) (the console and the CLI call it a private region), you provide the key yourself. The key stays in your cluster. Diagrid's control plane only knows the name of the Kubernetes Secret that holds it, never the key itself.

You can set it up on your own, without contacting Diagrid. Once the region reports that it has a working key, Diagrid sets up the assistant there.

## Before you start

- **Your organization's plan includes the Diagrid Assistant, and it is turned on.** If the plan includes it, it's on by default. To check, run [`diagrid org assistant get`](https://docs.diagrid.io/references/diagrid/cli-reference/org/assistant/get). If your plan doesn't include it, contact Diagrid.
- **The region runs Diagrid data plane 1.119.0 or later.** Starting with that version, the Diagrid agent in the region tells Diagrid whether it has a working key. Earlier versions don't report this, so a region on one never gets an assistant. See [Lifecycle management](https://docs.diagrid.io/operate/hosting/enterprise-self-hosted/upgrades-operations) to upgrade.
- **An OpenAI API key.** The assistant uses OpenAI models. The region's cluster must be able to reach OpenAI's API over HTTPS.
- **Your users can reach the region's gateway.** The console in their browser and `diagrid chat` on their machine talk to the assistant through the region's gateway, not through Diagrid. If the region can only be reached from your own network, users need to be on that network, for example through a VPN.
- **Access to the Helm release** you installed the region with, and to its values file (`catalyst-values.yaml` in the install guides).

## Add the model key

The examples use the default release name `catalyst` and namespace `cra-agent`. Use your own if they differ.

1. Create a Secret that holds the key, in the namespace you installed the chart into. Read the key from an environment variable so it doesn't land in your shell history:

   ```bash
   kubectl -n cra-agent create secret generic assistant-model-key \
     --from-literal=api_key="$OPENAI_API_KEY"
   ```

2. Point the chart at the Secret in `catalyst-values.yaml`:

   ```yaml
   agent:
     reagent:
       llm:
         existingSecret:
           name: "assistant-model-key"
           key: "api_key"
   ```

   `agent.reagent` is where the chart keeps the assistant's settings. `key` defaults to `api_key`.

3. Upgrade the release with your current chart version, so nothing else changes:

   ```bash
   helm upgrade catalyst oci://public.ecr.aws/diagrid/catalyst \
        -n cra-agent \
        --version <current-version> \
        -f catalyst-values.yaml
   ```

Because `agent.reagent.llm` changes the agent's configuration, the upgrade restarts the agent. When the agent connects to Diagrid, it checks that the Secret exists, that it can read it, and that the key isn't empty, then reports back. Diagrid then sets up the assistant in the region, which usually takes a few minutes. Until it's ready, the console and `diagrid chat` say the assistant is being set up.

Create the Secret **before** you upgrade. The agent only checks the key when it connects, so if you create or fill in the Secret while the agent is running, it won't notice until it restarts. To restart it:

```bash
kubectl -n cra-agent rollout restart deployment agent
```

### Put the key in the values file instead

For a local or test install, you can put the key straight into the values file:

```yaml
agent:
  reagent:
    llm:
      apiKey: "sk-..."
```

The chart creates the Secret for you. But the key then sits in your values file and in Helm's release history, so use `existingSecret` for anything else. If you set both, `existingSecret` is used.

All the settings are listed in the Helm reference under [Diagrid Assistant model credential](https://docs.diagrid.io/operate/hosting/enterprise-self-hosted/reference#diagrid-assistant-model-credential).

## Check that it works

1. In the console, open **Regions**. Organization admins see an **Assistant** column for self-managed regions, and the region's page shows **Assistant: Available** under **Capabilities**. **Not set up** means the agent reported that it doesn't have a working key. See [Not set up in this region](https://docs.diagrid.io/operate/platform-operations/diagrid-assistant-troubleshooting#region-not-set-up).
2. Open the assistant from one of the region's projects, or ask from the terminal:

   ```bash
   diagrid chat --region <region-name>
   ```

   The first line shows the region. In the console, the drawer's header shows it.

If the region's gateway uses a certificate from a private CA, `diagrid chat` needs to trust that CA. Export it from the gateway's Secret, as described in [Private or self-signed gateway certificate](https://docs.diagrid.io/operate/hosting/enterprise-self-hosted/private-gateway-ca#make-your-applications-trust-the-ca), and pass it to the command:

```bash
diagrid chat --region <region-name> --gateway-ca-file ca.crt
```

For the console, add the same CA to the trust store on the machine that runs the browser.

## Rotate the key

Replace the value in the Secret, under the same name and key:

```bash
kubectl -n cra-agent create secret generic assistant-model-key \
  --from-literal=api_key="$OPENAI_API_KEY" \
  --dry-run=client -o yaml | kubectl apply -f -
```

You don't need to upgrade the release or restart the agent. The agent copies the new key to the assistant the next time it syncs (its drift-reconcile interval, 15 minutes by default), and the assistant starts using it a few seconds later. Until then, the assistant keeps using the old key.

To stop using the old key right away, for example if you think it leaked, replace the Secret and then restart the assistant. It runs in its own project namespace:

```bash
kubectl get deployments --all-namespaces -l dapr.io/app-id=reagent
kubectl rollout restart deployment -n <assistant-namespace> -l dapr.io/app-id=reagent
```

If you used `apiKey`, change it in the values file and run the same `helm upgrade`.

## Turn it off

- **For the whole organization:** an organization admin turns the assistant off on the **Organization Overview** page in the console, under **Diagrid Assistant**, or with [`diagrid org assistant disable`](https://docs.diagrid.io/references/diagrid/cli-reference/org/assistant/disable). This covers every region and leaves the data-sharing level and MCP access as they are.
- **For this region only:** remove `agent.reagent.llm` from `catalyst-values.yaml`, run `helm upgrade` again, and then delete the Secret. The agent restarts and reports that it has no key, and Diagrid removes the assistant from the region. Users can still use the assistant in your organization's other regions.

If Diagrid turns the assistant off for your organization, an organization admin can't turn it back on. Contact Diagrid support.

## Next steps

- [Manage the Diagrid Assistant](https://docs.diagrid.io/operate/platform-operations/diagrid-assistant) — What an operator decides: whether it is on, what it may read, and where it runs.
- [Troubleshoot the assistant](https://docs.diagrid.io/operate/platform-operations/diagrid-assistant-troubleshooting) — What each message from the console and the CLI means, and what to do.
- [diagrid chat](https://docs.diagrid.io/references/diagrid/cli-reference/chat) — The command reference for talking to the assistant from your terminal.
