Skip to content

Tool Use API: Function Calling in Practice

Finish tool use over the API: full loop, parallel tools, error feedback, and schema validation.

Şükrü Yusuf KAYA
12 min read
Intermediate
Tool use loop API perspektifi

Tam Loop

python
import json
 
def run_tool(name, args):
if name == "get_weather":
return {"temp_c": 18, "cond": "yağmurlu"}
raise ValueError(f"Unknown tool: {name}")
 
messages = [{"role":"user","content":"İstanbul'da bugün ne giyeyim?"}]
tools = [{
"name":"get_weather",
"description":"...",
"input_schema":{"type":"object","properties":{"city":{"type":"string"}},"required":["city"]}
}]
 
while True:
resp = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
tools=tools,
messages=messages,
)
if resp.stop_reason != "tool_use":
print(resp.content[0].text)
break
 
# Modelin tüm tool çağrılarını işle
tool_results = []
for block in resp.content:
if block.type == "tool_use":
try:
result = run_tool(block.name, block.input)
tool_results.append({
"type":"tool_result",
"tool_use_id": block.id,
"content": json.dumps(result, ensure_ascii=False),
})
except Exception as e:
tool_results.append({
"type":"tool_result",
"tool_use_id": block.id,
"is_error": True,
"content": str(e),
})
 
# Konuşmaya assistant + user(tool_result) ekle
messages.append({"role":"assistant","content": resp.content})
messages.append({"role":"user","content": tool_results})
Tool use tam loop — error feedback dahil.
Boşluk doldur · text
Tool çıktısını döndürürken tipik tip _____ , id alanı ise _____ olarak gönderilir. Hata olduğunda _____ true ile feedback ver.

Frequently Asked Questions

Tool input streams; results are produced by you. The final assistant answer typically streams.

Yorumlar & Soru-Cevap

(0)
Yorum yazmak için giriş yap.
Yorumlar yükleniyor...

Related Content

Connected pillar topics

Pillar topics this article maps to