Structure với XML tags

3 — Prompt EngineeringTrung cấp15 phút

Bạn muốn Claude debug code. Viết prompt:

Bạn sẽ học được
  • Dùng XML tags để phân tách instruction khỏi data trong prompt
  • Tạo custom tag names có ý nghĩa
  • Nhận diện khi nào XML tags là must-have vs nice-to-have
  • Áp dụng pattern kết hợp: XML + guidelines + examples

Pattern cơ bản

Claude giờ:

Output đúng: "Code chỉ áp 20% cho mọi income. Cần implement progressive theo brackets."

  • Biết <my_code> là code cần debug, không phải ref
  • Biết <tax_docs> là reference, không phải code
  • Focus vào bug trong code + compare với docs
Đây là code của tôi:

<my_code>
def calculate_tax(income):
    return income * 0.2
</my_code>

Đây là docs tham khảo:

<tax_docs>
Bracket 1: 0-10000, tax 10%
Bracket 2: 10000-50000, tax 20%
Bracket 3: > 50000, tax 30%
</tax_docs>

Tìm bug trong <my_code> dựa vào quy định trong <tax_docs>.

Tại sao XML?

Anthropic train Claude với nhiều XML data — Claude đặc biệt "quen" parse XML tags.

So với alternatives:

Winner: XML tags.

DelimiterClaude hiểu?Human readable?
<tag>...</tag>✅ Best✅ Yes
TAG ###⚠️ OK⚠️ Ambiguous với header
---TAG---⚠️ OK⚠️ Ambiguous
"""TAG"""⚠️ OK❌ No
No delimiter❌ Poor❌ Unclear

Custom tag names

Không cần dùng XML "chuẩn". Tạo tên có ý nghĩa:

Rule:

  • Descriptive — <customer_email> > <data>
  • Lowercase with underscore — theo convention XML
  • Consistent — cùng data type → cùng tag name qua prompts
  • Matching close tag — <x>...</x> (không <x>...</X>)
<sales_records>...</sales_records>
<athlete_info>...</athlete_info>
<user_query>...</user_query>
<product_catalog>...</product_catalog>
<previous_attempt>...</previous_attempt>

Khi nào dùng XML tags?

✅ Must use

⚠️ Optional

❌ Skip

  • Large context data (10+ dòng) — article, document, code
  • Mixed content types — code + docs + examples
  • Multiple variables — template fill nhiều fields
  • Examples — wrap mỗi example (bài 6.18)
  • Short single-variable prompt ("Summarize this: [text]")
  • Simple Q&A
  • Ultra-short ("Translate: hello")
  • When XML makes simpler prompt harder to read

Ví dụ: Email drafter template

Claude giờ biết exactly mỗi block là gì → output email coherent.

def draft_email(recipient, topic, tone, context):
    prompt = f"""Draft professional email based on below info.

<recipient>
Name: {recipient['name']}
Role: {recipient['role']}
Company: {recipient['company']}
Relationship: {recipient['relationship']}
</recipient>

<topic>{topic}</topic>

<tone>{tone}</tone>

<context>
{context}
</context>

<previous_interactions>
{previous_interactions_summary}
</previous_interactions>

Guidelines:
- Length: 100-150 words
- Open with relevant reference to previous interaction
- 1 CTA at end
- Sign off appropriate to tone

Output only the email body, no subject line."""
    
    return call_claude(prompt)

Pattern: All techniques combined

Final form của production prompt thường dùng cả:

  • Clear & direct instruction
  • XML tags cho structure
  • Guidelines cho quality control
  • Examples cho edge case
prompt = """Classify support ticket into category and priority.

<categories>
- Bug: technical error, crash, broken feature
- Feature_Request: suggestion, wish list
- Billing: invoice, payment, refund
- Other: misc
</categories>

<priorities>
- High: system down, data loss, major billing error
- Medium: feature broken but workaround exists
- Low: minor issue, feature request, questions
</priorities>

<examples>
<example>
<ticket>App crashes when I click Save</ticket>
<output>{"category": "Bug", "priority": "High"}</output>
</example>
<example>
<ticket>Would be nice to have dark mode</ticket>
<output>{"category": "Feature_Request", "priority": "Low"}</output>
</example>
<example>
<ticket>Invoice total is wrong</ticket>
<output>{"category": "Billing", "priority": "High"}</output>
</example>
</examples>

<guidelines>
- Return JSON with exactly 2 fields: category, priority
- Base priority on impact (not urgency in language)
- If ambiguous category, prefer more specific
</guidelines>

Now classify:
<ticket>{ticket_text}</ticket>"""

Case studies theo ngành

📄 Legal — Contract clause extraction

💻 Developer — Code review

<contract>
...500 trang hợp đồng...
</contract>

<target_clauses>
1. Payment terms
2. Termination conditions
3. IP ownership
4. Dispute resolution
</target_clauses>

Extract each target clause from contract. Return as JSON.

💻 Developer — Code review

📊 Data Analyst — Report generation

<code>
{user_code}
</code>

<style_guide>
{company_style_guide}
</style_guide>

<tests>
{existing_tests}
</tests>

Review code against style guide. Point to specific lines.

📊 Data Analyst — Report generation

<data_summary>
{metrics}
</data_summary>

<previous_report>
{last_quarter_report}
</previous_report>

<target_audience>
C-level execs, 5 min read max
</target_audience>

Generate quarterly report drawing comparison with previous.

Anti-patterns

❌ Random tag names

Fix: Descriptive names.

❌ Không match close tag

<x>...</x>
<data1>...</data1>
<thing>...</thing>

❌ Không match close tag

Fix: Consistent case. Lowercase.

❌ Nested quá sâu

<email>
...
</Email>   ← typo: cap khác

❌ Nested quá sâu

Vấn đề: Claude khó parse, Claude đôi khi nhầm scope.

Fix: Max 2 level. Flatten nếu cần.

❌ XML cho prompt ngắn

<outer>
  <middle>
    <inner>
      <deep>...</deep>
    </inner>
  </middle>
</outer>

❌ XML cho prompt ngắn

Overkill. Prompt 5 dòng không cần XML.

Fix: Chỉ dùng khi prompt 20+ dòng hoặc mixed content.

❌ XML nhưng không reference trong instruction

<task>Translate this:</task>
<input>Hello</input>

❌ XML nhưng không reference trong instruction

Fix:

<data>...</data>
Generate summary.  ← Không nói summary về cái gì

Anti-patterns (tiếp)

<data>...</data>
Generate summary based on <data>.

Áp dụng ngay

Bài tập 1: Refactor prompt với XML (15 phút)

Lấy 1 prompt production của bạn. Nếu có:

Wrap từng phần trong XML tag descriptive. Test output quality trước/sau.

Bài tập 2: Multi-variable template (15 phút)

Viết prompt generate product review summary với 4 variables:

Test với 2 sản phẩm khác nhau. Output có consistent structure không?

  • Instruction
  • Context data
  • Examples
  • <product> info
  • <reviews> list
  • <target_length>
  • <tone>

Tóm tắt bài học

🎯 XML tags phân tách instruction / data / examples — Claude parse đúng boundary.

🎯 Custom tag names descriptive — <sales_data> > <data>.

🎯 Must-use khi prompt > 20 dòng, mixed content, nhiều variable.

🎯 Combine tất cả techniques: Clear & direct + XML + Guidelines + Examples = production-grade prompt.

🎯 Max 2 level nesting. Tag names consistent qua prompts.

Tài liệu tham khảo
  • Anthropic: Use XML tags
Nội dung này có hữu ích không?