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
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

Metadata

Release files for xg-agent-sdk 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xg-agent-sdk 0.1.1
File Size Uploaded
xg_agent_sdk-0.1.1.tar.gz 28.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for xg-agent-sdk 0.1.1
File Interpreter ABI Platform
xg_agent_sdk-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 56.1 kB

Release files / xg_agent_sdk-0.1.1.tar.gz

Download URL xg_agent_sdk-0.1.1.tar.gz
Size 28.2 kB
Tags Source
SHA-256 checksum
How to use checksums
f0fcdaa6afbcac35d4fdf7b334bde88e006a1f2133e961da5ded740e90f93ef9
BLAKE2b-256 checksum
How to use checksums
9ac38389daf7e8ec7a61463f3219a7d0a6775ae9a6b8464aada427f384a83ba8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 19, 2026.

Transparency log

Release files / xg_agent_sdk-0.1.1-py3-none-any.whl

Download URL xg_agent_sdk-0.1.1-py3-none-any.whl
Size 27.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
72f3e361779394e6269aaa1bb22394df839630f919ad2f454ec76466e8a13594
BLAKE2b-256 checksum
How to use checksums
1fff0aad1720172df258635e4d2cfd92e22d3e0a45caccd16743ecde608990d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 19, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page