官方API

Grok 4.5 API 官方对接:从请求到生产部署的工程全清单

xAI Grok 4.5 官方 API 接入手册:OpenAI 兼容调用、reasoning effort 参数、Responses 协议工具调用、速率限制与错误码规范,助力本地团队快速切换到 GrokCode 中转战场。

Full article body is primarily in Chinese for SEO depth; key points above are localized. Use the language switcher and deep links for global navigation.

## Grok 4.5 API 官方对接:从请求到生产部署的工程全清单

这是 xAI Grok 4.5(2026 年 7 月发布,专为编码与 Agent 能力设计)的官方 API 完整对接指南。谁适用?需要支持 OpenAI 兼容调用、工具调用(web_search / x_search / code_interpreter)和 configurable reasoning 的本地团队或生产团队。怎么决策?优先选择官方协议,避免中转踩坑——GrokCode 中转平台提供 xAI API 中转倍率、模型天梯缓存与本地 vLLM 部署实验室,助力你从请求到生产无缝切换。

Grok 4.5 官方 API 是当前最强官方接口,主战场必须掌握官方协议。以下工程可核验清单覆盖概览、兼容对接、reasoning effort 参数、Responses 协议工具调用、速率限制与密钥管理,全部基于 xAI 官方文档(x.ai/docs/developers)。

Grok 4.5 官方 API 概览与定价

Grok 4.5 官方 API 基于 OpenAI Responses API 和 Chat Completions 标准,支持 500K context 窗口。知识截止日期为 2026 年 2 月 1 日,可通过 Responses API 调用工具执行 real-time 操作。

定价(USD / 1M tokens,<200K prompt 默认)

模型类别InputCached InputOutput
Grok 4.5$2.00$0.30$6.00
Grok 4.5(≥200K)$4.00$0.60$12.00
  • 缓存命中可显著降低成本。
  • Reasoning tokens 按 output 价格计费。
  • Batch API 支持 20% 折扣(部分模型)。
  • Imagine / Video / Voice 另有定价(非本文重点)。

官方端点https://api.x.ai/v1/responses(推荐)与 /v1/chat/completionsQuickstart:创建 xAI Console 密钥,Authorization: Bearer $XAI_API_KEY

OpenAI 兼容接口标准对接示例

Grok 4.5 官方 API 完全兼容 OpenAI 接口,只需将 base_url 设置为 https://api.x.ai/v1。支持 model: "grok-4.5"input(array of messages)与 stream: true

Python(OpenAI SDK)示例: ```python from openai import OpenAI import os

client = OpenAI(api_key=os.getenv("XAI_API_KEY"), base_url="https://api.x.ai/v1") response = client.responses.create( model="grok-4.5", input=[ {"role": "user", "content": "Find and fix the bug: function median(a){a.sort();return a[a.length/2]}"} ], stream=False ) print(response.choices[0].message.content) ```

curl 示例: ``bash curl https://api.x.ai/v1/responses \ -H "Authorization: Bearer $XAI_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "grok-4.5", "input": [{"role": "user", "content": "Hello"}] }' ``

推荐设置

  • store: false(避免缓存重复计费)
  • include: ["web_search_call"](获取工具输出)
  • max_tokens: 1024

reasoning effort 低/中/高参数详解与性能影响

Grok 4.5 支持 reasoning.effort 参数(Responses API),默认 "high",无法完全禁用。控制模型思考深度:

  • low:少量 reasoning tokens,适合延迟敏感的 Agentic 工具调用,响应更快。
  • medium:平衡思考与速度,适合复杂数据分析。
  • high(默认):深度逻辑,多步推理,适合竞争级任务、编码与 Agent。

Python 示例: ``python response = client.responses.create( model="grok-4.5", reasoning={"effort": "medium"}, input=[{"role": "user", "content": "Prove why 1+1=2 in two steps."}] ) ``

影响

  • 更高 effort 消耗更多 reasoning_tokens(按 output 计费),但质量提升 2x(官方基准)。
  • 低 effort 适合高频短任务,可节省 50%+ tokens。
  • 不能与 presencePenaltyfrequencyPenaltystop 同时使用。

Responses 协议工具调用(web_search / x_search / code_interpreter)

Grok 4.5 官方 API 在 Responses 协议下原生支持内置工具,无需自定义 function calling。工具由 xAI 服务器端执行。

示例(tools 数组): ``json { "model": "grok-4.5", "input": [{"role": "user", "content": "最新 xAI 更新"}], "tools": [ {"type": "web_search"}, {"type": "x_search"}, {"type": "code_interpreter"} ], "stream": true } ``

工具详情

  • web_search:实时网页搜索 + 引用(sources)。
  • x_search:X(Twitter)帖子搜索,支持 keyword / semantic / user。
  • code_interpreter:Python sandbox 执行(支持 matplotlib 等可视化)。

输出获取(include 参数): ``json "include": ["web_search_call", "code_interpreter_call"] ``

多工具并行:可同时调用多个工具,Grok 自动决定调用顺序与次数。 高级:client-side function calling + server-side 组合。

常见错误码与合规检查表

官方 API 错误码规范清晰,便于生产监控。

常见错误码表(移动端横向滚动友好):

状态码原因解决方案合规建议
400无效参数 / 模型不存在检查 JSON 字段与模型名称使用官方 SDK
401密钥无效 / 未授权检查 Authorization Bearer严格密钥轮换
403团队/模型权限不足联系 xAI Console 管理员最小权限 ACL
404模型或 endpoint 找不到确认 base_url 与路径版本锁定在官方文档
422请求体格式错误校验 JSON schema单元测试 payload
429速率限制超出指数退避 + 随机抖动监控 tier 增长
5xx服务器内部错误立即重试接入 GrokCode 中转重试层

合规检查清单

  • 所有请求必须含 Authorization: Bearer ...
  • 响应中包含 usage(prompt_tokens, completion_tokens, reasoning_tokens)
  • 缓存使用 prompt_cache_key 避免重复计费
  • 工具输出需显式 include,避免意外费用

批量请求与流式优化

批量:使用 Batch API(异步,24h 内完成),部分模型享 20% 折扣。

流式优化

  • stream: true 实时输出。
  • 使用 include: ["verbose_streaming"] 获取 reasoning 进度。
  • 客户端缓存历史(prompt_cache_key)实现对话记忆。
  • vLLM 本地部署时,可用 reasoning_effort 模拟官方效果。

生产环境速率限制与密钥管理

速率限制(Tier 0 默认,基于累计花费):

  • grok-4.5:Tier 0 ~150 RPS / 50M TPM(Tier 4 显著提升)。
  • 多 Agent 模型:更紧限制。
  • Image:固定 5 RPS。

处理 429:指数退避 + jitter,最大重试 5 次。

密钥管理

  • xAI Console 创建管理密钥(Management API)。
  • 推荐方案:

- 生产密钥限制 qps / qpm / tpm。 - 环境变量分 dev/staging/prod。 - 定期轮换(Management API 支持)。 - 避免浏览器直接调用。

推荐:通过 GrokCode API 中转(xAI API 中转倍率 + 本地部署实验室),实现自动重试、缓存与成本优化。

风险与边界

Grok 4.5 API 官方对接存在以下风险:

  • Reasoning tokens 计费较高,复杂任务可能超预算。
  • 工具调用依赖服务器端执行,隐私敏感数据不推荐直接使用。
  • 速率限制随团队累计花费自动升级,突发高峰可能触发 429。
  • 模型知识截止 2026 年 2 月,实时信息需结合工具。

免责声明:本文非法律意见,仅供工程参考。使用 API 前请查阅官方文档并自行评估风险。GrokCode 实验室不承担任何因使用本指南导致的责任。

延伸阅读

English summary

This is the complete engineering guide for official xAI Grok 4.5 API integration (released July 2026, strongest for coding and agentic tasks). It covers overview, OpenAI-compatible calling, reasoning effort parameters, Responses protocol tool support (web_search / x_search / code_interpreter), error codes, batch/streaming, and production rate limits/key management. Grok 4.5 offers 500K context, $2/$6 per M tokens pricing, and native server-side tools. Use reasoning.effort (low/medium/high) to balance speed and quality. Compatible with OpenAI SDK at api.x.ai/v1. Rate limits scale by team spend (e.g., 150 RPS default). For production, implement exponential backoff on 429 errors and use prompt caching. GrokCode provides API transit, model ladder, and local vLLM deployment for verified engineering workflows. Full docs at x.ai/docs.

适用于 GrokCode 倍率榜。信息仅供参考,不构成购买、投资或法律意见。