# 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.

- **Source:** https://ainativesoftware.engineering/roadmap/day-2/prompt-engineering-for-engineers
- **Site:** AI-Native Software Engineering — https://ainativesoftware.engineering/book

- **Day:** 2 · position 2 of 7
- **Reading time:** 3 minutes
- **Day overview:** [Day 2](https://ainativesoftware.engineering/roadmap/day-2.md)

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

## 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.

**Theory resources**

- [The Ultimate Guide to Prompt Engineering – Lakera](https://www.lakera.ai/blog/prompt-engineering-guide)

## Practice

Run this in a repository you already know, not a toy project.

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](https://gandalf.lakera.ai/) 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?

**Practice resources**

- [Gandalf – AI Red Teaming Challenge by Lakera](https://gandalf.lakera.ai/)

- **Previous topic:** [How Large Language Models Work](https://ainativesoftware.engineering/roadmap/day-2/how-large-language-models-work.md)
- **Next topic:** [From Autocomplete to Agents](https://ainativesoftware.engineering/roadmap/day-2/from-autocomplete-to-agents.md)

---

_AI-Native Software Engineering by Alfonso Graziano (O'Reilly Media, Early Release; print edition February 2027). Every page of ainativesoftware.engineering is also served as Markdown: append `.md` to any URL. Index: https://ainativesoftware.engineering/llms.txt — whole site in one file: https://ainativesoftware.engineering/llms-full.txt._
