# xAI Grok API Quickstart — OpenAI-Compatible Frontier Model > xAI Grok API is OpenAI-compatible at api.x.ai/v1. Swap base URL + key, keep the SDK. Grok-3, Grok-2 Vision, 1M-token context. ## Install Copy the content below into your project: ## Quick Use 1. Get key at console.x.ai 2. `OpenAI(base_url='https://api.x.ai/v1', api_key=XAI_KEY)` 3. Set `model='grok-3'` — done --- ## Intro xAI's Grok API is OpenAI-compatible — point the OpenAI SDK at api.x.ai/v1 with an XAI_API_KEY, change `model='grok-3'`, and you're running on Grok. Grok-3 is the flagship reasoning model with 1M-token context; Grok-2 Vision handles images; Live Search gives real-time web results inside the call. Best for: apps that need fresh real-time knowledge (news, prices, sports) without a separate retrieval layer; very long context tasks (>200K tokens). Works with: openai-python, openai-node, LangChain, LlamaIndex via OpenAI-compatible adapter. Setup time: 2 minutes. --- ### Python (openai SDK) ```python from openai import OpenAI client = OpenAI( base_url="https://api.x.ai/v1", api_key=os.environ["XAI_API_KEY"], ) resp = client.chat.completions.create( model="grok-3", messages=[{"role": "user", "content": "What's the latest on the SpaceX launch this week?"}], ) print(resp.choices[0].message.content) ``` ### Vision with Grok-2 ```python resp = client.chat.completions.create( model="grok-2-vision-latest", messages=[{ "role": "user", "content": [ {"type": "text", "text": "What's wrong with this UI screenshot?"}, {"type": "image_url", "image_url": {"url": "https://example.com/screenshot.png"}}, ], }], ) ``` ### Live Search (real-time web grounding) ```python resp = client.chat.completions.create( model="grok-3", messages=[{"role": "user", "content": "What's BTC price right now and the top 3 reasons it moved today?"}], extra_body={ "search_parameters": { "mode": "on", # off | auto | on "sources": [{"type": "web"}, {"type": "x"}, {"type": "news"}], "max_search_results": 8, } }, ) print(resp.choices[0].message.content) print(resp.usage.num_sources_used) # how many sources Grok grounded against ``` ### Model lineup | Model ID | Context | Best for | |---|---|---| | `grok-3` | 1,000,000 | Long-context reasoning, complex agents | | `grok-3-mini` | 131,072 | Fast cheap reasoning | | `grok-2-vision-latest` | 32,768 | Image understanding | | `grok-2-image-latest` | n/a | Image generation | ### Pricing (per 1M tokens, May 2026) - Grok-3: $5 input / $15 output - Grok-3-mini: $0.30 input / $0.50 output ### Migration from OpenAI The only changes: `base_url` and the `model` string. Tools, vision, JSON mode, streaming — all work identically. --- ### FAQ **Q: How does Grok 1M context compare to Gemini 2M?** A: Both work for full-corpus tasks. Grok-3's 1M is denser per dollar at $5/M input. Gemini 2.5 Pro is ~$1.25/M but has stricter rate limits. For 100K–500K typical jobs, Grok is faster end-to-end; for >800K, both work. **Q: What sources does Live Search index?** A: Web (search engine), X (Twitter posts), News (press articles), and RSS. You whitelist via the `sources` array. Grok returns inline citations and a `num_sources_used` count for verification. **Q: Is the OpenAI-compat layer feature-complete?** A: Mostly — chat.completions, streaming, tools, vision, JSON mode all work. Audio (TTS / Whisper) is not on the xAI API; use OpenAI for those. Embeddings: not yet on xAI as of May 2026. --- ## Source & Thanks > Built by [xAI](https://x.ai). API docs at [docs.x.ai](https://docs.x.ai/docs). > > Public SDK: [xai-org](https://github.com/xai-org) --- ## 快速使用 1. 在 console.x.ai 拿 key 2. `OpenAI(base_url='https://api.x.ai/v1', api_key=XAI_KEY)` 3. 设 `model='grok-3'` —— 完事 --- ## 简介 xAI 的 Grok API 跟 OpenAI 兼容 —— OpenAI SDK 指向 api.x.ai/v1,带 XAI_API_KEY,改 `model='grok-3'` 就跑 Grok。Grok-3 是旗舰推理模型,1M token 上下文;Grok-2 Vision 处理图片;Live Search 让调用内拿到实时网页结果。适合需要新鲜实时知识(新闻 / 价格 / 体育)的应用、不想单独搭 retrieval 层、超长上下文(>20 万 token)任务。兼容 openai-python、openai-node、LangChain、LlamaIndex(走 OpenAI-compatible 适配器)。装机时间 2 分钟。 --- ### Python(openai SDK) ```python from openai import OpenAI client = OpenAI( base_url="https://api.x.ai/v1", api_key=os.environ["XAI_API_KEY"], ) resp = client.chat.completions.create( model="grok-3", messages=[{"role": "user", "content": "本周 SpaceX 发射最新进展是什么?"}], ) print(resp.choices[0].message.content) ``` ### Grok-2 视觉 ```python resp = client.chat.completions.create( model="grok-2-vision-latest", messages=[{ "role": "user", "content": [ {"type": "text", "text": "这个 UI 截图哪里不对?"}, {"type": "image_url", "image_url": {"url": "https://example.com/screenshot.png"}}, ], }], ) ``` ### Live Search(实时网页 grounding) ```python resp = client.chat.completions.create( model="grok-3", messages=[{"role": "user", "content": "BTC 现在多少钱?今天上涨/下跌的前 3 大原因?"}], extra_body={ "search_parameters": { "mode": "on", # off | auto | on "sources": [{"type": "web"}, {"type": "x"}, {"type": "news"}], "max_search_results": 8, } }, ) print(resp.choices[0].message.content) print(resp.usage.num_sources_used) # Grok grounding 用了几个源 ``` ### 模型阵容 | 模型 ID | 上下文 | 最佳用途 | |---|---|---| | `grok-3` | 1,000,000 | 长上下文推理、复杂 agent | | `grok-3-mini` | 131,072 | 快速便宜推理 | | `grok-2-vision-latest` | 32,768 | 图像理解 | | `grok-2-image-latest` | 不适用 | 图像生成 | ### 价格(每百万 token,2026 年 5 月) - Grok-3:输入 $5 / 输出 $15 - Grok-3-mini:输入 $0.30 / 输出 $0.50 ### 从 OpenAI 迁移 只改 `base_url` 和 `model` 字符串。Tools、视觉、JSON 模式、流式 —— 全一致。 --- ### FAQ **Q: Grok 1M 上下文跟 Gemini 2M 比?** A: 全语料任务两个都行。Grok-3 的 1M 单位美元更稠 $5/M 输入。Gemini 2.5 Pro ~$1.25/M 但限流更严。10–50 万 token 典型任务 Grok 端到端更快;>80 万都行。 **Q: Live Search 索引哪些源?** A: Web(搜索引擎)、X(Twitter post)、News(媒体文章)、RSS。在 `sources` 数组里白名单。Grok 返回内联引用和 `num_sources_used` 计数供核对。 **Q: OpenAI 兼容层功能齐全吗?** A: 基本齐 —— chat.completions、流式、tools、视觉、JSON 模式都能用。音频(TTS / Whisper)xAI API 没有,那个得用 OpenAI。Embeddings:截至 2026 年 5 月 xAI 还没上。 --- ## 来源与感谢 > Built by [xAI](https://x.ai). API docs at [docs.x.ai](https://docs.x.ai/docs). > > Public SDK: [xai-org](https://github.com/xai-org) --- Source: https://tokrepo.com/en/workflows/xai-grok-api-quickstart-openai-compatible-frontier-model Author: xAI