grok-cli Documentation

repository·main·Indexed 25 days ago

https://github.com/superagent-ai/grok-cli

An open-source terminal-based AI coding agent powered by the xAI Grok API, built with Bun and OpenTUI. It features a rich TUI, sub-agent delegation, macOS desktop automation via a computer sub-agent, and remote control via Telegram. The tool supports interactive and headless modes, secure execution in Shuru microVM sandboxes, and a verification system to inspect and test applications locally.

Tokens
10.4K
Snippets
21
Records
73
Agent score
86%

What's inside grok-cli

  1. Use Sandbox mode for secure execution

    main

    Grok can run shell commands inside a Shuru microVM sandbox to isolate the agent from your host filesystem and network.

    Requirements: macOS 14+ on Apple Silicon.

    Usage:

    • CLI: Use the --sandbox flag.
    • TUI: Use the /sandbox command.

    Sandbox Configuration Options:

    • Network: Off by default. Use --allow-net to enable or --allow-host to restrict.
    • Port forwards: Use --port 8080:80.
    • Resource limits: Configure CPUs, memory, and disk size via settings or the /sandbox TUI panel.
    • Checkpoints: Start from a saved environment snapshot.
    • Secrets: Inject API keys into the VM without exposing them to the guest.

    Settings are stored in ~/.grok/workspace-trust.json for workspace-specific persistence, ~/.grok/user-settings.json for user-wide settings, and .grok/settings.json for project-specific settings.

  2. Configure Telegram remote control

    main

    You can control Grok via Telegram by creating a bot and pairing it with the CLI.

    1. Create a bot with @BotFather and copy the token.
    2. Set the TELEGRAM_BOT_TOKEN environment variable or add telegram.botToken to ~/.grok/user-settings.json.
    3. Start grok, open the **/remote-control** flow in the TUI, and select Telegram.
    4. In Telegram, send the **/pair** command to your bot and enter the 6-character code provided in your terminal.

    Note: The CLI process must remain running for the bot to work (long polling).

    For a headless setup without the TUI, use:

    grok telegram-bridge
    {
      "telegram": {
        "botToken": "YOUR_BOT_TOKEN"
      }
    }
  3. Use the Computer sub-agent for macOS automation

    main

    The computer sub-agent allows for host desktop automation on macOS using agent-desktop. It follows a snapshot -> refs -> action -> snapshot workflow.

    • Capabilities: computer_click, computer_type, computer_scroll, and computer_snapshot (using stable refs like @e1).
    • Screenshots: Visual confirmations are saved under ~/.grok/computer/.
    • Requirement: You must grant your terminal app Accessibility permissions in macOS System Settings.
    • Troubleshooting: If Bun blocks the native binary download during install, run: node ./node_modules/agent-desktop/scripts/postinstall.js.
    # Example natural language commands
    grok "Use the computer sub-agent to take a screenshot of my host desktop and tell me what is open."
    grok "Use the computer sub-agent to launch Google Chrome, snapshot the UI, and tell me which refs correspond to the address bar and tabs."
  4. Install grok-cli

    main

    You can install grok-cli using a shell script or via bun if you have it installed on your PATH.

    Prerequisites:

    • A Grok API key from x.ai.
    • A modern terminal emulator (e.g., WezTerm, Alacritty, Ghostty, or Kitty) for the interactive OpenTUI experience.
    • For macOS desktop automation, ensure your terminal app has Accessibility permissions enabled in System Settings.
  5. Schedule Grok tasks

    main

    You can schedule headless prompts to run on a recurring schedule or once using natural language.

    Recurring Schedules: Require the background daemon to be running:

    grok daemon --background

    To manage schedules in the TUI, use the /schedule command.

  6. Configure Telegram voice and audio transcription

    main

    Grok can transcribe voice notes and audio files sent via Telegram using the Grok Speech-to-Text API. This requires a valid GROK_API_KEY.

    Configure these settings in ~/.grok/user-settings.json:

    SettingDefaultDescription
    enabledtrueSet to false to ignore voice/audio messages entirely.
    languageenLanguage code for /v1/stt. Enables Inverse Text Normalization (e.g., numbers/currencies to written form).
    {
      "telegram": {
        "botToken": "YOUR_BOT_TOKEN",
        "audioInput": {
          "enabled": true,
          "language": "en"
        }
      }
    }
  7. Configure custom sub-agents

    main

    You can define custom foreground sub-agents in ~/.grok/user-settings.json. Each agent requires a name, model, and instruction.

    Note: You cannot use the names general, explore, vision, verify, or computer as these are reserved for built-in agents.

    {
      "subAgents": [
        {
          "name": "security-review",
          "model": "grok-4.3",
          "instruction": "Prioritize security implications and suggest concrete fixes."
        }
      ]
    }
  8. Configure lifecycle hooks

    main

    Hooks allow you to execute shell commands at specific agent lifecycle events (e.g., PreToolUse, PostToolUse, UserPromptSubmit).

    Execution Logic:

    • Hook commands receive JSON event details on stdin.
    • They can return JSON on stdout.
    • Exit codes: 0 = success, 2 = block the action, other = non-blocking error.

    Configure hooks in ~/.grok/user-settings.json using a matcher to target specific tools (like bash).

    {
      "hooks": {
        "PreToolUse": [
          {
            "matcher": "bash",
            "hooks": [
              {
                "type": "command",
                "command": "./scripts/lint-before-edit.sh",
                "timeout": 10
              }
            ]
          }
        ]
      }
    }
  9. Configure Grok API Key

    main

    You can provide your Grok API key using one of the following methods:

    1. Environment Variable (Recommended for CI): export GROK_API_KEY=your_key_here
    2. Project .env file: Add GROK_API_KEY=your_key_here to a .env file in your project root.
    3. CLI Flag: Pass it once using grok -k your_key_here.
    4. User Settings: Save it permanently in ~/.grok/user-settings.json.
    export GROK_API_KEY=your_key_here
  10. Use hook matchers for specific events

    main

    When defining hooks, you can use a matcher string to target specific values for certain events. For example, in PreToolUse events, the matcher string is compared against the tool_name.

    • If a hook entry has no matcher defined, it matches every occurrence of that event.
    • If a matcher is defined, it will only trigger when the event's specific value (e.g., a tool name) exactly matches the matcher string.
  11. Define and discover Agent Skills

    main

    Agent Skills are specialized workflows that the Grok agent can use to improve task consistency. Skills are defined by a directory containing a SKILL.md file with YAML frontmatter.

    Skill Structure

    Each skill must reside in a directory with a SKILL.md file. The SKILL.md file must include a YAML frontmatter block containing:

    • name: The unique identifier for the skill.
    • description: A summary of what the skill does.

    Skill Discovery Locations

    The agent discovers skills in two scopes:

    1. User Scope: Located in ~/.agents/skills/<skill_name>/SKILL.md.
    2. Project Scope: Located in <project_root>/.agents/skills/<skill_name>/SKILL.md (and searched upwards through parent directories until the Git root is reached).

    Note: Project-level skills override user-level skills if they share the same name.