The whole hunk
from line 2078, old and new numbered
/
lines
from line 2078
20782078<CodeGroup exclude="shell">
20792079 ```python Python
20802080 def handle_response(response):
2081 if response.stop_reason == "tool_use":
2082 return handle_tool_use(response)
2083 elif response.stop_reason == "max_tokens":
2084 return handle_truncation(response)
2085 elif response.stop_reason == "model_context_window_exceeded":
2086 return handle_context_limit(response)
2087 elif response.stop_reason == "pause_turn":
2088 return handle_pause(response)
2089 elif response.stop_reason == "refusal":
2090 return handle_refusal(response)
2091 else:
2092 # Handle end_turn and other cases
2093 return next(
2094 (block.text for block in response.content if block.type == "text"), ""
2095 )
2081 match response.stop_reason:
2082 case "tool_use":
2083 return handle_tool_use(response)
2084 case "max_tokens":
2085 return handle_truncation(response)
2086 case "model_context_window_exceeded":
2087 return handle_context_limit(response)
2088 case "pause_turn":
2089 return handle_pause(response)
2090 case "refusal":
2091 return handle_refusal(response)
2092 case _:
2093 # Handle end_turn and other cases
2094 return next(
2095 (block.text for block in response.content if block.type == "text"),
2096 "",
2097 )
20962098 ```
20972099
20982100 ```typescript TypeScript
from line 2655
26532655
26542656 except anthropic.APIStatusError as e:
26552657 # Handle actual errors
2656 if e.status_code == 429:
2657 print("Rate limit exceeded")
2658 elif e.status_code == 500:
2659 print("Server error")
2658 match e.status_code:
2659 case 429:
2660 print("Rate limit exceeded")
2661 case 500:
2662 print("Server error")
26602663 ```
26612664
26622665 ```typescript TypeScript
from line 2679
26762679 } catch (err) {
26772680 // Handle actual errors
26782681 if (err instanceof Anthropic.APIError) {
2679 if (err.status === 429) {
2680 console.log("Rate limit exceeded");
2681 } else if (err.status === 500) {
2682 console.log("Server error");
2682 switch (err.status) {
2683 case 429:
2684 console.log("Rate limit exceeded");
2685 break;
2686 case 500:
2687 console.log("Server error");
2688 break;
26832689 }
26842690 } else {
26852691 throw err;
from line 2965
29592965 );
29602966
29612967 foreach ($stream as $event) {
2962 if ($event instanceof RawMessageDeltaEvent && $event->delta->stopReason !== null) {
2968 if ($event instanceof \Anthropic\Messages\RawMessageDeltaEvent && $event->delta->stopReason !== null) {
29632969 echo "Stream ended with: {$event->delta->stopReason}", PHP_EOL;
29642970 }
29652971 }
from line 3464
34583464 max_tokens=20000, # Python SDK requires streaming for max_tokens above ~21k
34593465 )
34603466
3461 if response.stop_reason == "model_context_window_exceeded":
3462 # Got the maximum possible tokens given input size
3463 print(
3464 f"Generated {response.usage.output_tokens} tokens (context limit reached)"
3465 )
3466 elif response.stop_reason == "max_tokens":
3467 # Got exactly the requested tokens
3468 print(f"Generated {response.usage.output_tokens} tokens (max_tokens reached)")
3469 else:
3470 # Natural completion
3471 print(f"Generated {response.usage.output_tokens} tokens (natural completion)")
3467 match response.stop_reason:
3468 case "model_context_window_exceeded":
3469 # Got the maximum possible tokens given input size
3470 print(
3471 f"Generated {response.usage.output_tokens} tokens (context limit reached)"
3472 )
3473 case "max_tokens":
3474 # Got exactly the requested tokens
3475 print(
3476 f"Generated {response.usage.output_tokens} tokens (max_tokens reached)"
3477 )
3478 case _:
3479 # Natural completion
3480 print(
3481 f"Generated {response.usage.output_tokens} tokens (natural completion)"
3482 )
34723483
34733484 return next((block.text for block in response.content if block.type == "text"), "")
34743485 ```
from line 3493
34823493 });
34833494
34843495 const tokens = response.usage.output_tokens;
3485 if (response.stop_reason === "model_context_window_exceeded") {
3486 // Got the maximum possible tokens given input size
3487 console.log(`Generated ${tokens} tokens (context limit reached)`);
3488 } else if (response.stop_reason === "max_tokens") {
3489 // Got exactly the requested tokens
3490 console.log(`Generated ${tokens} tokens (max_tokens reached)`);
3491 } else {
3492 // Natural completion
3493 console.log(`Generated ${tokens} tokens (natural completion)`);
3496 switch (response.stop_reason) {
3497 case "model_context_window_exceeded":
3498 // Got the maximum possible tokens given input size
3499 console.log(`Generated ${tokens} tokens (context limit reached)`);
3500 break;
3501 case "max_tokens":
3502 // Got exactly the requested tokens
3503 console.log(`Generated ${tokens} tokens (max_tokens reached)`);
3504 break;
3505 default:
3506 // Natural completion
3507 console.log(`Generated ${tokens} tokens (natural completion)`);
34943508 }
34953509
34963510 const textBlock = response.content.find(