SPB Git forge

spb/doc-api

Public
2commits 1branches 0releases
15.7 MBsize
maindefault branch
14 days agolast push
Python 88.3% TypeScript 7.6% Shell 4.1%
2.9 KB · 49 lines python
Raw Blame History
1"""Extended thinking + tool use round trip (Python SDK): turn 1 returns [thinking, tool_use]; turn 2 passes the2assistant content back UNCHANGED (thinking block + signature) with the tool_result. Also streams a request to show3thinking_delta / signature_delta events.4STATUS: LIVE_VERIFIED 2026-09-18 (claude-haiku-4-5-20251001): turn1 stop_reason tool_use (47 thinking tokens),5turn2 200 text answer; stream: 10 thinking_delta + 1 signature_delta. ≈$0.002.6Run: .venv/bin/python examples/anthropic/thinking/thinking_tool_roundtrip.py7"""8from __future__ import annotations910import os11import sys12from pathlib import Path1314sys.path.insert(0, str(Path(__file__).resolve().parents[3]))15from scripts import live  # noqa: E4021617import anthropic  # noqa: E4021819MODEL = os.environ.get("ANTHROPIC_MODEL", "claude-haiku-4-5-20251001")20client = anthropic.Anthropic(max_retries=1)21THINKING = {"type": "enabled", "budget_tokens": 1024}22TOOL = {"name": "get_time", "description": "Returns the current UTC time.", "input_schema": {"type": "object", "properties": {}}}23ask = "Use the get_time tool, then tell me the time in one short sentence."2425turn1 = client.messages.create(model=MODEL, max_tokens=1500, thinking=THINKING, tools=[TOOL],26                               messages=[{"role": "user", "content": ask}])27print("turn1:", [b.type for b in turn1.content], turn1.stop_reason, turn1.usage.output_tokens_details)28live.log_request("anthropic", "POST", "/v1/messages", 200, 0.001, f"example thinking/thinking_tool_roundtrip.py turn1 {MODEL}")29tool_use = next((b for b in turn1.content if b.type == "tool_use"), None)30if tool_use:31    turn2 = client.messages.create(model=MODEL, max_tokens=1500, thinking=THINKING, tools=[TOOL], messages=[32        {"role": "user", "content": ask},33        {"role": "assistant", "content": turn1.content},  # thinking + tool_use blocks, verbatim (required)34        {"role": "user", "content": [{"type": "tool_result", "tool_use_id": tool_use.id, "content": "12:00:00 UTC"}]},35    ])36    print("turn2:", [b.type for b in turn2.content], turn2.content[-1].text if turn2.content[-1].type == "text" else "")37    live.log_request("anthropic", "POST", "/v1/messages", 200, 0.001, f"example thinking/thinking_tool_roundtrip.py turn2 {MODEL}")3839# Streaming: thinking_delta events then a single signature_delta before content_block_stop.40kinds: list[str] = []41with client.messages.stream(model=MODEL, max_tokens=1200, thinking=THINKING,42                            messages=[{"role": "user", "content": "What is 2+2? Reply with the number."}]) as stream:43    for ev in stream:44        if ev.type == "content_block_delta":45            kinds.append(ev.delta.type)46    final = stream.get_final_message()47print("stream deltas:", kinds, "| final:", [b.type for b in final.content], final.usage.output_tokens_details)48live.log_request("anthropic", "POST", "/v1/messages", 200, 0.0003, f"example thinking/thinking_tool_roundtrip.py stream {MODEL}")49