Skip to main content

Call OpenAI APIs with a Choreo Identity & Access Management Agent Identity

Cloud has the newest features. Some may not be in a release yet; the newest release is Identity & Access Management 7.3.0.

OpenAI workload identity federation accepts a signed token from an external issuer and returns a short-lived OpenAI access token in exchange. Point it at Choreo Identity & Access Management and your agent reaches the OpenAI API with the identity it already has. No OpenAI API key sits in the agent's environment, no key is shared between agents, and disabling the agent in Choreo Identity & Access Management ends its OpenAI access at the next exchange.

This guide explains how to authenticate an agent to the OpenAI API using workload identity federation. The agent gets an Agent Token from Choreo Identity & Access Management. OpenAI verifies that token and issues a short-lived access token of its own. The agent sends that access token on its API calls.

See Workload Identity Federation for what federation replaces and how the exchange works, and Agent Token for how Choreo Identity & Access Management issues that token and what it carries.

Prerequisites​

  • Access to a Choreo Identity & Access Management tenant.
  • An OpenAI developer account with access to Organization Settings.
  • curl and jq available in your terminal.

Create the Agent​

  1. Sign in to the Choreo Identity & Access Management Console at https://console.cloud.choreolabs.com/iam/.
  2. Navigate to Agents → New Agent.
  3. Enter an Agent name (for example research-agent).
  4. Select an owner for the agent.
  5. Click Create Agent.
  6. Note down the Agent ID, OpenAI matches on this value.
  7. Record the Client ID and Client Secret. The secret is shown once.

A new agent has the client_credentials grant enabled, so it can already request a token for itself. See Agent Token on how to get a token.

Setting the audience

OpenAI matches the token's aud claim against the audience registered on the provider, so the agent token has to carry that value. You can configure a default audience value from the console, under agent's Advanced → OAuth 2 configuration → Default audience (aud).

A client_credentials request that carries no resource parameter now produces a token whose aud claim is the value of default audience (for example https://api.openai.com). This is the value you register with OpenAI below.

Deploy your changes

Saving a change does not make it live. You author in the control plane, and your users sign in on a separate data plane, so a change takes effect in an environment only after you deploy it. Go to Deploy, deploy the latest version to your first environment, then promote it to the next environment when you are ready.

Auto-sync deploys saved changes to your first environment within seconds. Turning it on is your choice, per project. See Deploy a Version, or learn why you deploy and how auto-sync works.

Register Choreo Identity & Access Management as a Workload Identity Provider​

OpenAI verifies the agent token against a key set it holds for your issuer. It can either fetch that key set from your discovery document or store a copy you upload. Discovery needs an issuer served over public HTTPS with no custom port, which Choreo Identity & Access Management is, so this guide uses discovery.

  1. In the OpenAI platform dashboard, navigate to Settings → Organization settings → Security → Workload Identity Provider.
  2. Click Create identity provider.
  3. Enter a Name, for example Choreo Identity & Access Management, and select OIDC as the provider type.
  4. Set Issuer URL to https://<DATA_PLANE_URL>.
  5. Set Audience to the agent's default audience, https://api.openai.com.
  6. Leave the discovery settings untouched. Or you can configure a custom OIDC discovery URL by turning on Use custom URL for OIDC discovery.
  7. Click Create, then record the provider ID, a string such as idp_a1B2c3D4e5F6g7H8i9J0kLmN.

A signing key rotation needs no change on the OpenAI side, because OpenAI refreshes the key set itself when it meets a kid it does not hold.

Map the Agent to a Service Account​

The provider establishes which issuer OpenAI trusts. A service account mapping decides which OpenAI identity a given token may assume. Match on the sub claim: it carries the agent's ID, it is unique to that agent, and it survives a client secret rotation.

  1. Open the provider you created, then click on create mapping.
  2. Enter a Name, for example mapping-research-agent.
  3. Under the key value section, Set Key to sub and use the Agent ID as the value. You can add additional claim mappings if needed.
  4. Select the Project and the Service Account the agent acts as.
  5. Set Permissions to the narrowest scope set the agent needs, or leave it unrestricted to inherit the service account's own access.
  6. Save the mapping, then record the service account ID, a string such as user-Ab1Cd2Ef3Gh4Ij5Kl6Mn7Op8.

Give each agent its own mapping, and keep the values distinct. OpenAI rejects an exchange when more than one enabled mapping matches it.

Run the Flow​

Request the Agent Token​

Send a client_credentials request with the agent's credentials:

TOKEN_RESPONSE=$(curl -X POST https://<DATA_PLANE_URL>/oauth2/token \
-H 'Content-Type: application/x-www-form-urlencoded' \
-u '<CLIENT_ID>:<CLIENT_SECRET>' \
-d 'grant_type=client_credentials')

AGENT_TOKEN=$(echo "$TOKEN_RESPONSE" | jq -r .access_token)

You can inspect the claims before you exchange the token if needed. Following three claims decide whether the exchange succeeds:

ClaimExpected value
issThe Issuer URL on the provider, for example https://<DATA_PLANE_URL>.
audThe Audience on the provider, which is the agent's default audience.
subThe Value on the mapping, which is the Agent ID.

See Agent Token for a detailed explanation on getting an agent token.

Exchange the Token at OpenAI​

ACCESS_TOKEN=$(curl -s https://auth.openai.com/oauth/token \
-H 'content-type: application/json' \
--data '{
"grant_type": "urn:ietf:params:oauth:grant-type:token-exchange",
"subject_token_type": "urn:ietf:params:oauth:token-type:jwt",
"subject_token": "'"$AGENT_TOKEN"'",
"identity_provider_id": "<IDENTITY_PROVIDER_ID>",
"service_account_id": "<SERVICE_ACCOUNT_ID>"
}' | jq -r .access_token)

OpenAI returns an access token that lasts at most 3600 seconds:

{
"access_token": "<ACCESS_TOKEN>",
"issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
"token_type": "Bearer",
"expires_in": 3600
}

The exchange assumes an existing service account. It never creates a principal, a project, or a workspace membership, so an agent reaches only what you granted that service account beforehand.

Call the OpenAI API​

curl -s https://api.openai.com/v1/responses \
-H "authorization: Bearer $ACCESS_TOKEN" \
-H 'content-type: application/json' \
--data '{
"model": "gpt-5.4-mini",
"input": "write a 3 word sentence",
"store": true
}'

Use Choreo Identity & Access Management with OpenAI SDK​

The OpenAI SDKs can run the exchange for you. Supply a subject token provider that fetches the agent token from Choreo Identity & Access Management, and pass the provider ID and service account ID alongside it.

The workload_identity option is available in the OpenAI Python SDK from version 2.31.0 onwards, so install openai>=2.31.0 before you run the example below.

import os

import requests
from openai import OpenAI
from openai.auth import SubjectTokenProvider

TOKEN_ENDPOINT = "https://<DATA_PLANE_URL>/oauth2/token"


def thunderid_agent_token_provider() -> SubjectTokenProvider:
def get_token() -> str:
response = requests.post(
TOKEN_ENDPOINT,
data={"grant_type": "client_credentials"},
auth=(os.environ["AGENT_CLIENT_ID"], os.environ["AGENT_CLIENT_SECRET"]),
timeout=10,
)
response.raise_for_status()
return response.json()["access_token"]

return {"token_type": "jwt", "get_token": get_token}


client = OpenAI(
workload_identity={
"identity_provider_id": os.environ["OPENAI_IDENTITY_PROVIDER_ID"],
"service_account_id": os.environ["OPENAI_SERVICE_ACCOUNT_ID"],
"provider": thunderid_agent_token_provider(),
},
)

response = client.responses.create(
model="gpt-5.4-mini",
input="write a 3 word sentence",
)

print(response.output_text)

Store AGENT_CLIENT_ID and AGENT_CLIENT_SECRET in your secret manager, not in the image or the repository. An agent that must not hold a shared secret at all can authenticate with a signed assertion instead, which changes only how the subject token provider fetches the agent token. See Agent Authentication.

Troubleshooting​

SymptomLikely cause
invalid_client from Choreo Identity & Access ManagementWrong client ID or secret, or the agent has no OAuth configuration.
invalid_subject_token from OpenAIThe agent token expired, its iss does not match the provider's issuer URL, or the signing key rotated and the uploaded JWKS is stale.
The exchange fails on the audienceThe token's aud is not the audience on the provider. Check whether you have configured the default audience in Choreo Identity & Access Management.
No mapping matched the exchangeThe token's sub differs from the Value on the mapping. Decode the token and compare it against the Agent ID.
Several mappings matched the exchangeTwo enabled mappings on the provider accept the same token. Disable the extra one.
401 from api.openai.comThe OpenAI access token expired. It lasts at most 3600 seconds, so exchange again rather than caching it longer.
403 from api.openai.comThe mapping's permissions are narrower than the call needs, or the service account has no access to the project.

Next Steps​

Was this page helpful?