AI Coding Prompts: 22 for ChatGPT, Claude and Gemini

AI Coding Prompts: 22 for ChatGPT, Claude and Gemini

Short answer

The coding prompts that work best in ChatGPT, Claude or Gemini all carry the same four things: the real code or error, the language and versions, the constraints (what must not change) and a way to check the answer, such as expected output or a test. Below are 22 copy-ready prompts grouped by job: understanding code, writing functions, debugging, tests, refactoring, code review, SQL, regex, docs and migrations. Replace everything in [square brackets] and paste.

A chat model can't open your repository. It sees exactly what you paste and nothing else, so a one-line request like "fix my login bug" makes it guess the framework, the file and the bug. These prompts are built for that limit. Each one asks for the missing facts up front and tells the model how to prove its answer.

How to use these coding prompts

Paste the code, not a description of it. Include the full error, the exact versions ("Python 3.12, Django 5.1", not "Python") and one example input with the output you expected. Keep one task per chat: when you move from debugging to a new feature, start a fresh conversation so old code doesn't leak into the answer.

If you repeat the same context every time, save it once. ChatGPT's Projects keep chats, files and instructions together, and its custom instructions apply to every chat. Claude's Projects give each workspace its own knowledge base and chat history, and they are available on free accounts too (up to five projects). In Gemini, Gems save a name, instructions and optional files, though Google says Gems on personal accounts move to skills starting November 2026. A saved line such as "TypeScript 5, React 19, Vitest, no new dependencies without asking" saves you typing it into every prompt.

For habits beyond the prompt itself, like reviewing every diff and keeping changes small, see our AI coding best practices. And if the job needs the model to read many files or run your tests, a chat window is the wrong tool: our Cursor prompts and Claude Code tips cover agents that work inside your repository.

Which prompt for which job

JobPromptsWhat to paste
Understand code or an error1, 2The code or the full error, plus versions
Write new code3, 4Inputs, outputs and 2 to 3 examples
Debug5, 6, 7Stack trace, exact input and output, timings
Tests and test data8, 9, 10The function, test framework, schema
Refactor11, 12The code and how it is tested today
Review13, 14The diff and what it is meant to do
SQL15, 16CREATE TABLE statements and the query plan
Regex17, 18Flavor and strings that must and must not match
Docs19, 20The code or the diff
Migrations and upgrades21, 22Old and new schema, or the official migration notes

Prompts to understand and write code

Start here when you're new to a codebase or a library. Prompt 3 is the one to memorize: a spec with examples gets you a function you can check in seconds.

1. Explain unfamiliar code

For code you inherited or found. Asking for the inputs, outputs and side effects first stops a vague line-by-line tour.

Explain this [language] code to someone who knows [language] but has never seen this codebase.
1. In two sentences: what does it do and when is it called?
2. Inputs, outputs and side effects (writes, network calls, global state).
3. Anything surprising or easy to break.
Don't rewrite it. If something depends on code I haven't pasted, say what you'd need to see.

[paste code]

2. Decode an error message

When the error text makes no sense yet. You want the meaning and the likely cause, not a guessed fix.

I got this error running [command] with [language/framework and version]:

[paste the full error]

Explain in plain words what it means, then list the 3 most likely causes in order of likelihood. For each cause, tell me one thing to check that would confirm or rule it out. Don't write a fix yet.

3. Write a function from a spec

The spec does the work here. Examples of input and expected output beat a paragraph of description.

Write a [language] function named [name].
Purpose: [one sentence].
Inputs: [names and types]. Output: [type and shape].
Examples:
- [input] -> [expected output]
- [input] -> [expected output]
- [edge case input] -> [expected output or error]
Constraints: [version], no new dependencies, [performance limit if any].
Return the function, then 3 assertions that check the examples above.

4. Port code to another language

Ports fail on the details nobody wrote down: integer division, null handling, date and time zone behavior.

Port this [source language] code to [target language] [version]. Keep the behavior identical, including error cases.
Before the code, list every place where the two languages behave differently for this code (numbers, nulls, string encoding, dates, error handling) and how you handled each one.
Use idiomatic [target language], and only the standard library unless I name a package.

[paste code]
Build this with a prompt on Greta.sh — describe the app, Greta writes and deploys it.

Prompts to debug

Debugging prompts work when they force a cause before a fix. A model that jumps straight to code will often patch the symptom and leave the bug.

5. Debug from a stack trace

Paste the trace and the code it points to. The model can't open your files from a chat window.

This [language] code throws an error. Stack trace:
[paste full stack trace]

Code at the lines the trace points to:
[paste the relevant functions]

What I expected: [behavior]. What happened: [behavior].
Find the root cause, not just the line that throws. Explain it in two sentences, then give the smallest fix and one test that would have caught it.

6. Wrong output, no error

The hardest bugs throw nothing. This prompt makes the model trace one real input through the code.

This function returns the wrong result but raises no error.
Input: [exact input]
Expected: [exact output]
Actual: [exact output]

[paste function]

Trace the input through the code step by step and show the value of each variable that changes. Stop at the first step where the value differs from what the expected output needs, and explain why.

7. Find why code is slow

Give it your measurement. Without numbers, models tend to suggest generic speedups.

This [language] code takes [time] for [input size]. I need it under [target].
[paste code]
[paste profiler output or timing, if you have it]

Identify the bottleneck and its time complexity. Suggest the single change with the biggest effect first, explain why it is faster, and tell me how to measure the difference. Don't change the output format.

Prompts for tests

Tests are where AI help pays off fastest, and where it's easiest to fool yourself. A generated test that only checks the happy path will pass forever and prove nothing.

8. Unit tests for a function

Name your test framework, or you'll get one you don't use.

Write [framework, e.g. pytest / Jest / JUnit] tests for this function.
Cover: the normal case, empty and null input, boundary values, invalid types and [domain-specific case].
One behavior per test, with a name that says what it checks.
If a test exposes a bug in the function, mark it clearly and explain the bug instead of changing the test to pass.

[paste function]

9. A failing test that reproduces a bug

Write the test before the fix. When it passes later, you know the fix worked.

Bug report: [what the user saw]. Steps: [steps].
Relevant code:
[paste code]

Write one [framework] test that fails today because of this bug and will pass once it's fixed. Use the smallest input that triggers it. Explain why it fails now. Don't fix the bug yet.

10. Test data and fixtures

Realistic fake data finds bugs that tidy examples hide.

Generate test data for this [schema / type definition]:
[paste schema]

Give me [number] records as [JSON / SQL inserts / factory functions in language]. Include: names with accents and apostrophes, empty optional fields, the longest allowed values, dates around month ends and leap years, and [case]. No real people's data. Label which record covers which edge case.

Prompts to refactor and review

Refactors go wrong when the model "improves" behavior you depend on. Both refactor prompts pin the behavior down first, and both review prompts ask for a triggering input so you can confirm each finding yourself.

11. Refactor without changing behavior

Ask for a plan before the code, and keep your tests running between steps.

Refactor this [language] code for readability. Behavior must not change: same inputs, same outputs, same errors.
First list the changes you'd make (naming, duplication, nesting), in order, and wait for my OK.
Don't add features, don't change the public function signatures, and don't add dependencies.
Existing tests: [paste or describe].

[paste code]

12. Split a long function

For the 200-line function everybody avoids touching.

This function is [number] lines long. Split it into smaller functions, each doing one job.
For each new function: its name, signature and one-line purpose.
Keep the original function as a short coordinator that calls them, so every caller still works.
Point out any hidden coupling, such as a variable that is set in one part and read much later.

[paste function]

13. Review a diff

A first pass before a person reviews it, not a replacement for one.

Review this diff. The change is meant to [goal].
[paste diff]

Look for bugs, missing edge cases, error handling gaps and anything that breaks existing callers. Skip style comments unless they hide a bug.
For each finding: file and line, what goes wrong, an input that triggers it, and a suggested fix. Rank by severity. If you find nothing serious, say so.

14. Security review

Name the threats you care about. 'Is this secure?' gets a generic checklist.

Review this [language/framework] code for security problems. It handles [what: user input / file uploads / payments / auth].
Check specifically for: injection (SQL, command, template), missing authorization checks, secrets in code or logs, unsafe deserialization, and [other concern].
For each issue: the line, how an attacker would use it, and the fix. Don't report theoretical issues without a realistic path.

[paste code]

Prompts for SQL and regex

SQL and regex answers look right more often than they are right. Make the model show sample rows and test strings, then run them.

15. Write a SQL query from a schema

Paste the real table definitions. Guessed column names are the most common failure.

Database: [PostgreSQL / MySQL / SQLite] [version].
Tables:
[paste CREATE TABLE statements]

Write a query that returns [result in plain words], sorted by [column], for [time range or filter].
Explain any JOIN that could duplicate rows, and how NULLs in [column] are handled.
Then give me 3 tiny sample rows per table and the result I should expect from them.

16. Fix a slow SQL query

The query plan matters more than the query. Paste both.

This [database] query takes [time] on [row count] rows:
[paste query]

Plan output:
[paste EXPLAIN or EXPLAIN ANALYZE output]

Existing indexes: [list].
Explain what the plan shows in plain words, then suggest changes in order of impact (rewrite, index, or both). For any new index, say what it costs on writes.

17. Write a regex with test cases

A regex without test strings is a guess. Make the model prove it.

Write a regex for [regex flavor: JavaScript / Python / PCRE] that matches [description].
Must match:
- [example]
- [example]
Must not match:
- [example]
- [example]
Return the pattern, a part-by-part explanation, and a short [language] snippet that runs every example above and prints pass or fail.

18. Explain a regex

For the pattern in a config file that nobody dares to change.

Explain this [flavor] regex piece by piece:
[paste regex]

Then give 3 strings it matches, 3 similar strings it rejects, and any input that could make it slow (catastrophic backtracking). If it looks like it has a bug for [intended purpose], say where.
Want to try one of these? Start on Greta.sh — no setup, no boilerplate — just a prompt.

Prompts for docs and migrations

Docs prompts save time on the writing nobody enjoys. Migration prompts carry the most risk on this page, so both ask for a rollback or a quote from the official notes. For more on structuring prompts like these, see our guide to AI prompting techniques for code quality.

19. Docstrings and comments

Comments should say why, not repeat what the code says.

Add documentation to this [language] code in [docstring style, e.g. Google / JSDoc].
For each public function: purpose, parameters, return value, errors raised and one usage example.
Add inline comments only where the reason for the code isn't obvious. Don't change any code, and don't comment lines that explain themselves.

[paste code]

20. Pull request description

Turns a diff into something a reviewer can read in a minute.

Write a pull request description for this diff:
[paste diff]

Sections: what changed and why (2 to 3 sentences), how to test it (numbered steps), risks and rollback, and anything the reviewer should look at closely.
Plain language, no marketing tone, under 200 words. Context: [ticket or goal].

21. Database migration plan

For schema changes on tables that already hold data. Ask for the rollback in the same breath.

I need to change [table] from [current schema] to [new schema] in [database]. The table has about [row count] rows and the app reads and writes it constantly.
Write the migration as steps that keep the app working during each one (add, backfill, switch reads, drop later).
For each step: the SQL, how long it might lock the table, and the rollback.
Flag anything that needs a maintenance window.

22. Library or framework upgrade

Paste the official changelog or migration guide. The model may not know the newest version.

I'm upgrading [library] from [old version] to [new version] in a [framework] project.
Official migration notes:
[paste the relevant part of the changelog or migration guide]

My code that uses it:
[paste the relevant files or functions]

List every change my code needs, quoting the line from the notes that requires it. Mark anything you're unsure about instead of guessing an API.

Common mistakes

  • Describing the code instead of pasting it. The model fills gaps with plausible code that isn't yours.
  • Leaving out versions. An answer for React 17 or Python 2 can look right and fail on your stack.
  • Pasting secrets. Swap keys, tokens and customer data for placeholders before you hit send.
  • Asking for everything at once. "Refactor, add tests and fix the bug" returns a large diff you can't review. One job per prompt.
  • Trusting an API you haven't looked up. Models can invent function names and package names. Check them in the official docs before you install or call anything.
  • Arguing in a long thread. After two failed corrections, start a new chat with a better prompt that includes what you learned.

Claude users can go further with our best Claude prompts, which build on Anthropic's own prompting guidance for writing, research and code.

Frequently Asked Questions

What are good coding prompts for ChatGPT?
Good coding prompts give the model what it can't guess: the language and version, the code or error itself, what the code should do, and what done looks like, such as a test that passes. The 22 prompts on this page follow that shape for explaining code, writing functions, debugging, tests, refactors, reviews, SQL, regex, docs and migrations.

Do these coding prompts work in Claude and Gemini too?
Yes. They are plain text with no app-specific syntax, so you can paste them into ChatGPT, Claude or Gemini. Replace everything in [square brackets] with your own code, errors and versions before you send them.

How do I write a good prompt for coding?
State the job in one sentence, paste the real code and error output, name the language, framework and versions, list the constraints (what must not change, which libraries to avoid), and say how you will check the result. Ask for one change at a time, and ask the model to list its assumptions.

Is it safe to paste my code into ChatGPT, Claude or Gemini?
Never paste API keys, passwords, tokens or customer data; replace them with placeholders. For company code, follow your employer's AI policy and check each app's data controls first. OpenAI's help page on custom instructions, for example, says that information is used to improve model performance and explains how to opt out.

When should I use a coding agent instead of a chat window?
Use a chat window when you can paste everything the model needs: one function, one error, one query. When the job spans many files, needs the model to run tests, or changes your repository, a coding agent such as Cursor or Claude Code is the better fit, because it can read the files and run commands itself.

Ready to build it?
Start with a prompt.

Try building on Greta.sh

left-gradient
left-gradient
Questera Logo
SOC 2 Type II Cert.
SOC 2 Type II Cert.
AI Security Framework
AI Security Framework
Enterprise Encryption
Enterprise Encryption
Security Monitoring
Security Monitoring

© 2026 Questera

Follow in Google