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

# Workflows

> Run automation workflows in your self-hosted CostGraph with your own Anthropic account

Workflows run automated tasks on your cost data from the **Workflows** page in the CostGraph dashboard. In a self-hosted deployment, they run as Claude agents in your own Anthropic account, and they read your costs through your CostGraph MCP server.

## Before you begin

* An Anthropic API key. See [Get an Anthropic API key](#get-an-anthropic-api-key).
* Graph AI turned on, with `graphAi.mcp.host` set to a public `https` hostname that Anthropic can reach. See [Connect MCP clients](https://docs.costgraph.ai/costgraph/self-hosted/graph-ai.md#connect-mcp-clients).

Workflows need Anthropic to reach your MCP hostname over the internet. An installation without internet access can't run workflows.

## Get an Anthropic API key

Workflows use Claude Managed Agents, which is available to every Anthropic API account.

1. Sign in to the [Claude Console](https://platform.claude.com), or create an account.
2. Open the [API keys page](https://platform.claude.com/settings/keys) and click **Create key**.
3. Set **Linked account** to a service account, so the key keeps working when the person who created it leaves your organization. Scope it to a workspace if you use more than one.
4. Choose an expiration. A key that expires stops workflows until you store a new one, so pick **Never** if you rotate keys yourself.
5. Copy the key, which starts with `sk-ant-`. The Console shows it a single time, when you create it.

Usage is billed to that Anthropic account. Managed Agents keeps session history on Anthropic's side, so it isn't covered by Zero Data Retention or a HIPAA Business Associate Agreement. See [Anthropic's documentation](https://platform.claude.com/docs/en/managed-agents/overview).

## Turn on workflows

**1. Store the API key in a Secret**

```shell theme={null}
kubectl -n <namespace> create secret generic costgraph-automation \
  --from-literal=ANTHROPIC_API_KEY=<your key>
```

**2. Add the Secret to your values file**

```yaml theme={null}
backend:
  automation:
    existingSecret: costgraph-automation
```

Workflows are on by default. To turn them off, set `backend.automation.enabled: false`.

**3. Upgrade the release**

Run `helm upgrade` with your values file. The **Workflows** page is ready after the backend restarts.

## Workflow agent initialization

The first time the backend starts with a key, it creates the workflow agents in your Anthropic account. Later restarts reuse those agents. When an upgrade changes an agent, the backend publishes a new version of it.

Anthropic bills workflow usage to the account that owns the API key.

## Run workflows in your cluster

A workflow can run the commands its agent executes in a sandbox in your own cluster. Sandboxes take their work from a relay that the chart runs next to the backend. The relay polls a self-hosted environment in your Anthropic account and serves the sandboxes on its own `https` hostname.

**1. Create a self-hosted environment**

In the [Claude Console](https://platform.claude.com), in the workspace your API key belongs to, create a self-hosted environment and an environment key for it. Copy the environment ID, which starts with `env_`, and the key.

**2. Add the environment key to your Secret**

Add the key to the Secret named in `backend.automation.existingSecret` as `RELAY_ENVIRONMENT_KEY`.

```shell theme={null}
kubectl -n <namespace> create secret generic costgraph-automation \
  --from-literal=ANTHROPIC_API_KEY=<your API key> \
  --from-literal=RELAY_ENVIRONMENT_KEY=<your environment key> \
  --dry-run=client -o yaml | kubectl apply -f -
```

**3. Turn on the relay**

Give the relay its own hostname, add it to `ingress.tls`, and point DNS for it at your ingress. Sandboxes must be able to reach it. The relay keeps a connection open for as long as a session runs, so raise your ingress controller's timeouts for it. This example uses ingress-nginx annotations.

```yaml theme={null}
agentRelay:
  enabled: true
  host: relay.costgraph.example.com
  environmentId: env_...
  ingress:
    annotations:
      nginx.ingress.kubernetes.io/proxy-read-timeout: "3600"
      nginx.ingress.kubernetes.io/proxy-send-timeout: "3600"
      nginx.ingress.kubernetes.io/proxy-buffering: "off"
  deployment:
    envFrom:
      automation:
        name: costgraph-automation
ingress:
  tls:
    - secretName: costgraph-tls
      hosts:
        - costgraph.example.com
        - relay.costgraph.example.com
```

`agentRelay.deployment.envFrom.automation.name` must match `backend.automation.existingSecret`.

**4. Upgrade and create a worker key**

Run `helm upgrade` with your values file, then restart the backend so it picks up the relay.

```shell theme={null}
kubectl -n <namespace> rollout restart deployment/costgraph-selfhosted
```

In your CostGraph dashboard, create an API key with the `sandbox:worker` scope and store it in a Secret.

```shell theme={null}
kubectl -n <namespace> create secret generic costgraph-sandbox-key \
  --from-literal=api-key=<your worker key>
```

**5. Turn on the agent runtime**

Point the runtime at your relay and upgrade again.

```yaml theme={null}
agentRuntime:
  enabled: true
  dispatcher:
    workerName: my-cluster
    existingSecret: costgraph-sandbox-key
    relayUrl: https://relay.costgraph.example.com
```

`relayUrl` must be `https://` followed by `agentRelay.host`. To run sandboxes on a managed sandbox provider instead of Jobs in the cluster, see [Run workflows on a managed sandbox provider](https://docs.costgraph.ai/costgraph/workflows/managed-sandboxes.md).

**6. Pick the cluster for a goal**

Open a goal and choose your cluster, listed under the `workerName` you set, in its **Sandbox** setting.

Each session runs as a Job in the release namespace. See [Run workflows in your environment](https://docs.costgraph.ai/costgraph/workflows/your-environment.md) to change the sandbox image, resources and network access.

### Troubleshoot sandboxes

| Symptom | What to check |
| - | - |
| The relay pod is not ready | The Secret contains `RELAY_ENVIRONMENT_KEY` and the key belongs to the environment in `agentRelay.environmentId`. Check `kubectl -n <namespace> logs deployment/costgraph-selfhosted-agent-relay`. |
| The cluster never appears in the **Sandbox** setting | The worker key has the `sandbox:worker` scope, and `agentRuntime.dispatcher.relayUrl` is `https://` followed by `agentRelay.host`. |
| A run stays at its first step | The environment ID in `agentRelay.environmentId` is the environment you created the key for, in the same workspace as `ANTHROPIC_API_KEY`. |
| A run stops after about a minute | Your ingress controller's read and send timeouts for the relay hostname are long enough for a full session. |

## Add other Secrets to the backend

To pass any other Secret to the backend, list it under `backend.deployment.envFrom`. Each entry exposes every key in the Secret as an environment variable.

```yaml theme={null}
backend:
  deployment:
    envFrom:
      automation-keys:
        type: secret
        name: costgraph-automation
```

To pass a single key, use `backend.deployment.env`.

```yaml theme={null}
backend:
  deployment:
    env:
      ANTHROPIC_API_KEY:
        valueFrom:
          secretKeyRef:
            name: costgraph-automation
            key: ANTHROPIC_API_KEY
```

## Troubleshoot setup

When workflows are on but not set up, the **Workflows** page shows "Workflows are not set up yet" and links to this guide. Check the cause that matches your installation.

| Cause | What to check |
| - | - |
| No API key | The Secret exists in the release namespace, contains `ANTHROPIC_API_KEY`, and is named in `backend.automation.existingSecret`. |
| MCP hostname missing or not `https` | Graph AI is turned on, `graphAi.mcp.host` is set, and your CostGraph URL uses `https`. |
| The agents could not be created | The key is valid, its account has access to Claude Managed Agents, and the backend can reach `api.anthropic.com`. |

After you fix the cause, restart the backend.

```shell theme={null}
kubectl -n <namespace> rollout restart deployment/costgraph-selfhosted
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.