2 / 6

Lesson · 12 min

Your first completion

A POST you can read, with the fields that matter.

Server-side, key from the environment
curl https://api.x.ai/v1/chat/completions \
  -H "Authorization: Bearer $XAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "grok-4.5",
    "messages": [
      {"role": "system", "content": "Be concise. Mark unknowns."},
      {"role": "user", "content": "In one sentence, what is Colossus?"}
    ],
    "max_tokens": 128
  }'

Read three fields on the way back: choices[0].message.content (the text), usage (prompt and completion tokens), and any error. Log the error body. Do not retry in a hot loop.

  • system — durable instructions for this call or session.
  • user / assistant — the thread. You send prior turns if you want memory.
  • max_tokens — a cap, not a suggestion. Set it on anything user-facing.