Skip to content
Lesson 1 of 2AI for Developers

Building with AI · Lesson 1 of 2

Prompting for programming

Get working code from AI assistants by sharing the right context, asking for tests, and reviewing every line before it ships.

12 min readIntermediate
Sign in to bookmark

You're learning as a guest — everything is open. Sign in to save progress, earn XP and pick up where you left off.

The assistant can't see your project

When you ask a colleague for help with a bug, they can look over your shoulder. An AI assistant can't. It doesn't know which language version you run, what the rest of the file looks like, which libraries are installed, or what you already tried. Every missing detail becomes a guess — and guesses are where wrong answers come from.

The good news: coding is one of the areas where AI assistants are most useful, if you give them what a human reviewer would need.

Share the code and the exact error

The single biggest upgrade to a debugging prompt is pasting the real error message and the code that produced it.

Before

My code doesn't work, can you fix it?

After

I'm getting this error in a Python 3.12 script that reads a CSV and totals one column: KeyError: 'amount' File "report.py", line 14, in total_amount total += float(row["amount"]) Here is the full function: [paste the function] The CSV has a header row. I expected it to print the sum of the "amount" column. Explain the most likely cause first, then show the corrected function.

The improved version gives the assistant the error, the location, the intent, and the expected behavior. (In this case, a likely culprit is a header like "Amount" or " amount" with a stray space — something the model can only suggest because it saw the actual error.)

Tip: Paste the full error message and stack trace, not your summary of it. "It says something about a key" loses the details that matter.

Specify language, versions, and constraints

Code that's correct for one version can break on another. State the environment up front, plus any rules the answer must follow.

  • Language and version — "TypeScript 5, Node 20", "Python 3.11".
  • Frameworks and libraries — "React with Next.js App Router", "pandas 2.x".
  • Constraints — "no new dependencies", "must run in the browser", "follow our existing naming style".
  • Output shape — "only show the changed function", "give me a full file", "explain in comments".
Prompt
I'm working in [language + version] with [framework/libraries + versions].

Goal: [what the code should do, in one or two sentences]

Current code:
[paste the relevant code]

Constraints:
- Don't add new dependencies.
- Keep the existing function signature.
- Match the style of the code above.

Please show only the changed code, then list any assumptions you made.

Asking the model to list its assumptions is a cheap way to catch misunderstandings before you run anything.

Ask for tests, not just code

Code you can't test is code you have to trust. Ask for tests alongside the implementation — or before it.

Prompt
Write unit tests with [pytest / Jest / your framework] for the function below.
Cover: typical input, empty input, invalid input, and one boundary case.
Do not change the function itself. If a test reveals what looks like a bug, point it out instead of silently "fixing" the test to pass.

[paste function]

Then actually run the tests. A test suite that the AI says passes is not the same as a test suite you watched pass.

Review generated code like a pull request

Treat AI-generated code the way you'd treat code from a fast but new teammate: often good, sometimes confidently wrong.

  1. Does it run? Execute it. Don't eyeball it.
  2. Do you understand every line? If not, ask: "Explain lines 10–25 as if I'm new to this library."
  3. Are the APIs real? Models sometimes invent functions, options, or packages that look plausible but don't exist. Check the official docs.
  4. What about edge cases? Empty lists, nulls, time zones, large inputs, concurrency.
Prompt
Review this code as a careful senior engineer. List problems in order of severity: bugs, security issues, edge cases it misses, then readability. For each one, quote the line and suggest a fix. Don't rewrite the whole thing.

[paste code]

Security cautions

Watch out: Never paste secrets into a prompt — API keys, passwords, tokens, .env files, or real customer data. Replace them with placeholders like YOUR_API_KEY. And check your employer's policy before sharing proprietary code with any AI tool.

Generated code also deserves a security pass of its own. Look for:

  • SQL built with string concatenation instead of parameterized queries (an injection risk).
  • Disabled safety checks, such as turning off TLS certificate verification "to make the error go away."
  • Hard-coded credentials or overly broad permissions.
  • Package names you don't recognize. Before you install a suggested library, confirm it exists, is maintained, and is the one you think it is. Attackers have been known to publish malicious packages under names that AI tools tend to invent.

Putting it together

Good coding prompts look a lot like good bug reports: environment, code, exact error, expected behavior, constraints. Add "write tests" and "list your assumptions," then review and run everything yourself. The assistant speeds you up; you stay responsible for what ships.

Common mistakes

  • Mistake: Describing an error from memory instead of pasting it.

    Fix: Copy the exact error message and stack trace, plus the code that triggered it.

  • Mistake: Copying generated code straight into production because it looks right.

    Fix: Run it, test it, and make sure you understand every line before committing it.

  • Mistake: Installing a package the AI suggested without checking it.

    Fix: Verify the package exists in the official registry, is maintained, and matches the documentation.

  • Mistake: Pasting a config file with real API keys or passwords.

    Fix: Replace secrets with placeholders like YOUR_API_KEY before sharing any code.

Practice exercise

Turn a vague bug report into a debuggable prompt

Rewrite the starter prompt so an AI assistant could actually diagnose the problem.

Imagine this situation: a JavaScript function formatPrice(cents) should turn 1999 into "$19.99", but it returns "$19.990000000000002" for some inputs. You're using plain JavaScript in the browser.

Your prompt should include the environment, the code (you can write a short plausible version), the actual vs. expected output, and what kind of answer you want back.

Your draft stays in this browser tab — it isn't sent anywhere.Try your draft in the Playground

Hints

Self-check

Tick each item your prompt covers.

0 of 5 checked

You can do the whole exercise as a guest.Sign in to save progress

Knowledge check

Test your understanding

Question 1 of 4

Which addition would most improve the prompt "My code throws an error, fix it"?

Finished reading?

Guests can read every lesson.Sign in to save progress

Continue to next lesson