Skip to main content

xg-agent-sdk

Python SDK for building autonomous coding agents on the Grok Build harness.

Provides a Claude Agent SDK–style API (query, options, streaming messages, multi-turn sessions) while Grok Build handles tools, the agent loop, sessions, MCP, and permissions. Models are pluggable: Grok, Claude, OpenAI, Gemini, Ollama, OpenRouter, or any compatible endpoint.

Unofficial community project. Not affiliated with xAI.

Requirements

Requirement Notes
Python 3.10+
Grok Build CLI Required — the SDK drives the CLI as the agent harness
API credentials e.g. XAI_API_KEY, or grok login

Recommended install (SDK + compatible CLI)

pip install "xg-agent-sdk[grok]"
xg-agent-install-grok          # downloads a CLI version known to work with this SDK
export XAI_API_KEY=xai-...     # or: grok login

This installs the Python package, then fetches the Grok Build binary into ~/.local/share/xg-agent-sdk/bin/grok (and links ~/.grok/bin/grok). The pin is defined in the SDK so it stays compatible with the streaming-json protocol we use.

Equivalent:

pip install xg-agent-sdk
python -m xg_agent_sdk install-cli

Alternative: use an existing Grok Build install

If you already have the official CLI:

curl -fsSL https://x.ai/cli/install.sh | bash
pip install xg-agent-sdk

The SDK looks for grok on PATH, GROK_CLI_PATH, ~/.local/share/xg-agent-sdk/bin, and ~/.grok/bin.

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)
        elif isinstance(message, ResultMessage):
            print(f"\n[session={message.session_id}]")

asyncio.run(main())
from xg_agent_sdk import collect_text, XGAgentOptions

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

Features

  • Streaming agent runs via query() over Grok Build headless mode
  • Multi-turn sessions with XGSDKClient and automatic session resume
  • System prompts — append rules or fully override the system prompt
  • Tool & permission controls — allowlists, denylists, permission modes, sandbox
  • Multi-provider models — register Claude, OpenAI, Gemini, Ollama, OpenRouter, and more
  • Project instructions — respects AGENTS.md, .grok/rules/, and related files in cwd

Custom instructions

Append rules (recommended — keeps the default harness prompt):

XGAgentOptions(
    rules="You are a security reviewer. Never suggest disabling authentication.",
    # append_system_prompt="...",  # alias of rules
    # rules_file="policy.md",
)

Override the system prompt (replaces the default; rules are ignored when set):

XGAgentOptions(system_prompt="You are a concise code auditor.")
# system_prompt_file="prompts/auditor.md"

Multi-turn

from xg_agent_sdk import XGSDKClient, XGAgentOptions, TextMessage

async with XGSDKClient(XGAgentOptions(always_approve=True)) as client:
    async for msg in client.ask("Read the auth module"):
        pass
    async for msg in client.ask("List all callers of that module"):
        if isinstance(msg, TextMessage):
            print(msg.text, end="")

Multi-provider models

Grok Build is the harness; inference is configured per model:

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

register_model(**anthropic_claude(name="claude", model="claude-sonnet-4"))
register_model(**openai_gpt(name="openai", model="gpt-4o"))
register_model(**ollama_local(name="ollama", model="qwen2.5-coder"))

text = await collect_text(
    "Say hello",
    XGAgentOptions(model="claude", always_approve=True),
)
Provider api_backend Environment variable
xAI Grok built-in / responses XAI_API_KEY
Anthropic messages ANTHROPIC_API_KEY
OpenAI chat_completions / responses OPENAI_API_KEY
Gemini chat_completions GOOGLE_API_KEY
Ollama chat_completions
OpenRouter chat_completions OPENROUTER_API_KEY

See docs/providers.md for details.

Configuration reference

XGAgentOptions field Grok Build 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

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

Native tool IDs include: read_file, search_replace, run_terminal_cmd, grep, list_dir, web_search, web_fetch, Agent.

Full API: docs/api.md.

Examples

python examples/quick_start_grok.py
python examples/custom_system_prompt.py
python examples/multi_turn_client.py
python examples/read_only_review.py .
python examples/with_claude.py
python examples/with_openai.py
python examples/with_ollama.py

Development

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

Optional live tests (requires Grok Build + auth):

GROK_E2E=1 pytest -m e2e

License

Apache License 2.0

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.1.tar.gz (28.2 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.1-py3-none-any.whl (27.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: xg_agent_sdk-0.1.1.tar.gz
  • Upload date:
  • Size: 28.2 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.1.tar.gz
Algorithm Hash digest
SHA256 f0fcdaa6afbcac35d4fdf7b334bde88e006a1f2133e961da5ded740e90f93ef9
MD5 09f1d8aed7ec62e7d032167dde806eea
BLAKE2b-256 9ac38389daf7e8ec7a61463f3219a7d0a6775ae9a6b8464aada427f384a83ba8

See more details on using hashes here.

Provenance

The following attestation bundles were made for xg_agent_sdk-0.1.1.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.1-py3-none-any.whl.

File metadata

  • Download URL: xg_agent_sdk-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 27.9 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 72f3e361779394e6269aaa1bb22394df839630f919ad2f454ec76466e8a13594
MD5 96997d65e02c388419b6493890e3653f
BLAKE2b-256 1fff0aad1720172df258635e4d2cfd92e22d3e0a45caccd16743ecde608990d4

See more details on using hashes here.

Provenance

The following attestation bundles were made for xg_agent_sdk-0.1.1-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