Grok CLI Manual
Workflows and Troubleshooting
A clear goal, scope, output shape, and verification step matter more than an unnecessarily long prompt.
Resume sessions
grok sessions list
grok --continue
grok --resume <ID_OR_TITLE>--continue resumes the latest session for the current directory. grok sessions also offers search and delete, grok export <session-id> exports a session, and --fork-session branches from an existing one. Use grok sessions list to find sessions and --resume with an ID or a title from the current directory to restore a specific one.
Use a separate worktree
grok --worktree=feat "Implement this feature, run tests, and summarize the diff"This is useful for experimental changes or work that should run alongside your current checkout. Use grok worktree list to see existing worktrees and grok worktree gc to clean up unused ones.
Headless mode with self-checking
grok --max-turns 8 -p 'Diagnose the current test failure, make the smallest fix, and rerun the relevant tests; before finishing, inspect the diff and test results'Put the verification requirement directly in the prompt. --max-turns limits the number of agent turns in headless mode. --output-format accepts plain, json, and streaming-json (newline-delimited events).
JSON output
grok --output-format json -p 'Summarize this project build and test commands'For a stable shape:
grok \
--json-schema '{"type":"object","properties":{"build":{"type":"string"},"test":{"type":"string"}},"required":["build","test"]}' \
-p 'Return the build and test commands for this project'MCP and plugins
grok mcp --help
grok plugin --helpRead the subcommand help before adding an MCP server or plugin for a project. grok mcp doctor diagnoses MCP connection problems, and grok plugin supports install, update, enable, disable, validate, and more.
Common problems
| Symptom | Check first |
|---|---|
grok models fails | models_base_url, XAI Router Key, and network connectivity |
Router Key returns Incorrect API key provided for images | Check xai_api_base_url; with only models_base_url, image requests may still go directly to api.x.ai |
| Backend or request-format error | Confirm api_backend = "responses" is inside the correct model block |
| Configuration change has no effect | Run grok inspect and confirm the file used by this environment |
| Model is missing | Check XAI Router model permissions and name mapping |
| Long sessions compact too early or late | Adjust auto_compact_threshold_percent |
grok command is missing | Reopen the terminal and check PATH and the install location |
Start troubleshooting with grok inspect, grok models, and one minimal grok -p request. This quickly narrows the problem to local configuration, XAI Router, or model inference.