Sai lầm của dev mới học: viết system prompt 1 lần, test 1 case, ship. Kết quả: 20% use case lỗi.
- Áp dụng template 5-block để viết system prompt cho use case thật
- So sánh output giữa các biến thể system prompt để chọn tối ưu
- Nhận diện khi nào system prompt cần iteration thêm
- Tạo thư viện system prompt tái sử dụng
Mở đầu: Viết system prompt là kỹ năng, không phải tài năng
Dev có kinh nghiệm: viết v1 → test 10 case → thấy fail 3 → v2 fix 3 đó → test thêm → v3... đến khi đạt >90% accuracy.
Bài này là cơ hội luyện đúng quy trình đó trên 3 use case cụ thể.
Đề bài
Viết 3 system prompt cho 3 scenario sau. Với mỗi cái:
- Draft v1 theo template 5-block (Role, Objective, Constraints, Output, Examples)
- Test với 5-10 câu hỏi mẫu (bao gồm edge case)
- Iterate v2 dựa trên output sai
- Báo cáo kết quả (bao nhiêu pass/fail)
Scenario 1: Email summarizer
Context
Team support nhận 50 email/ngày, muốn tóm tắt nhanh trước khi đọc chi tiết.
Input format
From: [tên khách]
Subject: [tiêu đề]
Body: [nội dung email, 100-500 từ]
Output yêu cầu
- Priority: High/Medium/Low
- Category: Bug/Feature Request/Billing/Other
- Summary (< 50 từ)
- Action needed: Yes/No
Test cases (dùng để validate system prompt)
Case 1 (Bug, High priority):
From: John Smith
Subject: App crash on payment
Body: "Hi team, the app crashed twice when I tried to pay.
I lost my cart. Really frustrating. Please fix urgently."
Case 2 (Feature request, Low):
From: Sarah
Subject: Dark mode?
Body: "Hey, just wondering if you have plans for dark mode?
Would be nice to have."
Case 3 (Billing, Medium):
From: Acme Inc
Subject: Invoice question
Body: "We received invoice #4523 but the amount seems wrong.
Should be $1200 not $1500. Can you check?"
Case 4 (Ambiguous):
From: Mark
Subject: Hello
Body: "Can you call me back? My number is 555-0123."
Case 5 (Spam/Other):
From: casino@spam.ru
Subject: You won $1000000
Body: "Click here to claim your prize..."
Gợi ý v1
system_v1 = """Bạn là email classifier.
Phân loại email support.
{
"priority": "High|Medium|Low",
"category": "Bug|Feature|Billing|Other",
"summary": "< 50 words",
"action_needed": true/false
}
"""
Chạy thử
def classify_email(email_text: str):
msg = client.messages.create(
model=model,
max_tokens=300,
messages=[{"role": "user", "content": email_text}],
system=system_v1,
)
return msg.content[0].text
for case in test_cases:
result = classify_email(case)
print(result)
print("-" * 40)
Điểm cần kiểm tra
Case 4 (Mark "hello"): system v1 sẽ confuse. Bạn cần:
- Định nghĩa rõ priority khi no action
- Handle ambiguous case
Case 5 (spam): có thể nhầm là "Other" thay vì flag spam. Cần thêm category.
Iteration v2
Thêm:
- Spam category
- Default Low priority khi không clear
- Example ambiguous case
system_v2 = """Bạn là email classifier cho customer support team.
JSON strict:
{
"priority": "High|Medium|Low",
"category": "Bug|Feature|Billing|Spam|Other",
"summary": "< 50 words, neutral tone",
"action_needed": true/false
}
- High: system down, data loss, billing error > $100, urgent customer language
- Medium: feature request, clarification, refund < $100
- Low: casual question, compliment, no clear request
- Bug: error, crash, broken feature
- Feature: "would be nice", "wish you had", "any plans to add"
- Billing: invoice, payment, refund
- Spam: promotional, suspicious links, unknown sender domain
- Other: greetings with no context, misc
Input: "Hi, please call me back, number 555-0123"
Output: {"priority":"Low","category":"Other","summary":"User requests callback, no context","action_needed":true}
Input: "System down! Can't login!"
Output: {"priority":"High","category":"Bug","summary":"Login outage reported","action_needed":true}
"""
Self-review
- 5/5 case pass?
- JSON format parse được bằng
json.loads()? - Output consistent qua 3 run liên tiếp cùng input?
Scenario 2: Interview question generator
Context
Hiring manager cần list câu hỏi phỏng vấn theo role và level.
Input
Role: [Software Engineer / Product Manager / Sales]
Level: [Junior / Senior / Staff]
Focus areas: [comma-separated]
Output
- 5 câu behavioral
- 3 câu technical/domain
- 2 câu assess culture fit
- Gợi ý follow-up cho mỗi câu
Test cases
- SWE Junior, focus: Python, React
- PM Senior, focus: B2B, analytics
- Sales Staff, focus: Enterprise, new market
- (edge case) Role không tồn tại: "Ninja"
- (edge case) Level không rõ: "Medium"
Gợi ý approach
System prompt cần:
- Define rõ structure output
- Adapt depth theo level (Junior ≠ Staff)
- Handle edge case (unknown role → yêu cầu clarify)
Đây là bài self-directed — bạn viết v1, test, iterate. Không có solution key.
Scenario 3: Product description writer
Context
E-commerce, generate product description cho new SKU. Style phải khớp brand voice.
Input
Product: [name]
Category: [clothing / electronics / home]
Key features: [bullet list]
Target audience: [who]
Brand voice: [friendly / luxurious / bold]
Output
- Headline (< 10 từ)
- Short description (2-3 câu)
- Bullet list 5 key benefits
- CTA line
Test cases
- T-shirt organic cotton, friendly voice
- Luxury watch, luxurious voice
- Power drill, bold voice
- (ambiguous) Skincare cream, minimal info
- (edge) Product name chứa competitor brand
Tự làm từ đầu
Viết v1 → 5 test → iterate. Pay attention:
- Brand voice matter (adjectives, sentence length)
- SEO — key features nên lặp lại ở bullets
- Edge case 5: system phải refuse hoặc rewrite không dùng competitor
Pattern chung: Iteration workflow
┌─────────────────────────────────────────────────┐
│ │
│ 1. Viết v1 theo template │
│ │ │
│ ▼ │
│ 2. Tạo test dataset │
│ (5-10 case, 20% edge) │
│ │ │
│ ▼ │
│ 3. Run tất cả case │
│ │ │
│ ▼ │
│ 4. Đếm pass/fail │
│ (nếu < 70% → must iterate) │
│ │ │
│ ▼ │
│ 5. Analyze failures │
│ - Pattern nào fail? │
│ - Thiếu rule? Edge case? Format? │
│ │ │
│ ▼ │
│ 6. Viết v2 fix specific failures │
│ │ │
│ ▼ │
│ 7. Re-run → goto 4 │
│ │
│ Stop khi: > 90% pass + 3 lần run stable │
│ │
└─────────────────────────────────────────────────┘
Đây là manual eval loop. Ở Module 4 bạn sẽ học cách automate nó qua test framework + graders.
Thư viện system prompt tái sử dụng
Sau khi hoàn thành 3 scenario, lưu vào file prompts.py:
EMAIL_CLASSIFIER = """Bạn là email classifier cho customer support...
..."""
INTERVIEW_GENERATOR = """Bạn là hiring manager...
..."""
PRODUCT_DESCRIPTION = """Bạn là copywriter e-commerce...
..."""
Pattern production: Prompt như code — version control, review, test.
Checklist submit bài tập
Với mỗi 3 scenario, document:
Scenario X
- v1 prompt (paste full text)
- Test cases (10 cases)
- v1 results: X/10 pass
- Failures analysis (which rules missed?)
- v2 prompt (paste)
- v2 results: Y/10 pass
- (optional) v3
- Final: Z/10 pass + stable
Không pass nếu: không có iteration, không có analysis, chỉ v1.
Tips để prompt work tốt hơn
Tip 1: Start simple, add constraints
v1 cực ngắn. Chỉ thêm rule khi thấy output sai.
Over-engineered v1 thường performance tệ hơn simple v1.
Tip 2: Put examples đúng format
Nếu bạn muốn JSON output:
Input: "..."
Output: {"key": "value"} ← JSON thật, không phải text
Claude học từ example format.
Tip 3: Đặt constraint dương > phủ định
❌ "Đừng dùng ngôn ngữ phức tạp"
✅ "Dùng ngôn ngữ đơn giản, câu < 15 từ"
Constraint dương dễ follow hơn.
Tip 4: Cho "escape hatch"
"Nếu không chắc category → output 'Other' với confidence < 0.5"
Thay vì force Claude phải pick, cho Claude option "tôi không biết".
Tip 5: Test same input 3 lần
Với temperature=1 (default), cùng input có thể khác output. Nếu output quá khác biệt qua 3 lần → prompt chưa đủ deterministic, cần rule cụ thể hơn.
Hoặc: set temperature=0 cho tác vụ cần determinism (bài 6.12).
Mẹo nâng cao
Mẹo 1: Chain-of-thought trong system prompt
system = """
Trước khi output final, hãy:
1. Phân tích input (output <thinking>...</thinking>)
2. Apply rules
3. Format output theo spec
Output FINAL luôn sau </thinking>.
"""
Cải thiện accuracy 10-30% cho tác vụ reasoning.
Mẹo 2: Negative examples
Good: "Summary: Bug in checkout flow"
Bad: "Well, the user is saying..." ← verbose, nên tránh
Claude học từ cả ví dụ âm lẫn dương.
Mẹo 3: XML section delimiter
system = """<role>...</role>
<constraints>...</constraints>
<examples>
<example>
<input>...</input>
<output>...</output>
</example>
</examples>"""
Claude đặc biệt "quen" với XML tags (training data dùng XML). Cấu trúc rõ → output ổn định hơn. Chi tiết ở bài 6.19.
Tóm tắt bài học
- Viết system prompt = iteration, không phải 1 lần. v1 → test → v2 → test. Stop khi > 90% stable.
- Template 5-block là starting point tốt: Role, Objective, Constraints, Output, Examples.
- Edge case phải có trong test dataset — 20% test phải là unusual input.
- Lưu prompt như code —
prompts.py, version control, review. - Prompt dương > phủ định + escape hatch + test 3 lần cùng input.
Tài liệu tham khảo
- Anthropic prompt library — 40+ prompt production-grade
- Prompt engineering overview