Prompt Engineering Basics

System prompts, few-shot examples, XML-tagged context and structured output for RAG.

Last Updated: 27, Nov 2026

Search Tutorials

--Ads--

Module 0 · Lesson 2 of 9 · Beginner

Prompt Engineering Basics

System prompts, few-shot examples, XML-tagged context and structured output for RAG.

The final step of every RAG pipeline is a prompt: instructions, retrieved context and the user's question, sent to an LLM. A good retriever paired with a weak prompt still gives poor answers. This lesson covers the prompting techniques you will use throughout the tutorial.

The anatomy of a request

A request to a chat model has two main parts:

  • The system prompt sets the model's role, rules and output style. It stays the same across requests.
  • The messages carry the conversation: user turns and assistant turns. In RAG, the retrieved context and the question usually go in the user message.
import anthropic

client = anthropic.Anthropic()

response = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=1000,
    system="You are a support assistant for Acme Cloud. Answer briefly and politely.",
    messages=[{"role": "user", "content": "How do I reset my password?"}],
)
print(response.content[0].text)

Be clear and specific

Write instructions as you would for a capable new colleague who has no background on your project. Say what the task is, who the answer is for, what to do in edge cases and what the output should look like. Explaining why a rule exists helps the model apply it sensibly.

VagueClear
Answer the question.Answer the customer's question using only the documents provided. Customers are non-technical, so avoid jargon. If the documents do not contain the answer, say so and suggest contacting support.
Summarise this.Summarise this incident report in three bullet points for an executive: what happened, the customer impact, and the fix.

Separate context from instructions with XML tags

When a prompt contains documents, wrap them in tags so the model can tell instructions, reference material and the question apart. Any tag names work; consistent, descriptive names are best.

documents = [
    {"source": "returns-policy.md", "text": "Items can be returned within 30 days of delivery..."},
    {"source": "shipping-faq.md", "text": "Standard shipping takes 3-5 business days..."},
]

context = "\n".join(
    f'<document index="{i}" source="{d["source"]}">\n{d["text"]}\n</document>'
    for i, d in enumerate(documents, start=1)
)

prompt = f"""<documents>
{context}
</documents>

Using only the documents above, answer the question below.
If the answer is not in the documents, say "I don't know based on the provided documents."

<question>How long do I have to return an item?</question>"""
Tip

Put long documents before the question and the final instructions. Models tend to answer better when the query comes at the end of a long prompt.

Few-shot examples

Showing the model one to three examples of input and ideal output is often the quickest way to fix formatting or tone. Make the examples varied so the model does not copy one of them too literally.

system = """You classify support questions into one category:
billing, technical, account, or other. Reply with the category only.

<examples>
<example><question>Why was I charged twice this month?</question><category>billing</category></example>
<example><question>The app crashes when I upload a photo.</question><category>technical</category></example>
<example><question>How do I change the email on my profile?</question><category>account</category></example>
</examples>"""

Ask for structured output

RAG applications often need answers in a machine-readable shape: an answer plus the list of sources, or extracted fields. The most reliable option is to ask the API to enforce a JSON schema. With the Python SDK you can describe the shape as a Pydantic model:

from pydantic import BaseModel
import anthropic

class Answer(BaseModel):
    answer: str
    sources: list[str]
    answered_from_documents: bool

client = anthropic.Anthropic()
response = client.messages.parse(
    model="claude-opus-5-5",
    max_tokens=2000,
    messages=[{"role": "user", "content": prompt}],
    output_format=Answer,
)
result = response.parsed_output
print(result.answer, result.sources)

The lesson Structured RAG Outputs builds on this.

Allow "I don't know"

A model that is not told it may decline will try to answer anyway. In RAG, always give it an explicit, acceptable way out when the context does not contain the answer. This single instruction removes a large share of hallucinated answers.

Iterate with real examples

Prompting is empirical. Keep a small set of real questions, run them after every prompt change and read the answers. Module 12 turns this habit into an automated evaluation.

Key takeaways

  • Use the system prompt for role and rules, and the user message for context and the question.
  • Be specific about the task, audience, edge cases and format, and explain the reasons behind rules.
  • Wrap retrieved documents in XML tags and put the question after them.
  • Use a few varied examples to fix format and tone, and schemas to get reliable JSON.
  • Always give the model permission to say it does not know.