Getting Started with Grok Build: From Installation to Your First Code Change
Grok Build is a terminal AI coding agent introduced by SpaceXAI. Unlike traditional plugins that float in the editor sidebar, it runs as a full-screen TUI, capable of understanding the entire codebase, directly modifying files, executing Shell commands, and even searching the web. For developers who prefer keyboard operations, this interaction style becomes quite handy once you get used to it.
This article walks through the complete process using a minimal Python repository: installation, login, plan mode, code modification, and test acceptance. You'll see how Grok Build provides a plan before changes, shows diffs after modifications, and how to integrate a custom model (using Defapi as an example).
Core Dependencies and Environment
First, verify your local environment. You'll need:
- Git: to create a practice repository and view diffs
- Python 3: the practice repository's code and tests depend on it
- Terminal: on macOS use Terminal/iTerm, Linux system terminal, Windows PowerShell or Git Bash
- Grok account: the first launch will open a browser to complete login
Install Grok Build using the official script. macOS / Linux / Git Bash:
curl -fsSL https://x.ai/cli/install.sh | bash
Windows PowerShell:
irm https://x.ai/cli/install.ps1 | iex
After installation, verify the version:
grok --version
If you see a version number, it's installed correctly. If you get a command not found error, check whether the installation path is in your PATH. We'll cover handling for different systems in the troubleshooting section.
Project Structure
Grok Build's Rust source repository is quite large, but we only need to understand a few core directories' responsibilities. The layout of the official repository is as follows:
crates/
βββ codegen/
β βββ xai-grok-pager-bin/ # Entry package, builds the xai-grok-pager binary
β βββ xai-grok-pager/ # TUI: scroll buffer, prompt, popups, rendering
β βββ xai-grok-shell/ # Agent runtime + various entry points
β βββ xai-grok-tools/ # Tool implementations (terminal, file editing, search, etc.)
β βββ xai-grok-workspace/ # File system, VCS, execution, checkpoints
β βββ ... # Configuration, MCP, markdown, etc.
βββ common/ # Shared foundational libraries
βββ build/ # Build-related
βββ ...
We don't compile these sources directly in daily use, but understanding the responsibilities of xai-grok-tools and xai-grok-workspace helps: the former is the concrete toolset for the agent to modify files and run commands, while the latter is its awareness of the workspace state. Later, when you use /plan, you'll see the modification plan generated based on these capabilities.
Step-by-Step Guide
1. Set Up a Practice Repository
First, create a directory for practice code and initialize a Git repository inside it:
mkdir grok-build-practice && cd grok-build-practice
git init
Write a slightly "clumsy" name formatting function. Create formatter.py:
def format_name(first: str, last: str) -> str:
"""Format first and last names as 'Last, First'."""
full = f"{last.strip()}, {first.strip()}"
return full
This function uses strip() to handle leading/trailing spaces, but if both arguments are pure whitespace strings, like " " and "\t", it returns a meaningless string like ", ". Let's also write a test file test_formatter.py:
import unittest
from formatter import format_name
class TestFormatName(unittest.TestCase):
def test_normal_input(self):
self.assertEqual(format_name("Alan", "Turing"), "Turing, Alan")
def test_whitespace_input(self):
# Current implementation returns ", ", we expect an empty string
self.assertEqual(format_name(" ", "\t"), "")
if __name__ == "__main__":
unittest.main()
Run the tests to confirm the failure:
python -m unittest -v
You'll see test_whitespace_input fail, which is exactly the issue we want Grok Build to fix.
2. Log In and Verify for the First Time
Start Grok Build from the practice repository directory:
grok
On first run, it will open a browser to the login page. After completing authorization, return to the terminal and you'll see the full-screen TUI interface. Let's do a minimal verification: type Hello, introduce yourself in one sentence and press Enter. If the model responds normally, authentication and basic connectivity are working.
Optional: Connect a Custom Model (Defapi)
If you don't want to use the official model, or want to compare different models, you can configure a custom endpoint. Grok Build reads ~/.grok/config.toml. Add a custom model configuration:
[model.defapi-gpt]
model = "gpt-5.6-terra"
base_url = "https://api.defapi.org/api/v1"
env_key = "DEFAPI_API_KEY"
api_backend = "chat_completions"
Here's what each field means:
model: model name; must match the identifier on Defapi's model pagebase_url: API base address. Defapi's Chat Completions endpoint path ishttps://api.defapi.org/api/v1/chat/completions, so fill in up to the/api/v1levelenv_key: environment variable name; Grok Build reads the API key from thisapi_backend: protocol type; Chat Completions is one of the supported protocols
Set the environment variable:
export DEFAPI_API_KEY="your_Defapi_API_Key"
In the Grok Build session, type /model to bring up the model selector, and choose defapi-gpt. Then ask a random question, like "Explain in one sentence the difference between Git's rebase and merge", to confirm it responds correctly.
Defapi's model is compatible with the v1/chat/completions protocol, and the configuration approach is the same as for the official model. As an alternative, its price is roughly half of the official model, and API compatibility is decent. If you need to control costs or compare multiple models, it's worth trying. After configuration, subsequent tool calls and plan mode will use the custom model.
Once you confirm the custom model works, use /model to switch back to the official model and continue with the following steps.
3. Use /plan to Propose a Fix Task
Now let Grok Build handle our bug in plan mode. In the TUI input box, type:
/plan Fix the issue where the format_name function handles pure whitespace input incorrectly. Current implementation returns ", ", should return an empty string. Add a regression test for test_whitespace_input.
After pressing Enter, Grok Build will enter plan mode. It will first read formatter.py and test_formatter.py, then provide a detailed modification plan. You'll see it plan:
- Modify
format_name: when bothfirst.strip()andlast.strip()are empty strings, return an empty string - Modify
test_formatter.py: ensure the assertion intest_whitespace_inputmatches expectations
The plan is displayed on the interface; let's review it. Focus on two points: whether the scope of changes is limited to those two files, and whether the test plan covers pure whitespace input and normal input. Once confirmed, press the approval shortcut (usually y or the key indicated on the interface).
4. Inspect Diffs and Run Tests
After the plan is approved, Grok Build will start modifying the code. Once changes are complete, exit the TUI and return to the terminal. First, check the changes with Git:
git diff
You'll see a diff similar to this:
diff --git a/formatter.py b/formatter.py
index 0b65cd0..3f8f0f1 100644
--- a/formatter.py
+++ b/formatter.py
@@ -1,4 +1,7 @@
def format_name(first: str, last: str) -> str:
"""Format first and last names as 'Last, First'."""
- full = f"{last.strip()}, {first.strip()}"
- return full
+ first_clean = first.strip()
+ last_clean = last.strip()
+ if not first_clean and not last_clean:
+ return ""
+ return f"{last_clean}, {first_clean}"
The change is clean: strip whitespace, check if empty, return an empty string if both are empty. The test file also has updated assertions. Run the tests to verify:
python -m unittest -v
Both tests should pass. This time, we not only verified test_whitespace_input but also confirmed test_normal_input wasn't broken. The entire flow is complete: plan β approval β modification β test acceptance.
Common Issues and Troubleshooting
1. grok Command Not Found
After executing the install script, grok --version reports command not found. This usually means the installation path isn't in your PATH. On macOS / Linux, the script installs to ~/.local/bin by default. Execute export PATH="$HOME/.local/bin:$PATH" for a temporary fix, or add it to ~/.zshrc / ~/.bashrc. On Windows, check whether %USERPROFILE%\.local\bin is in the system environment variables.
2. Browser Login Not Completed
After starting grok, the browser doesn't pop up, or it does but the terminal doesn't react after login. First, confirm that your network can access x.ai; if you use a proxy, check the terminal proxy settings. If the browser opens but the page loads slowly, wait a bit longer. After successful login authorization, the terminal will automatically enter the TUI interface; no manual action is needed.
3. Custom Model Authentication or Configuration Errors
After configuring Defapi, switching models reports authentication failure. First, confirm DEFAPI_API_KEY is set: test -n "${DEFAPI_API_KEY:-}" && echo set should produce output. Next, check that the model name in config.toml matches Defapi's model page, and that base_url is written as https://api.defapi.org/api/v1 rather than the full path. If all of the above is correct, directly curl the endpoint to see if it returns anything:
curl -s https://api.defapi.org/api/v1/models -H "Authorization: Bearer $DEFAPI_API_KEY"
If you get a model list, the endpoint is reachable and the issue lies in Grok Build's configuration; if not, check the API key itself.
4. Model Can Reply but Tool Calls Fail
The model can chat, but when asked to modify files or run commands, it always errors. These issues often stem from the model's or configuration's support for tool calls. If using a custom model, confirm whether api_backend supports the tool-calling protocol; switch to a backend other than chat_completions if necessary. Official models generally don't have this problem, so you can test tool calls with the official model first, then switch to a custom model for comparison.
5. Tests Still Fail After Plan Approval
Plan mode differs from regular conversation; it restricts how file-editing tools are used. If the plan description you provide isn't clearβfor example, if you didn't specify the expected behaviorβGrok Build might make incomplete changes. You can supplement the description with /plan, or directly ask the model to continue fixing in normal mode. Another common cause is an issue with the test case itself, like a wrong assertion; check whether the expected values in test_formatter.py are reasonable.
Further Reading / Advanced Directions
This time we used the interactive TUI. Next advanced directions include:
- Project Conventions (AGENTS.md): When the repository structure becomes complex or there are many custom commands, place an
AGENTS.mdfile in the repository root to inform Grok Build about project conventions, such as usingpytestinstead ofunittestfor tests, code style requirements, etc. When multiple people work in one repository, this can significantly improve the agent's modification accuracy. - Headless Mode: Useful for batch processing multiple repositories or running read-only checks in CI. It doesn't start a TUI; instead, it directly has the model complete tasks and output results. Suitable for read-only operations like code style checks or documentation generation. It's not recommended to have the model perform large modifications in headless mode unless you have robust version control.
The official documentation is a great place to learn: installation and getting started, authentication flow, and custom model configuration are all fully covered in docs.x.ai/build/overview. The repository source code has manuals for various topics under crates/codegen/xai-grok-pager/docs/user-guide/, covering everything from keyboard shortcuts to MCP server configuration. If you're interested in the Rust source itself, note that the root Cargo.toml is generated; to change dependencies or configuration, work with the specific crate's Cargo.toml.