AI can generate code that works in a demo but fails in production because of missing error handling, insecure defaults, or unreadable naming. A well-structured prompt is the difference between getting a snippet and getting something you can actually merge.
There is a wide gap between code that executes and code that belongs in a production codebase. Production-ready code handles errors gracefully, validates inputs, avoids common security pitfalls, is readable to your teammates, and follows the conventions of your project. This tutorial shows you the prompt anatomy that makes AI aim for that bar rather than the lowest viable bar.
A code-generation prompt has four distinct layers — think of them as concentric rings of specificity. The outer rings are easy to skip, but skipping them is where "good enough for a demo" sneaks in.
The what. Describe the function signature, the inputs, and the expected output in plain terms. Include type information even if your language is dynamically typed — it forces you to think clearly and the AI to generate precise code.
The how and what not. Error handling strategy, security requirements, performance limits, libraries to use or avoid, authentication context. This is where most weak prompts stop short.
Input/output pairs and edge cases. Even one concrete example dramatically reduces ambiguity. Describe what should happen with null inputs, empty arrays, or concurrent calls if relevant.
Language version, coding style (camelCase vs snake_case, arrow functions vs named functions, type annotations), and what to include or exclude in the response (just the function, or the full file with imports?).
Without these layers, the AI fills the gaps with assumptions — often optimistic ones. Here is what a typical lazy prompt produces.
Weak prompt
write a function to send emails
No language, no library, no error handling, no retry logic, no mention of async behaviour, no input validation. The output will be a synchronous example using a library the AI guessed at, with no error handling and no tests.
Strong prompt
Language: TypeScript (Node.js 20, strict mode)
Library: nodemailer ^6, already installed
Write an async function `sendTransactionalEmail` with this signature:
async function sendTransactionalEmail(
to: string,
subject: string,
htmlBody: string
): Promise<{ success: true; messageId: string } | { success: false; error: string }>
Requirements:
- Use a pre-configured SMTP transporter (assume a `transporter` instance is
exported from `../lib/mailer.ts` — do not create it inside this function)
- Validate that `to` is a non-empty string containing "@" before sending
- On nodemailer error: catch it, log `error.message` to console.error,
and return { success: false, error: error.message }
- On success: return { success: true, messageId: info.messageId }
- No retries needed; keep it simple
Edge cases to handle:
- Empty `to` string → return { success: false, error: "Invalid recipient" }
- Empty `subject` or `htmlBody` → return { success: false, error: "Missing subject or body" }
Return only the function, with a JSDoc comment above it. No test code.
Every layer is present: spec (function signature and return type), constraints (validation, error handling, no retries), examples (specific edge cases), and output format (TypeScript strict mode, JSDoc, no tests). The output will be directly usable.
Pick any utility function you currently use in a project (a date formatter, a slug generator, a price calculator). Write a full four-layer prompt for it as if you were generating it fresh. Run the prompt and compare the output to your existing code. What did the AI do better? What did it miss?
Generate the same function twice: once with only the Spec layer, and once with all four layers. Count the lines of error-handling code in each output. The difference illustrates exactly what each layer buys you.
Ask the AI to review a piece of code you generated previously and answer:
What production concerns does this code not address? List at least five.
Use its answer to update your prompt template for that task.
Sign in to join the discussion and post comments.
Sign inPrompt Engineering for Education & Learning
Use AI as your personal tutor. Learn how to study faster, create lesson plans, generate practice questions, master languages, and prepare for competitive exams with smart prompts.
Prompt Engineering for Business & Productivity
Use AI to work smarter — automate tasks, make better decisions, and communicate professionally. 12 practical business prompt tutorials for professionals.
Prompt Engineering for Content & Copywriting
Write blogs, ads, emails, and social media content ten times faster with AI. 13 practical tutorials on prompt engineering for content creators and copywriters.
Prompt Engineering Projects & Real-World Applications
Twelve hands-on projects that turn prompt engineering theory into a portfolio. Build chatbots, content generators, RAG systems, and more.
Foundations of Prompt Engineering
The must-know basics of prompt engineering. Learn what prompts are, how AI models read them, and how to write clear instructions that get great results.
Prompt Engineering for Specific AI Tools
Tool-by-tool mastery — deep dives into ChatGPT, Claude, Gemini, GitHub Copilot, Midjourney, Stable Diffusion, and more. Learn the exact prompting techniques each platform rewards.