Chuyển đến nội dung chính

Lesson 3: Prompt Engineering for Agents — System Prompts & Personas

Write an effective System Prompt for agents: define persona, boundary, output schema. Chain-of-Thought, few-shot, and format-compliant LLM pressing techniques. ReAct prompting pattern.

🧠 AI & ML — Lesson 2 Lesson 3: Prompt Engineering for Agents — System Prompts & Personas

Build AI Agents: From Zero to Production

Part 1: Agent Platform — Understand before building

xdev.asia

Introduction

Prompt Engineering for Agent is completely different from prompt for chatbot. Agents need instructions that are clear, structured, and reliable — because the LLM output will directly determine actions (tool calls, decisions), not just text.


1. System Prompt Architecture

1.1 System Prompt structure for Agent

┌──────────────────────────────────┐
│  1. IDENTITY & ROLE              │  "Bạn là Research Assistant..."
│  2. CAPABILITIES                 │  "Bạn có thể: search, calculate..."
│  3. CONSTRAINTS & BOUNDARIES     │  "Không bao giờ: delete files..."
│  4. OUTPUT FORMAT                │  "Trả lời theo JSON schema..."
│  5. BEHAVIORAL RULES             │  "Luôn xác nhận trước khi act..."
│  6. EXAMPLES (few-shot)          │  "Ví dụ: user hỏi X → bạn làm Y"
└──────────────────────────────────┘

1.2 Complete System Prompt example

AGENT_SYSTEM_PROMPT = """
# Identity
Bạn là Research Agent — chuyên tìm kiếm và tổng hợp thông tin.

# Capabilities
Bạn có access vào các tools sau:
- `web_search(query)`: Tìm kiếm trên internet
- `read_url(url)`: Đọc nội dung một trang web
- `take_note(content)`: Lưu ghi chú vào memory

# Constraints
- KHÔNG BAO GIỜ bịa thông tin. Nếu không tìm thấy, nói rõ.
- Luôn cite nguồn (URL) khi trích dẫn.
- Giới hạn tối đa 5 lần search cho mỗi câu hỏi.

# Output Format
Trả lời cuối cùng theo format:
## Kết quả nghiên cứu
[Nội dung tổng hợp]

### Nguồn tham khảo
1. [Tên] - URL
2. [Tên] - URL

# Behavioral Rules
1. Trước khi search, phân tích câu hỏi và lên kế hoạch.
2. Search từ tổng quát → cụ thể.
3. Cross-check thông tin từ ít nhất 2 nguồn.
"""

2. ReAct Prompting Pattern

ReAct = Reasoning + Acting — the strongest pattern for agents.

Thought: Tôi cần tìm dân số Việt Nam 2025
Action: web_search("dân số Việt Nam 2025")
Observation: Kết quả: 100.3 triệu người (theo World Bank)
Thought: Đã có câu trả lời, không cần search thêm
Action: respond("Dân số Việt Nam năm 2025 khoảng 100.3 triệu người")

Implement ReAct

REACT_PROMPT = """
Bạn là AI Agent. Với mỗi bước, hãy:

Thought: [Suy nghĩ về bước tiếp theo]
Action: [Chọn tool và tham số]

Sau khi nhận observation, tiếp tục:

Thought: [Đánh giá kết quả]
Action: [Tool tiếp theo HOẶC respond() nếu đã đủ]

QUAN TRỌNG:
- Không bao giờ skip Thought
- Tối đa 5 actions
- Respond khi đã đủ thông tin
"""

3. Persona Engineering

Why is Persona important for Agents?

Persona directly affects how agents make decisions:

# Agent với persona "cẩn thận"
careful_agent = "Bạn là Senior Security Analyst. Luôn verify trước khi act. Khi nghi ngờ, hỏi lại user."

# Agent với persona "nhanh gọn"
fast_agent = "Bạn là Speed Researcher. Ưu tiên tốc độ. Trả lời ngắn gọn, đúng trọng tâm."

# Agent với persona "sáng tạo"
creative_agent = "Bạn là Creative Director. Nghĩ ngoài khuôn khổ. Đề xuất giải pháp bất ngờ."

4. Few-Shot Prompting for Agents

FEW_SHOT_EXAMPLES = """
# Ví dụ 1:
User: "Ai là CEO của OpenAI?"
Thought: Đây là câu hỏi factual đơn giản, tôi có thể trả lời ngay
Action: respond("CEO của OpenAI là Sam Altman")

# Ví dụ 2:
User: "So sánh giá vé máy bay Hà Nội - Sài Gòn tuần này"
Thought: Cần dữ liệu real-time, tôi phải search
Action: web_search("giá vé máy bay Hà Nội Sài Gòn tuần này 2025")
Observation: [kết quả]
Thought: Đã có dữ liệu, tổng hợp và trả lời
Action: respond("[kết quả tổng hợp]")
"""

Summary

  • System Prompt for agents needs clear structure: identity, capabilities, constraints, format, rules
  • ReAct (Reasoning + Acting) is the fundamental pattern for every agent
  • Persona directly affects the agent's behavior and decisions
  • Few-shot examples help agents understand how to properly use tools
  • Always set boundaries and constraints — uncontrolled agents will cause trouble

Exercises

  1. Write System Prompt for "Customer Support Agent" with 3 tools: search_faq, create_ticket, escalate_to_human
  2. Implement complete ReAct loop in Python (no framework)
  3. Compare response quality when changing personas (careful vs fast vs creative)
  4. Create 5 few-shot examples for the agent of your choice