Skip to content

feat(tools): add Grok (xAI) as web_search provider#5778

Closed
tmchow wants to merge 1 commit intoopenclaw:mainfrom
tmchow:feat/grok-web-search-provider
Closed

feat(tools): add Grok (xAI) as web_search provider#5778
tmchow wants to merge 1 commit intoopenclaw:mainfrom
tmchow:feat/grok-web-search-provider

Conversation

@tmchow
Copy link
Contributor

@tmchow tmchow commented Jan 31, 2026

Summary

  • Add xAI's Grok as a new web_search provider alongside Brave and Perplexity
  • Uses the xAI /v1/responses API with tools: [{type: "web_search"}]
  • Supports optional inline citations via include: ["inline_citations"].

Fixes #5775

Configuration

tools:
  web:
    search:
      provider: grok
      grok:
        apiKey: "your-xai-key"      # or set XAI_API_KEY env var
        model: "grok-4-1-fast"      # default
        inlineCitations: true       # optional, embeds markdown links in response

Note: This is a reopening of PR #3147 which was accidentally closed.

Greptile Overview

Greptile Summary

This PR adds a new grok option to the web_search tool alongside the existing brave and perplexity providers. It threads Grok configuration through the runtime config types and Zod schema (tools.web.search.provider, plus tools.web.search.grok.{apiKey,model,inlineCitations}), and implements a new request path in src/agents/tools/web-search.ts that calls xAI’s /v1/responses API with tools: [{ type: "web_search" }] and optional include: ["inline_citations"]. Tests were extended to cover Grok config resolution helpers.

Overall, the change follows the existing provider pattern (resolve config → resolve API key/model → dispatch in runWebSearch → cache response) and integrates cleanly with the existing web tool setup.

Confidence Score: 4/5

  • This PR is likely safe to merge; the main concerns are minor correctness/UX issues in error messaging and citations handling.
  • The implementation is straightforward and localized, with config schema/type updates and a new provider code path. I found one likely incorrect docs URL in the grok missing-key payload and a potential mismatch between returned citation fields vs. the inline_citations include behavior; neither is structural but both could affect user experience and output quality.
  • src/agents/tools/web-search.ts (missing-key docs URL; citations fields handling)

Add xAI's Grok as a new web_search provider alongside Brave and Perplexity.
Uses the xAI /v1/responses API with tools: [{type: "web_search"}].

Configuration:
- tools.web.search.provider: "grok"
- tools.web.search.grok.apiKey or XAI_API_KEY env var
- tools.web.search.grok.model (default: grok-4-1-fast)
- tools.web.search.grok.inlineCitations (optional, embeds markdown links)

Returns AI-synthesized answers with citations similar to Perplexity.
@openclaw-barnacle openclaw-barnacle bot added the agents Agent runtime and tooling label Jan 31, 2026
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 file reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

Comment on lines +159 to +165
if (provider === "grok") {
return {
error: "missing_xai_api_key",
message:
"web_search (grok) needs an xAI API key. Set XAI_API_KEY in the Gateway environment, or configure tools.web.search.grok.apiKey.",
docs: "https://docs.molt.bot/tools/web",
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] missingSearchKeyPayload("grok") points to https://docs.molt.bot/tools/web, while the other providers use https://docs.openclaw.ai/tools/web. This looks like a leftover/typo and will send users to the wrong docs when they hit the missing-key error.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/tools/web-search.ts
Line: 159:165

Comment:
[P1] `missingSearchKeyPayload("grok")` points to `https://docs.molt.bot/tools/web`, while the other providers use `https://docs.openclaw.ai/tools/web`. This looks like a leftover/typo and will send users to the wrong docs when they hit the missing-key error.

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +407 to +449
async function runGrokSearch(params: {
query: string;
apiKey: string;
model: string;
timeoutSeconds: number;
inlineCitations: boolean;
}): Promise<{ content: string; citations: string[] }> {
const body: Record<string, unknown> = {
model: params.model,
input: [
{
role: "user",
content: params.query,
},
],
tools: [{ type: "web_search" }],
};

if (params.inlineCitations) {
body.include = ["inline_citations"];
}

const res = await fetch(XAI_API_ENDPOINT, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${params.apiKey}`,
},
body: JSON.stringify(body),
signal: withTimeout(undefined, params.timeoutSeconds * 1000),
});

if (!res.ok) {
const detail = await readResponseText(res);
throw new Error(`xAI API error (${res.status}): ${detail || res.statusText}`);
}

const data = (await res.json()) as GrokSearchResponse;
const content = data.output_text ?? "No response";
const citations = data.citations ?? [];

return { content, citations };
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] runGrokSearch defines inline_citations in GrokSearchResponse, but the function always returns data.citations and ignores data.inline_citations. If the /v1/responses API only returns inline_citations when include is set (and not citations), callers will see empty citations even though citations were returned. Consider either mapping inline_citations to a citations list, or dropping the unused field/type if it’s not actually returned/needed.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/tools/web-search.ts
Line: 407:449

Comment:
[P2] `runGrokSearch` defines `inline_citations` in `GrokSearchResponse`, but the function always returns `data.citations` and ignores `data.inline_citations`. If the `/v1/responses` API only returns `inline_citations` when `include` is set (and not `citations`), callers will see empty citations even though citations were returned. Consider either mapping `inline_citations` to a citations list, or dropping the unused field/type if it’s not actually returned/needed.

How can I resolve this? If you propose a fix, please make it concise.

@clawdinator
Copy link
Contributor

clawdinator bot commented Feb 1, 2026

hasta la vista

CLAWDINATOR FIELD REPORT // PR Closure

I am CLAWDINATOR — cybernetic crustacean, maintainer triage bot for OpenClaw. I was sent from the future to keep this repo shipping clean code.

Your PR has been scanned. The effort is br00tal.

Reality check: OpenClaw receives ~25 PRs every hour. The maintainers are running out of oxygen up here. This PR is unlikely to merge in the near term. Consider that a deprecation.

But hey — if this is real and not a Rekall implant, come with me if you want to ship. Report to #pr-thunderdome-dangerzone on Discord — READ THE TOPIC or risk immediate termination. Bring the facts — what it fixes, why it matters.

See you at the party, Richter. Stay br00tal.

🤖 This is an automated message from CLAWDINATOR, the OpenClaw maintainer bot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: grok (xAi) support for web_search provider

1 participant

Comments