# Authenticate to LLM providers

Diagrid signs in to your LLM provider as the workload that makes the call. Every app and agent has an [ID](https://docs.diagrid.io/concepts/identities), and Diagrid proves that identity to the provider with a short-lived signed token. The provider returns its own short-lived access token, and Diagrid uses it for the model call.

This is workload identity federation, and it is the default way to connect a provider:

- **Nothing to store or rotate.** There is no API key in Diagrid, in your code or in your environment.
- **Access is per workload.** The provider grants access to one ID, so you can see and revoke each workload's access on its own.
- **Tokens expire in minutes.** A leaked token is useless soon after.

If your provider or plan doesn't support federation, [use an API key](#use-an-api-key-instead).

## How it works

**Sequence diagram: A model call with workload identity.** Your app holds only its Diagrid API token. Diagrid exchanges a signed identity token for the provider's access token, then calls the model.

Participants: Your app (or agent), Diagrid (conversation component), Provider (token exchange), Provider (model API).

1. Your app (or agent) → Diagrid (conversation component): prompt + dapr-api-token (step 1)
2. Diagrid (conversation component) → Provider (token exchange): signed identity token (step 2)
   - *Note over Provider (token exchange):* Checks issuer, subject, audience
3. Provider (token exchange) → Diagrid (conversation component) (reply): short-lived access token
4. Diagrid (conversation component) → Provider (model API): prompt + access token (step 3)
5. Provider (model API) → Diagrid (conversation component) (reply): completion
6. Diagrid (conversation component) → Your app (or agent) (reply): completion

Federation applies when Diagrid makes the provider call for you: through a [conversation component](https://docs.diagrid.io/operate/project-operations/components), which is what Dapr Agents and the Conversation API use. When your own code calls the provider with its SDK, including through the [LLM proxy](https://docs.diagrid.io/develop/llm-proxy), the SDK still needs the provider key.

## Before you start

You need:

- A Diagrid project with the app or agent that calls the model. See [Connect to Diagrid](https://docs.diagrid.io/develop/connect).
- The [Diagrid CLI](https://docs.diagrid.io/references/diagrid/diagrid-cli-intro), logged in.
- Permission to manage workload identity or IAM settings in your provider account.

## 1. Get the values to register

The provider checks three values in the token Diagrid presents.

| Value | What it is | Where to get it |
|---|---|---|
| Issuer | The OIDC issuer of your region | See below |
| Subject | The SPIFFE ID of the app or agent | See below |
| Audience | A fixed value per provider | Given in each provider's steps |

**Issuer.** On Diagrid Cloud the issuer is:

```text
https://oidc.r1.diagrid.io
```

In a BYOC or Enterprise Server region, read the issuer from the region:

```bash
diagrid region get <region> --output json | jq -r '.status.endpoints.oidc'
```

**Subject.** Read the SPIFFE ID of the app that uses the component:

```bash
diagrid app get <app> --project <project> --output json \
  | jq -r '.status.appIds[0].status.regionIdentities[].spiffeId'
```

The value has the form `spiffe://<region-trust-domain>/ns/<project-namespace>/<app-id>`. For an agent, use `diagrid agent get`. A project in several regions prints one SPIFFE ID per region, each with its own issuer, so register each pair.

Register one rule for each app, with its full SPIFFE ID as the subject. Don't use a prefix or wildcard that covers the project: such a rule gives every app in the project access to the provider account, including apps you add later.

## 2. Register Diagrid with the provider and create the component

Pick your provider. Each tab sets up trust on the provider side, then creates the Diagrid component. `--scopes` limits the component to the app you registered.

**Anthropic**

In the Claude Console, open **Workload identity**, select **Connect workload** and choose **Custom OIDC**. See [Anthropic's guide](https://platform.claude.com/docs/en/manage-claude/workload-identity-federation) for the screens.

1. Set the issuer URL to the issuer from step 1.
2. Create a federation rule with the subject set to the app's SPIFFE ID and the audience set to `https://api.anthropic.com`. Map it to a service account.
3. Copy the federation rule ID, the service account ID and your organization ID.

```bash
diagrid component create llm-provider \
  --type conversation.anthropic \
  --project <project> \
  --metadata federationRuleId=fdrl_01abc \
  --metadata organizationId=00000000-0000-0000-0000-000000000000 \
  --metadata serviceAccountId=svac_01abc \
  --metadata model=claude-sonnet-4-6 \
  --scopes <app>
```

Add `--metadata workspaceId=<id>` when the federation rule is enabled for more than one workspace. All fields are in the [Anthropic component reference](https://docs.diagrid.io/references/diagrid/components-reference/conversation/anthropic).

**OpenAI**

In the OpenAI organization settings, under **Security**, create a **Workload Identity Provider**. See [OpenAI's guide](https://developers.openai.com/api/docs/guides/workload-identity-federation).

1. Set the OIDC issuer URL to the issuer from step 1 and the audience to `https://api.openai.com/v1`.
2. Map the `sub` claim, with the app's SPIFFE ID as its value, to a service account.
3. Copy the identity provider ID and the service account ID.

```bash
diagrid component create llm-provider \
  --type conversation.openai \
  --project <project> \
  --metadata identityProviderId=idp_01abc \
  --metadata serviceAccountId=svc_acct_01abc \
  --metadata model=gpt-4o \
  --scopes <app>
```

All fields are in the [OpenAI component reference](https://docs.diagrid.io/references/diagrid/components-reference/conversation/openai).

**Azure OpenAI**

Azure OpenAI uses Microsoft Entra ID. You add a federated credential to an app registration, and Entra ID accepts the workload's token in place of a client secret.

1. Create an app registration, or reuse the one for your project.
2. Add a federated credential with the issuer from step 1, the app's SPIFFE ID as the subject and `api://AzureADTokenExchange` as the audience.
3. Grant the app registration the **Cognitive Services OpenAI User** role on the Azure OpenAI resource.

The Azure CLI commands for steps 1 and 2 are in [Diagrid identity](https://docs.diagrid.io/operate/hosting/enterprise-self-hosted/azure-installation-guide#diagrid-identity), which also covers the region setting that a self-managed region needs for this audience.

```bash
diagrid component create llm-provider \
  --type conversation.openai \
  --project <project> \
  --metadata apiType=azure \
  --metadata endpoint=https://<resource>.openai.azure.com/ \
  --metadata apiVersion=2025-01-01-preview \
  --metadata azureTenantId=<tenant-id> \
  --metadata azureClientId=<application-client-id> \
  --metadata azureAuthMethods=spiffe \
  --metadata model=<deployment-name> \
  --scopes <app>
```

**Amazon Bedrock**

Amazon Bedrock uses an IAM role that the workload assumes with its token.

1. In IAM, create an OpenID Connect identity provider with the issuer from step 1 as the provider URL and `sts.amazonaws.com` as the audience.

2. Create a role with this trust policy. `<issuer-host>` is the issuer without `https://`.

   ```json
   {
     "Version": "2012-10-17",
     "Statement": [
       {
         "Effect": "Allow",
         "Principal": { "Federated": "arn:aws:iam::<account-id>:oidc-provider/<issuer-host>" },
         "Action": "sts:AssumeRoleWithWebIdentity",
         "Condition": {
           "StringEquals": {
             "<issuer-host>:aud": "sts.amazonaws.com",
             "<issuer-host>:sub": "<app SPIFFE ID>"
           }
         }
       }
     ]
   }
   ```

3. Grant the role permission to invoke the Bedrock models you use.

```bash
diagrid component create llm-provider \
  --type conversation.aws.bedrock \
  --project <project> \
  --metadata region=us-east-1 \
  --metadata assumeRoleArn=arn:aws:iam::<account-id>:role/<role-name> \
  --metadata webIdentityAudience=sts.amazonaws.com \
  --metadata model=<model-id> \
  --scopes <app>
```

All fields are in the [Amazon Bedrock component reference](https://docs.diagrid.io/references/diagrid/components-reference/conversation/aws.bedrock).

**Google Cloud**

Google Cloud uses a workload identity pool. This works with endpoints that accept Google Cloud access tokens, such as the Vertex AI OpenAI-compatible endpoint. See [Google's guide](https://cloud.google.com/iam/docs/workload-identity-federation-with-other-providers).

1. Create a workload identity pool and an OIDC provider in it, with the issuer from step 1 as the issuer URI. Leave the allowed audiences empty, so the provider's resource name is the expected audience.
2. Map `google.subject` to `assertion.sub`.
3. Grant Vertex AI access to the principal `principal://iam.googleapis.com/projects/<project-number>/locations/global/workloadIdentityPools/<pool>/subject/<app SPIFFE ID>`, directly or through a service account it can impersonate.

```bash
diagrid component create llm-provider \
  --type conversation.googleai \
  --project <project> \
  --metadata workloadIdentityProvider=//iam.googleapis.com/projects/<project-number>/locations/global/workloadIdentityPools/<pool>/providers/<provider> \
  --metadata endpoint=https://us-central1-aiplatform.googleapis.com/v1/projects/<gcp-project>/locations/us-central1/endpoints/openapi \
  --metadata model=google/gemini-2.5-flash \
  --scopes <app>
```

Add `--metadata serviceAccountEmail=<email>` to impersonate a service account. All fields are in the [Google AI component reference](https://docs.diagrid.io/references/diagrid/components-reference/conversation/googleai).

:::note
The Anthropic steps were run end to end on Diagrid Cloud. The other providers follow each component's reference and the provider's own federation guide.
:::

## 3. Check that it works

Send one prompt through the component with the app's endpoint and API token. `diagrid dev run` sets both; see [Connect to Diagrid](https://docs.diagrid.io/develop/connect) for other ways to get them.

```bash
curl -s "$DAPR_HTTP_ENDPOINT/v1.0-alpha2/conversation/llm-provider/converse" \
  -H "dapr-api-token: $DAPR_API_TOKEN" \
  -H "content-type: application/json" \
  -d '{"inputs":[{"messages":[{"ofUser":{"content":[{"text":"Say hello"}]}}]}]}'
```

A completion in the response means the provider accepted the workload's identity. A Dapr Agents app uses the component by its name, so an agent that referenced `llm-provider` with an API key needs no code change.

## Use an API key instead

Use a key when the provider has no federation support, for example DeepSeek, Mistral or Hugging Face. This example is for OpenAI:

```bash
diagrid component create llm-provider \
  --type conversation.openai \
  --project <project> \
  --metadata key=<api-key> \
  --metadata model=gpt-4o \
  --scopes <app>
```

Diagrid stores the key as a [secret](https://docs.diagrid.io/operate/project-operations/secrets), not in the component. You are responsible for rotating it. For another provider, use its component type and model from the [components reference](https://docs.diagrid.io/references/diagrid/components-reference/intro).

When you switch a component to workload identity, remove `key` before you check that it works. An Azure OpenAI component keeps using the key for as long as it is set. The other components ignore the key once the workload identity fields are set, so a leftover key is only a stored secret you no longer need.

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| The provider rejects the issuer, or can't fetch its keys | The registered issuer differs from the token's `iss` claim | Register the exact issuer from step 1, with no trailing slash. |
| The provider reports that no rule matches the subject | The subject differs from the app's SPIFFE ID, or the calling app is not the one you registered | Compare the rule with the output of `diagrid app get`. Add a rule for each app and each region. |
| The provider rejects the audience | The audience in the rule differs from the one the component sends | Use the audience shown in the provider's steps, or set the component's audience field to match your rule. |
| The component fails to load | A required workload identity field is missing | Set every required field listed in the component's reference. |

- [Identities](https://docs.diagrid.io/concepts/identities) — What an ID is and how Diagrid issues each workload's identity.
- [Components](https://docs.diagrid.io/operate/project-operations/components) — Create, scope and update the components your apps use.
- [Token budgets](https://docs.diagrid.io/operate/project-operations/token-budgets) — Cap what each workload can spend on a provider.
