본문 바로가기
C.W.K.
Stream
Lesson 06 of 07 · published

Tool Error Handling — 모델이 recover 하게

~22 min · tool-errors, recovery

Level 0Tokenizer
0 XP0/54 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

tool 이 실패했다고 agent 전체를 중단하지 마. exception 을 잡아 {error: 'rate-limited', detail: '...'} 같은 결과로 반환하면 모델이 다른 tool 을 시도하거나 사용자에게 필요한 정보를 묻거나 안전하게 멈출 수 있어.

중단과 오류 반환은 결과가 달라

agent 를 중단하면 사용자는 막연한 실패 메시지만 보게 돼. 오류를 tool 결과로 반환하면 모델이 fallback 을 선택할 기회를 얻어. 같은 실패도 처리 방식에 따라 사용자 경험이 크게 달라져.

traceback 에서 secret 을 제거해

exception traceback 에는 API key, 내부 hostname, query string 이 들어갈 수 있어. 그대로 모델에 보내지 말고 안전한 내용만 남겨 짧게 요약해. 사용자에게 공개하는 로그와 같은 위생 기준을 적용해.

구조화한 오류로 다음 행동을 알려줘

{error: '<class>', message: '<short>', detail: '<safe>'} 를 기본 형태로 쓸 수 있어. error class 가 rate-limited 면 기다리고, auth 면 다른 경로를 찾고, not-found 면 사용자에게 확인하도록 모델이 판단할 수 있어. detail 에는 secret 을 넣지 마.

Code

Structured tool error 반환·python
for tc in tool_calls:
    try:
        func = TOOLS_MAP[tc.name]
        result = func(**json.loads(tc.arguments))
        output = json.dumps(result)
    except KeyError:
        output = json.dumps({"error": f"Unknown tool: {tc.name}"})
    except json.JSONDecodeError:
        output = json.dumps({"error": "Invalid JSON in tool arguments"})
    except TimeoutError:
        output = json.dumps({"error": f"Tool {tc.name} timed out"})
    except Exception as e:
        output = json.dumps({"error": str(e), "success": False})

    input_items.append({
        "type": "function_call_output",
        "call_id": tc.call_id,
        "output": output,  # always return JSON, even for errors
    })

External links

Exercise

tool loop 의 handler 하나에서 일부러 exception 을 내. (a) agent 를 중단하는 방식과 (b) exception 을 잡아 {error,...}를 반환하는 방식에서 assistant 의 다음 행동을 비교해.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.