Ever typed "fix my code" into an AI chat and gotten back something completely unhelpful? The problem usually isn't the model — it's the prompt.

Prompt engineering is the craft of asking AI tools for what you actually want. It sounds fancy, but it boils down to a handful of habits you can learn in one sitting. Remember the oldest rule in computing: garbage in, garbage out. A model can only work with what's in its context window — if you leave out the goal, the constraints, or the code itself, it has to guess. Let's make sure it never has to guess.

Pattern 1: Be specific about inputs, outputs, and language

"Write a function to process the data" forces the model to invent a language, a data shape, and a definition of "process." Instead, spell out three things every time:

  • Input: what goes in, and what type or shape it has.
  • Output: what should come back, including the edge cases.
  • Language and environment: C# or Python? A web app or a console script?

Specific prompts don't need to be long — they need to be unambiguous. One precise sentence beats three vague paragraphs.

Pattern 2: Give context and examples

The model can't see your project unless you show it. Paste the relevant class, the exact error message, or the JSON you're parsing. Even better, show an example of what you want: "here's an input, here's the output I expect." Models are exceptionally good at spotting a pattern from one or two examples and continuing it.

Tip: When you hit an error, paste the full error message plus the few lines of code it points to. That one habit resolves a huge share of "the AI doesn't get it" moments.

Pattern 3: Set constraints

Without guardrails, the model picks its own style, its own libraries, and its own assumptions. Tell it your rules up front:

  • Style: "match this naming convention," "keep it under 30 lines," "add comments only where logic is non-obvious."
  • Libraries: "use only the standard library" or "we use LINQ here, not manual loops."
  • Edge cases: "handle an empty list," "the input may contain duplicates," "never throw on null — return a default instead."

Constraints aren't rude. The model isn't offended by instructions — it thrives on them.

Pattern 4: Iterate, and ask for explanations

Your first prompt is a draft, not a contract. If the answer misses, don't start over — refine: "good, but make it handle time zones" or "simpler, please, I'm still learning." Each reply narrows in on what you want.

And here's the habit that separates learners from copy-pasters: ask "explain your solution" or "why this approach over X?" You get working code and a lesson — and explanations often expose weak spots in the AI's own reasoning before they become bugs in your project.

Before and after: a prompt makeover

Let's see all four patterns in action. Here's the weak version:

Write a function to calculate revenue.

Grammatically fine, practically hopeless. Which language? What data? Revenue over what period? Now the strong version:

Write a C# method that calculates total revenue from recent orders.

- Input: List<Order>, where Order has Total (decimal)
  and PlacedAt (DateTime)
- Output: decimal — the sum of Total for orders placed
  in the last 30 days
- Use LINQ; no external libraries
- An empty list should return 0
- Briefly explain your date-filtering choice

Same request, but now the model knows the language, the shapes, the rules, and the edge case — and it will teach you something on the way out. That last line is pattern 4 baked right into the prompt.

Note: Notice what prompt engineering really is: clear thinking, written down. If you can state the input, output, and constraints of a problem, you've done half the programming already — the prompt is just the proof.

Your cheat sheet

  • Specify input, output, and language.
  • Show your code, your errors, your examples.
  • Constrain style, libraries, and edge cases.
  • Iterate — and always ask for the "why."

That's it — four patterns, no magic words required. Practice them for a week and your AI tools will feel twice as smart, even though you're the one who got smarter. Next step: understand why these patterns work by peeking inside the machine in what is a large language model?, then zoom out to the full toolkit with what is AI-assisted coding?