工具

引用

查看 Markdown

智能体工具 API 提供两类引用信息:全部引用(遇到的所有来源的完整列表)和 行内引用(直接嵌入响应文本的 Markdown 风格链接)。


全部引用

citations 属性位于 response 对象上,提供智能体在搜索过程中遇到的所有来源 URL 的完整列表。该列表默认始终返回,无需额外配置。

引用会从成功的工具执行中自动收集,为智能体的信息来源提供完整可追溯性,并在智能体请求完成时返回。

请注意,该列表中的 URL 不一定都会在最终答案中被直接引用。智能体可能在研究过程中检查某个来源,随后判断其与用户查询的相关性不足;为了保持透明,该 URL 仍会出现在列表中。

response.citations

行内引用

行内引用是 Markdown 风格链接(例如 [[1]](https://x.ai/news)),会直接插入响应文本中模型引用来源的位置。除这些可见链接外,响应对象上还提供 结构化元数据,其中包含精确的位置信息。

重要:启用行内引用并不保证模型会在每个答案中引用来源。模型会根据查询的上下文和性质决定何时、何处包含引用。

配置行内引用

行内引用在 Responses APIxAI Python SDK(gRPC chat API)中的行为不同。

Responses API 行为适用于以下客户端:

  • 针对 /v1/responses

  • Python(OpenAI SDK)

  • JavaScript(通过 xai.responses()

  • JavaScript(OpenAI SDK)

Responses API(cURL、Python/JS OpenAI SDK、JS AI SDK)xAI Python SDK
默认值启用,响应文本可能包含 [[N]](url) 链接,无需额外配置禁用,省略 include,或不传入 "inline_citations"
启用默认启用,无需额外操作。include=["inline_citations"] 传给 chat.create() 方法
禁用include=["no_inline_citations"]默认禁用

禁用行内引用时,响应文本不会包含任何 [[N]](url) Markdown 链接。annotations 内容块上的 output_text 字段可能仍然存在,但注释只会列出搜索过程中遇到的来源,不会包含指向响应文本的位置引用。

启用(Responses API 默认;xAI Python SDK 需主动启用)

# Inline citations are enabled by default for the Responses API
curl https://api.x.ai/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $XAI_API_KEY" \
  -d '{
  "model": "grok-4.7",
  "input": [
    {"role": "user", "content": "What is xAI?"}
  ],
  "tools": [{"type": "web_search"}]
}'

禁用(Responses API 需主动禁用;xAI Python SDK 默认)

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.getenv("XAI_API_KEY"),
    base_url="https://api.x.ai/v1",
)

response = client.responses.create(
    model="grok-4.7",
    input=[
        {"role": "user", "content": "What is xAI?"}
    ],
    tools=[
        {"type": "web_search"},
    ],
    include=["no_inline_citations"],  # Disable inline citations
)

# Response text will not contain inline citation markdown
for item in response.output:
    if item.type == "message":
        for content in item.content:
            if content.type == "output_text":
                print(content.text)

Markdown 引用格式

启用行内引用后,模型会将 Markdown 风格的引用链接直接插入响应文本:

Output

The latest announcements from xAI, primarily from their official X account (@xai) and website (x.ai/news), date back to November 19, 2025.[[1]](https://x.ai/news/)[[2]](https://x.ai/)[[3]](https://x.com/i/status/1991284813727474073)

渲染为 Markdown 后,会显示为可点击链接:

xAI 的最新公告主要来自其官方 X 账号(@xai)和网站(x.ai/news),日期可追溯至 2025 年 11 月 19 日。[1][2][3]

格式为 [[N]](url),其中:

  • N 是引用的连续显示编号,从 1 开始

  • url 是来源 URL

引用编号:引用编号始终从 1 开始并依次递增。如果响应后文再次引用同一来源,会复用原引用编号。

图像嵌入

enable_image_searchweb_search 工具上启用时,Grok 可能将图像结果嵌入为 Markdown 图像,而不是带编号的文本引用:

Output

Here are images of Starship on the launch pad:
![Why the SpaceX Starship launch pad matters](https://www.astronomy.com/wp-content/uploads/2024/09/starship-test-flight-mission-scaled.jpg)

格式为 ![alt](url),其中:

  • alt 是图像的简短说明或标题

  • url 是图像来源 URL


访问结构化行内引用数据

结构化行内引用数据提供响应文本中每个引用的精确位置信息。

响应格式

启用行内引用后,每个 output_text 内容块都包含一个 annotations 数组,其中包含结构化引用元数据(URL、字符偏移量和标签):

JSON

{
  "created_at": 1781829888,
  "completed_at": 1781829888,
  "id": "5808284d-ae14-9981-9289-73515f67ebda",
  "max_output_tokens": null,
  "model": "grok-4.7",
  "object": "response",
  "output": [
    ...
    {
      "content": [
        {
          "type": "output_text",
          "text": "**xAI is an artificial intelligence company founded by Elon Musk in March 2023.** Its stated mission is to \"understand the universe\" by building advanced AI systems that accelerate human scientific discovery.[[1]](https://x.ai/company)\n\n### Key Details\n- **Flagship product**: Grok, a family of frontier AI models focused on reasoning, code, voice, image generation, and video. These are trained on massive infrastructure, including what the company describes as the world's largest supercluster (Colossus). Grok powers chatbots, APIs, and multimodal tools available via a unified API.[[2]](https://x.ai/)\n- **Current status (as of mid-2026)**: xAI operates as a subsidiary of SpaceX following an acquisition in February 2026. It is also connected to the X social platform (formerly Twitter), which xAI effectively became the parent of in 2025. The company has expanded into data centers and enterprise AI offerings (e.g., integrations with Amazon Bedrock and Databricks).[[3]](https://en.wikipedia.org/wiki/XAI_(company))\n- **Headquarters and team**: Based in the Stanford Research Park in Palo Alto, California. It was initially founded with a team of AI researchers and is led by Elon Musk as CEO.\n\nxAI positions itself as building useful AI, distinct from other labs in its approach. Its official website (x.ai) highlights developer tools, API access, and ongoing model releases. Note that there is an unrelated blockchain/gaming project called Xai (xai.games), but the primary reference to \"xAI\" in this context is Musk's AI venture.[[4]](https://xai.games/)\n\nFor the latest updates, check x.ai or @xai on X.",
          "logprobs": [],
          "annotations": [
            {
              "type": "url_citation",
              "url": "https://x.ai/company",
              "start_index": 208,
              "end_index": 235,
              "title": "1"
            },
            {
              "type": "url_citation",
              "url": "https://x.ai/",
              "start_index": 585,
              "end_index": 605,
              "title": "2"
            },
            {
              "type": "url_citation",
              "url": "https://en.wikipedia.org/wiki/XAI_(company)",
              "start_index": 972,
              "end_index": 1022,
              "title": "3"
            },
            {
              "type": "url_citation",
              "url": "https://xai.games/",
              "start_index": 1555,
              "end_index": 1580,
              "title": "4"
            }
          ]
        }
      ],
      "id": "msg_5808284d-ae14-9981-9289-73515f67ebda",
      "role": "assistant",
      "type": "message",
      "status": "completed"
    }
  ],
  "parallel_tool_calls": true,
  "previous_response_id": null,
  "reasoning": {
    "effort": "low",
    "summary": "detailed"
  },
  ...
}

每个引用注释包含:

字段类型说明
typestring始终为 "url_citation"
urlstring来源 URL
start_indexint引用在响应文本中的起始字符位置
end_indexint引用的结束字符位置(不包含该位置)
titlestring引用标签;对于文本引用,即可见引用编号(例如 "1"、"2")

图像嵌入也可以生成注释元数据。该注释 title 不会显示在 Markdown 图像中。

# After streaming or sampling completes, access the structured inline citations:
for citation in response.inline_citations:
    print(f"Citation [{citation.id}]:")
    print(f"  Position: {citation.start_index} to {citation.end_index}")
    
    # Check citation type
    if citation.HasField("web_citation"):
        print(f"  Web URL: {citation.web_citation.url}")
    elif citation.HasField("x_citation"):
        print(f"  X URL: {citation.x_citation.url}")

使用位置索引

start_indexend_index 的值遵循 Python 切片约定:

  • start_index:引用中第一个 [ 的字符位置

  • end_index:紧接着闭合 之后紧接着的字符位置(),不包含该位置)

使用简单切片从响应文本中提取精确的引用 Markdown:

Python

content = response.content

for citation in response.inline_citations:
    # Extract the markdown link from the response text
    citation_text = content[citation.start_index:citation.end_index]
    print(f"Citation text: {citation_text}")

流式行内引用

流式传输期间,行内引用会持续累积,并可在最终响应中获取。随着模型生成文本,Markdown 链接会实时出现在 chunk.content 中:

Python

for response, chunk in chat.stream():
    # Markdown links appear in chunk.content in real-time
    if chunk.content:
        print(chunk.content, end="", flush=True)
    
    # Inline citations can also be accessed per-chunk during streaming
    for citation in chunk.inline_citations:
        print(f"\nNew citation: [{citation.id}]")

# After streaming, access all accumulated inline citations
print("\n\nAll inline citations:")
for citation in response.inline_citations:
    url = ""
    if citation.HasField("web_citation"):
        url = citation.web_citation.url
    elif citation.HasField("x_citation"):
        url = citation.x_citation.url
    print(f"  [{citation.id}] {url}")

最后更新:2026 年 8 月 25 日