C.W.K.
Stream
Lesson 03 of 05 · published

Anthropic Messages — Tool 카테고리 셋

~22 min · anthropic, claude, tool-categories, computer-use

Level 0호기심 많은 독자
0 XP0/48 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

Anthropic 의 tool-use 스토리엔 — 나중에 갚는 — 작은 트위스트가 있어: tool 을 세 카테고리 로 나눠. 모두 같은 tools 배열에 정의되지만 모양이 달라.

  1. Custom tools — 네 tool, name, description, input_schema (JSON Schema) 로 정의. 95% 의 개발자가 처음 손대는 거고 다른 provider 와 동일해 보임.
  2. Anthropic-defined tools — Anthropic 이 ship 해서 모델이 native 로 아는 pre-built tool — computer_20250124, text_editor_20250124, bash_20250124. 타입으로 선언; Anthropic 이 schema 소유. 그 존재가 Claude 가 Claude Code 에서 — 액션 동사를 직접 가르치지 않고도 — 진짜 컴퓨터를 조작할 수 있는 이유야.
  3. MCP server tools — Anthropic API 가 MCP server 와 직접 대화할 수 있고, custom tool 옆에 같은 배열에 광고. MCP track 에서 다시 볼 거야.

기계적으로 Anthropic loop 는 깔끔: tool 과 메시지 보내고 content block 받음. Response 의 content 는 list — text block 은 type: "text", tool call 은 type: "tool_use"nameinput (진짜 dict, JSON 문자열 아님). Tool 결과는 user 메시지에 원본 tool_use.id 참조하는 tool_result block 으로 다시 보냄.

stop_reason 필드가 loop 의 권위 있는 신호: "end_turn" 은 모델 끝, "tool_use" 는 tool 호출, "max_tokens" 는 budget 소진. 거기서 분기 — 텍스트에서 절대 분기 X.

Code

Anthropic — custom tool 한 개로 round-trip·python
import anthropic

client = anthropic.Anthropic()
tools = [{
    "name": "get_weather",
    "description": "Get current weather for a city.",
    "input_schema": {
        "type": "object",
        "properties": {"location": {"type": "string"}},
        "required": ["location"],
    },
}]

def get_weather(location: str) -> str:
    return f"68°F and clear in {location}"

messages = [{"role": "user", "content": "Weather in Seoul?"}]
while True:
    resp = client.messages.create(
        model="claude-opus-4-7",
        max_tokens=1024,
        tools=tools,
        messages=messages,
    )
    messages.append({"role": "assistant", "content": resp.content})

    if resp.stop_reason == "end_turn":
        break

    results = []
    for block in resp.content:
        if block.type == "tool_use":
            output = get_weather(**block.input)
            results.append({"type": "tool_result", "tool_use_id": block.id, "content": output})
    messages.append({"role": "user", "content": results})

print(messages[-1]["content"])
Custom tool 과 Anthropic-defined tool 같은 배열에 섞기·python
tools = [
    {"type": "computer_20250124", "name": "computer", "display_width_px": 1024, "display_height_px": 768},
    {  # 네 tool, 옆에 나란히
        "name": "lookup_user",
        "description": "Find user by id.",
        "input_schema": {"type": "object", "properties": {"id": {"type": "string"}}, "required": ["id"]},
    },
]

External links

Exercise

Single custom tool 로 30 줄 Anthropic agent loop 짜. 같은 tools 배열에 Anthropic-defined tool (계정에 access 있는 text_editor 나 computer) 추가. 모델이 prompt 따라 둘 사이 고를 수 있는 거 확인. Loop 가 새 카테고리 지원하려고 안 바뀐 거 봐.

Progress

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

댓글 0

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

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