Prompt Engineering for Developers: Techniques That Work With Claude, GPT and Gemini
Practical prompt engineering for developers: clear instructions, examples, XML tags, long-context layout, reasoning and structured output, from vendor docs.
Table of contents
- 1. Be explicit about the task, audience and constraints
- 2. Show examples (few-shot prompting)
- 3. Separate instructions from data with clear structure
- 4. Put roles and standing rules in the system message
- 5. Lay out long context carefully
- 6. Let the model reason, but don't micromanage it
- 7. Specify the output format, and use native structured output
- 8. Break big jobs into steps, and test like code
- From prompts to loops
- Quick checklist
Prompt engineering is the practice of writing instructions that get a language model to produce the output you actually want, reliably. For developers, it comes down to a few habits that Anthropic, OpenAI and Google all recommend in their official docs: be explicit about the task, give the model the context and examples it needs, separate instructions from data, and say exactly what the output should look like. This guide walks through those techniques with prompts you can adapt, and points out where the vendors disagree.
If you want a refresher on what a model does with your prompt under the hood, read our explainer on how large language models work first.
1. Be explicit about the task, audience and constraints
The most common prompt failure is vagueness. Models do what you ask, not what you meant. Anthropic's docs suggest a simple test: would a capable colleague with no background understand your prompt? If not, the model won't either.
Compare:
Summarize this incident report.
with:
Summarize the incident report below for our on-call engineers.
- Audience: backend engineers who were not involved.
- Length: at most 5 bullet points.
- Include: root cause, affected services, customer impact, and follow-up actions.
- If the report does not state the root cause, say "root cause not stated" instead of guessing.
The second version names the audience, the length, the required content and what to do when information is missing. That last line matters: telling the model it may say "I don't know" reduces invented answers, a point Claude's prompting best-practices article makes explicitly.
Two more habits from the vendor guides:
- Explain why a rule exists. "Use plain text, because this goes into an SMS" works better than "no markdown", because the model can generalise from the reason.
- Say what to do, not only what to avoid. "Write in flowing paragraphs" beats "don't use bullet points".
2. Show examples (few-shot prompting)
Examples are the fastest way to pin down format and tone. Instead of describing the output, show two or three input/output pairs.
Classify each support message as "bug", "billing" or "question".
<examples>
<example>
<message>The export button returns a 500 error since this morning.</message>
<label>bug</label>
</example>
<example>
<message>Why was I charged twice in November?</message>
<label>billing</label>
</example>
<example>
<message>Can I invite guests to a workspace?</message>
<label>question</label>
</example>
</examples>
<message>{{user_message}}</message>
The vendors don't agree on how many examples to use. Anthropic suggests 3 to 5 varied examples, OpenAI's guide says a handful, and Google's Gemini docs recommend always including some but warn that too many can make the model overfit to them. In practice: make examples diverse, keep their formatting identical, and add more only when outputs are inconsistent.
3. Separate instructions from data with clear structure
When a prompt mixes instructions, reference documents and user input, the model can confuse them. Wrap each part in clearly named sections. Anthropic and OpenAI both recommend XML-style tags; Google accepts XML or Markdown headings but asks you to pick one style and stick to it.
<instructions>
Answer the user's question using only the documentation provided.
If the answer is not in the documentation, reply "Not covered in the docs."
</instructions>
<documentation>
{{docs}}
</documentation>
<question>
{{question}}
</question>
This structure also helps against prompt injection: untrusted content sits inside a clearly labelled block, and your instructions can tell the model to treat it as data. It is not a complete defence, so never give a model tools or secrets that untrusted input could abuse. Our report on AI inference providers leaking prompts is a reminder to keep credentials out of prompts entirely.
4. Put roles and standing rules in the system message
Every major API separates standing instructions from the user's turn. Anthropic uses a system prompt, OpenAI uses developer messages (or the instructions parameter), and Google uses a system instruction. Put the role, rules and output format there, and keep the user turn for the actual input.
from anthropic import Anthropic
MODEL = "your-claude-model-id" # pick a current model ID from Anthropic's models page
client = Anthropic()
response = client.messages.create(
model=MODEL,
max_tokens=1024,
system=(
"You are a senior Python reviewer. "
"Point out bugs and security issues first, then style. "
"Quote the exact line for every issue."
),
messages=[{"role": "user", "content": code_to_review}],
)
print(response.content[0].text)
Keep the role short and relevant. Claude's best-practices article notes that elaborate personas are often unnecessary; a one-line role plus clear rules usually does more.
5. Lay out long context carefully
With long inputs such as contracts, logs or codebases, placement matters. Anthropic and Google both recommend putting long documents first and the question last. Anthropic reports that placing the query at the end improved response quality by up to 30% in its tests. Google suggests bridging with a phrase like "Based on the information above...".
A reliable pattern:
- Put each document in its own tagged block, with a source or index attribute.
- Put your instructions and question after the documents.
- For accuracy-sensitive tasks, ask the model to quote the relevant passages before answering.
If you call an API repeatedly with the same reference material, keep that material at the start of the prompt and the changing input at the end. OpenAI's guide points out that this ordering also lets prompt caching work, which cuts cost and latency.
6. Let the model reason, but don't micromanage it
Older advice was to add "think step by step" to everything. Today's reasoning models think internally, and the vendors now say different things:
- Anthropic recommends using the API's built-in thinking settings and general instructions such as "think thoroughly", rather than hand-written step plans. Manual chain-of-thought is the fallback when thinking is off.
- OpenAI says reasoning models do best when given a goal and trusted to work out the steps, while non-reasoning GPT models benefit from more explicit, step-by-step direction.
- Google suggests phrases like "Think very hard before answering" for heavy reasoning on Gemini.
The common thread is to give the model the goal and the success criteria, then let it reason. Spell out the steps only when the process itself matters, such as a compliance checklist.
7. Specify the output format, and use native structured output
Say exactly what you want back: length, sections, JSON keys, or "only the code, no explanation". For machine-readable output, don't rely on the prompt alone. Google and OpenAI both point developers to their native structured output features, which validate responses against a JSON schema. Use them whenever your code parses the result.
Two warnings from the vendor docs:
- Anthropic's docs say prefilling the assistant's reply (starting it with
{, for example) is not supported on its newer models (Claude 4.6 and later) and returns an error, even though older guides still recommend it. - Google says its newest Gemini models ignore
temperature,top_pandtop_k. Check each model's docs before tuning sampling parameters.
8. Break big jobs into steps, and test like code
One giant prompt that drafts, critiques and formats is harder to debug than three small ones. Prompt chaining runs separate calls for each stage, such as extract, then draft, then review. It costs a little latency but makes each step easier to check and improve.
Treat prompts as code:
- Keep them in version control, not scattered in a dashboard.
- Build a small evaluation set of real inputs with expected outputs, and rerun it whenever you change the prompt or switch models.
- Pin model versions in production so behaviour doesn't shift under you.
From prompts to loops
Prompt engineering optimises a single instruction. As teams run AI agents on longer tasks, attention is shifting to the systems around those prompts: context, tools, verification and the automated loops that keep an agent working towards a goal. Our explainer on loop engineering covers that next layer. If you are building on Gemini specifically, our Gemini 4 Neon API guide shows these ideas in working code.
Quick checklist
- Task, audience, length and constraints are stated explicitly.
- The model is allowed to say "not enough information".
- Two or three diverse examples show the format.
- Instructions, documents and user input are in separate, labelled blocks.
- Role and standing rules are in the system or developer message.
- Long documents come first, the question last.
- Output format is specified; structured output is used when code parses it.
- Prompts are versioned and tested against a small evaluation set.
Frequently asked questions
+Is prompt engineering still useful now that models reason on their own?
Yes, but the emphasis has shifted. Reasoning models need less step-by-step hand-holding, yet they still depend on a clear goal, the right context, good examples and a defined output format. Those are the parts of a prompt that matter most today.
+How many examples should a few-shot prompt include?
Vendors differ. Anthropic's docs suggest 3 to 5 varied examples, OpenAI says a handful, and Google warns that too many can cause overfitting. Start with one or two, test, and add more only if outputs are inconsistent.
+Should I use XML tags or Markdown to structure prompts?
Either works. Anthropic and OpenAI both recommend XML tags to separate instructions from reference material, while Google accepts XML or Markdown but asks you to pick one style and use it consistently.
+What is the difference between prompt engineering and loop engineering?
Prompt engineering improves a single instruction to a model. Loop engineering designs the automated system that repeatedly runs an AI agent, with stop conditions, state, verification and budgets. Good prompts are one ingredient inside a good loop.
Sources & references
- Anthropic — Prompting best practices (Claude Platform docs)
- Claude — Best practices for prompt engineering
- OpenAI — Prompt engineering guide
- Google — Prompt design strategies, Gemini API (updated Oct 13, 2026)
Spotted an error? See our Corrections Policy.
- #prompt-engineering
- #llm
- #ai
- #claude
- #openai
- #gemini
- #developer-tools
Dipak SuryawanshiFounder & Editor
Dipak Suryawanshi is a backend developer who works with Java and Spring Boot, focused on building APIs, clean architectures and developer tools. He founded KernelBrief, an independent technology publication based in India, sets its editorial direction and is responsible for everything published on it. Every KernelBrief article is researched and checked against primary sources such as official documentation, vendor advisories and release notes before it is published.
Related articles
What Is Loop Engineering? How AI Agent Loops Work, Their Building Blocks and Risks
Loop engineering designs the automated loops that run AI coding agents: triggers, stop conditions, state, verifiers and budgets. What it is and where it fails.
· 6 min read
Google Launches Gemini 4 Neon: Pricing, Availability and What Google Claims
Gemini 4 Neon costs $3/$15 per million tokens with a 1M-token context window. What Google confirmed, where to use it, and how to read its benchmarks.
· 6 min read
How Large Language Models Work: Tokens, Context Windows and Parameters Explained
How large language models work, in plain terms: what tokens are, how context windows fill up, what parameters mean, and how to count tokens in code.
· 10 min read