Skip to content

Tool Definitions vs Tool Results: İki Farklı Cache Kararı

Tool definitions statik cache'lenir; tool results dinamik. Ama tool result'lar context'te birikiyor — onlar history cache'inin parçası. Pattern netleştirmesi.

Şükrü Yusuf KAYA
11 min read
Advanced

Tool Definitions vs Tool Results

Agent caching'de iki farklı veri tipi var:
  1. Tool Definitions — function schema'ları, isim, açıklama
  2. Tool Results — function dönüşleri, yürütme çıktıları

Tool Definitions#

Statik: Kod release ile değişir Boyut: ~300 token/tool × 20 tool = 6K Cache stratejisi: Tools array son elemana cache_control, 1h TTL Yer: API'nin
tools
parametresinde Lifetime: Aylar

Tool Results#

Dinamik: Her çağrıda farklı Boyut: ~500-5K per result Cache stratejisi: Conversation history içinde, incremental cache Yer: Messages array içinde (assistant ve tool_result mesajları) Lifetime: Conversation süresi

Anthropic API Yapısı#

Anthropic'te tool results assistant ve user mesajları içinde geçer:
python
messages = [
{"role": "user", "content": "Sipariş ver: kahve makinesi İstanbul'a"},
 
# Assistant: tool çağrısı yapar
{
"role": "assistant",
"content": [
{"type": "tool_use", "id": "tool_1", "name": "search_product", "input": {"q": "kahve makinesi"}}
],
},
 
# User: tool result döner
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "tool_1",
"content": "Bulunan ürünler: ...", # ← bu kısım büyük olabilir
}
],
},
 
# Assistant: bir sonraki tool
{"role": "assistant", "content": [{"type": "tool_use", "name": "get_details", ...}]},
 
# User: result
{"role": "user", "content": [{"type": "tool_result", "tool_use_id": "tool_2", "content": "..."}]},
 
# ... agent loop devam eder
]
Agent loop mesaj yapısı

Cache Stratejisi: Growing Prefix#

Tool result'lar tarih sırasıyla mesajlar arasında biriker. Cache'leme:
  • Her N adımda 1: son tool_result mesajına cache_control breakpoint
  • TTL: 5m (active agent'lar için)
  • 4 breakpoint limit'i nedeniyle agent çok adımlıysa rotation gerekir
python
def cache_recent_tool_result(messages, max_breakpoints=4):
"""Son N tool_result mesajına breakpoint koy (eski breakpoint'leri kaldır)."""
# Tool_result mesajlarını bul
tool_result_indices = [
i for i, m in enumerate(messages)
if isinstance(m.get("content"), list)
and any(c.get("type") == "tool_result" for c in m["content"])
]
 
# Sadece son max_breakpoints kadarına cache_control koy
breakpoints_to_use = tool_result_indices[-max_breakpoints:]
 
for i in breakpoints_to_use:
messages[i]["content"][-1]["cache_control"] = {"type": "ephemeral", "ttl": "5m"}
 
return messages
Cache breakpoint rotation — son 4 tool_result
4 Breakpoint Allocation
Optimum pattern: 1 breakpoint system+tools'a, 3 breakpoint son tool result'lara. Bu sayede 20+ adımlık agent'larda hâlâ %90+ hit rate.

✓ Pekiştir#

Bir Sonraki Derste#

Anthropic Skills + caching pattern'i. Yeni bir agentic primitiv.

Yorumlar & Soru-Cevap

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

Related Content