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.
Prompt chaining splits a complex task into a sequence of smaller prompts where the previous output feeds the next. The workflow is statically designed by the developer.
Example "Write a blog post" chain:
1. Prompt 1: "Generate 5 outlines for topic X" → pick 1.
2. Prompt 2: "Given this outline, write the introduction" → text1.
3. Prompt 3: "Write the body matching intro tone" → text2.
4. Prompt 4: "Summary + CTA" → text3.
5. Prompt 5: "Proofread and format as markdown" → final.
Vs agents:
- Chain: flow developer-decided up front — deterministic, debuggable, predictable latency, cheap.
- Agent: flow LLM-decided at runtime (picks tools, decides when to stop) — flexible, handles open-ended tasks, but hard to control, expensive, error-prone.
Use chain when: the task decomposes cleanly into steps (blog writing, doc processing pipeline, onboarding workflow), no runtime decisions needed, high reliability required, cost must be predictable.
Use agent when: the task needs exploration (research), unpredictable branching, interaction with many systems.
Common chain patterns:
- Sequential: A → B → C.
- Router / Branching: classifier picks a path (FAQ → chain1, technical → chain2).
- Parallel + merge: split the task, run in parallel, aggregate (map-reduce over long docs).
- Self-refine: generate → critic → revise loop (bounded iterations, not a true agent).
- Conditional: if/else on output.
Frameworks: LangChain Expression Language (LCEL), LlamaIndex Query Pipeline, Haystack pipelines, or hand-rolled async primitives. KISS rule: always try a chain before an agent — most "agent" tasks only need a well-structured chain.