← All guides

Claude API tool_use_error (invalid_request_error): 원인과 해결법

Claude API tool_use_error invalid_request_error 해결: Tool use 형식 오류 · 재시도 비효과

Claude API tool_use_error (invalid_request_error): 원인과 해결법

Claude API tool_use_error invalid_request_error는 tool_choice / tool_result / tool_use 형식이 schema와 불일치에 발생합니다. Tool use 형식 오류이며, 재시도하지 말고 요청 자체를 수정해야 합니다. 이 글은 5가지 흔한 원인과 Python/TypeScript 코드 예시를 다룹니다.

전반적인 Claude API 에러 처리 패턴은 Claude API Error Handling 가이드를 참고하세요.


무엇을 의미하는가?

tool_use_error 에러 서브타입는 tool_choice / tool_result / tool_use 형식이 schema와 불일치을 의미합니다. Anthropic API의 에러 응답 본문에는 error.type"invalid_request_error"로 명시되며, error.message에 구체적 사유가 옵니다.

응답 예시:

{
  "type": "error",
  "error": {
    "type": "invalid_request_error",
    "message": "..."
  }
}

흔한 원인 5가지

  1. tool_use_id가 tool_result와 매칭 안 됨
  2. tool_choice를 specific tool로 했지만 해당 tool 미정의
  3. input_schema가 JSON Schema draft 2020-12 위반
  4. messages에 tool_use는 있는데 다음 turn에 tool_result 없음

해결 코드 (Python)

# Validate tool_use → tool_result pairing before sending
def validate_tool_pairing(messages):
    pending_tool_uses = []
    for msg in messages:
        if msg["role"] == "assistant":
            for block in msg.get("content", []):
                if isinstance(block, dict) and block.get("type") == "tool_use":
                    pending_tool_uses.append(block["id"])
        elif msg["role"] == "user":
            for block in msg.get("content", []):
                if isinstance(block, dict) and block.get("type") == "tool_result":
                    if block["tool_use_id"] in pending_tool_uses:
                        pending_tool_uses.remove(block["tool_use_id"])
    if pending_tool_uses:
        raise ValueError(f"Unmatched tool_use IDs: {pending_tool_uses}")

해결 코드 (TypeScript)

function validateToolPairing(messages) {
  const pending: string[] = [];
  for (const msg of messages) {
    if (msg.role === "assistant") {
      for (const b of msg.content ?? []) {
        if (b.type === "tool_use") pending.push(b.id);
      }
    } else if (msg.role === "user") {
      for (const b of msg.content ?? []) {
        if (b.type === "tool_result") {
          const idx = pending.indexOf(b.tool_use_id);
          if (idx >= 0) pending.splice(idx, 1);
        }
      }
    }
  }
  if (pending.length) throw new Error(`Unmatched: ${pending.join(",")}`);
}

재시도하지 마세요

이 에러는 클라이언트 측 문제라 재시도해도 같은 결과입니다. 위 원인을 확인하고 요청을 수정한 뒤 재발송하세요.


비용 영향

이 에러는 요청이 처리되지 않았으므로 비용이 청구되지 않습니다. 단, 잘못된 모델로 요청을 반복하다가 다른 에러가 발생할 수 있으니 모니터링이 필요합니다.

자세한 비용 절감 패턴은 Claude API Cost and Prompt Caching Break-Even 또는 무료 비용 계산기를 참고하세요.


관련 에러


자주 묻는 질문

tool_use_error 에러가 떴을 때 비용이 청구되나요?

처리되지 않은 요청이므로 청구되지 않습니다. 단, 무한 재시도 루프는 다른 에러로 비용을 발생시킬 수 있습니다.

tool_use_error와 다른 에러의 차이는?

tool_use_error는 클라이언트 측 문제로 요청 수정 없이는 재시도해도 같은 결과입니다. 5xx 에러 (500/529)는 일시적 서버 이슈라 재시도가 효과적입니다.

Bedrock/Vertex에서도 같은 에러 코드인가요?

네, Anthropic의 error.type 명명 규칙은 모든 deployment (Direct API, AWS Bedrock, GCP Vertex AI)에서 동일합니다. 다만 HTTP 상태 코드는 platform 별로 wrapping되어 다를 수 있습니다 (예: Bedrock은 에러 type을 다른 prefix로 변경).


다음 단계

에러 처리 패턴 30개 + Pydantic 검증 코드Claude API Cost Optimization 마스터클래스 ($59)에 retry 미들웨어, 비용 가드레일, 에러 알림 패턴 12편 포함.

Tools and references