llm.md

How to Write Prompts for Code Generation & Programming

PromptCraft Team
#programming #coding #software engineering

Using Large Language Models (LLMs) to write code is one of the most popular use cases for developers. Models like GPT-4o, Claude, and Gemini (as well as tiny local models like Gemma running on-device for auto-completions) can write entire functions, scripts, or components in seconds.

However, if you write a basic request like “write a React login form,” you will often get code that includes annoying placeholders like // TODO: handle authentication, relies on outdated libraries, or misses essential validation checks.

To get production-ready, type-safe, and secure code, you need to structure your coding prompts. Here is the formula.


1. Specify the Exact Tech Stack & Versions

AI models are trained on years of historical code. If you don’t specify versions, they will often write code using deprecated functions or mix syntaxes (like mixing CommonJS require() and ES6 import in Node.js).

Always specify:

  • Language & Version: E.g., TypeScript 5.4, Python 3.11.
  • Libraries & Frameworks: E.g., React 18 (Functional Components, Hooks), Tailwind CSS 3.
  • Typing Requirements: E.g., “Ensure strict typing with TypeScript interface exports.”

2. Block the Placeholder Trap (Negative Constraints)

When generating larger blocks of code, LLMs will often write the boilerplate and skip the actual logic, leaving comments like // implement API call here.

To prevent this, add a strict negative constraint to your system prompt:

“CRITICAL: Write the COMPLETE, operational code. Do not use placeholders, stubs, mock data, or TODO comments. Do not omit any function bodies or import statements.”

This forces the model to write out the full logic, saving you from having to fill in the blanks manually.


3. Request Error Handling & Test Cases

Code that works in a “happy path” can crash your app in production. Always instruct the model to write validation and error guards:

  • “Add validation for all input variables (check for null, undefined, empty strings, or division by zero).”
  • “Wrap network fetches or file operations in try/catch blocks and return descriptive error messages.”
  • “Export a unit test suite along with the function using [Insert framework, e.g., Vitest] to verify edge cases.”

4. Example: The Standard vs. Optimized Code Prompt

The Standard Prompt

  • Prompt: “Write a Python script to download an image from a URL and save it.”
  • Response: A simple script using urllib that crashes if the URL returns a 404, fails to check if the directory exists, and doesn’t handle redirect chains.

The Optimized Prompt

Act as a senior Python developer. Write a complete, ready-to-run Python 3.11 script to download an image from a URL and save it to a target directory.

Requirements:
- Use the `requests` library.
- Handle exceptions (ConnectionError, Timeout, HTTPError) and log clean, human-readable errors.
- Verify that the URL returns a valid image Content-Type header.
- Ensure the destination directory exists (create it if missing).
- Set a request timeout limit of 10 seconds.

Constraints:
- Do not use stubs or mock functions.
- Return the full script code including imports.

<code>
[Insert parameters if any]
</code>

This structured prompt outputs a production-ready script with error logging, timeouts, and directory checks on the first attempt.


Streamline Your Coding Prompts

Writing out detailed requirements and negative constraints for every function takes time. That is why we built PromptCraft.

Our homepage optimizer takes your quick coding requests and automatically enhances them with version checks, typing requirements, and error-prevention parameters.

Head to the homepage to craft perfect coding prompts and accelerate your development workflows today.

Refine Your AI Prompts Automatically

Put the prompt engineering concepts in this guide to work. Use PromptCraft to instantly rewrite, structure, and optimize your prompts.