import anthropic
client = anthropic.Anthropic()
# 第一個帶有網頁搜尋和快取中斷點的請求
messages = [
{
"role": "user",
"content": "What's the current weather in San Francisco today?"
}
]
response1 = client.messages.create(
model="claude-opus-4-1-20250805",
max_tokens=1024,
messages=messages,
tools=[{
"type": "web_search_20250305",
"name": "web_search",
"user_location": {
"type": "approximate",
"city": "San Francisco",
"region": "California",
"country": "US",
"timezone": "America/Los_Angeles"
}
}]
)
# 將 Claude 的回應添加到對話中
messages.append({
"role": "assistant",
"content": response1.content
})
# 第二個在搜尋結果後帶有快取中斷點的請求
messages.append({
"role": "user",
"content": "Should I expect rain later this week?",
"cache_control": {"type": "ephemeral"} # 快取到此點
})
response2 = client.messages.create(
model="claude-opus-4-1-20250805",
max_tokens=1024,
messages=messages,
tools=[{
"type": "web_search_20250305",
"name": "web_search",
"user_location": {
"type": "approximate",
"city": "San Francisco",
"region": "California",
"country": "US",
"timezone": "America/Los_Angeles"
}
}]
)
# 第二個回應將受益於快取的搜尋結果
# 同時仍能在需要時執行新的搜尋
print(f"Cache read tokens: {response2.usage.get('cache_read_input_tokens', 0)}")