XAI's Grok models on Gemini Enterprise Agent Platform support the Responses API for generating responses.
This page shows how to make calls to Grok models using the Responses API.
To use Grok models with Gemini Enterprise Agent Platform, you must perform the following steps. The Gemini Enterprise Agent Platform API (aiplatform.googleapis.com) must be enabled.
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
roles/resourcemanager.projectCreator), which contains the
resourcemanager.projects.create permission. Learn how to grant
roles.
Verify that billing is enabled for your Google Cloud project.
Enable the Gemini Enterprise Agent Platform API.
Roles required to enable APIs
To enable APIs, you need the serviceusage.services.enable permission. If you
created the project, then you likely already have this permission through the
Owner role (roles/owner). Otherwise, you can get this permission through the
Service Usage Admin role (roles/serviceusage.serviceUsageAdmin).
Learn how to grant roles.
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
roles/resourcemanager.projectCreator), which contains the
resourcemanager.projects.create permission. Learn how to grant
roles.
Verify that billing is enabled for your Google Cloud project.
Enable the Gemini Enterprise Agent Platform API.
Roles required to enable APIs
To enable APIs, you need the serviceusage.services.enable permission. If you
created the project, then you likely already have this permission through the
Owner role (roles/owner). Otherwise, you can get this permission through the
Service Usage Admin role (roles/serviceusage.serviceUsageAdmin).
Learn how to grant roles.
The following samples show how to make a non-streaming call to the Responses API:
Before trying this sample, follow the Python setup instructions in the Agent Platform quickstart using client libraries.
To authenticate to Agent Platform, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Before running this sample, make sure to set the OPENAI_BASE_URL environment variable or set up oauth credentials.
For more information, see Authentication and credentials.
from openai import OpenAI client = OpenAI() response = client.responses.create( model="MODEL", input="INPUT", max_output_tokens=MAX_OUTPUT_TOKENS, stream=False, ) print(response)
xai/grok-4.20-reasoning.Specify a lower value for shorter responses and a higher value for potentially longer responses.
After you set up your environment, you can use REST to test a text prompt. The following sample sends a request to the publisher model endpoint.
Before using any of the request data, make the following replacements:
xai/grok-4.20-reasoning.Specify a lower value for shorter responses and a higher value for potentially longer responses.
HTTP method and URL:
POST https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses
Request JSON body:
{
"model": "MODEL",
"input": "INPUT",
"max_output_tokens": MAX_OUTPUT_TOKENS,
"stream": false
}
To send your request, choose one of these options:
Save the request body in a file named request.json,
and execute the following command:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses"
Save the request body in a file named request.json,
and execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses" | Select-Object -Expand Content
The following example shows a complete curl request:
curl -s -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ "https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses" \ -d '{ "model": "xai/grok-4.20-reasoning", "input": "Explain black holes in one short sentence.", "max_output_tokens": 100, "stream": false }'
Based on the Responses API definition, a non-streaming response will contain a unique ID, model metadata, usage statistics, and an output array containing the generated text.
{
"background": false,
"completed_at": 1778892918,
"created_at": 1778892916,
"error": null,
"frequency_penalty": 0,
"id": "c8AHavnIMP6UifEPgIfcgAg",
"incomplete_details": null,
"instructions": null,
"max_output_tokens": null,
"max_tool_calls": null,
"metadata": {
"system_fingerprint": "fp_39c5j0a3e9"
},
"model": "MODEL",
"object": "response",
"output": [
{
"content": [
{
"annotations": [],
"logprobs": [],
"text": "OUTPUT_TEXT",
"type": "output_text"
}
],
"id": "msg_c8AHavnIMP6UifEPgIfcgAg",
"role": "assistant",
"status": "completed",
"type": "message"
}
],
"parallel_tool_calls": true,
"presence_penalty": 0,
"previous_response_id": null,
"prompt_cache_key": null,
"reasoning": {
"effort": "medium",
"summary": "detailed"
},
"safety_identifier": null,
"service_tier": "default",
"status": "completed",
"store": false,
"temperature": 0.7,
"text": {
"format": {
"type": "text"
}
},
"tool_choice": "auto",
"tools": [],
"top_logprobs": 0,
"top_p": 0.95,
"truncation": "disabled",
"usage": {
"extra_properties": {
"google": {
"traffic_type": "ON_DEMAND"
}
},
"input_tokens": 335,
"input_tokens_details": {
"cached_tokens": 320
},
"num_server_side_tools_used": 0,
"num_sources_used": 0,
"output_tokens": 305,
"output_tokens_details": {
"reasoning_tokens": 284
},
"total_tokens": 640
},
"user": null
}
The following samples show how to make a streaming call to the Responses API:
Before trying this sample, follow the Python setup instructions in the Agent Platform quickstart using client libraries.
To authenticate to Agent Platform, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Before running this sample, make sure to set the OPENAI_BASE_URL environment variable or set up oauth credentials.
For more information, see Authentication and credentials.
from openai import OpenAI client = OpenAI() stream = client.responses.create( model="MODEL", input="INPUT", max_output_tokens=MAX_OUTPUT_TOKENS, stream=True, ) for event in stream: if event.type == "response.output_text.delta": print(event.delta, end="")
xai/grok-4.20-reasoning.Specify a lower value for shorter responses and a higher value for potentially longer responses.
After you set up your environment, you can use REST to test a text prompt. The following sample sends a request to the publisher model endpoint.
Before using any of the request data, make the following replacements:
xai/grok-4.20-reasoning.Specify a lower value for shorter responses and a higher value for potentially longer responses.
HTTP method and URL:
POST https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses
Request JSON body:
{
"model": "MODEL",
"input": "INPUT",
"max_output_tokens": MAX_OUTPUT_TOKENS,
"stream": true
}
To send your request, choose one of these options:
Save the request body in a file named request.json,
and execute the following command:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses"
Save the request body in a file named request.json,
and execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses" | Select-Object -Expand Content
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-07-23 UTC.