
Use Grok on Microsoft Foundry
Use Grok on Microsoft Foundry
Grok runs on Azure AI Foundry through an OpenAI-compatible API. Usage bills to your Azure subscription via Azure Marketplace. Authenticate with Microsoft Entra ID in production. Streaming, tool calling, and structured outputs are supported.
Prerequisites
- Azure subscription with permission to create Foundry resources and deployments
- Role that can call the model (for example Cognitive Services OpenAI User or equivalent)
- Python packages:
pip install openai azure-identity
# optional: azure-ai-projects
Deploy the model
- Open Azure AI Foundry → Models + endpoints.
- Deploy a Grok model (Serverless or PTU).
- Note the deployment name — that string is the
modelparameter (for examplegrok-4.3). You cannot rename a deployment after creation. - Copy the project endpoint host. The OpenAI-compatible base URL looks like:
https://{resource-name}.services.ai.azure.com/api/projects/{project-name}/openai/v1
No api-version query parameter is required on this path.
Entra ID auth (recommended)
import os
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
from openai import OpenAI
token_provider = get_bearer_token_provider(
DefaultAzureCredential(),
"https://ai.azure.com/.default",
)
client = OpenAI(
base_url=os.environ["AZURE_FOUNDRY_OPENAI_BASE_URL"],
api_key=token_provider,
)
response = client.responses.create(
model=os.environ["AZURE_FOUNDRY_DEPLOYMENT_NAME"], # e.g. grok-4.3
input="Summarize three benefits of running Grok on Azure AI Foundry.",
)
print(response.output_text)
Set AZURE_FOUNDRY_OPENAI_BASE_URL to the .../openai/v1 URL above and AZURE_FOUNDRY_DEPLOYMENT_NAME to the exact deployment name from the portal. Sign in with az login (or another DefaultAzureCredential path) before running.
API key alternative
If Keys and Endpoint exposes keys for the resource, you can pass a key as api_key. Prefer Entra ID for production.
Support headers
When you open a support case, include correlation ids from the response headers: request-id, apim-request-id, and x-ms-request-id.
Pitfalls
- Passing a catalog model id instead of the deployment name — Foundry expects the deployment name you created.
- Expecting usage on an xAI console key — Foundry traffic bills through Azure Marketplace / your Azure subscription.
- Adding an
api-versionquery on this OpenAI v1 path — it is not required. - Skipping the Cognitive Services OpenAI User (or equivalent) role — Entra calls fail with authorization errors.