Projects Blog Tools Music Contact
← All Posts
Tech July 17, 2026

Claude Prompt Engineering: Techniques That Work Best

By: Evgeny Padezhnov

Illustration for: Claude Prompt Engineering: Techniques That Work Best

Claude does not reward clever wording. It rewards structure, explicit instructions, and examples.

Prompt engineering for Claude differs from generic "write a good prompt" advice. Anthropic trained these models on specific patterns. Match those patterns and output quality jumps. Ignore them and even a detailed prompt drifts. The techniques below come from Anthropic's own prompt engineering documentation and hold up across real workloads.

Why Structure Beats Clever Wording

Claude models were fine-tuned with structured inputs. XML tags, clear role definitions, and worked examples appear throughout the training signal. The model expects them.

That has a practical consequence. A prompt that reads well to a human may confuse the model if fields blur together. A prompt that looks mechanical often performs better.

Key point: prompt engineering for Claude is closer to writing an API contract than writing an essay. Define inputs, mark boundaries, state the output shape.

XML Tags: Claude's Native Structure

XML tags are the single highest-leverage technique. They tell Claude where each piece of content starts and ends. Anthropic recommends them directly in the XML tags guide.

Wrap documents, instructions, and examples in named tags:

<document>
{{USER_TEXT}}
</document>

<instructions>
Summarize the document in three bullet points.
Return only the bullets. No preamble.
</instructions>

The model now knows {{USER_TEXT}} is data, not a command. This blocks a common failure: user text that contains instructions and hijacks the prompt. Separation also improves parsing when the response feeds another system.

Tag names carry meaning. Use <contract>, <email>, <code>, <rules> instead of generic <text>. Reference the same tags in your instructions: "Using the rules in <rules>, review the code in <code>."

Common mistake: nesting tags inconsistently or leaving one unclosed. Claude tolerates it, but structured extraction gets noisier. Keep tags balanced.

Be Explicit and Add Motivation

Current Claude models — such as Opus 4.8 and Sonnet 5 — follow instructions literally and precisely. Vague prompts that worked on older models can underperform now. Anthropic documents this shift in its Claude 4 best practices.

Two rules follow from that.

First, state the desired behavior directly. Instead of "don't be too verbose," write "respond in under 100 words." Instead of "format nicely," specify the exact structure.

Second, explain why. Motivation improves compliance. Compare:

Do not use markdown in your response.

against:

Do not use markdown in your response. The output is inserted
into a plain-text SMS field that renders asterisks and
backticks literally.

The second version generalizes better. The model understands the constraint instead of memorizing a rule, so it handles edge cases the rule did not spell out.

In plain terms: tell Claude what to do, not only what to avoid, and give the reason.

Multishot Prompting: Examples Beat Descriptions

Describing a format in prose is weaker than showing it. Three to five examples usually outperform a paragraph of instructions.

Few-shot examples anchor tone, structure, and edge-case handling at once. For a classification task, show inputs paired with the exact output format:

<examples>
<example>
Input: "The package arrived cracked and late."
Output: {"sentiment": "negative", "category": "shipping"}
</example>
<example>
Input: "Fast delivery, works perfectly."
Output: {"sentiment": "positive", "category": "product"}
</example>
</examples>

Make examples diverse. Cover the tricky cases, not just the easy ones. Include a borderline input if borderline inputs exist in production. The model copies the pattern it sees, including how you handle ambiguity.

In practice, a task that failed on a written spec often succeeds once two well-chosen examples appear. Examples cost tokens, but prompt caching offsets that — covered below.

Let Claude Think Before It Answers

For reasoning, math, and multi-step analysis, output quality improves when the model works through the problem first. Two mechanisms exist.

The classic technique is chain-of-thought prompting. Ask the model to reason inside a tag before the final answer:

Think step by step inside <scratchpad> tags.
Then give the final answer inside <answer> tags.

Downstream code reads only <answer>. The scratchpad stays available for debugging.

The newer mechanism is extended thinking, introduced in Claude 3.7 Sonnet. Note the API has changed: manual budget_tokens is deprecated and returns a 400 error on Claude Opus 4.7 and later (including Opus 4.8) and on Sonnet 5, which instead use adaptive thinking controlled by the effort parameter. Older Claude 4 models still accept a token budget:

thinking={"type": "enabled", "budget_tokens": 10000}

The model produces internal reasoning before the visible response. Larger budgets help hard problems. Smaller budgets keep latency and cost down. Match the budget to task difficulty — a simple format conversion needs no thinking budget at all.

Key point: do not stack manual chain-of-thought on top of extended thinking. Pick one. Doubling up wastes tokens and can muddy the output.

Prefill the Response to Control Format

Prefilling has been deprecated. Starting with Claude 4.6, providing a partial assistant message for Claude to continue from (on the last turn) is no longer supported and returns a 400 error on current models such as Opus 4.8 and Sonnet 5. Model instruction-following has advanced enough that Anthropic now recommends migrating away from prefill.

Force JSON by opening the brace:

messages=[
  {"role": "user", "content": "Return the user record as JSON."},
  {"role": "assistant", "content": "{"}
]

Claude continues from { and skips the "Here is the JSON you requested" preamble. Output starts as valid data. This removes a whole class of parsing bugs.

Prefilling also steers persona and format. Open with [ for a list. Open with a character name to hold a role in a dialogue. On current models, use a direct system-prompt instruction (e.g. "Respond directly without preamble"), an opening XML output tag, structured outputs, or tool calling to suppress conversational filler — prefilling the assistant turn is no longer supported.

Note that prefilling is no longer supported at all on current Claude models (Claude 4.6 and later, including Opus 4.8 and Sonnet 5); a prefilled assistant turn returns a 400 error. Use a system-prompt instruction or XML output tags instead.

Give Claude a Role via the System Prompt

The system parameter sets Claude's frame for the whole conversation. A specific role sharpens vocabulary, depth, and judgment.

Generic:

system="You are a helpful assistant."

Specific:

system="You are a senior SRE reviewing Terraform for a
regulated fintech. Flag anything that risks a compliance
audit. Assume the reader is a mid-level engineer."

The second version changes what the model notices and how it explains. Role prompting works best when the role matches the task exactly. A "senior SRE" reviews infrastructure differently from a "security auditor," and the difference shows in the findings.

Prompt Caching Cuts Cost and Latency

Long prompts get expensive when the same context repeats across calls. Prompt caching solves that. Mark a stable block with cache_control and Anthropic caches it:

{
  "type": "text",
  "text": LARGE_SYSTEM_CONTEXT,
  "cache_control": {"type": "ephemeral"}
}

Cached input tokens read at a fraction of the base price — roughly 90% cheaper on cache hits. Latency on long prompts drops sharply, up to around 85% for prompts dominated by cached content. The cache write costs slightly more than a normal input token, so caching pays off when the same context is reused several times.

Put the stable content first: system instructions, examples, reference documents. Put the variable content — the user's actual question — last. The cache boundary sits between them. This ordering matters and connects to the next point.

Structure Long-Context Prompts Correctly

With large documents, order changes accuracy. Anthropic's guidance is consistent: place long documents and data near the top of the prompt, above the query.

A 50-page contract should appear before the question about it. The instruction to act on that data comes at the end. Models attend more reliably to instructions positioned close to the generation point.

Wrap each source in its own tag and add metadata:

<documents>
<document index="1" title="MSA-2024">
{{CONTRACT_TEXT}}
</document>
</documents>

Question: What is the termination notice period in MSA-2024?

The index and title let the model cite sources precisely. For retrieval-augmented tasks, ask it to quote the relevant passage before answering. Grounding in a quote reduces fabrication.

Chain Prompts for Complex Tasks

One prompt doing five things does each one worse. Splitting the work into a chain raises quality on every step.

A document pipeline might run three calls: extract key clauses, analyze each clause, then draft a summary. Each call gets a focused prompt and clean input. Errors are easier to locate because each stage is inspectable.

Chaining also enables verification. Run the task, then send the output back with "Review this for factual errors against the source." A second pass catches mistakes a single pass misses. The cost is more calls; the benefit is fewer silent failures.

Common mistake: forcing a single mega-prompt because it feels efficient. It is cheaper in tokens and worse in results. Chain when the task has distinct phases.

What Does Not Work

Not every popular trick helps with Claude.

Threats and urgency ("this is critical, you must not fail") add noise without improving accuracy. Excessive politeness padding wastes tokens. Long lists of contradictory rules degrade compliance — the model cannot satisfy conflicting constraints, so it picks unpredictably.

Over-specifying format when prefill would do the job is another trap. Three sentences of formatting instructions often lose to a single prefilled {. If a simpler mechanism exists, use it. If it works — it is correct.

What to Try Right Now

Take one existing prompt that produces inconsistent output. Do two things: wrap every distinct input in a named XML tag, and add a system-prompt instruction (or an opening XML output tag) that forces the output format. Run both versions on ten real inputs and compare.

Try it: measure format-failure rate before and after. The structured version almost always wins, and the change takes minutes.

Prompt engineering for Claude is not about finding magic words. It is about giving the model clean structure, explicit instructions, and concrete examples — then removing everything that does not earn its tokens.

Information is accurate as of the publication date. Terms, prices, and regulations may change — verify with relevant professionals.

✦ Squeeze AI
  1. XML tags are the single highest-leverage technique: wrapping user-supplied content in named tags (e.g. <document>, <rules>) prevents it from being interpreted as instructions, blocking the common failure where user text hijacks the prompt.
  2. Adding the reason behind a constraint — not just the constraint itself — measurably improves compliance in Claude 4 models, which follow instructions more literally than earlier versions.
  3. Prompt engineering for Claude is closer to writing an API contract than crafting persuasive prose: define inputs, mark boundaries, and state the exact output shape, because the model was fine-tuned on structured patterns, not on clever wording.

Powered by B1KEY