20

LangGraph State 설계: 제조 워크플로우 패턴

Day 3: LangGraph 멀티에이전트

학습 목표

LangGraph State를 제조 워크플로우에 맞게 설계하는 방법을 이해한다 Conditional Edge로 분기하는 고장 진단 그래프를 설계할 수 있다 Checkpoint를 활용한 워크플로우 이력 관리를 이해한다

LangGraph State 설계 가이드

제조 진단 State 설계

from typing import TypedDict, List, Optional, Literal
from langgraph.graph import StateGraph, END

class ManufacturingDiagnosticState(TypedDict):
    # 입력
    equipment_id: str
    symptom: str

    # 진단 결과
    sensor_data: Optional[dict]    # MES 센서 값
    alarm_codes: Optional[List[str]]  # 활성 알람
    maintenance_history: Optional[List[dict]]  # 정비 이력

    # 분석
    probable_causes: Optional[List[str]]  # 추정 원인
    recommended_action: Optional[str]  # 권고 조치
    urgency: Optional[Literal["urgent", "normal", "monitor"]]

    # 최종
    final_report: Optional[str]
    requires_human: bool  # 사람 검토 필요 여부

State 설계 원칙:

  • Optional 필드는 각 Node에서 순차적으로 채움
  • 불변 데이터(장비 ID)와 가변 데이터(진단 결과) 분리
  • requires_human 같은 플래그로 Human-in-the-Loop 구현

조건부 분기 설계

def route_by_urgency(state: ManufacturingDiagnosticState):
    """긴급도에 따라 다음 노드를 결정"""
    if state["requires_human"]:
        return "human_review"  # 사람 검토 필요
    elif state["urgency"] == "urgent":
        return "escalate"      # 즉시 에스컬레이션
    elif state["urgency"] == "normal":
        return "create_work_order"  # 정비 요청 생성
    else:
        return "monitor"           # 모니터링 지속

graph = StateGraph(ManufacturingDiagnosticState)
graph.add_conditional_edges(
    "analyze_fault",  # 이 노드 실행 후
    route_by_urgency,  # 이 함수로 다음 노드 결정
    {
        "human_review": "human_review_node",
        "escalate": "escalate_node",
        "create_work_order": "work_order_node",
        "monitor": "monitor_node"
    }
)

Checkpoint: 워크플로우 이력

from langgraph.checkpoint.memory import MemorySaver

# 메모리 기반 Checkpoint
checkpointer = MemorySaver()
app = graph.compile(checkpointer=checkpointer)

# 실행 (thread_id로 대화 이력 관리)
config = {"configurable": {"thread_id": "CNC-M500-incident-001"}}
result = app.invoke({"equipment_id": "CNC-M500", "symptom": "주축 진동"}, config)

# 이후 같은 thread_id로 이어서 대화
result2 = app.invoke({"symptom": "이번엔 온도도 상승"}, config)
# → 이전 진단 결과를 기억하고 계속 분석

실무 가치: 동일 설비의 연속 이상 신호를 하나의 이력으로 관리하면, 누적 데이터 기반의 정확한 진단이 가능하다.

AI로 학습하기 — 꿀팁
🤖고장 진단 StateGraph 설계 코드 생성AI 학습 팁

제조 고장 진단 워크플로우를 LangGraph State로 설계하는 코드 템플릿을 생성하면 즉시 실습에 활용할 수 있습니다.

CNC 머신 고장 진단을 위한 LangGraph StateGraph를 Python으로 설계해줘. State에는 sensor_data, fault_code, diagnosis, action_taken 필드를 포함하고, 데이터 수집→1차 진단→심층 분석→조치 결정 흐름에 Conditional Edge로 분기(즉각 정지 vs 점검 예약 vs 정상 운영)하는 완성 코드를 작성해줘.
이 팁이 도움이 됐나요?
핵심 포인트
  • State에 urgency, requires_human 같은 플래그를 포함하면 조건부 분기 설계가 자연스러워진다
  • Conditional Edge의 라우팅 함수는 State를 읽어 다음 Node를 반환하는 단순 함수다
  • Checkpoint의 thread_id로 동일 설비의 연속 이상 신호를 하나의 이력으로 추적할 수 있다