Messages API for Grok
Updated:
Generate text, pass system and user messages, read token usage, and continue a conversation through the OpenAI-compatible Responses API.
To display text while Grok is generating it, use the Streaming API.
Request example
curl https://api.llm-gate.tech/v1/responses \
-H "Authorization: Bearer $GROK_API_DEV_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "grok-4.5",
"input": [
{"role": "system", "content": "Answer clearly and concisely."},
{"role": "user", "content": "Explain binary search in two sentences"}
]
}'Request parameters
| Field | Type | Required | Description |
|---|---|---|---|
| model | string | Yes | Model ID. Use grok-4.5 for the model documented on this site. |
| input | string | array | Yes | A plain prompt or an ordered list of messages with role and content. |
| instructions | string | No | High-level behavior or context applied to this response. |
| max_output_tokens | integer | No | Upper bound for generated output tokens. Useful for latency and cost control. |
| temperature | number | No | Sampling randomness. Omit it unless the use case needs explicit tuning. |
| stream | boolean | No | Return incremental SSE events instead of waiting for the complete response. |
| store | boolean | No | Controls whether the response can be referenced later by ID when supported. |
| previous_response_id | string | No | ID of the previous response used to continue a conversation. |
Message roles and instructions
Roles separate model instructions, the current user request, and previous assistant responses.
systemApplication-level rules, tone, boundaries, and context.
userThe request or data supplied by the end user.
assistantA prior model answer included when you manage history locally.
Response format
{
"id": "resp_01abc",
"object": "response",
"status": "completed",
"model": "grok-4.5",
"output": [{
"type": "message",
"role": "assistant",
"content": [{
"type": "output_text",
"text": "A binary search repeatedly halves the search range."
}]
}],
"usage": {
"input_tokens": 24,
"output_tokens": 12,
"total_tokens": 36
}
}statuscompletedThe response is ready.
in_progressGeneration is still running.
incompleteThe response stopped before completion.
id- The resp_… identifier is used to continue a stateful conversation through previous_response_id and to diagnose a specific request.
usageinput_tokens is the processed context, output_tokens is generated text, and total_tokens is their sum. Input and output costs are calculated separately.
See the calculation formula and current rates on the Pricing page.
Stateful and stateless conversations
The xAI Responses API format supports both approaches. In stateless mode your application stores the full message history and sends the required context with every request. In stateful mode the API chains turns through previous_response_id, so the next request only needs the new user message.
Stateless
const history = [
{ role: "user", content: "Explain binary search in one sentence." },
{ role: "assistant", content: "Binary search repeatedly halves a sorted search range." },
{ role: "user", content: "Now show a JavaScript implementation." },
];
const response = await client.responses.create({
model: "grok-4.5",
input: history,
store: false,
});
console.log(response.output_text);Stateful
const first = await client.responses.create({
model: "grok-4.5",
input: "Explain binary search in one sentence.",
});
const followUp = await client.responses.create({
model: "grok-4.5",
previous_response_id: first.id,
input: "Now show a JavaScript implementation.",
});
console.log(followUp.output_text);