Coding prompts for Grok
Prompts for the daily grind of software work: reading unfamiliar code, tracking bugs, writing tests and docs, and turning a screenshot into a working layout.
These twenty coding prompts cover the work that fills a normal developer week: decoding error messages, tracing a bug that does not crash but gives the wrong result, reviewing a diff, writing tests and fixtures, building SQL queries and regular expressions, writing shell scripts, documenting an API, untangling Git, and learning a new language by comparing it with one you already know.
Grok answers faster and better when it has the facts. Say which language, framework and version you use, paste the smallest piece of code that shows the problem together with the complete error text, and describe what you expected. For bugs, several prompts suggest Grok 4.3 Thinking: it traces the code before replying, which helps with off-by-one errors, race conditions and tricky conditions. Grok 4.20 is fine for quick explanations and boilerplate. Two prompts use image understanding, one for a screenshot of a broken layout and one for a whiteboard sketch of a data model.
The usual warnings apply, maybe more than anywhere else. The models have a knowledge cut-off and cannot read current documentation, so a library function may have been renamed or never existed. Code that looks right can fail, so run it and test it. Never paste API keys, passwords, tokens or customer data; your messages pass through our server to the model provider to produce the answer. Long messages are trimmed, so send the function that matters, not the whole repository.
See how developers use Grok, open the Code Assistant, or read about the Grok API.
How to adapt these prompts
- State your stack with versions: "Node 22, TypeScript 5, Express" beats "JavaScript".
- Ask for the plan before the code on anything bigger than one function.
- Request an explanation per change, so you can review the reasoning, not just the diff.
- Save Grok 4.7 for the hardest problems; it is limited to 10 messages a day.
-
01
Decode an error message
I'm using [language or framework and version]. Explain this error in plain words, list its three most likely causes in order of probability, and tell me what to check first for each one: [paste the full error and stack trace]
-
02
Trace a wrong result line by line
This function returns [actual result] when called with [input], but I expect [expected result]. Walk through the execution line by line with that input, tracking every variable, and stop at the first line where the state differs from what it should be: [paste code]
-
03
Hunt a race condition
This [language] code sometimes produces [symptom] under load, but never when I test it alone: [paste code]. Look for race conditions, shared state and ordering assumptions. For each suspect, describe the exact interleaving that would cause the bug and how to fix it.
-
04
Review a pull request
Review this diff as a demanding senior developer on a [language] codebase. Group comments into bugs, risks, readability and tests, give line references, and finish with a verdict: approve, approve with changes, or request changes, plus a one-sentence reason: [paste diff]
-
05
List edge cases, then write tests
For the function below, list every edge case you would test and why, then write the tests in [test framework]. Include at least one test that you expect to fail with the current code, and tell me which one: [paste function]
-
06
Generate awkward test fixtures
Write realistic test fixtures for this data model in [format, e.g. JSON or a factory function]: [paste model]. Include ten normal records and five awkward ones, such as empty strings, very long names, non-Latin characters and boundary dates, and label which is which.
-
07
SQL from a plain-English question
Using this schema: [paste schema], write a [database] query that returns [what you need]. Explain the query clause by clause, then show a variant that also handles [edge case, e.g. customers with no orders].
-
08
Debug a misbehaving regex
This regular expression should [intended behaviour] in [language or tool], but it [what goes wrong]: [paste regex]. Break it into pieces and explain each one, show an input that exposes the problem, and give a corrected version plus three strings it must reject.
-
09
Script a boring chore safely
Write a [bash or PowerShell] script that [task, e.g. archives log files older than 30 days into a zip]. Make it safe: a dry-run mode that only prints what it would do, clear error messages, and a comment on every line that deletes or overwrites files.
-
10
Document an API endpoint
Write reference documentation for this endpoint: [paste handler code or route]. Include the method and path, parameters with types and whether they are required, an example request, example success and error responses, and every status code it can return.
-
11
Get out of a Git mess
My Git repository is in this state: [paste git status and the last few lines of git log]. I wanted to [goal]. Explain what happened in plain words and give me the exact commands to fix it, starting with one that backs up my current work. Warn me before any command that rewrites history.
-
12
Rewrite my code the native way
I'm fluent in [language I know] and picking up [new language]. Take this snippet written in my usual style, [paste snippet], and rewrite it the way an experienced [new language] developer would. Then explain the idioms you used that have no direct equivalent where I come from.
-
13
Fix a layout from a screenshot
I've attached a screenshot of my page. The problem: [what looks wrong]. Here is the HTML and CSS for that section: [paste code]. Find the cause, give me the minimal CSS fix, and explain why it happened so I don't repeat it.
-
14
Whiteboard sketch to database tables
I've attached a photo of a whiteboard sketch of a data model for [app]. Turn it into [database] table definitions with primary keys, foreign keys and sensible types. List any relationship the sketch leaves ambiguous and ask me about it before assuming.
-
15
Find the complexity and a faster way
What is the time and memory complexity of this function, and why? [paste code] If there is a faster approach for inputs of about [size], show it and explain what it costs in readability.
-
16
Refactor without changing behaviour
Refactor this function to be easier to read without changing what it does: [paste code]. Work in small, named steps, explain each change in one line, and list any behaviour you weren't sure about so I can confirm it before merging.
-
17
Write a README from scratch
Write a README for my project [name], which [what it does]. Sections: what it is, a quick start with copy-paste commands, configuration options in a table, common problems, and how to contribute. Here are the key files and commands: [paste details]
-
18
Explain code I didn't write
Explain what this [language] code does to someone who knows programming but not this codebase. Start with a two-sentence summary, go through it block by block, and point out anything that looks like a bug or a leftover hack: [paste code]
-
19
Poke holes in my design
I'm planning to build [feature] like this: [describe approach]. Play devil's advocate. Name the three ways this design is most likely to fail in production, what I would see when it does, and a cheaper alternative if one exists. Be blunt; I'd rather hear it now than at 3 a.m.
-
20
Port code to another language
Convert this [source language] code to idiomatic [target language], not a line-by-line copy: [paste code]. Use the target language's standard library where possible, keep the same behaviour, and list any place where the two languages behave differently, such as integer division or string encoding.
Ready to ask Grok something?
No account, no download, no card. Open the chat, type your question and read Grok's answer as it appears.
Open Chat