工具

Streaming 与同步请求

Agentic request 可以通过 streaming 或同步模式执行。本页介绍这两种方式及其有效用法。

使用 agentic tool calling 时,强烈建议使用 streaming mode。它可以提供:

  • 实时可观察性,在 tool call 发生时即可查看

  • 即时反馈,适用于可能耗时较长的请求

  • Reasoning token 计数,随 model 思考过程更新

Streaming 示例

import os

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

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

chat.append(user("What are the latest updates from xAI?"))

is_thinking = True
for response, chunk in chat.stream():
    # View server-side tool calls in real-time
    for tool_call in chunk.tool_calls:
        print(f"\\nCalling tool: {tool_call.function.name}")
    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("\\nCitations:", response.citations)

同步模式

对于较简单的使用场景,或者希望等待完整 agentic workflow 结束后再处理 response,可以使用同步请求:

import os

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

client = Client(api_key=os.getenv("XAI_API_KEY"))
chat = client.chat.create(
    model="grok-4.5",
    tools=[
        web_search(),
        x_search(),
        code_execution(),
    ],
)

chat.append(user("What is the latest update from xAI?"))

# Get the final response in one go once it's ready
response = chat.sample()

print("Final Response:")
print(response.content)

print("\\nCitations:")
print(response.citations)

print("\\nUsage:")
print(response.usage)
print(response.server_side_tool_usage)

同步请求会等待整个 agentic process 完成后再返回。这对基础场景更简单,但对中间步骤的可见性较低。

在 Responses API 中使用 Tool

Responses API 同时支持 streaming 和 non-streaming mode:

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.5",
    store_messages=True,  # Enable Responses API
    tools=[
        web_search(),
        x_search(),
    ],
)

chat.append(user("What is the latest update from xAI?"))
response = chat.sample()

print(response.content)
print(response.citations)

# The response id can be used to continue the conversation
print(response.id)

访问 Tool Output

Server-side tool call output 可能很大,因此默认不返回。不过,可以选择接收这些 output:

xAI SDK

Tool用于 include 字段的值
"web_search""web_search_call_output"
"x_search""x_search_call_output"
"code_execution""code_execution_call_output"
"collections_search""collections_search_call_output"
"attachment_search""attachment_search_call_output"
"mcp""mcp_call_output"

Python

import os
from xai_sdk import Client
from xai_sdk.chat import user
from xai_sdk.tools import code_execution

client = Client(api_key=os.getenv("XAI_API_KEY"))
chat = client.chat.create(
    model="grok-4.5",
    tools=[
        code_execution(),
    ],
    include=["code_execution_call_output"],
)
chat.append(user("What is the 100th Fibonacci number?"))

# stream or sample the response...

Responses API

ToolResponses API tool 名称用于 include 字段的值
"web_search""web_search""web_search_call.action.sources"
"code_execution""code_interpreter""code_interpreter_call.outputs"
"collections_search""file_search""file_search_call.results"
"mcp""mcp"在 Responses API 中始终返回