工具
Citation
Agent tools API 提供两类 citation 信息:全部 Citation(遇到的所有 source 的完整列表)和 Inline Citation(直接嵌入 response 文本的 Markdown 风格链接)。
全部 Citation
该 citations attribute 位于 response object 上,提供 Agent 搜索过程中遇到的所有 source URL 完整列表。该列表默认始终返回,无需额外配置。
Citation 会从成功的 tool 执行中自动收集,为 Agent 的信息 source 提供完整可追溯性,并在 agentic request 完成时返回。
请注意,该列表中的 URL 不一定都会在最终答案中被直接引用。Agent 可能在研究过程中检查某个 source,随后判断其与用户 query 的相关性不足;为了保持透明,该 URL 仍会出现在列表中。
response.citationsInline Citation
Inline citation 是 Markdown 风格链接(例如 [[1]](https://x.ai/news)),会直接插入 response 文本中 model 引用 source 的位置。除这些可见链接外,response object 上还提供 structured metadata,其中包含精确 position 信息。
重要:启用 inline citation 并不保证 model 会在每个答案中引用 source。Model 会根据 query 的 context 和性质决定何时、何处包含 citation。
配置 Inline Citation
Inline citation 在 Responses API 和 xAI Python SDK(gRPC chat API)中的行为不同。
该 Responses API 行为适用于以下 client:
针对
/v1/responsesPython(OpenAI SDK)
JavaScript(通过
xai.responses())JavaScript(OpenAI SDK)
| Responses API(cURL、Python/JS OpenAI SDK、JS AI SDK) | xAI Python SDK | |
|---|---|---|
| 默认值 | 启用,response 文本可能包含 [[N]](url) 链接,无需额外配置 | 禁用,省略 include,或不传入 "inline_citations" |
| 启用 | 默认启用,无需额外操作。 | 将 include=["inline_citations"] 传给 chat.create() method |
| 禁用 | 将 include=["no_inline_citations"] | 默认禁用 |
禁用 inline citation 时,response 文本不会包含任何 [[N]](url) Markdown 链接。annotations content block 上的 output_text 字段可能仍然存在,但 annotation 只会列出搜索过程中遇到的 source,不会包含指向 response 文本的 position reference。
启用(Responses API 默认;xAI Python SDK 需 opt-in)
# 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.5",
"input": [
{"role": "user", "content": "What is xAI?"}
],
"tools": [{"type": "web_search"}]
}'禁用(Responses API 需 opt-out;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.5",
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 Citation 格式
启用 inline citation 后,model 会将 Markdown 风格 citation 链接直接插入 response 文本:
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是 citation 的连续显示编号,从 1 开始url是 source URL
Citation 编号:Citation 编号始终从 1 开始并依次递增。如果 response 后文再次引用同一 source,会复用原 citation 编号。
Image Embed
当 enable_image_search 在 web_search tool 上启用时,Grok 可能将图像结果嵌入为 Markdown image,而不是带编号的文本 citation:
Here are images of Starship on the launch pad:
格式为 ,其中:
alt是图像的简短说明或标题url是图像 source URL
访问结构化 Inline Citation Data
Structured inline citation data 提供 response 文本中每个 citation 的精确 position 信息。
Response 格式
启用 inline citation 后,每个 output_text content block 都包含 annotations array,其中包含 structured citation metadata(URL、character offset 和 label):
{
"created_at": 1781829888,
"completed_at": 1781829888,
"id": "5808284d-ae14-9981-9289-73515f67ebda",
"max_output_tokens": null,
"model": "grok-4.5",
"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 maximally truth-seeking 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"
},
...
}每个 citation annotation 包含:
| 字段 | 类型 | 说明 |
|---|---|---|
type | string | 始终为 "url_citation" |
url | string | Source URL |
start_index | int | Citation 在 response 文本中的起始字符位置 |
end_index | int | Citation 的结束字符位置(不包含该位置) |
title | string | Citation label;对于文本 citation,即可见 citation 编号(例如 "1"、"2") |
Image embed 也可以生成 annotation metadata。Annotation title 不会显示在 Markdown image 中。
# 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}")使用 Position Index
该 start_index 和 end_index 的值遵循 Python slice 约定:
start_index:Citation 第一个[的字符位置end_index:closing 之后紧接着的字符位置(),不包含该位置)
使用简单 slice 从 response 文本中提取精确的 citation Markdown:
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}")Streaming Inline Citation
Streaming 期间,inline citation 会持续累积,并可在最终 response 中获取。随着 model 生成文本,Markdown 链接会实时出现在 chunk.content 中:
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}")