Skip to main content

xg-agent-sdk

Programmatic agents with the Grok Build harness — any model.

A Claude Agent SDK–shaped Python library. You get the same agent loop, tools, sessions, MCP, and permissions that power Grok Build, callable from Python. Inference can use Grok, Claude, OpenAI, Gemini (OpenAI-compat), Ollama, or any endpoint Grok Build supports.

Not an official xAI product. This is a thin open wrapper around the open-source Grok Build CLI. Grok Build, Grok, and xAI are trademarks of their owners.

Why this exists

Layer What you get
Claude Agent SDK Library on top of Claude Code
xai-sdk Chat/API client — you implement the tool loop
Grok Build CLI Full harness (open source) — headless + ACP, no Python API
xg-agent-sdk Library on top of Grok Build (this project)

There is no published official “XG Agent SDK” package today. Grok Build’s CLI already exposes headless streaming JSON and multi-provider custom models; this SDK makes that ergonomic.

Install

# Prerequisites: Grok Build CLI
curl -fsSL https://x.ai/cli/install.sh | bash

# From PyPI (after publish)
pip install xg-agent-sdk

# From source
git clone https://github.com/manick2411/xg-agent-sdk.git
cd xg-agent-sdk
pip install -e ".[dev]"

Note: PyPI name is xg-agent-sdk (import xg_agent_sdk). The name grok-agent-sdk was already reserved on PyPI by a third-party placeholder.

Auth (any one):

export XAI_API_KEY=xai-...   # for default Grok models
# or: grok login

Quick start

import asyncio
from xg_agent_sdk import query, XGAgentOptions, TextMessage, ResultMessage

async def main():
    async for message in query(
        prompt="Find and fix the bug in auth.py",
        options=XGAgentOptions(
            allowed_tools=["read_file", "search_replace", "run_terminal_cmd"],
            permission_mode="acceptEdits",
            always_approve=True,
            cwd=".",
        ),
    ):
        if isinstance(message, TextMessage):
            print(message.text, end="", flush=True)
        if isinstance(message, ResultMessage):
            print(f"\n[session={message.session_id} cost={message.total_cost_usd}]")

asyncio.run(main())

One-shot helper:

from xg_agent_sdk import collect_text, XGAgentOptions

text = await collect_text("Summarize this repo", XGAgentOptions(always_approve=True))

Multi-provider (Claude, OpenAI, Gemini, Ollama, …)

Grok Build is the harness; the model is pluggable via custom models (chat_completions, responses, or Anthropic messages).

from xg_agent_sdk import (
    register_model,
    anthropic_claude,
    openai_gpt,
    gemini_openai_compat,
    ollama_local,
    openrouter,
    XGAgentOptions,
    collect_text,
)

# Writes [model.*] into ~/.grok/config.toml (creates a .bak backup)
register_model(**anthropic_claude(name="claude", model="claude-sonnet-4"))
register_model(**openai_gpt(name="openai", model="gpt-4o"))
register_model(**gemini_openai_compat(name="gemini", model="gemini-2.5-pro"))
register_model(**ollama_local(name="ollama", model="qwen2.5-coder"))
register_model(**openrouter(name="or", model="anthropic/claude-sonnet-4"))

text = await collect_text(
    "Say hi",
    XGAgentOptions(model="claude", always_approve=True),
)
Provider Backend Typical env
xAI Grok built-in / responses XAI_API_KEY
Anthropic Claude messages ANTHROPIC_API_KEY
OpenAI chat_completions or responses OPENAI_API_KEY
Gemini OpenAI-compat chat_completions GOOGLE_API_KEY
Ollama chat_completions local none
OpenRouter chat_completions OPENROUTER_API_KEY

Caveat: Tool-calling quality varies by model. Stronger models work better with multi-step agent tools; small local models may struggle.

Custom system prompts & instructions

You can shape agent behavior three ways:

1. Append rules (recommended)

Keeps Grok Build’s default harness prompt (tools, safety, agent loop) and adds yours:

options = XGAgentOptions(
    always_approve=True,
    rules="You are a strict security reviewer. Never suggest disabling auth.",
    # or Claude-style alias:
    # append_system_prompt="...",
    # rules_file="prompts/policy.md",
)

2. Full system prompt override

Replaces the entire default system prompt (you own the wording; tool quality may drop if you strip agent guidance):

options = XGAgentOptions(
    system_prompt="You are a pirate engineer. Always end with Arrr.",
    # system_prompt_file="prompts/pirate.md",
)

When system_prompt is set, Grok ignores rules / append_system_prompt for that run.

3. Project files (automatic)

If cwd points at a repo with AGENTS.md, .grok/rules/*.md, or CLAUDE.md, Grok Build loads those automatically — no SDK flag required.

XGAgentOptions(cwd="/path/to/project", always_approve=True)

See examples/custom_system_prompt.py.

Multi-turn sessions

from xg_agent_sdk import XGSDKClient, XGAgentOptions, TextMessage

async with XGSDKClient(XGAgentOptions(always_approve=True)) as client:
    async for msg in client.ask("Remember codeword: pineapple"):
        ...
    async for msg in client.ask("What was the codeword?"):
        if isinstance(msg, TextMessage):
            print(msg.text, end="")

Architecture

Your app  →  xg-agent-sdk  →  grok CLI (subprocess, streaming-json)
                                   │
                                   ├─ tools / sessions / MCP / permissions
                                   └─ HTTP → Grok | Claude | OpenAI | Gemini | Ollama

The SDK does not reimplement the agent loop. Grok Build remains the brain.

Options → CLI map

XGAgentOptions CLI flag
model -m
cwd --cwd
system_prompt --system-prompt-override
rules --rules
max_turns --max-turns
permission_mode --permission-mode
allowed_tools --tools
disallowed_tools --disallowed-tools
resume --resume
continue_session --continue
always_approve --always-approve
sandbox --sandbox
agents --agents JSON

Claude-style tool aliases (Read, Bash, …) map to Grok IDs (read_file, run_terminal_cmd, …) when map_tool_aliases=True (default).

Tool IDs (native)

read_file, search_replace, run_terminal_cmd, grep, list_dir, web_search, web_fetch, Agent, …

Examples

python examples/quick_start_grok.py
python examples/read_only_review.py /path/to/repo
python examples/multi_turn_client.py
# optional providers:
python examples/with_claude.py
python examples/with_openai.py
python examples/with_ollama.py

Development

pip install -e ".[dev]"
pytest                    # unit tests
GROK_E2E=1 pytest -m e2e  # live CLI (optional)

Comparison with Claude Agent SDK

Concept Claude Agent SDK xg-agent-sdk
One-shot query() query()
Options ClaudeAgentOptions XGAgentOptions
Multi-turn ClaudeSDKClient XGSDKClient
Harness Claude Code CLI Grok Build CLI
Multi-model limited / platform first-class custom models

License

Apache-2.0. See LICENSE.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xg_agent_sdk-0.1.0.tar.gz (23.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xg_agent_sdk-0.1.0-py3-none-any.whl (23.0 kB view details)

Uploaded Python 3

File details

Details for the file xg_agent_sdk-0.1.0.tar.gz.

File metadata

  • Download URL: xg_agent_sdk-0.1.0.tar.gz
  • Upload date:
  • Size: 23.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for xg_agent_sdk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c7ed302fe8d3f1ec838b981f4038a37796afec24a9eabad28f0e725c66a54bc3
MD5 7369d9bdea51b2a9b7571330f8b7aa80
BLAKE2b-256 67764ff37f6e43b68918f9aef6614b34508a11e357626d595b5afd268ee7dca6

See more details on using hashes here.

Provenance

The following attestation bundles were made for xg_agent_sdk-0.1.0.tar.gz:

Publisher: publish.yml on manick2411/xg-agent-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file xg_agent_sdk-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: xg_agent_sdk-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 23.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for xg_agent_sdk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1ddf5e97526d854f29a4441578cc7233bf9e26c0291bf0e7d2589d51541de722
MD5 1f7c00dd41b4110013f421eb2dc3f8a1
BLAKE2b-256 1a2485652b6cbf2753aacd17700e2d03aef4338d0b155b83ddd9e0438ea1cd37

See more details on using hashes here.

Provenance

The following attestation bundles were made for xg_agent_sdk-0.1.0-py3-none-any.whl:

Publisher: publish.yml on manick2411/xg-agent-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page