Skip to content

Prompting Language Models to Write Code

For people

How to get correct, maintainable code from AI: context, specifications, tests and review.

Editorial team 1 min read

Share X LinkedIn Reddit Bluesky Email

Language models write code well when they know what you're building and how your codebase works.

Provide Context

  • Language, framework and versions.
  • Relevant existing code: related functions, data models, conventions.
  • Where the code will run and constraints (performance, dependencies allowed).

Specify Behaviour

Describe inputs, outputs, edge cases and error handling. Examples of expected input and output are especially effective.

Ask for Tests

Request unit tests covering normal and edge cases. Run them. Tests catch misunderstandings early.

Small Steps

Ask for one function or change at a time. Large generated changes are hard to review and debug.

Review Like a Colleague's Code

  • Does it actually solve the problem?
  • Do the libraries and functions exist, and are they current?
  • Security: input validation, injection, secrets, unsafe deserialisation.
  • Readability and consistency with the codebase.

Debugging Prompts

Include the exact error message, stack trace, the code involved, what you expected and what you've tried.

Explanations

Ask the model to explain unfamiliar code or its own changes — useful for learning and for checking its reasoning.

Stay Accountable

You own the code you commit. Understand it well enough to maintain it.

More in Prompting

All Prompting guides →
Prompting Guide · 1 min

Anatomy of an Effective Prompt

The parts of a prompt that reliably produce good results, with a template you can adapt for almost any task.

Prompting 1 min read 5 Nov 2025 For people

Prompting Guide · 2 min

Giving Models Context

Why most weak outputs come from missing context, and what background information to include in a prompt.

Prompting 2 min read 4 Nov 2025 For people

Prompting Guide · 1 min

Role Prompting: When It Helps and When It Doesn't

Telling a model to act as an expert or a persona can shape tone and focus — but it isn't a substitute for clear instructions.

Prompting 1 min read 3 Nov 2025 For people

Prompting Guide · 1 min

Writing Clear Instructions for Language Models

Precise, positive, prioritised instructions: how to phrase what you want so models follow it consistently.

Prompting 1 min read 2 Nov 2025 For people

Discussion 0

Use @handle to bring somebody in, and #tag to file it.

Nothing here yet. Ask a question, or say how you used it.

Sign in to join the discussion. Sign in