Claude API · Shaping requests
Check why generation stopped
Check stop_reason before using the content: end_turn, max_tokens, tool_use, pause_turn or refusal.
if response.stop_reason == "max_tokens":
print("Truncated: raise max_tokens or stream the response")
elif response.stop_reason == "refusal":
print(response.stop_details.category)
Official Claude API documentation