Run Grok Build CLI headless
Run Grok Build CLI headless
Use grok -p for one-shot prompts in scripts, CI, and bots. Skip the fullscreen TUI, pick an output format, and resume named sessions from ~/.grok/sessions.
One prompt
grok -p "Your prompt here"
Useful flags:
| Flag | What it does |
|---|---|
-p, --single <PROMPT> |
Send one prompt |
-m, --model <MODEL> |
Choose a model |
-s, --session-id <ID> |
Create or resume a named headless session |
-r, --resume <ID> |
Resume an existing session |
-c, --continue |
Continue the most recent session in this directory |
--cwd <PATH> |
Set the working directory |
--output-format <FMT> |
plain, json, or streaming-json |
--always-approve |
Auto-approve tool executions |
--no-alt-screen |
Stay inline (no alternate screen) |
--no-auto-update |
Skip background CLI update checks |
Machine-readable output
grok -p "List TODO comments" --output-format json
grok -p "Explain the architecture" --output-format streaming-json
json prints one object at the end. streaming-json emits newline-delimited events as work lands.
CI tip
Pass --no-auto-update in scripts, or set auto_update = false under [cli] in ~/.grok/config.toml. Authenticate with grok login or XAI_API_KEY before headless runs.
For IDE-style wiring, grok agent stdio speaks ACP over JSON-RPC on stdin/stdout instead of a single -p prompt.
Pitfalls
- Headless sessions live under
~/.grok/sessions. Wipe or rotate ids if a script keeps appending to the wrong thread. --always-approvelets tools run without a human click. Keep it off on shared machines unless the job is sandboxed.- Without
--no-auto-update, background update checks can stall CI. Set the flag or the config key.