AI Agent Patterns
AI Agent adalah LLM yang bisa mengambil action secara otonom: panggil API, baca file, jalankan kode, navigasi web. Bukan hanya menjawab pertanyaan — tapi menyelesaikan tugas.
Agent Loop
// Simplified agent loop
async function agentLoop(task) {
const messages = [
{ role: "system", content: "Kamu adalah agent yang bisa menggunakan tools." },
{ role: "user", content: task },
];
while (true) {
const response = await llm.chat({ messages, tools });
// If model wants to use a tool
if (response.tool_calls) {
for (const call of response.tool_calls) {
const result = await executeTool(call);
messages.push({ role: "tool", content: result, tool_call_id: call.id });
}
continue; // Loop back to LLM with tool results
}
// Model is done — return final answer
return response.content;
}
}
Common Patterns
- ReAct (Reason + Act) — Model berpikir (reason), lalu bertindak (act), observe hasil, ulangi
- Plan and Execute — Buat plan dulu, lalu execute step by step
- Multi-agent — Beberapa agent dengan spesialisasi berbeda berkolaborasi
Guardrails
- Max iterations — Batasi loop (max 10 steps)
- Tool permissions — Agent hanya bisa akses tools yang diizinkan
- Human-in-the-loop — Konfirmasi sebelum destructive actions
- Cost limits — Stop jika token usage melebihi budget