Trung BìnhAI Engineering iconAI Engineering

Prompt chaining là gì? Khác gì agent và khi nào nên dùng?

Prompt chaining là pattern chia task phức tạp thành chuỗi prompt nhỏ, output của prompt trước thành input của prompt sau. Workflow có cấu trúc tĩnh do dev thiết kế.

Ví dụ chain "Viết blog post":
1. Prompt 1: "Sinh 5 outline cho chủ đề X" → chọn 1.
2. Prompt 2: "Với outline này, viết introduction" → text1.
3. Prompt 3: "Viết body theo outline, kế thừa tone của intro" → text2.
4. Prompt 4: "Tóm tắt + call-to-action" → text3.
5. Prompt 5: "Proofread và format markdown" → final.

Khác agent:
- Chain: flow do developer quyết định trước — deterministic, dễ debug, latency dự đoán được, rẻ.
- Agent: flow do LLM quyết định runtime (chọn tool, quyết khi dừng) — linh hoạt, xử lý được task mở, nhưng khó control, đắt, dễ lỗi.

Khi dùng chain: task phân rã rõ ràng thành bước (viết bài, pipeline xử lý doc, workflow onboarding), không cần quyết định runtime, cần reliability cao, cost predictable.

Khi dùng agent: task cần exploration (research), branching không lường trước, interact nhiều hệ thống.

Pattern chain phổ biến:
- Sequential: A → B → C.
- Router / Branching: classifier chọn path (FAQ → chain1, technical → chain2).
- Parallel + merge: phân task, chạy song song, aggregate (map-reduce cho long doc).
- Self-refine: generate → critic → revise loop (có bounded iterations, không phải agent thực sự).
- Conditional: có if/else theo output.

Framework: LangChain Expression Language (LCEL), LlamaIndex Query Pipeline, Haystack pipelines, hoặc tự code với async primitives. Nguyên tắc KISS: luôn thử chain trước agent — phần lớn task "agent" thực ra chỉ cần chain tốt.

Xem toàn bộ AI Engineering cùng filter theo level & chủ đề con.

Mở danh sách AI Engineering