14.Anthropic Prompt Engineering
Part 1: Context and Purpose
Why is this a thing
Prompt engineering is the practice of crafting highly specific instructions to guide Large Language Models (LLMs) like Claude. Because LLMs are fundamentally text-prediction engines fine-tuned on vast amounts of data, they lack inherent context about your specific goals. The Anthropic Prompt Engineering Interactive Tutorial exists to teach users—especially developers—how to explicitly construct prompts that maximize Claude's accuracy, reduce hallucinations, and format outputs reliably.
How does it connect to other things?
Prompt engineering sits directly between human intent and machine execution. It connects raw data (documents, code, logs) with application logic by using the LLM as a reasoning engine. Mastering these techniques allows you to build reliable AI-powered chatbots, data extraction pipelines, and automated reasoning tools.
Part 2: Foundational Mechanics (Chapters 1–3)
The foundational chapters emphasize treating Claude like a highly capable but completely uncontextualized new employee. - Basic Structure & Clarity: You must be literal. If you want a haiku without introductory text, you must explicitly say: "Write a haiku. Skip the preamble." - Assigning Roles (Role Prompting): Giving Claude a specific persona (e.g., "You are an expert financial analyst speaking to a beginner") drastically shifts the model's tone, vocabulary, and problem-solving approach. It primes the model's neural networks to favor concepts related to that specific domain.
Part 3: Deep Dive into System Prompts
What is a System Prompt?
A system prompt is a specialized set of instructions placed at the very beginning of an API call or chat thread, separate from the standard "user" message. It acts as the foundational rulebook for the AI's behavior throughout the entire interaction. - API call: a direct request sent by a software program to a remote server, such as an AI model, asking it to process data and return a result - Chat thread: the chronological log of alternating messages between a human and an AI that serves as the memory and context for an ongoing conversation.
Why are they a thing?
System prompts establish persistent "guardrails." Instead of repeating instructions in every single user message, the system prompt holds the overarching context, tone, and strict rules. This improves the model's adherence to instructions over long conversations and makes it harder for end-users to accidentally (or maliciously) override your core application logic.
How is it used?
- Setting Boundaries: "You are a customer support bot. You may only answer questions about our software. If asked about outside topics, decline politely."
- Defining Output Rules: "Always respond in Spanish. Never use bullet points."
- Implementation: In Anthropic's API, the system prompt is passed as a top-level parameter, completely distinct from the alternating
UserandAssistantturns, ensuring Claude prioritizes these foundational rules above all else.
Step by Step
- Step 1: The Foundation (Writing the Code)
When you are building an agent in a code editor like VS Code, you do not type the system prompt into a standard chat box. Instead, you hardcode it directly into your local script as a permanent rule. It is assigned to a specific, hidden variable that the end-user never sees.
- Example:
System: "You are a specialized assistant that strictly helps organize local markdown notes. Do not answer questions outside of this topic." - To hardcode: type it directly into your program, such as in VSCode to build your agent
- Example:
- Step 2: The User Input
Once your agent is running, you (or the user) type a standard question into the chat interface.
- Example:
"Can you give me a recipe for chocolate chip cookies?"
- Example:
- Step 3: The Package (The API Call)
When you hit "send," your code does not just send your question. It packages the data to send to the AI model's servers. Inside this data package, the system prompt is placed at the very top in its own special, isolated compartment. It looks structurally like this:
- System Compartment: "You are a specialized assistant..."
- User Compartment: "Can you give me a recipe..."
- Step 4: The AI Processing When the AI model receives the package, it is programmed to read the System Compartment first. This acts as the model's absolute truth. It instantly adopts the persona, tone, and rules defined there before it even begins to process the user's request.
- Step 5: The Output
The AI then reads the user's question. Because the system prompt acts as a permanent guardrail, the AI filters its response through those rules.
- Result: Instead of giving you a recipe, your agent replies: "I am an assistant designed to help with your local markdown notes. I cannot provide baking recipes."
Part 4: Deep Dive into XML Tags (Chapter 4)
What are XML Tags?
XML (eXtensible Markup Language) tags are structural markers that look like <tag_name>...</tag_name>. In prompt engineering, they are used to wrap and isolate different sections of your prompt.
Why are they a thing?
While LLMs can recognize markdown or random delimiters (like ### or ---), Claude was specifically trained and fine-tuned by Anthropic to recognize XML tags as its primary organizing mechanism. Using XML tags aligns perfectly with Claude's underlying architecture, maximizing its ability to parse complex instructions, especially those involving massive context windows.
How are they used?
- Separating Data from Instructions: If you ask an LLM to summarize a text, a user might accidentally include rogue instructions inside the text. XML tags cleanly separate the "what to do" from the "what to process."
XML
Summarize the following article: <article> [Insert text here] </article>
<instructions>
Read the following report. First, extract the key financial numbers into the scratchpad. Then, provide a final summary.
</instructions>
<document>
[Insert massive 50-page financial PDF text here]
</document>
<scratchpad>
[The AI will do its math and thinking here]
</scratchpad>
<answer>
[The AI will put the final, clean summary here]
</answer>
Does seeing them laid out like this help clarify how XML tags act as the structural "bones" of a prompt?
1. Structuring Output (Formatting): You can force Claude to wrap its answers in specific tags, making it trivial for your software's code to extract the exact data it needs using a simple regular expression (e.g., extracting just the data inside <json_output> tags).
2. Creating a "Scratchpad": You can instruct Claude to "think" inside a specific tag before giving the final answer, keeping the messy reasoning hidden from your final output parsing.
Commonly Used XML
| XML Tag | Purpose / Meaning | Why It Is Used | |
|---|---|---|---|
<scratchpad> or <thinking> |
Gives the AI a designated space to "think out loud," outline its logic, or do math before it gives you the final answer. | Prevents logic errors. By forcing the AI to break down a problem step-by-step first, it is much less likely to make a mistake in its final answer. | |
<document>, <article>, or <context> |
Wraps the raw data, text, or code that you want the AI to analyze. | Separation of concerns. It ensures the AI doesn't confuse the text it is supposed to be reading with the instructions it is supposed to be following. | |
<instructions> or <rules> |
Clearly isolates the specific commands or tasks the AI needs to execute. | Keeps your prompt organized. If an end-user tries to trick the AI (prompt injection), these tags help the AI remember which text contains the real rules. | |
<example> or <examples> |
Used to hold sample inputs and ideal outputs (a technique called "Few-Shot Prompting"). | "Shows" instead of "tells." It is the fastest way to get the AI to perfectly match a specific tone, format, or layout you want. | |
<quotes> or <evidence> |
A place for the AI to extract and store exact, verbatim sentences from the source document before it writes a summary. | Prevents hallucinations. If the AI has to physically copy/paste facts into a <quotes> tag first, it is much less likely to invent fake information in its final response. |
|
<answer>, <response>, or <output> |
Defines exactly where the final, polished result should be placed. | Makes it easy for your software (like your VS Code script) to grab only the final answer and ignore all the messy <scratchpad> thinking that came before it. |
Part 5: Advanced Techniques (Chapters 5–9)
Precognition (Thinking Step-by-Step)
This connects deeply to the XML tags mentioned above. By forcing Claude to map out its arguments or list steps before formulating an answer, you significantly increase its accuracy on logic, math, and reasoning tasks. It reduces the chance of the model painting itself into a linguistic corner. Example: "Write your step-by-step logic inside <scratchpad> tags, then place your final answer in <answer> tags."
Using Examples (Few-Shot Prompting)
Showing is almost always better than telling. By providing 2-3 examples of ideal inputs and outputs (often wrapped in <example> tags), you calibrate Claude's expectations, leading to highly consistent formatting and tone.
Avoiding Hallucinations
When processing long documents, LLMs can confidently invent facts. The tutorial teaches a mitigation strategy: Evidence Gathering. You instruct Claude to first extract exact, verbatim quotes from the source text and place them in <quotes> tags. Then, you instruct it to base its final answer only on the extracted quotes.
Speaking for Claude (Prefill)
A unique feature of Anthropic's API is the ability to "put words in Claude's mouth" by prefilling the Assistant response. For example, ending your prompt by passing {"role": "assistant", "content": "{"} forces Claude to immediately begin generating a JSON object, completely bypassing any unwanted conversational filler like "Here is the JSON you requested: { "