openclaw icon indicating copy to clipboard operation
openclaw copied to clipboard

[Feature]: xai support for responses and native tools: x_search, web_search, code_exec

Open ivelin opened this issue 10 months ago β€’ 15 comments

Summary

I know this issues has been brought up multiple times before and PRs have been suggested but all have been shut down so far.

Grok 4.1 Fast feels artificially handicapped as compared to the Grok app use. Quite possible because openclaw does not allow configuring the xAI API to use responses API and native server side tools:

  • web_search - parallel and much faster than client side brave search. Brave index does not keep up with API docs. Try for example asking Brave to search for Resend inbound email polling API. It keeps insisting that it does not exist (only webhook option). Grok chat app instantly spawns multiple search queries and finds the new emails/receiving API using its own native web search backend.
  • x_search - real time, up to date information - super helpful for fast moving openclaw with constant stream of x updates. No client side library can possibly match up to the minute native X search from Grok.
  • sandboxed code exec - very useful for grounding calculations, timeseries research , charts/visualisation , etc.

On top of being a very powerful model it is also practically Free ($0.2/1M output tokens + input caching) and has a massive 2M context. Grok 4.2 is about to be released and its already SOTA for market trading and other benchmarks.

Proposed solution

Ideally enable native xAI provider via Vercel's AI SDK, which is referenced in the official xAI docs.

Alternatives considered

If not, then allow the extra config options in the openclaw.json schema for /responses end point and native tools via existing OpenRouter and Vercel Providers.

Additional context

This is a prompt-style PR as requested by Peter in his podcast appearances.

What this PR does
Extends support for xAI/Grok (building on existing partial integrations like Grok as web_search provider) by adding Responses API usage to the xAI provider config. This enables Grok models (e.g., grok-4.1-fast) to use native server-side tools (web_search, x_search, code_execution, etc.) with autonomous multi-step loops handled by xAI servers.

It keeps everything under a single "xai" provider entry in openclaw.json β€” no new top-level provider type. Adds config flags to toggle Responses mode and hybrid behavior, so users can opt into native server-side while still using OpenClaw's client-side tools/skills (browser, canvas, nodes, cron, sessions, ClawHub, etc.) when needed.

Why this is useful

  • Builds incrementally on current xAI/Grok usage (OpenAI-compat + recent search provider additions) instead of forking provider logic.
  • Unlocks full Grok agentic power (low-latency native tools, 2M context) without breaking existing setups or requiring a separate provider.
  • Enables hybrid workflows: native server-side for research/code β†’ client-side OpenClaw tools for local actions β†’ back to Grok.
  • Improves Grok reliability on agentic tasks (vs. pure client-side limitations) while preserving local ecosystem.
  • Backward compatible: standard xAI calls unchanged; Responses is opt-in.

How to test

  1. Update/add to ~/.openclaw/openclaw.json (example below).
  2. Native-only test: openclaw agent --model xai/grok-4.1-fast "Search recent X posts about OpenClaw and run code to count mentions" β†’ uses native x_search + code_execution.
  3. Hybrid test: "Search web for latest OpenClaw skills, install one via ClawHub, then list sessions" β†’ native web_search β†’ client-side ClawHub/sessions tools.
  4. Disable Responses: set "useResponsesApi": false β†’ falls back to standard chat/completions + full client-side tools.

Config example (under "models.providers"):

"xai": {
  "provider": "xai",                          // or "openai" compat if preferred
  "apiKey": "xai-YourKeyHere",
  "baseUrl": "https://api.x.ai/v1",           // or Vercel proxy: https://gateway.ai.vercel.com/v1/xai
  "models": {
    "default": "grok-4.1-fast",
    "reasoning": "grok-4.1-fast-reasoning"
  },
  "useResponsesApi": true,                    // toggle for native server-side mode
  "tools": {
    "native": {                               // xAI natives (only active when useResponsesApi: true)
      "web_search": { "allowedDomains": [] },
      "x_search": { "allowedXHandles": [] },
      "code_execution": true
    },
    "clientSide": ["browser", "canvas", "nodes", "cron", "sessions_list", "clawhub_install"]  // exposed as custom tools
  },
  "reasoningEffort": "high",                  // optional Responses flag
  "hybrid": true                              // opt-in for mixing
}

Changes overview

  • Detect "useResponsesApi": true in xAI provider config β†’ switch to Responses API endpoint via Vercel AI SDK (@ai-sdk/xai). Install: pnpm add @ai-sdk/xai ai.
  • Wrapper logic (e.g., extend existing xAI/OpenAI compat handler or add to src/providers/xai.js): use xai.responses(model) + generateText/streamText when enabled.
  • Pass native tool configs + custom client-side schemas (from OpenClaw's tool registry/TOOLS.md).
  • Hybrid loop: If Responses returns client-side tool calls β†’ execute locally via agent runtime β†’ feed results back (continue Responses or fallback).
  • Prioritize native tools for speed; fallback/delegate to OpenClaw skills when matched or hybrid disabled.
  • Security: allowlists for natives and client tools; no auto-enable.
  • Fallback: when "useResponsesApi": false, use standard /chat/completions path.

Testing status

  • Lightly tested: native tools work via direct xAI; hybrid prototype (native search β†’ client browser) succeeds manually.
  • Not battle-tested in multi-agent/high-load β€” feedback appreciated on edges.
  • Verified macOS/Linux; no regressions to existing providers/tools.

AI-assisted notes

  • This PR / plan was vibe-coded / assisted by Grok 4.1 (xAI).
  • Prompt logs / reasoning available on request (based on xAI docs + Vercel SDK examples).
  • I understand the changes: toggles within existing provider, adds SDK integration, secure hybrid mapping, preserves fallbacks.

Related issues / motivation

  • Builds on recent PRs like feat(tools): add Grok (xAI) as web_search provider.
  • No core issue for full Responses/hybrid yet (repo search: "grok", "xai", "responses").
  • Community demand (Discord/X) for better Grok beyond compat mode.
  • Refs: xAI Responses docs (https://docs.x.ai/docs/responses-api), Vercel AI SDK xAI provider, Advanced mixing server/client tools.

Happy to iterate β€” mark as draft if needed. Let's make xAI/Grok shine natively in OpenClaw! πŸ¦žπŸ€–

ivelin avatar Feb 02 '26 04:02 ivelin

Yes! We need grok powering more openclaw bots! Here are some thoughts on this: X Post

glittle avatar Feb 02 '26 15:02 glittle

Yes, this is VERY much needed!! Please!!

zigmoo avatar Feb 03 '26 19:02 zigmoo

Would love this as well. Grok 4.2 coming out soon too

kaardilugeja avatar Feb 04 '26 16:02 kaardilugeja

Yes please,

KateWilkins avatar Feb 04 '26 19:02 KateWilkins

this is what I need.

bigsk1 avatar Feb 04 '26 22:02 bigsk1

Yes, this is VERY much needed!! Please!!

mehmood-baryalai avatar Feb 05 '26 10:02 mehmood-baryalai

add support

mehmood-baryalai avatar Feb 05 '26 10:02 mehmood-baryalai

Add Support please !!!

mehmood-baryalai avatar Feb 05 '26 10:02 mehmood-baryalai

we needed this like a week ago =) still need a lot! Please πŸ™

RonySpark avatar Feb 20 '26 03:02 RonySpark

please also support openrouter grok variants model! i am using grok model via openrouter, and its model name is openrouter/x-ai/grok-4.1-fast:online, however, openclaw cannot parse the β€œ:online”… which makes the grok model lose its capability to do web search or x search! this is really really frustrating. not just me, lots of people having this same issue

NikoKVCS avatar Feb 22 '26 18:02 NikoKVCS

Proposal: xAI Responses API mode for native tools (x_search first-class)

Goal: Enable Grok models in OpenClaw to use xAI's server-side toolchain (especially x_search) by adding an opt-in Responses API mode to the existing xai provider.

Problem

Today Grok in OpenClaw typically runs via chat/completions-style calls. That works for text, but it does not unlock the "Grok app" behavior:

  • server-side agent loops
  • native tools like x_search (real-time X search), plus web_search, code_execution

This makes Grok feel artificially limited vs the official app, and users resort to brittle workarounds.

Proposed solution (MVP, safe + minimal)

  1. Add a provider-level switch (OFF by default), e.g.
  • models.providers.xai.mode = "chat_completions" | "responses"
    • or models.providers.xai.useResponsesApi: true
  1. When mode="responses":
  • call xAI Responses API endpoint
  • allow xAI server-side tools to be used by the model (tool calls + tool results handled by xAI)
  • normalize the response back into OpenClaw's internal message format so sessions/UI behave normally
  1. Tool collision policy (important):
  • In Responses mode, disable client-side tools that collide with xAI server-side tools (at minimum web_search) to avoid double-search / inconsistent behavior.
  • Keep OpenClaw local tools (browser/canvas/nodes/exec) available, but avoid auto-injecting anything that shadows xAI tool names.
  1. Scope (MVP):
  • x_search (primary)
  • web_search
  • code_execution
  1. Test plan (MVP):
  • smoke test: prompt that triggers x_search β†’ verifies tool call + results returned
  • regression: default mode unchanged
  • guard: no duplicated web_search when using xAI server-side tools

Why this is worth it

  • x_search is uniquely valuable (real-time, high-signal)
  • closes the perceived gap between Grok in OpenClaw and Grok in the native app
  • keeps changes opt-in and low risk

If maintainers are OK with the direction, I'm happy to submit a minimal PR implementing the config switch + adapter plumbing first, then a follow-up for tool enablement + tests.

eventh0riz0n avatar Mar 10 '26 07:03 eventh0riz0n

It's been over a month... Is OpenAI putting its finger on the scale to intentionally suppress support for competitive cloud providers?

As a workaround I am using nanobot , which uses litellm for model routing. Works fine. Quick setup. Openclaw has a new rule to ask nanobot to do deep research. We will see over time how the balance of workloads shifts between openclaw and nanobot.

ivelin avatar Mar 10 '26 19:03 ivelin

add support this, as grok ai seems very handicapped in using server side tools eg. save files , open files, sometimes not working.

anthonykhy avatar Mar 13 '26 03:03 anthonykhy

bump. yes please add xAI native tool support :3

ItsSunshineXD avatar Mar 13 '26 14:03 ItsSunshineXD

Looks like the openclaw dev team / OpenAI are not particularly interested in this improvement. In the meanwhile, I've been able to successfully use xai Grok 4.20 with native + local tools via a custom skill. The main SKILL.md follows. Not including scripts for safety concerns. Just ask your openclaw to add the scripts for direct integration via xai's python SDK:

SKILL.md

---
name: deep-research
description: Deep research and audit skill using Grok 4.20 with native xAI tools (web search, X search, code execution) combined with OpenClaw read-only local tools.
version: 1.0.3
trigger: research|audit|analyze|search|deep research|x search|twitter|x twitter
requires:
  env:
    - XAI_API_KEY
run_location: main
metadata:
  user-invocable: true
  disable-model-invocation: false
---

# Deep Research Skill

Research and audit using Grok 4.20 with native xAI tools + OpenClaw local tools.

## Triggers

- `research <topic>` β€” Deep research mode (web + X + code)
- `x search <topic>` β€” X/Twitter search only (using xAI's native x_search)
- `twitter <topic>` β€” Same as x search
- `analyze <topic>` β€” Quick analysis
- `audit <target>` β€” Security/compliance audit
- `search <topic>` β€” General search (web + X)
- `deep research skill` β€” Show help

## Why X Search via xAI?

xAI's native `x_search` tool is the best way to search X/Twitter because:
- Direct API access to X data
- Semantic search capability
- User search, keyword search, thread fetching
- No rate limits or authentication issues

## Tools

### xAI Native Tools
| Tool | Purpose |
|------|---------|
| `web_search` | Real-time web search + browsing |
| `x_search` | X/Twitter post search (user, keyword, semantic, threads) |
| `code_execution` | Python sandbox for data analysis |

### OpenClaw Local Tools (read-only)
| Tool | Purpose |
|------|---------|
| `read` | Read file contents |
| `exec` | Read-only shell (ls, find, grep, tree, stat) |
| `web_fetch` | URL content extraction |
| `browser` | Interactive web browsing |

## Workflow

### X Search Mode (for x search / twitter triggers)
1. Parse topic
2. Use xAI's x_search tool
   - x_semantic_search for topic understanding
   - x_keyword_search for exact matches
   - x_user_search for user-specific posts
   - x_thread_fetch for thread details
3. Compile findings with sentiment analysis

### Research Mode
1. Parse topic, determine scope
2. Web search for latest info
3. X search for social sentiment
4. Code execution for data analysis
5. File/directory exploration if needed
6. Compile findings with citations

### Audit Mode
1. Target identification (URL, repo, code, docs)
2. Security/compliance web search
3. Code analysis via file reads
4. Generate audit report

## Output

- Structured research report with citations
- X/Twitter analysis with post examples
- Audit findings with severity ratings

## Requirements

- `XAI_API_KEY` β€” xAI API key (required)
- Virtual environment at `skills/deep-research/venv/`

## Installation

Run the setup script to create the virtual environment and install dependencies:

```bash
cd skills/deep-research
./setup.sh

Or manually:

cd skills/deep-research
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt

Execution (IMPORTANT)

Must run in MAIN AGENT, NOT subagent β€” subagents don't get access to xAI's native x_search tool.

How it Works

The skill uses xai-sdk directly (not OpenClaw model switching) to call xAI's API with native tools. It streams results back to the user in real-time.

How to Run

  1. Activate venv: source skills/deep-research/venv/bin/activate
  2. Run research.py with your query:
    python scripts/research.py "<your topic>"
    
  3. For streaming support, use client.py's chat_complete_with_streaming() method

Invocation

When user says "x search " or "research ":

  1. Recognize trigger
  2. Run the Python script in main agent with streaming
  3. Stream results to user in real-time

Never spawn a subagent for this skill β€” it needs direct xai-sdk access.

ivelin avatar Mar 13 '26 18:03 ivelin