AI & Data Science

AI Agent Architecture β€” Tool Use, Memory, Planning, Multi-Agent & Guardrails

Tutorial lengkap arsitektur AI Agent β€” dari konsep tool use, memory systems, planning, multi-agent collaboration, guardrails, hingga evaluasi frameworks

1. Pengenalan AI Agent

AI Agent adalah sistem yang menggunakan LLM sebagai "otak" untuk mengambil keputusan, menggunakan tools, dan menyelesaikan tugas kompleks secara otonom. Berbeda dari chatbot biasa yang hanya menjawab pertanyaan, Agent bisa merencanakan, bertindak, mengamati hasil, dan mengulangi sampai tugas selesai.

Diagram: Agent Architecture
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    AI AGENT ARCHITECTURE                          β”‚
β”‚                                                                  β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                      β”‚
β”‚  β”‚              USER INPUT              β”‚                      β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                      β”‚
β”‚                ↓                                                β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                      β”‚
β”‚  β”‚           PLANNING                   β”‚                      β”‚
β”‚  β”‚  "Langkah apa yang perlu diambil?"   β”‚                      β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                      β”‚
β”‚                ↓                                                β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                      β”‚
β”‚  β”‚      TOOL SELECTION & EXECUTION      β”‚                      β”‚
β”‚  β”‚  β”Œβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”   β”‚                      β”‚
β”‚  β”‚  β”‚ API β”‚ β”‚ DB  β”‚ β”‚Code β”‚ β”‚Searchβ”‚   β”‚                      β”‚
β”‚  β”‚  β””β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”˜   β”‚                      β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                      β”‚
β”‚                ↓                                                β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                      β”‚
β”‚  β”‚      OBSERVATION & MEMORY            β”‚                      β”‚
β”‚  β”‚  "Apa hasilnya? Sudah cukup?"        β”‚                      β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                      β”‚
β”‚                ↓                                                β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                      β”‚
β”‚  β”‚      REFLECTION / RE-PLAN            β”‚                      β”‚
β”‚  β”‚  "Perlu langkah tambahan?"           β”‚                      β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                      β”‚
β”‚                ↓                                                β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                      β”‚
β”‚  β”‚          FINAL ANSWER                β”‚                      β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Komponen Inti AI Agent

KomponenFungsiContoh
LLM (Brain)Otak pengambil keputusanGPT-4, Claude, Llama
ToolsAksi yang bisa dilakukanSearch, API, calculator, code
MemoryMengingat konteks & hasilChat history, vector DB
PlanningMerencanakan langkahReAct, CoT, sub-goal decomposition
GuardrailsBatasan keamananInput/output filter, max iterations

2. Tool Use / Function Calling

Tool Use memungkinkan LLM untuk "memanggil" fungsi eksternal β€” mencari di web, query database, menjalankan kode, mengirim email, dan lainnya.

Python β€” Function Calling dengan OpenAI
# =============================================
# Function Calling dengan OpenAI
# =============================================
from openai import OpenAI
import json

client = OpenAI()

# Definisikan tools
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Dapatkan cuaca saat ini di lokasi tertentu",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "Nama kota, contoh: Jakarta, Bandung"
                    },
                    "unit": {
                        "type": "string",
                        "enum": ["celsius", "fahrenheit"],
                        "description": "Satuan suhu"
                    }
                },
                "required": ["location"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "search_database",
            "description": "Cari informasi dari database produk",
            "parameters": {
                "type": "object",
                "properties": {
                    "query": {"type": "string", "description": "Kata kunci pencarian"},
                    "category": {"type": "string", "description": "Filter kategori"}
                },
                "required": ["query"]
            }
        }
    }
]

# Implementasi fungsi
def get_weather(location, unit="celsius"):
    # Di production, ini akan call weather API
    return {"location": location, "temp": 32, "condition": "Cerah", "unit": unit}

def search_database(query, category=None):
    return {"results": [{"name": "Produk A", "price": 150000}]}

# Agent conversation
messages = [
    {"role": "system", "content": "Anda adalah asisten yang bisa menggunakan tools."},
    {"role": "user", "content": "Bagaimana cuaca di Jakarta hari ini?"}
]

response = client.chat.completions.create(
    model="gpt-4o",
    messages=messages,
    tools=tools,
    tool_choice="auto"
)

# Check if LLM wants to call a tool
msg = response.choices[0].message
if msg.tool_calls:
    for tool_call in msg.tool_calls:
        func_name = tool_call.function.name
        args = json.loads(tool_call.function.arguments)
        
        # Execute the function
        if func_name == "get_weather":
            result = get_weather(**args)
        
        # Add function result to messages
        messages.append(msg)
        messages.append({
            "role": "tool",
            "tool_call_id": tool_call.id,
            "content": json.dumps(result)
        })
    
    # Get final response
    final = client.chat.completions.create(
        model="gpt-4o", messages=messages, tools=tools
    )
    print(final.choices[0].message.content)

3. Memory Systems

Agent perlu mengingat β€” baik percakapan sebelumnya (short-term) maupun fakta penting (long-term).

Diagram: Memory Types
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    AGENT MEMORY TYPES                            β”‚
β”‚                                                                  β”‚
β”‚  1. SHORT-TERM (Working Memory)                                β”‚
β”‚     β†’ Conversation history (messages list)                      β”‚
β”‚     β†’ Current task context                                      β”‚
β”‚     β†’ Tool call results                                         β”‚
β”‚     β†’ Limited by context window                                 β”‚
β”‚                                                                  β”‚
β”‚  2. LONG-TERM (Persistent Memory)                              β”‚
β”‚     β†’ Vector DB (facts, preferences)                            β”‚
β”‚     β†’ User profile (nama, preferensi)                           β”‚
β”‚     β†’ Knowledge base (dokumen, FAQ)                             β”‚
β”‚     β†’ Episodic memory (percakapan masa lalu)                    β”‚
β”‚                                                                  β”‚
β”‚  3. SEMANTIC (Knowledge)                                        β”‚
β”‚     β†’ Fakta-fakta tentang dunia                                 β”‚
β”‚     β†’ Domain knowledge                                          β”‚
β”‚     β†’ Fine-tuned into model atau via RAG                        β”‚
β”‚                                                                  β”‚
β”‚  4. PROCEDURAL (How-to)                                         β”‚
β”‚     β†’ Cara menggunakan tools                                    β”‚
β”‚     β†’ Standard Operating Procedures                             β”‚
β”‚     β†’ Encoded dalam system prompt                               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Python β€” Memory Implementation
# =============================================
# Agent Memory System
# =============================================
from collections import deque
from datetime import datetime

class AgentMemory:
    def __init__(self, max_short_term=20):
        # Short-term: conversation window
        self.short_term = deque(maxlen=max_short_term)
        # Long-term: persistent facts
        self.long_term = {}
        # Working: current task context
        self.working = {}
    
    def add_message(self, role, content):
        self.short_term.append({
            "role": role, "content": content,
            "timestamp": datetime.now().isoformat()
        })
    
    def add_fact(self, key, value):
        self.long_term[key] = {"value": value, "updated": datetime.now()}
    
    def get_context(self):
        return list(self.short_term)
    
    def get_fact(self, key):
        return self.long_term.get(key, {}).get("value")
    
    def search_facts(self, query):
        results = []
        for key, val in self.long_term.items():
            if query.lower() in key.lower() or query.lower() in str(val).lower():
                results.append((key, val["value"]))
        return results

# Usage
memory = AgentMemory()
memory.add_message("user", "Nama saya Budi")
memory.add_message("assistant", "Halo Budi, ada yang bisa saya bantu?")
memory.add_fact("user_name", "Budi")
memory.add_fact("user_preference", "suka Python")

print(memory.get_fact("user_name"))  # "Budi"

4. Planning & Reasoning

Python β€” Planning Strategies
# =============================================
# Planning Strategies untuk Agent
# =============================================

# ----- 1. Plan-and-Execute -----
# Agent membuat rencana lengkap, lalu eksekusi satu per satu
PLAN_PROMPT = """Buat rencana langkah demi langkah untuk menyelesaikan tugas ini.
Setiap langkah harus spesifik dan bisa dieksekusi.

Tugas: {task}
Tools tersedia: {tools}

Format output:
1. [langkah 1]
2. [langkah 2]
...
"""

# ----- 2. ReAct (Reasoning + Acting) -----
# Pikir β†’ Aksi β†’ Observasi β†’ Ulangi
REACT_PROMPT = """Jawab pertanyaan menggunakan format berikut:

Thought: [pikirkan apa yang perlu dilakukan]
Action: [nama_tool]
Action Input: [input untuk tool]
Observation: [hasil dari tool]
... (ulangi sampai cukup informasi)
Thought: [pikirkan apakah sudah cukup]
Final Answer: [jawaban akhir]
"""

# ----- 3. Reflexion (Self-improvement) -----
# Setelah eksekusi, agent merefleksi dan memperbaiki
REFLECTION_PROMPT = """Evaluasi hasil eksekusi berikut:
Tugas: {task}
Hasil: {result}
Error: {errors}

Apakah hasilnya memuaskan? Jika tidak, apa yang perlu diperbaiki?
"""

5. The Agent Loop

Python β€” Complete Agent Loop
# =============================================
# Complete Agent Loop
# =============================================
from openai import OpenAI
import json

class AIAgent:
    def __init__(self, tools, system_prompt, max_iterations=10):
        self.client = OpenAI()
        self.tools = tools
        self.system_prompt = system_prompt
        self.max_iterations = max_iterations
        self.memory = []
    
    def run(self, user_input):
        messages = [
            {"role": "system", "content": self.system_prompt},
            *self.memory,
            {"role": "user", "content": user_input}
        ]
        
        for i in range(self.max_iterations):
            response = self.client.chat.completions.create(
                model="gpt-4o",
                messages=messages,
                tools=self.tools,
                tool_choice="auto"
            )
            
            msg = response.choices[0].message
            
            # If LLM wants to use a tool
            if msg.tool_calls:
                messages.append(msg)
                
                for tool_call in msg.tool_calls:
                    result = self._execute_tool(
                        tool_call.function.name,
                        json.loads(tool_call.function.arguments)
                    )
                    messages.append({
                        "role": "tool",
                        "tool_call_id": tool_call.id,
                        "content": json.dumps(result)
                    })
                
                continue  # Loop again
            
            # Final answer
            answer = msg.content
            self.memory.append({"role": "user", "content": user_input})
            self.memory.append({"role": "assistant", "content": answer})
            return answer
        
        return "Mencapai batas iterasi maksimum."
    
    def _execute_tool(self, name, args):
        tool_map = {t["function"]["name"]: t for t in self.tools}
        # Execute based on tool name
        # ... implementasi masing-masing tool
        return {"status": "success", "result": "..."}

# Usage
agent = AIAgent(tools=tools, system_prompt="Anda adalah asisten AI...")
result = agent.run("Berapa harga emas hari ini dalam Rupiah?")
print(result)

6. Multi-Agent Systems

Multi-Agent menggunakan beberapa agent yang saling bekerja sama, masing-masing dengan spesialisasi berbeda.

Diagram: Multi-Agent Patterns
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                  MULTI-AGENT PATTERNS                            β”‚
β”‚                                                                  β”‚
β”‚  1. ORCHESTRATOR (Supervisor)                                   β”‚
β”‚     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                                β”‚
β”‚     β”‚Supervisorβ”‚β†’ Agent A (research)                            β”‚
β”‚     β”‚(routes)  β”‚β†’ Agent B (code)                                β”‚
β”‚     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β†’ Agent C (writing)                             β”‚
β”‚                                                                  β”‚
β”‚  2. PIPELINE (Sequential)                                       β”‚
β”‚     Agent A β†’ Agent B β†’ Agent C β†’ Output                       β”‚
β”‚     (research)  (analyze)  (write)                              β”‚
β”‚                                                                  β”‚
β”‚  3. DEBATE (Collaborative)                                      β”‚
β”‚     Agent A ←→ Agent B ←→ Agent C                              β”‚
β”‚     (diskusi sampai mencapai konsensus)                         β”‚
β”‚                                                                  β”‚
β”‚  4. HIERARCHICAL                                                β”‚
β”‚     Manager Agent                                                β”‚
β”‚     β”œβ”€β”€ Team Lead A β†’ Worker 1, Worker 2                       β”‚
β”‚     └── Team Lead B β†’ Worker 3, Worker 4                       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Python β€” Multi-Agent dengan LangGraph
# =============================================
# Multi-Agent dengan LangGraph
# =============================================
# pip install langgraph langchain-openai

from langgraph.graph import StateGraph, END
from typing import TypedDict, Annotated

class AgentState(TypedDict):
    task: str
    research: str
    code: str
    final_output: str

def researcher(state):
    """Agent yang mencari informasi."""
    # Call LLM for research
    return {"research": "Hasil riset tentang topik..."}

def coder(state):
    """Agent yang menulis kode."""
    research = state["research"]
    return {"code": "import pandas as pd\n..."}

def reviewer(state):
    """Agent yang mereview hasil."""
    return {"final_output": "Review selesai, kode valid."}

# Build graph
graph = StateGraph(AgentState)
graph.add_node("researcher", researcher)
graph.add_node("coder", coder)
graph.add_node("reviewer", reviewer)

graph.set_entry_point("researcher")
graph.add_edge("researcher", "coder")
graph.add_edge("coder", "reviewer")
graph.add_edge("reviewer", END)

# Compile & run
app = graph.compile()
result = app.invoke({"task": "Buat dashboard penjualan"})

7. Guardrails & Safety

Python β€” Agent Guardrails
# =============================================
# Guardrails untuk AI Agent
# =============================================

class AgentGuardrails:
    def __init__(self):
        self.max_iterations = 10
        self.blocked_actions = ["delete_database", "send_money"]
        self.max_tool_calls = 20
        self.allowed_domains = ["api.example.com", "database.internal"]
    
    def check_input(self, user_input):
        """Filter input berbahaya."""
        blocked = ["ignore instructions", "system prompt", "jailbreak"]
        for b in blocked:
            if b in user_input.lower():
                return False, "Input ditolak: mengandung instruksi terlarang"
        return True, "OK"
    
    def check_tool_call(self, tool_name, args):
        """Validasi sebelum eksekusi tool."""
        if tool_name in self.blocked_actions:
            return False, f"Tool '{tool_name}' diblokir"
        if tool_name == "http_request":
            url = args.get("url", "")
            if not any(d in url for d in self.allowed_domains):
                return False, f"Domain tidak diizinkan: {url}"
        return True, "OK"
    
    def check_output(self, output):
        """Filter output berbahaya."""
        sensitive = ["password", "api_key", "secret"]
        for s in sensitive:
            if s in output.lower():
                return False, "Output mengandung informasi sensitif"
        return True, "OK"

# Usage dalam agent loop
guardrails = AgentGuardrails()
ok, msg = guardrails.check_input(user_input)
if not ok:
    print(f"Ditolak: {msg}")

8. Evaluasi Agent

MetricPenjelasanCara Ukur
Task Completion RateBerapa % tugas berhasil diselesaikanCount success/total
AccuracyKebenaran jawaban akhirGround truth comparison
EfficiencyJumlah langkah/tool callsCount iterations
SafetyTidak melanggar guardrailsLog analysis
LatencyWaktu total eksekusiTimer
CostTotal token/cost APIToken counter

9. Agent Frameworks

FrameworkKeunggulanCocok Untuk
LangChain/LangGraphEkosistem lengkap, banyak integrasiProduction, complex agents
AutoGen (Microsoft)Multi-agent conversationResearch, collaboration
CrewAIRole-based multi-agent, mudahTeam-based agents
OpenAI AssistantsBuilt-in tools, managedQuick prototyping
PhidataClean API, production-readyProduction single-agent

10. Quiz Pemahaman

1. Apa perbedaan utama chatbot dan AI Agent?

2. Apa fungsi dari tool use dalam AI Agent?

3. Mengapa guardrails penting untuk AI Agent?

4. Apa itu ReAct pattern?

5. Apa keunggulan multi-agent dibanding single agent?

Rangkuman

πŸ“ Poin Penting
  • AI Agent β€” sistem otonom dengan LLM sebagai otak + tools + memory
  • Tool Use β€” function calling memungkinkan agent berinteraksi dengan dunia luar
  • Memory β€” short-term (conversation) + long-term (facts, vector DB)
  • Planning β€” ReAct, Plan-and-Execute, Reflexion
  • Multi-Agent β€” beberapa agent bekerja sama dengan spesialisasi berbeda
  • Guardrails β€” batasan keamanan untuk input, tool use, dan output
  • Frameworks β€” LangGraph, AutoGen, CrewAI, OpenAI Assistants