2026/08/23 5

IX. AI Agent에 “기억과 기록” 기능을 붙이는 단계

# 1. 파일에 쓰고 읽기 with open("notes.txt", "w", encoding="utf-8") as f: f.write("에이전트 작동이 시작되었습니다. \n") f.write("tool: search \n") with open("notes.txt", "r", encoding="utf-8") as f: contents = f.read() print(contents) print("characters: ", len(contents)) #-------- 2. 로그 파일에 추가하기 with open("runlog.txt", "a", encoding="utf-8") as f: f.write("1단계 | searc..

Agentic AI/AI_AGENT 2026.08.23

VIII. Agent Loop + Tool Dispatcher 다음 단계인 예외 처리(Exception), 재시도(Retry), Logging 스터디

# 1. KeyError — 딕셔너리에 없는 키를 찾았을 때 # def get_units(record): # return record["units"] # get_units({"sku":"51001"}) # 결과: Exception has occurred: KeyError 'units' #-------- 2. try / except / else / finally def to_int(value): try: number = int(value) except ValueError: print(f" {value!r}을 숫자로 변환할 수 없습니다.") return None exce..

Agentic AI/AI_AGENT 2026.08.23

VII. Python 함수 → Tool 함수 → Agent의 Tool Dispatcher 구조

# 1. 함수의 기본값과 키워드 인수 def search(query, limit=3, safe=True): mode = "safe" if safe else "unfiltered" return f"searching '{query}' | limit={limit} | {mode}" print(search("환불정책")) print(search("환불정책", 10)) print(search("환불정책", limit=5, safe=False)) print(search(limit=1, query="환불정책")) #-------- 2. 함수 기본값의 함정 (mutable default) def add_bad(item, bucket=[]): ..

Agentic AI/AI_AGENT 2026.08.23

VI. Python의 반복문과 자료구조를 이용해 AI Agent의 실제 실행 흐름을 만드는 과정

# 1. history 순회 + range() history = [ {"role":"user","content":"제품번호 5001의 재고를 확인해라."}, {"role":"assistant", "content":"지금 찾아보고 있어요."}, {"role":"user", "content":"그 제품이 어느 창고로부터 배송되는지 확인해라."}, ] for message in history: print(f"{message['role']}: {message['content']}") print() for i in range(3): print(f"step {i}") #-------- 2. enumerate() + ..

Agentic AI/AI_AGENT 2026.08.23

V. AI Agent의 핵심 부품들 하나씩 만들어보기

# 1. Tool 실행과 안전장치 TOOLS = {"calculator": lambda a: eval_expr(a.get("expr",""))} def eval_expr(expr): return f"{expr}의 결과값" def run_tool(name, args, steps, max_steps): if steps >= max_steps: return "중지되었습니다. 단계 제한에 도달하였습니다." if name not in TOOLS: return f"중지되었습니다. {name}이라는 툴이 없습니다." if not isinstance(args, dict): return "중지..

Agentic AI/AI_AGENT 2026.08.23