Agent bị kẹt vòng lặp vô hạn hoặc tốn quá nhiều token. Xử lý thế nào?

Đây là failure mode phổ biến nhất của agent. Cần nhiều lớp phòng thủ:

1. Hard limits (bắt buộc): max_iterations (10-25 step), max_tokens_per_session (100K), timeout (30s/tool, 5min/session), max_tool_calls_per_tool.

2. Loop detection: hash (action + args) gần nhất — lặp lại N lần → ép break; semantic similarity thought sequence > 0.95 → đang spin.

3. Token reduction: context compression (tóm tắt history cũ), observation truncation (trim tool output dài), prompt caching (giảm 50-90% prefix cost), smaller model cho sub-task (Haiku/mini).

4. Planning trước: Plan-and-Execute (plan upfront → khó lạc), Reflexion (agent tự review progress mỗi N step).

5. Observability: log Thought/Action/Observation với trace ID + token count; alert khi cost/session > threshold. Tools: LangSmith, Langfuse, Arize Phoenix, Helicone.

6. Human-in-the-loop: confirm trước action destructive hoặc sau N step với task quan trọng.

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

Mở danh sách AI Engineering