跳转到内容

模型能力

多智能体

WARNING

此功能目前处于测试阶段。随着我们的迭代,API 接口和行为可能会发生变化。请注意,API 接口并非最终版本,将来可能包含破坏性更改。

实时多智能体研究使 Grok 能够协调多个 AI 智能体,这些智能体实时协作执行深度、多步骤的研究任务。智能体专门研究研究的特定方面(搜索网络、分析数据、综合发现)并合作提供全面、有充分依据的答案。

概述

多智能体研究通过协调一支专业智能体团队,超越了单轮工具使用,这些智能体可以:

  • 搜索和收集来自多个来源的信息
  • 分析和交叉验证来自不同领域的发现
  • 综合带有引用和支持证据的全面答案
  • 迭代研究,根据中间发现实时完善结果

入门指南

要使用实时多智能体研究,请在 API 请求中将 grok-4.20-multi-agent 指定为模型名称。此模型针对协调在研究任务上协作的多个智能体进行了优化。

python
import os

from xai_sdk import Client
from xai_sdk.chat import user
from xai_sdk.tools import web_search, x_search

client = Client(api_key=os.getenv("XAI_API_KEY"))
chat = client.chat.create(
    model="grok-4.20-multi-agent",
    tools=[web_search(), x_search()],
    include=["verbose_streaming"],
)

chat.append(user("Research the latest breakthroughs in quantum computing and summarize the key findings."))

is_thinking = True
for response, chunk in chat.stream():
    if response.usage.reasoning_tokens and is_thinking:
        print(f"\rThinking... ({response.usage.reasoning_tokens} tokens)", end="", flush=True)
    if chunk.content and is_thinking:
        print("\n\nFinal Response:")
        is_thinking = False
    if chunk.content and not is_thinking:
        print(chunk.content, end="", flush=True)

print("\n\nUsage:")
print(response.usage)
python
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.20-multi-agent",
    input=[
        {
            "role": "user",
            "content": "Research the latest breakthroughs in quantum computing and summarize the key findings.",
        },
    ],
    tools=[
        {"type": "web_search"},
        {"type": "x_search"},
    ],
)

print(response)
python
import os
import requests

url = "https://api.x.ai/v1/responses"
headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {os.getenv('XAI_API_KEY')}"
}
payload = {
    "model": "grok-4.20-multi-agent",
    "input": [
        {
            "role": "user",
            "content": "Research the latest breakthroughs in quantum computing and summarize the key findings."
        }
    ],
    "tools": [
        {"type": "web_search"},
        {"type": "x_search"}
    ]
}
response = requests.post(url, headers=headers, json=payload)
print(response.json())
bash
curl https://api.x.ai/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $XAI_API_KEY" \
  -d '{
  "model": "grok-4.20-multi-agent",
  "input": [
    {
      "role": "user",
      "content": "Research the latest breakthroughs in quantum computing and summarize the key findings."
    }
  ],
  "tools": [
    {"type": "web_search"},
    {"type": "x_search"}
  ]
}'
typescript
import { xai } from "@ai-sdk/xai";
import { generateText } from "ai";

const { text } = await generateText({
  model: xai.responses("grok-4.20-multi-agent"),
  prompt:
    "Research the latest breakthroughs in quantum computing and summarize the key findings.",
  tools: {
    web_search: xai.tools.webSearch(),
    x_search: xai.tools.xSearch(),
  },
});

console.log(text);

多智能体工作原理

当您向多智能体模型发送请求时,会启动多个智能体来讨论和协作处理您的查询。每个智能体贡献自己的观点、推理和发现。指定的领导智能体负责综合讨论并向您呈现最终答案。

支持的模型

  • grok-4.20-multi-agent

内置工具支持

xAI 提供了一组内置工具,您可以在请求中启用这些工具以帮助处理最常见的用例,例如 web_searchx_searchcode_executioncollections_search。查看此文档获取更多信息。

一旦您在请求中启用这些工具,服务器将执行智能体循环,根据您的查询在服务器端调用这些工具,直到生成最终答案。

NOTE

使用内置工具会产生额外费用。请查看内置工具的定价详情

输出行为

只有工具调用领导智能体的最终响应会发送回用户。所有子智能体状态(包括其中间推理、工具调用和输出)都是加密的,并且仅在 xAI SDK 中将 use_encrypted_content 设置为 True 时才包含在响应中。这使默认响应保持简洁和专注,同时仍允许您为多轮对话保留完整的多智能体上下文。

配置

您可以配置多少个智能体协作处理一个请求。两种可用的设置是4个智能体16个智能体。更多智能体意味着更深入、更全面的研究,但会消耗更多的 token 并增加延迟。

SDK / API参数4个智能体16个智能体
xAI SDKagent_count416
OpenAI SDKreasoning.effort"low""medium""high""xhigh"
Vercel AI SDKreasoningEffort"low""medium""high""xhigh"
REST APIreasoning.effort"low""medium""high""xhigh"

**最适合:**使用4个智能体进行快速研究和聚焦查询。使用16个智能体进行深度研究和复杂多方面主题的研究。

4个智能体设置

python
import os

from xai_sdk import Client
from xai_sdk.chat import user

client = Client(api_key=os.getenv("XAI_API_KEY"))
chat = client.chat.create(
    model="grok-4.20-multi-agent",
    agent_count=4,
)

chat.append(user("What are the key differences between TCP and UDP?"))
for response, chunk in chat.stream():
    if chunk.content:
        print(chunk.content, end="", flush=True)
python
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.20-multi-agent",
    reasoning={"effort": "low"},
    input=[
        {
            "role": "user",
            "content": "What are the key differences between TCP and UDP?",
        },
    ],
)

print(response.output_text)
python
import os
import requests

url = "https://api.x.ai/v1/responses"
headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {os.getenv('XAI_API_KEY')}"
}
payload = {
    "model": "grok-4.20-multi-agent",
    "reasoning": {"effort": "low"},
    "input": [
        {
            "role": "user",
            "content": "What are the key differences between TCP and UDP?"
        }
    ]
}
response = requests.post(url, headers=headers, json=payload)
print(response.json())
bash
curl https://api.x.ai/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $XAI_API_KEY" \
  -d '{
  "model": "grok-4.20-multi-agent",
  "reasoning": {"effort": "low"},
  "input": [
    {
      "role": "user",
      "content": "What are the key differences between TCP and UDP?"
    }
  ]
}'
typescript
import { xai } from "@ai-sdk/xai";
import { generateText } from "ai";

const { text } = await generateText({
  model: xai.responses("grok-4.20-multi-agent"),
  prompt: "What are the key differences between TCP and UDP?",
  providerOptions: {
    xai: { reasoningEffort: "low" },
  },
});

console.log(text);

16个智能体设置

python
import os

from xai_sdk import Client
from xai_sdk.chat import user

client = Client(api_key=os.getenv("XAI_API_KEY"))
chat = client.chat.create(
    model="grok-4.20-multi-agent",
    agent_count=16,
)

chat.append(user("Analyze the design trade-offs in modern programming languages: compare Rust's ownership model, Go's simplicity philosophy, and Haskell's pure functional approach. Cover memory safety, concurrency, developer productivity, and ecosystem maturity."))
for response, chunk in chat.stream():
    if chunk.content:
        print(chunk.content, end="", flush=True)
python
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.20-multi-agent",
    reasoning={"effort": "high"},
    input=[
        {
            "role": "user",
            "content": "Analyze the design trade-offs in modern programming languages: compare Rust's ownership model, Go's simplicity philosophy, and Haskell's pure functional approach. Cover memory safety, concurrency, developer productivity, and ecosystem maturity.",
        },
    ],
)

print(response.output_text)
python
import os
import requests

url = "https://api.x.ai/v1/responses"
headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {os.getenv('XAI_API_KEY')}"
}
payload = {
    "model": "grok-4.20-multi-agent",
    "reasoning": {"effort": "high"},
    "input": [
        {
            "role": "user",
            "content": "Analyze the design trade-offs in modern programming languages: compare Rust's ownership model, Go's simplicity philosophy, and Haskell's pure functional approach. Cover memory safety, concurrency, developer productivity, and ecosystem maturity."
        }
    ]
}
response = requests.post(url, headers=headers, json=payload)
print(response.json())
bash
curl https://api.x.ai/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $XAI_API_KEY" \
  -d '{
  "model": "grok-4.20-multi-agent",
  "reasoning": {"effort": "high"},
  "input": [
    {
      "role": "user",
      "content": "Analyze the design trade-offs in modern programming languages: compare Rust'\''s ownership model, Go'\''s simplicity philosophy, and Haskell'\''s pure functional approach. Cover memory safety, concurrency, developer productivity, and ecosystem maturity."
    }
  ]
}'
typescript
import { xai } from "@ai-sdk/xai";
import { generateText } from "ai";

const { text } = await generateText({
  model: xai.responses("grok-4.20-multi-agent"),
  prompt:
    "Analyze the design trade-offs in modern programming languages: compare Rust's ownership model, Go's simplicity philosophy, and Haskell's pure functional approach. Cover memory safety, concurrency, developer productivity, and ecosystem maturity.",
  providerOptions: {
    xai: { reasoningEffort: "high" },
  },
});

console.log(text);

NOTE

16个智能体设置使用的 token 显著多于4个智能体设置。根据研究任务的复杂性选择智能体数量——对于聚焦查询使用4个智能体,当您需要全面、多角度分析时使用16个智能体。

常见模式

不使用内置工具

多智能体可以在没有任何内置工具的情况下工作——智能体纯粹依靠其集体知识和推理来协作生成响应。

python
import os

from xai_sdk import Client
from xai_sdk.chat import user

client = Client(api_key=os.getenv("XAI_API_KEY"))
chat = client.chat.create(
    model="grok-4.20-multi-agent",
    include=["verbose_streaming"],
)

chat.append(user("Compare the major approaches to distributed consensus in computer science: Paxos, Raft, and Byzantine fault tolerance. Analyze the trade-offs in safety guarantees, performance, and implementation complexity."))

is_thinking = True
for response, chunk in chat.stream():
    if response.usage.reasoning_tokens and is_thinking:
        print(f"\rThinking... ({response.usage.reasoning_tokens} tokens)", end="", flush=True)
    if chunk.content and is_thinking:
        print("\n\nFinal Response:")
        is_thinking = False
    if chunk.content and not is_thinking:
        print(chunk.content, end="", flush=True)

print("\n\nUsage:")
print(response.usage)
python
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.20-multi-agent",
    input=[
        {
            "role": "user",
            "content": "Compare the major approaches to distributed consensus in computer science: Paxos, Raft, and Byzantine fault tolerance. Analyze the trade-offs in safety guarantees, performance, and implementation complexity.",
        },
    ],
)

print(response)
python
import os
import requests

url = "https://api.x.ai/v1/responses"
headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {os.getenv('XAI_API_KEY')}"
}
payload = {
    "model": "grok-4.20-multi-agent",
    "input": [
        {
            "role": "user",
            "content": "Compare the major approaches to distributed consensus in computer science: Paxos, Raft, and Byzantine fault tolerance. Analyze the trade-offs in safety guarantees, performance, and implementation complexity."
        }
    ]
}
response = requests.post(url, headers=headers, json=payload)
print(response.json())
bash
curl https://api.x.ai/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $XAI_API_KEY" \
  -d '{
  "model": "grok-4.20-multi-agent",
  "input": [
    {
      "role": "user",
      "content": "Compare the major approaches to distributed consensus in computer science: Paxos, Raft, and Byzantine fault tolerance. Analyze the trade-offs in safety guarantees, performance, and implementation complexity."
    }
  ]
}'
typescript
import { xai } from "@ai-sdk/xai";
import { generateText } from "ai";

const { text } = await generateText({
  model: xai.responses("grok-4.20-multi-agent"),
  prompt:
    "Compare the major approaches to distributed consensus in computer science: Paxos, Raft, and Byzantine fault tolerance. Analyze the trade-offs in safety guarantees, performance, and implementation complexity.",
});

console.log(text);

多轮对话

多智能体研究使用 previous_response_id 支持多轮对话,就像其他模型一样。您可以提出后续问题来完善或扩展先前的研究结果,智能体将使用之前的背景信息提供更有针对性的答案。

有关可重用函数和代码示例的完整多轮对话模式,请参见链接对话

定价

领导智能体子智能体消耗的所有 token 都会计费,包括输入 token、输出 token 和推理 token。同样,任何智能体(无论是领导智能体还是子智能体)进行的所有服务器端工具调用都会计入您的工具使用量并相应计费。

由于多个智能体可能并行运行,并且每个智能体都可以独立调用工具,单个多智能体请求可能比标准单智能体请求使用多得多的 token 和工具调用。您可以通过响应中的 usageserver_side_tool_usage 字段监控您的使用情况。

有关详细的定价信息,请参阅定价页面和工具定价页面。

提示指南

充分利用多智能体研究始于如何构建您的请求。以下是有效的模式:

明确设置范围和深度

不要提出宽泛的问题,而是告诉智能体要涵盖哪些具体方面:

text
❌  "Tell me about electric vehicles."
✅  "Compare the top 3 EV manufacturers by battery technology, range, charging infrastructure, and 2025 sales projections."

请求结构化输出

当您请求有组织的、结构化的响应时,多智能体研究表现出色:

text
✅  "Research the pros and cons of microservices vs monolithic architecture. Present your findings as a comparison table with categories: scalability, complexity, deployment, and team size requirements."

指定来源或观点

引导智能体朝着您重视的证据类型发展:

text
✅  "Analyze the environmental impact of large language model training, citing recent academic papers and industry reports from 2024-2025."

将复杂研究分解为对话

对于深度主题,先从宽泛的问题开始,然后通过后续问题缩小范围,而不是将所有内容都塞入一个提示中:

text
Turn 1: "What are the leading approaches to carbon capture technology?"
Turn 2: "Which of those has the best cost-per-ton economics today?"
Turn 3: "What are the main engineering challenges preventing that approach from scaling?"

在相关时提供上下文

如果您的研究建立在先验知识或特定约束之上,请在提示中包含这些上下文:

text
✅  "I'm building a fintech app targeting Southeast Asian markets. Research the regulatory requirements for digital payments in Singapore, Indonesia, and the Philippines."

限制

  • **仅暴露领导智能体输出:**仅返回领导智能体的输出,包括其工具调用和响应内容。子智能体状态是加密的,仅在启用 use_encrypted_content 时才包含——详情请参见输出行为
  • **不支持客户端或自定义工具:**多智能体模型变量目前不支持客户端工具(函数调用)和自定义工具。我们确实支持一组内置工具(例如 web_searchx_search)和远程 MCP 工具。有关更多详细信息,请参阅我们的内置工具文档
  • 不支持聊天完成 API:多智能体模型适用于 OpenAI 聊天完成 API。请使用 xAI SDKResponses API
  • **不支持 max_tokens:**多智能体模型变量目前不支持 max_tokens 参数。

本文档为 docs.x.ai 全站中文翻译,由 AI 自动翻译生成。代码示例请以原文为准。