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

Claude Tool Use: tool_use와 tool_result

~34 min · anthropic, claude, tool-use

Level 0Observer
0 XP0/40 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete

Claude는 tool을 message 안에 통합해

Claude Messages API에서는 tool call이 content block으로 표현돼. assistant가 `tool_use` block을 돌려주면, 다음 user message에 같은 `tool_use_id`를 가진 `tool_result` block을 넣는다. 루프는 같고 wire format만 다른 거야.

이 구조는 이해하면 깔끔하지만 ordering에는 엄격해. tool result는 protocol이 기대하는 위치에 있어야 한다. tool_use 바로 다음 흐름에서 result를 분리해버리면 API가 화낸다. 그리고 그건 API가 맞아.

Client tools vs server tools

Client tool은 우리 시스템에서 실행돼. schema도 우리가 정의하고 action도 우리가 구현한다. Server tool은 Anthropic 쪽에서 실행되는 hosted capability야. safety model이 다르니까 같은 것처럼 취급하면 안 돼.

Tool choice는 운전대

`auto`는 Claude가 고르게 하고, `any`는 어떤 tool이든 반드시 쓰게 하고, named tool은 특정 tool을 강제하고, `none`은 tool use를 꺼. 강제는 조심해서 써야 해. 너무 자주 강제하면 evidence가 아니라 architecture에 복종하는 시스템을 만들게 된다.

Code

Claude tool_use / tool_result loop·python
import anthropic, json

client = anthropic.Anthropic()

tools = [{
    "name": "lookup_order",
    "description": "Look up one order by id. Use only for order status questions.",
    "input_schema": {
        "type": "object",
        "properties": {
            "order_id": {"type": "string", "description": "Public order id, e.g. ORD-1042"}
        },
        "required": ["order_id"],
    },
}]

messages = [{"role": "user", "content": "Where is ORD-1042?"}]

response = client.messages.create(
    model="claude-sonnet-4-5",
    max_tokens=1024,
    tools=tools,
    messages=messages,
)

messages.append({"role": "assistant", "content": response.content})
tool_results = []
for block in response.content:
    if block.type == "tool_use":
        result = {"order_id": block.input["order_id"], "status": "shipped"}
        tool_results.append({
            "type": "tool_result",
            "tool_use_id": block.id,
            "content": json.dumps(result),
        })

messages.append({"role": "user", "content": tool_results})
final = client.messages.create(
    model="claude-sonnet-4-5",
    max_tokens=1024,
    tools=tools,
    messages=messages,
)

External links

Exercise

이전 lesson의 search_notes tool을 Claude의 input_schema 형식으로 다시 써봐. 그리고 결과를 돌려줄 tool_result message shape도 적어.
Hint
결과에는 Claude가 돌려준 block의 tool_use_id가 필요해.

Progress

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

댓글 0

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

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