Grok API

Messages API for Grok

View as Markdown

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

FieldTypeRequiredDescription
modelstringYesModel ID. Use grok-4.5 for the model documented on this site.
inputstring | arrayYesA plain prompt or an ordered list of messages with role and content.
instructionsstringNoHigh-level behavior or context applied to this response.
max_output_tokensintegerNoUpper bound for generated output tokens. Useful for latency and cost control.
temperaturenumberNoSampling randomness. Omit it unless the use case needs explicit tuning.
streambooleanNoReturn incremental SSE events instead of waiting for the complete response.
storebooleanNoControls whether the response can be referenced later by ID when supported.
previous_response_idstringNoID 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.

system

Application-level rules, tone, boundaries, and context.

user

The request or data supplied by the end user.

assistant

A prior model answer included when you manage history locally.

Response format

JSON
{
  "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
  }
}
status
completed

The response is ready.

in_progress

Generation is still running.

incomplete

The 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.
usage

input_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

JavaScript
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

JavaScript
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);