# Tool Use API: Function Calling in Practice

> Source: https://sukruyusufkaya.com/en/learn/claude-ustaligi/tool-use-api
> Updated: 2026-05-11T13:48:35.152Z
> Category: Claude Ustalığı
> Module: 8. Programmatic Claude with the API
**TLDR:** Finish tool use over the API: full loop, parallel tools, error feedback, and schema validation.

# 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})
```

### Paralel tool çağrıları

Claude bir cevapta birden fazla tool çağrısı verebilir. Hepsini paralel koştur (asyncio / Promise.all), tool_result listesinin sırası önemli değildir; `tool_use_id` eşler.

### Error feedback

Tool çalışmadığında `is_error: true` ile mesaj gönder; Claude alternatif yolu dener veya kullanıcıya açıklar. 'Sus ve dua et' yerine 'şu sebeple olmadı' demek üretimde altın.

### Schema validation

Anthropic tool input'u büyük oranda schema'ya uydurur ama yine de **kendi kodunda valide et**: jsonschema (Python) veya zod (TS). Sürpriz değerlerde fallback davranışı tanımla.

**Boşluk doldurma egzersizi (text):**
```text
Tool çıktısını döndürürken tipik tip _____ , id alanı ise _____ olarak gönderilir. Hata olduğunda _____ true ile feedback ver.
```

> ✋ Kontrol noktası: `q-804-mc1`