Bạn muốn Claude debug code. Viết prompt:
- 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
Mở đầu: Ranh giới không rõ = Claude nhầm lẫn
Đây là code của tôi:
def calculate_tax(income):
return income * 0.2
Và đây là docs về tax brackets:
Bracket 1: 0-10000, tax 10%
Bracket 2: 10000-50000, tax 20%
Bracket 3: > 50000, tax 30%
Tìm bug trong code.
Claude có thể:
- Nhầm docs là part of code → suggest sửa docs
- Nhầm code là part of docs → không hiểu task
- Bỏ sót bracket 1, 3 vì không phân biệt rõ
- Vấn đề
- không rõ chỗ nào là instruction, chỗ nào là data của bạn, chỗ nào là reference.
- Solution
- XML tags — delimiter rõ ràng.
Pattern cơ bản
Đâ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>.
Claude giờ:
- 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
Output đúng: "Code chỉ áp 20% cho mọi income. Cần implement progressive theo brackets."
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:
| Delimiter | Claude hiểu? | Human readable? |
|---|---|---|
| <tag>...</tag> | Best | Yes |
| ### TAG ### | OK | Ambiguous với header |
| ---TAG--- | OK | Ambiguous |
| """TAG""" | OK | No |
| No delimiter | Poor | Unclear |
Winner: XML tags.
Custom tag names
Không cần dùng XML "chuẩn". Tạo tên có ý nghĩa:
<sales_records>...</sales_records>
<athlete_info>...</athlete_info>
<user_query>...</user_query>
<product_catalog>...</product_catalog>
<previous_attempt>...</previous_attempt>
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>)
Khi nào dùng XML tags?
Must use
- 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)
Optional
- Short single-variable prompt ("Summarize this: [text]")
- Simple Q&A
Skip
- Ultra-short ("Translate: hello")
- When XML makes simpler prompt harder to read
Ví dụ: Email drafter template
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)
Claude giờ biết exactly mỗi block là gì → output email coherent.
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
<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
<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
<x>...</x>
<data1>...</data1>
<thing>...</thing>
Fix: Descriptive names.
Không match close tag
<email>
...
</Email> ← typo: cap khác
Fix: Consistent case. Lowercase.
Nested quá sâu
<outer>
<middle>
<inner>
<deep>...</deep>
</inner>
</middle>
</outer>
- 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
<task>Translate this:</task>
<input>Hello</input>
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
<data>...</data>
Generate summary. ← Không nói summary về cái gì
Fix:
<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ó:
- Instruction
- Context data
- Examples
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:
<product>info<reviews>list<target_length><tone>
Test với 2 sản phẩm khác nhau. Output có consistent structure không?
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.