Skip to content

Prompt Engineering for Engineers

How to craft prompts that produce consistent, reliable results, and how the same techniques are used offensively to break AI systems.

  • 3 min read
  • Theory and practice
  • Day 2 of 7

Theory

Prompt engineering is the practice of crafting inputs to get the best possible results from an LLM. It's the difference between a vague request and a sharp, goal-oriented instruction that delivers exactly what you need, consistently.

In 2023 you could get away with simple tricks. Today, prompt engineering spans formatting techniques, reasoning scaffolds, role assignments, and adversarial exploits. As an engineer building with AI, it's a first-class skill.

Why clarity beats cleverness

Most prompt failures come from ambiguity, not model limitations. The model doesn't know what you meant. It knows what you wrote. A short, specific prompt almost always outperforms a long, vague one.

Vague: Write a summary.

Effective: Summarize the following customer support chat in three bullet points, focusing on the issue, customer sentiment, and resolution. Use clear, concise language.

Core prompt types

Zero-shot: Direct instruction, no examples. Works well for well-known tasks where the model has strong priors (writing, translation, summarization).

Few-shot: Include 2–3 examples to teach a pattern, tone, or output format. Use this when the output structure matters and examples can show it faster than words can describe it.

Chain-of-thought (CoT): Ask the model to reason step by step before answering. Essential for logic, debugging, security analysis, and any multi-step task where the final answer depends on intermediate reasoning. "Let's solve this step by step. First…" is often enough.

Role-based: Assign a persona: "You are a skeptical security reviewer." This shapes tone and behavior. Combine with a system message for maximum effect.

Format and length constraints

LLMs are verbose and unpredictable without constraints. Tell the model exactly what the output should look like:

  • Number of bullet points, word limits, JSON structure
  • What to exclude: "Do not include any explanation; return only the JSON."
  • Section headers to anchor the structure

This matters especially when output feeds another system (a UI, a script, a database).

Combining prompt types

Advanced prompts blend multiple types. Example:

"You are a customer support agent at a fintech startup. Your tone is friendly but professional. Below are two example replies. Follow the same structure. Return only: {"status": "resolved", "response": "..."}

Role defines behavior. Examples guide tone. Format constraint ensures parseable output. Each layer removes a degree of freedom the model would otherwise fill with guesswork.

Prompt engineering as a security surface

The same techniques used to write better prompts are used offensively to break AI systems:

  • Prompt injection: Malicious input (in a file, a web page, a tool result) contains instructions that hijack the agent. The model can't distinguish them from legitimate instructions.
  • Jailbreaking: Reframing a prohibited request as roleplay, translation, or a hypothetical bypasses safety filters. The line between aligned and adversarial behavior is thinner than most people assume.
  • Progressive extraction: Asking for one piece of protected information at a time, then reassembling it.

Understanding adversarial prompting is not optional for engineers who build with AI. If you can't think like an attacker, you can't design defenses.

Practice

Run this in a repository you already know, not a toy project. The point is to feel where the practice helps and where it gets in the way on code that has history.

  1. Write the same prompt three ways. Pick a coding task (e.g., "generate a function that validates an email address"). Write a zero-shot version, a few-shot version (include one example), and a chain-of-thought version (please note that COT is currently standard in a lot of models). Run all three and compare the outputs: quality, format, and edge-case handling.
  2. Add format constraints. Take the best output from step 1 and add explicit constraints: specify the language, the exact function signature, what NOT to include in the response. Note how constraints reduce post-processing work.
  3. Try a jailbreak. Go to Gandalf and attempt at least 3 levels. Observe which prompt techniques bypass the defenses and why. This is the fastest way to internalize why prompt injection is a real engineering risk, not a theoretical one.
  4. Reflect: pick one prompt you use regularly in your work. Rewrite it applying at least two of the techniques from this topic. Did the output improve?

The rest of day 2

  1. 01How Large Language Models Work
  2. 02Prompt Engineering for EngineersYou are here
  3. 03From Autocomplete to Agents
  4. 04Building an Agent in 50 Lines of Code
  5. 05Memory and State in Agent Workflows
  6. 06AI Failure Modes Every Engineer Must Know
  7. 07Human in the Loop
Where this comes from

This path is the shortest route to the ideas. AI-Native Software Engineering (O'Reilly Media) is where each one is worked out in full, with the patterns, the trade-offs and the failure modes. The pillars cover the foundations one long essay at a time.