Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"docs/agents/claude-code",
"docs/agents/codex",
"docs/agents/devin",
"docs/agents/grok",
"docs/agents/openai-agents-sdk",
{
"group": "OpenClaw",
Expand Down
364 changes: 364 additions & 0 deletions docs/agents/grok.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,364 @@
---
title: "Grok"
description: "Run Grok in a secure E2B sandbox with full filesystem, terminal, and git access."
icon: "/images/icons/grok.svg"
---

[Grok](https://x.ai/news/grok-build-cli) is xAI's terminal coding agent. Its standout feature is parallel subagents: Grok automatically fans large tasks out to specialized agents that run concurrently, each in its own git worktree. E2B provides a pre-built `grok` template with the Grok CLI already installed.

Check warning on line 7 in docs/agents/grok.mdx

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/agents/grok.mdx#L7

Did you really mean 'xAI's'?

Check warning on line 7 in docs/agents/grok.mdx

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/agents/grok.mdx#L7

Did you really mean 'subagents'?

Check warning on line 7 in docs/agents/grok.mdx

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/agents/grok.mdx#L7

Did you really mean 'worktree'?

<Info>
Grok is in early beta and access is limited to SuperGrok and X Premium Plus subscribers. Get an API key from [console.x.ai](https://console.x.ai).
</Info>

## CLI

Create a sandbox with the [E2B CLI](/docs/cli).

```bash
e2b sbx create grok
```

Once inside the sandbox, start Grok.

```bash
grok
```

## Run headless

Use `-p` for non-interactive mode and `--always-approve` to auto-approve all tool calls (safe inside E2B sandboxes). Pass `XAI_API_KEY` as an environment variable so Grok can authenticate without the interactive browser flow.

<CodeGroup>
```typescript JavaScript & TypeScript
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create('grok', {
envs: { XAI_API_KEY: process.env.XAI_API_KEY },
})

const result = await sandbox.commands.run(
`grok --always-approve -p "Create a hello world HTTP server in Go"`
)

console.log(result.stdout)
await sandbox.kill()
```
```python Python
import os
from e2b import Sandbox

sandbox = Sandbox.create("grok", envs={
"XAI_API_KEY": os.environ["XAI_API_KEY"],
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔒 Agentic Security Review
Severity: HIGH
The guide labels --always-approve as safe while the same page also demonstrates running Grok against cloned repositories with credentials in environment/clone flows. In this setup, attacker-controlled repository content (for example instructions embedded in files) can drive autonomous tool execution without any approval checkpoint.

Impact: Prompt-injection content in a target repo can trigger automatic actions that exfiltrate tokens or make unauthorized code changes during headless runs.

Fix in Cursor Fix in Web

Reviewed by Cursor Security Reviewer for commit 5ce87bc. Configure here.

})

result = sandbox.commands.run(
'grok --always-approve -p "Create a hello world HTTP server in Go"',
)

print(result.stdout)
sandbox.kill()
```
</CodeGroup>

### Example: work on a cloned repository

Clone a repo into the sandbox, then run Grok against it. The sandbox keeps a full git environment, so you can inspect the diff afterwards.

<CodeGroup>
```typescript JavaScript & TypeScript
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create('grok', {
envs: { XAI_API_KEY: process.env.XAI_API_KEY },
timeoutMs: 600_000,

Check warning on line 73 in docs/agents/grok.mdx

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/agents/grok.mdx#L73

Did you really mean 'timeoutMs'?
})

await sandbox.git.clone('https://github.com/your-org/your-repo.git', {
path: '/home/user/repo',
username: 'x-access-token',
password: process.env.GITHUB_TOKEN,
depth: 1,
})

const result = await sandbox.commands.run(
`cd /home/user/repo && grok --always-approve -p "Add error handling to all API endpoints"`,
{ onStdout: (data) => process.stdout.write(data) }
)

const diff = await sandbox.commands.run('cd /home/user/repo && git diff')
console.log(diff.stdout)

await sandbox.kill()
```
```python Python
import os
from e2b import Sandbox

sandbox = Sandbox.create("grok", envs={
"XAI_API_KEY": os.environ["XAI_API_KEY"],
}, timeout=600)

sandbox.git.clone("https://github.com/your-org/your-repo.git",
path="/home/user/repo",
username="x-access-token",
password=os.environ["GITHUB_TOKEN"],
depth=1,
)

result = sandbox.commands.run(
'cd /home/user/repo && grok --always-approve -p "Add error handling to all API endpoints"',
on_stdout=lambda data: print(data, end=""),
)

diff = sandbox.commands.run("cd /home/user/repo && git diff")
print(diff.stdout)

sandbox.kill()
```
</CodeGroup>

## Parallel subagents

Check warning on line 120 in docs/agents/grok.mdx

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/agents/grok.mdx#L120

Did you really mean 'subagents'?

Grok's biggest differentiator is that it fans out large tasks to specialized subagents that run in parallel, each optionally in its own git worktree. The fan-out is automatic and decided by Grok based on task scope, so the SDK call shape is the same as a regular headless run. You just hand it a multi-part task and let it parallelize.

Check warning on line 122 in docs/agents/grok.mdx

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/agents/grok.mdx#L122

Did you really mean 'Grok's'?

Check warning on line 122 in docs/agents/grok.mdx

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/agents/grok.mdx#L122

Did you really mean 'subagents'?

Check warning on line 122 in docs/agents/grok.mdx

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/agents/grok.mdx#L122

Did you really mean 'worktree'?

This is where E2B sandboxes pair especially well with Grok: every subagent gets its own isolated filesystem and process tree inside the sandbox, and parallel worktrees never collide with your local checkout.

Check warning on line 124 in docs/agents/grok.mdx

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/agents/grok.mdx#L124

Did you really mean 'worktrees'?

<CodeGroup>
```typescript JavaScript & TypeScript
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create('grok', {
envs: { XAI_API_KEY: process.env.XAI_API_KEY },
timeoutMs: 1_800_000,

Check warning on line 132 in docs/agents/grok.mdx

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/agents/grok.mdx#L132

Did you really mean 'timeoutMs'?
})

await sandbox.git.clone('https://github.com/your-org/your-repo.git', {
path: '/home/user/repo',
username: 'x-access-token',
password: process.env.GITHUB_TOKEN,
depth: 1,
})

// A multi-part task encourages Grok to dispatch parallel subagents

Check warning on line 142 in docs/agents/grok.mdx

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/agents/grok.mdx#L142

Did you really mean 'subagents'?
const task = `
1. Migrate the auth module from JWT to PASETO
2. Add OpenAPI annotations to every handler in pkg/api
3. Write integration tests covering the new auth flow
`

const result = await sandbox.commands.run(
`cd /home/user/repo && grok --always-approve -p "${task}"`,
{ onStdout: (data) => process.stdout.write(data) }
)

const diff = await sandbox.commands.run('cd /home/user/repo && git diff')
console.log(diff.stdout)

await sandbox.kill()
```
```python Python
import os
from e2b import Sandbox

sandbox = Sandbox.create("grok", envs={
"XAI_API_KEY": os.environ["XAI_API_KEY"],
}, timeout=1800)

sandbox.git.clone("https://github.com/your-org/your-repo.git",
path="/home/user/repo",
username="x-access-token",
password=os.environ["GITHUB_TOKEN"],
depth=1,
)

# A multi-part task encourages Grok to dispatch parallel subagents
task = """
1. Migrate the auth module from JWT to PASETO
2. Add OpenAPI annotations to every handler in pkg/api
3. Write integration tests covering the new auth flow
"""

result = sandbox.commands.run(
f'cd /home/user/repo && grok --always-approve -p "{task}"',
on_stdout=lambda data: print(data, end=""),
)

diff = sandbox.commands.run("cd /home/user/repo && git diff")
print(diff.stdout)

sandbox.kill()
```
</CodeGroup>

To watch the subagents work in real time, pair this with `--output-format streaming-json` (next section) and inspect subagent events as they arrive.

Check warning on line 193 in docs/agents/grok.mdx

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/agents/grok.mdx#L193

Did you really mean 'subagents'?

## Streaming output

Use `--output-format streaming-json` to get a real-time JSONL event stream. Each line is a JSON event covering tool calls, subagent activity, and final results, which makes it suitable for piping into a CI pipeline or building a custom UI on top.

<CodeGroup>
```typescript JavaScript & TypeScript
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create('grok', {
envs: { XAI_API_KEY: process.env.XAI_API_KEY },
})

const result = await sandbox.commands.run(
`cd /home/user/repo && grok --always-approve --output-format streaming-json -p "Find and fix all TODO comments"`,
{
onStdout: (data) => {
for (const line of data.split('\n').filter(Boolean)) {
const event = JSON.parse(line)
console.log(`[${event.type}]`, event)
}
},
}
)

await sandbox.kill()
```
```python Python
import os
import json
from e2b import Sandbox

sandbox = Sandbox.create("grok", envs={
"XAI_API_KEY": os.environ["XAI_API_KEY"],
})

def handle_event(data):
for line in data.strip().split("\n"):
if line:
event = json.loads(line)
print(f"[{event['type']}]", event)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔒 Agentic Security Review
Severity: MEDIUM
This example logs the full streaming event object (console.log(..., event)), and the Python example below does the same. Streaming events can carry sensitive tool arguments/results, so copying this snippet into CI or shared logs may unintentionally expose secrets or private code context.

Impact: Sensitive runtime data may be persisted in log pipelines and become accessible beyond the intended operator scope.

Fix in Cursor Fix in Web

Reviewed by Cursor Security Reviewer for commit d4b6c9c. Configure here.

result = sandbox.commands.run(
'cd /home/user/repo && grok --always-approve --output-format streaming-json -p "Find and fix all TODO comments"',
on_stdout=handle_event,
)

sandbox.kill()
```
</CodeGroup>

## Project context with AGENTS.md

Grok reads an `AGENTS.md` file from the working directory and uses it as project context. Write one into the sandbox before running Grok to give it conventions, architecture notes, or task-specific instructions.

<CodeGroup>
```typescript JavaScript & TypeScript
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create('grok', {
envs: { XAI_API_KEY: process.env.XAI_API_KEY },
})

await sandbox.files.write('/home/user/repo/AGENTS.md', `
You are working on a Go microservice.
Always use structured logging with slog.
Follow the project's error handling conventions in pkg/errors.
`)

const result = await sandbox.commands.run(
`cd /home/user/repo && grok --always-approve -p "Add a /healthz endpoint"`
)

console.log(result.stdout)
await sandbox.kill()
```
```python Python
import os
from e2b import Sandbox

sandbox = Sandbox.create("grok", envs={
"XAI_API_KEY": os.environ["XAI_API_KEY"],
})

sandbox.files.write("/home/user/repo/AGENTS.md", """
You are working on a Go microservice.
Always use structured logging with slog.
Follow the project's error handling conventions in pkg/errors.
""")

result = sandbox.commands.run(
'cd /home/user/repo && grok --always-approve -p "Add a /healthz endpoint"',
)

print(result.stdout)
sandbox.kill()
```
</CodeGroup>

## Build a custom template

If you need to customize the environment (e.g. pre-install dependencies, add config files), build your own template on top of the pre-built `grok` template.

<CodeGroup>
```typescript JavaScript & TypeScript
// template.ts
import { Template } from 'e2b'

export const template = Template()
.fromTemplate('grok')
```
```python Python
# template.py
from e2b import Template

template = (
Template()
.from_template("grok")
)
```
</CodeGroup>

<CodeGroup>
```typescript JavaScript & TypeScript
// build.ts
import { Template, defaultBuildLogger } from 'e2b'
import { template as grokTemplate } from './template'

await Template.build(grokTemplate, 'my-grok', {
cpuCount: 2,
memoryMB: 2048,
onBuildLogs: defaultBuildLogger(),
})
```
```python Python
# build.py
from e2b import Template, default_build_logger
from template import template as grok_template

Template.build(grok_template, "my-grok",
cpu_count=2,
memory_mb=2048,
on_build_logs=default_build_logger(),
)
```
</CodeGroup>

Run the build script to create the template.

<CodeGroup>
```bash JavaScript & TypeScript
npx tsx build.ts

Check warning on line 345 in docs/agents/grok.mdx

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/agents/grok.mdx#L345

Did you really mean 'npx'?
```
```bash Python
python build.py
```
</CodeGroup>

## Related guides

<CardGroup cols={3}>
<Card title="Sandbox persistence" icon="clock" href="/docs/sandbox/persistence">
Auto-pause, resume, and manage sandbox lifecycle
</Card>
<Card title="Git integration" icon="code-branch" href="/docs/sandbox/git-integration">
Clone repos, manage branches, and push changes

Check warning on line 359 in docs/agents/grok.mdx

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/agents/grok.mdx#L359

Did you really mean 'repos'?
</Card>
<Card title="SSH access" icon="terminal" href="/docs/sandbox/ssh-access">
Connect to the sandbox via SSH for interactive sessions
</Card>
</CardGroup>
7 changes: 5 additions & 2 deletions docs/use-cases/coding-agents.mdx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
---
title: "Coding Agents"
description: "Run AI coding agents like Claude Code, Codex, and Amp in secure E2B sandboxes with full terminal, filesystem, and git access."
description: "Run AI coding agents like Claude Code, Codex, Grok, and Amp in secure E2B sandboxes with full terminal, filesystem, and git access."
icon: "robot"
---

Coding agents like [Claude Code](https://docs.anthropic.com/en/docs/agents-and-tools/claude-code/overview), [Codex](https://github.com/openai/codex), and [Amp](https://ampcode.com/) can write, debug, and refactor code autonomously. E2B sandboxes give each agent a full Linux environment with terminal, filesystem, and git — completely isolated from your infrastructure. Pre-built templates mean you can go from zero to a running agent in a single API call.
Coding agents like [Claude Code](https://docs.anthropic.com/en/docs/agents-and-tools/claude-code/overview), [Codex](https://github.com/openai/codex), [Grok](https://x.ai/news/grok-build-cli), and [Amp](https://ampcode.com/) can write, debug, and refactor code autonomously. E2B sandboxes give each agent a full Linux environment with terminal, filesystem, and git — completely isolated from your infrastructure. Pre-built templates mean you can go from zero to a running agent in a single API call.

## Why Use a Sandbox

Expand Down Expand Up @@ -36,6 +36,9 @@
<Card title="Amp" icon="/images/icons/amp.svg" href="/docs/agents/amp">
Coding agent with streaming JSON and thread management
</Card>
<Card title="Grok" icon="/images/icons/grok.svg" href="/docs/agents/grok">
xAI's terminal coding agent with plan mode and parallel subagents

Check warning on line 40 in docs/use-cases/coding-agents.mdx

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/use-cases/coding-agents.mdx#L40

Did you really mean 'xAI's'?

Check warning on line 40 in docs/use-cases/coding-agents.mdx

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/use-cases/coding-agents.mdx#L40

Did you really mean 'subagents'?
</Card>
<Card title="OpenCode" icon="/images/icons/opencode.svg" href="/docs/agents/opencode">
Open-source multi-provider agent with a built-in web UI
</Card>
Expand All @@ -45,7 +48,7 @@

<CardGroup cols={3}>
<Card title="Git Integration" icon="code-branch" href="/docs/sandbox/git-integration">
Clone repos, manage branches, and push changes from sandboxes

Check warning on line 51 in docs/use-cases/coding-agents.mdx

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/use-cases/coding-agents.mdx#L51

Did you really mean 'repos'?
</Card>
<Card title="Sandbox Persistence" icon="floppy-disk" href="/docs/sandbox/persistence">
Pause and resume sandboxes to preserve state
Expand Down
1 change: 1 addition & 0 deletions images/icons/grok.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading