Skip to content

Instantly share code, notes, and snippets.

@imshaiknasir
Created September 2, 2025 15:13
Show Gist options
  • Save imshaiknasir/d804416dab6a7b0bb5f03b7a15236f2f to your computer and use it in GitHub Desktop.
Save imshaiknasir/d804416dab6a7b0bb5f03b7a15236f2f to your computer and use it in GitHub Desktop.
This is a complete guide for grok-code prompting technique

A solid prompting structure for grok-code-fast-1 is goal-first, context-scoped, and agentic: define the objective, supply only the necessary code/docs, specify constraints and interfaces, and iterate rapidly in short loops leveraging its speed and low cost. Below is a consolidated, working guide distilled from official docs and community practice.

Core principles

Prioritize an explicit goal and minimal, relevant context; this model is tuned for fast, multi-step coding with tool use, not verbose one-shot essays.

Work iteratively: fire quick attempts, inspect reasoning/output, refine the next turn; the speed and pricing make rapid loops optimal.

Think agentic: break work into steps, define tools or functions, and request structured outputs to chain actions programmatically.

When to use it

Daily driver for repository edits, refactors, debug loops, and code generation across large contexts at low latency and cost.

IDE and agent workflows where quick multi-step interactions beat one-shot prompting.

Model capabilities to leverage

Large context window so entire subsystems or docs can be scoped in, but still curate for relevance.

Fast token generation and caching; iterate frequently and reuse stable context to exploit cache savings.

Function-calling/structured outputs and visible reasoning traces for controllable, stepwise work.

Final prompt structure

Use this layout for each task. It’s concise, deterministic, and built for iterative refinement.

Title

One line stating the goal and target subsystem.

Inputs

Only the files, functions, config, logs, and constraints truly needed. Use headers/tags to segment.

Requirements

Explicit acceptance criteria, performance/latency budgets, API contracts, and edge cases.

Plan (optional)

Ask for a 3–6 step plan first if the task is complex, then “confirm and execute.”

Output spec

Request exact diffs, file paths, code blocks, or JSON schemas to enable automation.

Guardrails

Boundaries (don’t change X, preserve ABI, tests must pass locally, no third-party deps).

Working template

Paste and fill sections; iterate by adjusting Inputs and Requirements between runs.

Title: Implement non-blocking DB I/O wrapper for service-X

Inputs:

File: services/db.py

relevant excerpt...

File: services/handlers.py

relevant excerpt...

Env: Python 3.11, uvloop, FastAPI 0.111

Current issue: I/O blocks event loop when heavy queries run.

Requirements:

Replace blocking calls with non-blocking pattern compatible with FastAPI’s event loop.

Maintain existing handler signatures and logging format.

Add retries with jitter and timeouts; ensure p95 latency ≤ 150ms for common queries.

Include unit tests for handlers and wrapper.

Plan first:

Propose a short plan with steps and file edits. Ask “confirm to execute?” Then wait.

Output spec:

Produce unified diffs for files changed.

Provide a final checklist: updated files, commands to run tests, and a brief risk note.

Guardrails:

Do not modify config.yml or shared/lib/metrics.py.

No new external dependencies.

Example “plan-then-execute” turn

Turn 1 (planning request): “Propose a concise plan and impacted files; wait for confirmation.”

Turn 2 (execution): “Apply plan; output unified diffs + test commands; return JSON summary of changes.”

Scoping large context

Curate slices: include only functions and call-sites involved; link interfaces through short stubs for clarity.

Use markers like headers or XML-like tags for sections, e.g., , code:services/db.py, , so the model maps constraints to code.

Iteration loop (fast cycles)

Observe result and reasoning; tighten constraints or add failing test cases; rerun quickly to converge.

Reuse stable context to benefit from prompt caching and keep costs low during many small retries.

Debugging and refactors

Provide failing test output and the minimal snippet that triggers it; request a reproduction test if missing.

Ask for a patch with an explanation of root cause and the invariant it preserves.

Structured outputs for automation

Request JSON summaries with fields like files_changed, commands, risks, followups to drive downstream scripts.

For tool use or agents, define callable functions/contracts and require the model to produce function calls with arguments.

“Do” and “don’t” checklist

Do: be explicit on goals, constraints, file paths, and acceptance tests.

Do: break bigger tasks into confirmable steps, then apply diffs.

Don’t: dump entire repos; select only relevant slices.

Don’t: rely on one-shot prompts for complex changes—loop fast instead.

OpenRouter and integration notes

Model metadata: x-ai/grok-code-fast-1, 256k context, low-cost pricing; suitable for high-frequency iterations.

IDE/agent integrations and previews bias toward inline edits and fast confirm-apply workflows.

Minimal starter prompts

Planning: “Read the snippets below and propose a 4-step plan with impacted files; wait for confirm.”

Execution: “Implement the confirmed plan; output diffs and a JSON summary; no unrelated edits.”

Debug: “Given this failing test output and snippet, isolate root cause, propose fix, and produce patch + test.”

Use the structure above with tight scopes and rapid confirm–execute loops to exploit grok-code-fast-1’s strengths: speed, low cost, and large context for practical, agentic coding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment