-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathextended_thinking_example.rb
More file actions
131 lines (116 loc) · 4.26 KB
/
extended_thinking_example.rb
File metadata and controls
131 lines (116 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'bundler/setup'
require 'claude_agent_sdk'
# Example: Using max_thinking_tokens for extended thinking
# This feature allows Claude to use extended thinking for complex reasoning tasks.
# Extended thinking gives Claude more "thinking time" before responding.
#
# NOTE: This option is defined in ClaudeAgentOptions for API parity with Python SDK,
# but is not yet supported by the Claude Code CLI. This example demonstrates the
# intended usage and how to handle ThinkingBlock content when the feature becomes available.
puts "=== Extended Thinking Example ==="
puts "Demonstrating max_thinking_tokens for complex reasoning\n\n"
# Example 1: Math problem with extended thinking
puts "--- Example 1: Complex Math Problem ---"
puts "Enabling extended thinking for complex calculations\n"
options = ClaudeAgentSDK::ClaudeAgentOptions.new(
max_thinking_tokens: 10000, # Allow up to 10,000 tokens for thinking
max_turns: 1
)
ClaudeAgentSDK.query(
prompt: "Solve this step by step: If a train travels at 60 mph for 2.5 hours, " \
"then at 80 mph for 1.75 hours, what is the total distance traveled? " \
"Show your reasoning.",
options: options
) do |message|
case message
when ClaudeAgentSDK::AssistantMessage
message.content.each do |block|
case block
when ClaudeAgentSDK::ThinkingBlock
puts "[Thinking...]"
# The actual thinking content can be accessed if needed
puts " (#{block.thinking.length} chars of reasoning)"
when ClaudeAgentSDK::TextBlock
puts "\nClaude: #{block.text}"
end
end
when ClaudeAgentSDK::ResultMessage
puts "\n--- Result ---"
puts "Cost: $#{message.total_cost_usd}" if message.total_cost_usd
puts "Turns: #{message.num_turns}"
end
end
puts "\n" + "=" * 50 + "\n"
# Example 2: Logic puzzle with extended thinking
puts "\n--- Example 2: Logic Puzzle ---"
puts "Using extended thinking for a logic puzzle\n"
logic_options = ClaudeAgentSDK::ClaudeAgentOptions.new(
max_thinking_tokens: 15000, # More thinking for complex logic
max_turns: 1
)
ClaudeAgentSDK.query(
prompt: "Solve this logic puzzle: Three friends - Alice, Bob, and Carol - " \
"have different favorite colors (red, blue, green) and different pets " \
"(dog, cat, bird). Alice doesn't like red. The person with the cat likes blue. " \
"Carol has a bird. Bob doesn't have a dog. Who has what color and pet?",
options: logic_options
) do |message|
case message
when ClaudeAgentSDK::AssistantMessage
thinking_shown = false
message.content.each do |block|
case block
when ClaudeAgentSDK::ThinkingBlock
unless thinking_shown
puts "[Extended thinking enabled - Claude is reasoning through the puzzle...]"
thinking_shown = true
end
when ClaudeAgentSDK::TextBlock
puts "\nSolution:\n#{block.text}"
end
end
when ClaudeAgentSDK::ResultMessage
puts "\n--- Result ---"
puts "Cost: $#{message.total_cost_usd}" if message.total_cost_usd
end
end
puts "\n" + "=" * 50 + "\n"
# Example 3: Code analysis with extended thinking
puts "\n--- Example 3: Code Analysis ---"
puts "Using extended thinking for code analysis\n"
code_options = ClaudeAgentSDK::ClaudeAgentOptions.new(
max_thinking_tokens: 8000,
max_turns: 1
)
code_snippet = <<~CODE
def mystery(arr)
return arr if arr.length <= 1
pivot = arr[arr.length / 2]
left = arr.select { |x| x < pivot }
middle = arr.select { |x| x == pivot }
right = arr.select { |x| x > pivot }
mystery(left) + middle + mystery(right)
end
CODE
ClaudeAgentSDK.query(
prompt: "Analyze this Ruby code and explain what algorithm it implements, " \
"its time complexity, and any potential improvements:\n\n#{code_snippet}",
options: code_options
) do |message|
case message
when ClaudeAgentSDK::AssistantMessage
message.content.each do |block|
case block
when ClaudeAgentSDK::ThinkingBlock
puts "[Analyzing code with extended thinking...]"
when ClaudeAgentSDK::TextBlock
puts "\nAnalysis:\n#{block.text}"
end
end
when ClaudeAgentSDK::ResultMessage
puts "\n--- Result ---"
puts "Cost: $#{message.total_cost_usd}" if message.total_cost_usd
end
end