SPB Git

Repository Public

streaming-and-sync.md 6.7 KB 267 lines markdown

1<!-- source: https://docs.x.ai/developers/tools/streaming-and-sync (from https://docs.x.ai/llms-full.txt, retrieved 2026-09-19T05:28:55Z) -->2#### Tools34# Streaming & Synchronous Requests56Agentic requests can be executed in either streaming or synchronous mode. This page covers both approaches and how to use them effectively.78## Streaming Mode (Recommended)910We strongly recommend using streaming mode when using agentic tool calling. It provides:1112* **Real-time observability** of tool calls as they happen13* **Immediate feedback** during potentially long-running requests14* **Reasoning token counts** as the model thinks1516### Streaming Example1718```pythonXAI19import os2021from xai_sdk import Client22from xai_sdk.chat import user23from xai_sdk.tools import code_execution, web_search, x_search2425client = Client(api_key=os.getenv("XAI_API_KEY"))26chat = client.chat.create(27    model="grok-4.6",28    tools=[29        web_search(),30        x_search(),31        code_execution(),32    ],33    include=["verbose_streaming"],34)3536chat.append(user("What are the latest updates from xAI?"))3738is_thinking = True39for response, chunk in chat.stream():40    # View server-side tool calls in real-time41    for tool_call in chunk.tool_calls:42        print(f"\\nCalling tool: {tool_call.function.name}")43    if response.usage.reasoning_tokens and is_thinking:44        print(f"\\rThinking... ({response.usage.reasoning_tokens} tokens)", end="", flush=True)45    if chunk.content and is_thinking:46        print("\\n\\nFinal Response:")47        is_thinking = False48    if chunk.content and not is_thinking:49        print(chunk.content, end="", flush=True)5051print("\\nCitations:", response.citations)52```5354```javascriptAISDK55import { xai } from '@ai-sdk/xai';56import { streamText } from 'ai';5758const { fullStream } = streamText({59  model: xai.responses('grok-4.6'),60  prompt: 'What are the latest updates from xAI?',61  tools: {62    web_search: xai.tools.webSearch(),63    x_search: xai.tools.xSearch(),64    code_execution: xai.tools.codeExecution(),65  },66});6768for await (const part of fullStream) {69  if (part.type === 'tool-call') {70    console.log(\`Calling tool: \${part.toolName}\`);71  } else if (part.type === 'text-delta') {72    process.stdout.write(part.text);73  } else if (part.type === 'source' && part.sourceType === 'url') {74    console.log(\`Citation: \${part.url}\`);75  }76}77```7879## Synchronous Mode8081For simpler use cases or when you want to wait for the complete agentic workflow to finish before processing the response, you can use synchronous requests:8283```pythonXAI84import os8586from xai_sdk import Client87from xai_sdk.chat import user88from xai_sdk.tools import code_execution, web_search, x_search8990client = Client(api_key=os.getenv("XAI_API_KEY"))91chat = client.chat.create(92    model="grok-4.6",93    tools=[94        web_search(),95        x_search(),96        code_execution(),97    ],98)99100chat.append(user("What is the latest update from xAI?"))101102# Get the final response in one go once it's ready103response = chat.sample()104105print("Final Response:")106print(response.content)107108print("\\nCitations:")109print(response.citations)110111print("\\nUsage:")112print(response.usage)113print(response.server_side_tool_usage)114```115116```javascriptAISDK117import { xai } from '@ai-sdk/xai';118import { generateText } from 'ai';119120// Synchronous request - waits for complete response121const { text, sources } = await generateText({122  model: xai.responses('grok-4.6'),123  prompt: 'What is the latest update from xAI?',124  tools: {125    web_search: xai.tools.webSearch(),126    x_search: xai.tools.xSearch(),127    code_execution: xai.tools.codeExecution(),128  },129});130131console.log('Final Response:');132console.log(text);133134console.log('\\nCitations:');135console.log(sources);136```137138Synchronous requests will wait for the entire agentic process to complete before returning. This is simpler for basic use cases but provides less visibility into intermediate steps.139140## Using Tools with Responses API141142We also support using the Responses API in both streaming and non-streaming modes:143144```pythonXAI145import os146147from xai_sdk import Client148from xai_sdk.chat import user149from xai_sdk.tools import web_search, x_search150151client = Client(api_key=os.getenv("XAI_API_KEY"))152chat = client.chat.create(153    model="grok-4.6",154    store_messages=True,  # Enable Responses API155    tools=[156        web_search(),157        x_search(),158    ],159)160161chat.append(user("What is the latest update from xAI?"))162response = chat.sample()163164print(response.content)165print(response.citations)166167# The response id can be used to continue the conversation168print(response.id)169```170171```pythonOpenAISDK172import os173from openai import OpenAI174175api_key = os.getenv("XAI_API_KEY")176client = OpenAI(177    api_key=api_key,178    base_url="https://api.x.ai/v1",179)180181response = client.responses.create(182    model="grok-4.6",183    input=[184        {185            "role": "user",186            "content": "what is the latest update from xAI?",187        },188    ],189    tools=[190        {191            "type": "web_search",192        },193        {194            "type": "x_search",195        },196    ],197)198199print(response)200```201202```bash203curl https://api.x.ai/v1/responses \\204  -H "Content-Type: application/json" \\205  -H "Authorization: Bearer $XAI_API_KEY" \\206  -d '{207  "model": "grok-4.6",208  "input": [209    {210      "role": "user",211      "content": "what is the latest update from xAI?"212    }213  ],214  "tools": [215    {216      "type": "web_search"217    },218    {219      "type": "x_search"220    }221  ]222}'223```224225## Accessing Tool Outputs226227By default, server-side tool call outputs are not returned since they can be large. However, you can opt-in to receive them:228229### xAI SDK230231| Tool | Value for `include` field |232|------|---------------------------|233| `"web_search"` | `"web_search_call_output"` |234| `"x_search"` | `"x_search_call_output"` |235| `"code_execution"` | `"code_execution_call_output"` |236| `"collections_search"` | `"collections_search_call_output"` |237| `"attachment_search"` | `"attachment_search_call_output"` |238| `"mcp"` | `"mcp_call_output"` |239240```pythonXAI241import os242from xai_sdk import Client243from xai_sdk.chat import user244from xai_sdk.tools import code_execution245246client = Client(api_key=os.getenv("XAI_API_KEY"))247chat = client.chat.create(248    model="grok-4.6",249    tools=[250        code_execution(),251    ],252    include=["code_execution_call_output"],253)254chat.append(user("What is the 100th Fibonacci number?"))255256# stream or sample the response...257```258259### Responses API260261| Tool | Responses API tool name | Value for `include` field |262|------|-------------------------|---------------------------|263| `"web_search"` | `"web_search"` | `"web_search_call.action.sources"` |264| `"code_execution"` | `"code_interpreter"` | `"code_interpreter_call.outputs"` |265| `"collections_search"` | `"file_search"` | `"file_search_call.results"` |266| `"mcp"` | `"mcp"` | Always returned in Responses API |267
Files main
Menu

Theme