anthropic 1.15.2 → 1.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (153) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -0
  3. data/README.md +10 -10
  4. data/lib/anthropic/helpers/tools/compaction_control.rb +105 -0
  5. data/lib/anthropic/helpers/tools/runner.rb +126 -13
  6. data/lib/anthropic/models/anthropic_beta.rb +3 -0
  7. data/lib/anthropic/models/beta/beta_code_execution_tool_20250522.rb +28 -1
  8. data/lib/anthropic/models/beta/beta_code_execution_tool_20250825.rb +28 -1
  9. data/lib/anthropic/models/beta/beta_content_block.rb +3 -1
  10. data/lib/anthropic/models/beta/beta_content_block_param.rb +3 -1
  11. data/lib/anthropic/models/beta/beta_direct_caller.rb +21 -0
  12. data/lib/anthropic/models/beta/beta_mcp_tool_config.rb +27 -0
  13. data/lib/anthropic/models/beta/beta_mcp_tool_default_config.rb +27 -0
  14. data/lib/anthropic/models/beta/beta_mcp_toolset.rb +58 -0
  15. data/lib/anthropic/models/beta/beta_memory_tool_20250818.rb +36 -1
  16. data/lib/anthropic/models/beta/beta_message.rb +2 -2
  17. data/lib/anthropic/models/beta/beta_message_param.rb +3 -3
  18. data/lib/anthropic/models/beta/beta_output_config.rb +34 -0
  19. data/lib/anthropic/models/beta/beta_raw_content_block_start_event.rb +5 -3
  20. data/lib/anthropic/models/beta/beta_server_tool_caller.rb +27 -0
  21. data/lib/anthropic/models/beta/beta_server_tool_use_block.rb +32 -1
  22. data/lib/anthropic/models/beta/beta_server_tool_use_block_param.rb +33 -1
  23. data/lib/anthropic/models/beta/beta_tool.rb +36 -1
  24. data/lib/anthropic/models/beta/beta_tool_bash_20241022.rb +36 -1
  25. data/lib/anthropic/models/beta/beta_tool_bash_20250124.rb +36 -1
  26. data/lib/anthropic/models/beta/beta_tool_computer_use_20241022.rb +36 -1
  27. data/lib/anthropic/models/beta/beta_tool_computer_use_20250124.rb +36 -1
  28. data/lib/anthropic/models/beta/beta_tool_computer_use_20251124.rb +114 -0
  29. data/lib/anthropic/models/beta/beta_tool_reference_block.rb +25 -0
  30. data/lib/anthropic/models/beta/beta_tool_reference_block_param.rb +36 -0
  31. data/lib/anthropic/models/beta/beta_tool_result_block_param.rb +8 -4
  32. data/lib/anthropic/models/beta/beta_tool_search_tool_bm25_20251119.rb +85 -0
  33. data/lib/anthropic/models/beta/beta_tool_search_tool_regex_20251119.rb +85 -0
  34. data/lib/anthropic/models/beta/beta_tool_search_tool_result_block.rb +43 -0
  35. data/lib/anthropic/models/beta/beta_tool_search_tool_result_block_param.rb +53 -0
  36. data/lib/anthropic/models/beta/beta_tool_search_tool_result_error.rb +44 -0
  37. data/lib/anthropic/models/beta/beta_tool_search_tool_result_error_param.rb +38 -0
  38. data/lib/anthropic/models/beta/beta_tool_search_tool_search_result_block.rb +26 -0
  39. data/lib/anthropic/models/beta/beta_tool_search_tool_search_result_block_param.rb +26 -0
  40. data/lib/anthropic/models/beta/beta_tool_text_editor_20241022.rb +36 -1
  41. data/lib/anthropic/models/beta/beta_tool_text_editor_20250124.rb +36 -1
  42. data/lib/anthropic/models/beta/beta_tool_text_editor_20250429.rb +36 -1
  43. data/lib/anthropic/models/beta/beta_tool_text_editor_20250728.rb +36 -1
  44. data/lib/anthropic/models/beta/beta_tool_union.rb +17 -1
  45. data/lib/anthropic/models/beta/beta_tool_use_block.rb +30 -1
  46. data/lib/anthropic/models/beta/beta_tool_use_block_param.rb +27 -1
  47. data/lib/anthropic/models/beta/beta_web_fetch_tool_20250910.rb +28 -1
  48. data/lib/anthropic/models/beta/beta_web_search_tool_20250305.rb +28 -1
  49. data/lib/anthropic/models/beta/message_count_tokens_params.rb +29 -4
  50. data/lib/anthropic/models/beta/message_create_params.rb +12 -3
  51. data/lib/anthropic/models/beta/messages/batch_create_params.rb +16 -7
  52. data/lib/anthropic/models/messages/batch_create_params.rb +4 -4
  53. data/lib/anthropic/models/model.rb +10 -0
  54. data/lib/anthropic/resources/beta/messages.rb +26 -11
  55. data/lib/anthropic/resources/messages.rb +8 -2
  56. data/lib/anthropic/version.rb +1 -1
  57. data/lib/anthropic.rb +19 -0
  58. data/manifest.yaml +1 -0
  59. data/rbi/anthropic/models/anthropic_beta.rbi +2 -0
  60. data/rbi/anthropic/models/beta/beta_code_execution_tool_20250522.rbi +77 -0
  61. data/rbi/anthropic/models/beta/beta_code_execution_tool_20250825.rbi +77 -0
  62. data/rbi/anthropic/models/beta/beta_content_block.rbi +1 -0
  63. data/rbi/anthropic/models/beta/beta_content_block_param.rbi +1 -0
  64. data/rbi/anthropic/models/beta/beta_direct_caller.rbi +31 -0
  65. data/rbi/anthropic/models/beta/beta_mcp_tool_config.rbi +46 -0
  66. data/rbi/anthropic/models/beta/beta_mcp_tool_default_config.rbi +46 -0
  67. data/rbi/anthropic/models/beta/beta_mcp_toolset.rbi +100 -0
  68. data/rbi/anthropic/models/beta/beta_memory_tool_20250818.rbi +88 -0
  69. data/rbi/anthropic/models/beta/beta_message.rbi +1 -0
  70. data/rbi/anthropic/models/beta/beta_output_config.rbi +82 -0
  71. data/rbi/anthropic/models/beta/beta_raw_content_block_start_event.rbi +2 -0
  72. data/rbi/anthropic/models/beta/beta_server_tool_caller.rbi +34 -0
  73. data/rbi/anthropic/models/beta/beta_server_tool_use_block.rbi +54 -1
  74. data/rbi/anthropic/models/beta/beta_server_tool_use_block_param.rbi +70 -1
  75. data/rbi/anthropic/models/beta/beta_tool.rbi +75 -0
  76. data/rbi/anthropic/models/beta/beta_tool_bash_20241022.rbi +88 -0
  77. data/rbi/anthropic/models/beta/beta_tool_bash_20250124.rbi +88 -0
  78. data/rbi/anthropic/models/beta/beta_tool_computer_use_20241022.rbi +88 -0
  79. data/rbi/anthropic/models/beta/beta_tool_computer_use_20250124.rbi +88 -0
  80. data/rbi/anthropic/models/beta/beta_tool_computer_use_20251124.rbi +204 -0
  81. data/rbi/anthropic/models/beta/beta_tool_reference_block.rbi +35 -0
  82. data/rbi/anthropic/models/beta/beta_tool_reference_block_param.rbi +67 -0
  83. data/rbi/anthropic/models/beta/beta_tool_result_block_param.rbi +3 -1
  84. data/rbi/anthropic/models/beta/beta_tool_search_tool_bm25_20251119.rbi +198 -0
  85. data/rbi/anthropic/models/beta/beta_tool_search_tool_regex_20251119.rbi +198 -0
  86. data/rbi/anthropic/models/beta/beta_tool_search_tool_result_block.rbi +81 -0
  87. data/rbi/anthropic/models/beta/beta_tool_search_tool_result_block_param.rbi +110 -0
  88. data/rbi/anthropic/models/beta/beta_tool_search_tool_result_error.rbi +104 -0
  89. data/rbi/anthropic/models/beta/beta_tool_search_tool_result_error_param.rbi +96 -0
  90. data/rbi/anthropic/models/beta/beta_tool_search_tool_search_result_block.rbi +48 -0
  91. data/rbi/anthropic/models/beta/beta_tool_search_tool_search_result_block_param.rbi +48 -0
  92. data/rbi/anthropic/models/beta/beta_tool_text_editor_20241022.rbi +88 -0
  93. data/rbi/anthropic/models/beta/beta_tool_text_editor_20250124.rbi +88 -0
  94. data/rbi/anthropic/models/beta/beta_tool_text_editor_20250429.rbi +88 -0
  95. data/rbi/anthropic/models/beta/beta_tool_text_editor_20250728.rbi +88 -0
  96. data/rbi/anthropic/models/beta/beta_tool_union.rbi +9 -1
  97. data/rbi/anthropic/models/beta/beta_tool_use_block.rbi +55 -2
  98. data/rbi/anthropic/models/beta/beta_tool_use_block_param.rbi +58 -1
  99. data/rbi/anthropic/models/beta/beta_web_fetch_tool_20250910.rbi +77 -0
  100. data/rbi/anthropic/models/beta/beta_web_search_tool_20250305.rbi +77 -0
  101. data/rbi/anthropic/models/beta/message_count_tokens_params.rbi +44 -5
  102. data/rbi/anthropic/models/beta/message_create_params.rbi +35 -4
  103. data/rbi/anthropic/models/beta/messages/batch_create_params.rbi +43 -10
  104. data/rbi/anthropic/models/messages/batch_create_params.rbi +6 -6
  105. data/rbi/anthropic/models/model.rbi +8 -0
  106. data/rbi/anthropic/resources/beta/messages.rbi +29 -3
  107. data/sig/anthropic/models/anthropic_beta.rbs +2 -0
  108. data/sig/anthropic/models/beta/beta_code_execution_tool_20250522.rbs +27 -0
  109. data/sig/anthropic/models/beta/beta_code_execution_tool_20250825.rbs +27 -0
  110. data/sig/anthropic/models/beta/beta_content_block.rbs +1 -0
  111. data/sig/anthropic/models/beta/beta_content_block_param.rbs +1 -0
  112. data/sig/anthropic/models/beta/beta_direct_caller.rbs +17 -0
  113. data/sig/anthropic/models/beta/beta_mcp_tool_config.rbs +23 -0
  114. data/sig/anthropic/models/beta/beta_mcp_tool_default_config.rbs +23 -0
  115. data/sig/anthropic/models/beta/beta_mcp_toolset.rbs +48 -0
  116. data/sig/anthropic/models/beta/beta_memory_tool_20250818.rbs +36 -0
  117. data/sig/anthropic/models/beta/beta_output_config.rbs +34 -0
  118. data/sig/anthropic/models/beta/beta_raw_content_block_start_event.rbs +1 -0
  119. data/sig/anthropic/models/beta/beta_server_tool_caller.rbs +23 -0
  120. data/sig/anthropic/models/beta/beta_server_tool_use_block.rbs +19 -0
  121. data/sig/anthropic/models/beta/beta_server_tool_use_block_param.rbs +25 -2
  122. data/sig/anthropic/models/beta/beta_tool.rbs +36 -0
  123. data/sig/anthropic/models/beta/beta_tool_bash_20241022.rbs +36 -0
  124. data/sig/anthropic/models/beta/beta_tool_bash_20250124.rbs +36 -0
  125. data/sig/anthropic/models/beta/beta_tool_computer_use_20241022.rbs +36 -0
  126. data/sig/anthropic/models/beta/beta_tool_computer_use_20250124.rbs +36 -0
  127. data/sig/anthropic/models/beta/beta_tool_computer_use_20251124.rbs +99 -0
  128. data/sig/anthropic/models/beta/beta_tool_reference_block.rbs +20 -0
  129. data/sig/anthropic/models/beta/beta_tool_reference_block_param.rbs +34 -0
  130. data/sig/anthropic/models/beta/beta_tool_result_block_param.rbs +1 -0
  131. data/sig/anthropic/models/beta/beta_tool_search_tool_bm25_20251119.rbs +79 -0
  132. data/sig/anthropic/models/beta/beta_tool_search_tool_regex_20251119.rbs +79 -0
  133. data/sig/anthropic/models/beta/beta_tool_search_tool_result_block.rbs +44 -0
  134. data/sig/anthropic/models/beta/beta_tool_search_tool_result_block_param.rbs +49 -0
  135. data/sig/anthropic/models/beta/beta_tool_search_tool_result_error.rbs +51 -0
  136. data/sig/anthropic/models/beta/beta_tool_search_tool_result_error_param.rbs +46 -0
  137. data/sig/anthropic/models/beta/beta_tool_search_tool_search_result_block.rbs +29 -0
  138. data/sig/anthropic/models/beta/beta_tool_search_tool_search_result_block_param.rbs +29 -0
  139. data/sig/anthropic/models/beta/beta_tool_text_editor_20241022.rbs +36 -0
  140. data/sig/anthropic/models/beta/beta_tool_text_editor_20250124.rbs +36 -0
  141. data/sig/anthropic/models/beta/beta_tool_text_editor_20250429.rbs +36 -0
  142. data/sig/anthropic/models/beta/beta_tool_text_editor_20250728.rbs +36 -0
  143. data/sig/anthropic/models/beta/beta_tool_union.rbs +4 -0
  144. data/sig/anthropic/models/beta/beta_tool_use_block.rbs +21 -2
  145. data/sig/anthropic/models/beta/beta_tool_use_block_param.rbs +21 -2
  146. data/sig/anthropic/models/beta/beta_web_fetch_tool_20250910.rbs +27 -0
  147. data/sig/anthropic/models/beta/beta_web_search_tool_20250305.rbs +27 -0
  148. data/sig/anthropic/models/beta/message_count_tokens_params.rbs +13 -0
  149. data/sig/anthropic/models/beta/message_create_params.rbs +9 -0
  150. data/sig/anthropic/models/beta/messages/batch_create_params.rbs +9 -0
  151. data/sig/anthropic/models/model.rbs +9 -1
  152. data/sig/anthropic/resources/beta/messages.rbs +3 -0
  153. metadata +54 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eac0d53d1d523a4281f4e2c76959df18acb441cc8886ef115bf3e338837c175c
4
- data.tar.gz: a20a1a833332b561169956f369ffbaccb31cbce2b2dedbf2f4a1b4e69c294a61
3
+ metadata.gz: 869d1baf3032284c138ec2bc77164f5c92fdd3bf79648bbe528f02413812c7bb
4
+ data.tar.gz: 78c540f8017f0ec7646dc9017f57659906adc040955ada92c71b6edcb376bffc
5
5
  SHA512:
6
- metadata.gz: 1dfb63f4c640201f3425b6456c7f411fba1eda5b915d4569df221ed9683f00780f02c0dea3beffeb1c0738963e807d92ec0a3f2ded95fbc7ac5768fd4f26f64d
7
- data.tar.gz: 4c7236a412cf9650f6ad6a651ec963ea0f9f6833a6e140a59c92e7e2331e94d76a84dd77ea2aa8d0a6f81dba13e2c12736ef0967d4e8b73e6db8bf3db1824a37
6
+ metadata.gz: 1c669e7cad89b2ce0da8ea7db521dfb5e9786824f3f1a74aae28ebff2330b047e1939dbaf5c04a580d5088cb1f831bffd094511db78bd59bf153455007a6612a
7
+ data.tar.gz: 82a57e61eb10f9f7f21422583c0de2f442083f8bba72e55cacde57afce694cf3906b5b80e5cf5d1713a7b786bdc63e24de213976f666c76ad1e459a79429e91d
data/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.16.0 (2025-11-24)
4
+
5
+ Full Changelog: [v1.15.2...v1.16.0](https://github.com/anthropics/anthropic-sdk-ruby/compare/v1.15.2...v1.16.0)
6
+
7
+ ### Features
8
+
9
+ * **api:** adds support for Claude Opus 4.5, Effort, Advance Tool Use Features, Autocompaction, and Computer Use v5 ([54d3512](https://github.com/anthropics/anthropic-sdk-ruby/commit/54d3512144cdcecaa44aa6f096cf7bc9b838528e))
10
+
11
+
12
+ ### Chores
13
+
14
+ * explicitly require "base64" gem ([1224a32](https://github.com/anthropics/anthropic-sdk-ruby/commit/1224a32052d701ffe6c36b999e536a0893faf8e9))
15
+
3
16
  ## 1.15.2 (2025-11-19)
4
17
 
5
18
  Full Changelog: [v1.15.1...v1.15.2](https://github.com/anthropics/anthropic-sdk-ruby/compare/v1.15.1...v1.15.2)
data/README.md CHANGED
@@ -15,7 +15,7 @@ To use this gem, install via Bundler by adding the following to your application
15
15
  <!-- x-release-please-start-version -->
16
16
 
17
17
  ```ruby
18
- gem "anthropic", "~> 1.15.2"
18
+ gem "anthropic", "~> 1.16.0"
19
19
  ```
20
20
 
21
21
  <!-- x-release-please-end -->
@@ -39,7 +39,7 @@ anthropic = Anthropic::Client.new(
39
39
  message = anthropic.messages.create(
40
40
  max_tokens: 1024,
41
41
  messages: [{role: "user", content: "Hello, Claude"}],
42
- model: :"claude-sonnet-4-5-20250929"
42
+ model: :"claude-opus-4-5-20251101"
43
43
  )
44
44
 
45
45
  puts(message.content)
@@ -53,7 +53,7 @@ We provide support for streaming responses using Server-Sent Events (SSE).
53
53
  stream = anthropic.messages.stream(
54
54
  max_tokens: 1024,
55
55
  messages: [{role: "user", content: "Hello, Claude"}],
56
- model: :"claude-sonnet-4-5-20250929"
56
+ model: :"claude-opus-4-5-20251101"
57
57
  )
58
58
 
59
59
  stream.each do |message|
@@ -168,7 +168,7 @@ begin
168
168
  message = anthropic.messages.create(
169
169
  max_tokens: 1024,
170
170
  messages: [{role: "user", content: "Hello, Claude"}],
171
- model: :"claude-sonnet-4-5-20250929"
171
+ model: :"claude-opus-4-5-20251101"
172
172
  )
173
173
  rescue Anthropic::Errors::APIConnectionError => e
174
174
  puts("The server could not be reached")
@@ -215,7 +215,7 @@ anthropic = Anthropic::Client.new(
215
215
  anthropic.messages.create(
216
216
  max_tokens: 1024,
217
217
  messages: [{role: "user", content: "Hello, Claude"}],
218
- model: :"claude-sonnet-4-5-20250929",
218
+ model: :"claude-opus-4-5-20251101",
219
219
  request_options: {max_retries: 5}
220
220
  )
221
221
  ```
@@ -234,7 +234,7 @@ anthropic = Anthropic::Client.new(
234
234
  anthropic.messages.create(
235
235
  max_tokens: 1024,
236
236
  messages: [{role: "user", content: "Hello, Claude"}],
237
- model: :"claude-sonnet-4-5-20250929",
237
+ model: :"claude-opus-4-5-20251101",
238
238
  request_options: {timeout: 5}
239
239
  )
240
240
  ```
@@ -330,7 +330,7 @@ message =
330
330
  anthropic.messages.create(
331
331
  max_tokens: 1024,
332
332
  messages: [{role: "user", content: "Hello, Claude"}],
333
- model: :"claude-sonnet-4-5-20250929",
333
+ model: :"claude-opus-4-5-20251101",
334
334
  request_options: {
335
335
  extra_query: {my_query_parameter: value},
336
336
  extra_body: {my_body_parameter: value},
@@ -379,7 +379,7 @@ You can provide typesafe request parameters like so:
379
379
  anthropic.messages.create(
380
380
  max_tokens: 1024,
381
381
  messages: [Anthropic::MessageParam.new(role: "user", content: "Hello, Claude")],
382
- model: :"claude-sonnet-4-5-20250929"
382
+ model: :"claude-opus-4-5-20251101"
383
383
  )
384
384
  ```
385
385
 
@@ -390,14 +390,14 @@ Or, equivalently:
390
390
  anthropic.messages.create(
391
391
  max_tokens: 1024,
392
392
  messages: [{role: "user", content: "Hello, Claude"}],
393
- model: :"claude-sonnet-4-5-20250929"
393
+ model: :"claude-opus-4-5-20251101"
394
394
  )
395
395
 
396
396
  # You can also splat a full Params class:
397
397
  params = Anthropic::MessageCreateParams.new(
398
398
  max_tokens: 1024,
399
399
  messages: [Anthropic::MessageParam.new(role: "user", content: "Hello, Claude")],
400
- model: :"claude-sonnet-4-5-20250929"
400
+ model: :"claude-opus-4-5-20251101"
401
401
  )
402
402
  anthropic.messages.create(**params)
403
403
  ```
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Anthropic
4
+ module Helpers
5
+ module Tools
6
+ # Default summary prompt used when compacting conversation history
7
+ DEFAULT_SUMMARY_PROMPT = <<~PROMPT.strip
8
+ You have been working on the task described above but have not yet completed it. Write a continuation summary that will allow you (or another instance of yourself) to resume work efficiently in a future context window where the conversation history will be replaced with this summary. Your summary should be structured, concise, and actionable. Include:
9
+ 1. Task Overview
10
+ The user's core request and success criteria
11
+ Any clarifications or constraints they specified
12
+ 2. Current State
13
+ What has been completed so far
14
+ Files created, modified, or analyzed (with paths if relevant)
15
+ Key outputs or artifacts produced
16
+ 3. Important Discoveries
17
+ Technical constraints or requirements uncovered
18
+ Decisions made and their rationale
19
+ Errors encountered and how they were resolved
20
+ What approaches were tried that didn't work (and why)
21
+ 4. Next Steps
22
+ Specific actions needed to complete the task
23
+ Any blockers or open questions to resolve
24
+ Priority order if multiple steps remain
25
+ 5. Context to Preserve
26
+ User preferences or style requirements
27
+ Domain-specific details that aren't obvious
28
+ Any promises made to the user
29
+ Be concise but complete—err on the side of including information that would prevent duplicate work or repeated mistakes. Write in a way that enables immediate resumption of the task.
30
+ Wrap your summary in <summary></summary> tags.
31
+ PROMPT
32
+
33
+ # Default token threshold for triggering compaction (100,000 tokens)
34
+ DEFAULT_THRESHOLD = 100_000
35
+
36
+ # Configuration for automatic conversation history compaction in tool runners.
37
+ #
38
+ # When the cumulative token count (input + output) across all messages exceeds
39
+ # the threshold, the message history will be automatically summarized and compressed.
40
+ #
41
+ # By default, a warning is printed to STDERR the first time compaction occurs for
42
+ # each runner instance. To silence the warning, provide an `on_compact` callback -
43
+ # when present, the warning is suppressed and you handle compaction events your way.
44
+ #
45
+ # @example Basic usage with defaults
46
+ # client.beta.messages.tool_runner(
47
+ # model: "claude-sonnet-4-20250514",
48
+ # max_tokens: 4000,
49
+ # tools: [my_tool],
50
+ # messages: [...],
51
+ # compaction_control: { enabled: true }
52
+ # )
53
+ # # => Prints: "[anthropic-ruby] Context compaction triggered (N tokens)..."
54
+ #
55
+ # @example Custom threshold and callback
56
+ # client.beta.messages.tool_runner(
57
+ # # ... other params ...
58
+ # compaction_control: {
59
+ # enabled: true,
60
+ # context_token_threshold: 50_000,
61
+ # on_compact: ->(tokens_before, tokens_after) do
62
+ # logger.info "Compacted conversation: #{tokens_before} → #{tokens_after} tokens"
63
+ # end
64
+ # }
65
+ # )
66
+ # # => No warning printed; callback handles it
67
+ #
68
+ # @example Custom model and summary prompt
69
+ # client.beta.messages.tool_runner(
70
+ # # ... other params ...
71
+ # compaction_control: {
72
+ # enabled: true,
73
+ # model: "claude-3-7-sonnet-20250219",
74
+ # summary_prompt: "Summarize the conversation concisely..."
75
+ # }
76
+ # )
77
+ #
78
+ # @!attribute enabled
79
+ # @return [Boolean] Whether to enable automatic compaction. Required.
80
+ #
81
+ # @!attribute context_token_threshold
82
+ # @return [Integer, nil] The cumulative token count threshold at which to trigger
83
+ # compaction. When the total of input tokens (including cache tokens) and output
84
+ # tokens exceeds this value, compaction will occur. Defaults to 100,000 tokens.
85
+ #
86
+ # @!attribute model
87
+ # @return [String, Symbol, nil] The model to use for generating the compaction summary.
88
+ # If not specified, defaults to the same model used for the tool runner.
89
+ #
90
+ # @!attribute summary_prompt
91
+ # @return [String, nil] The prompt used to instruct the model on how to generate
92
+ # the summary. If not specified, uses {DEFAULT_SUMMARY_PROMPT}.
93
+ #
94
+ # @!attribute on_compact
95
+ # @return [Proc, nil] Optional callback invoked when compaction occurs.
96
+ # Receives two arguments: tokens_before (Integer) and tokens_after (Integer).
97
+ # Use this to log, monitor, or track compaction events. When provided, the
98
+ # default warning to STDERR is suppressed (you're handling it yourself).
99
+ #
100
+ # @see DEFAULT_SUMMARY_PROMPT
101
+ # @see DEFAULT_THRESHOLD
102
+ module CompactionControl; end
103
+ end
104
+ end
105
+ end
@@ -25,7 +25,7 @@ module Anthropic
25
25
  message = nil
26
26
  unless finished?
27
27
  fold do
28
- message = @client.beta.messages.create(_1)
28
+ message = @client.beta.messages.create(with_helper_header(_1, "runner"))
29
29
  [true, message]
30
30
  end
31
31
  end
@@ -46,7 +46,7 @@ module Anthropic
46
46
  end
47
47
 
48
48
  fold do
49
- message = @client.beta.messages.create(_1)
49
+ message = @client.beta.messages.create(with_helper_header(_1, "runner"))
50
50
  blk.call(message)
51
51
  [false, message]
52
52
  end
@@ -59,7 +59,7 @@ module Anthropic
59
59
  end
60
60
 
61
61
  fold do
62
- stream = @client.beta.messages.stream(_1)
62
+ stream = @client.beta.messages.stream(with_helper_header(_1, "runner"))
63
63
  blk.call(stream)
64
64
  [false, stream.accumulated_message]
65
65
  ensure
@@ -73,24 +73,24 @@ module Anthropic
73
73
  private def fold(&blk)
74
74
  return nil if finished?
75
75
 
76
- count =
77
- case @param
78
- in {max_iterations: Integer => m}
79
- m
80
- else
81
- nil
82
- end
83
-
84
76
  # rubocop:disable Metrics/BlockLength
85
77
  # rubocop:disable Style/CaseEquality
86
78
  loop do
87
- return if (count = count&.pred)&.negative?
79
+ return if @max_iterations && @iteration_count >= @max_iterations
88
80
 
89
81
  tools = params[:tools].to_a.grep(Anthropic::Helpers::Tools::BaseTool)
90
82
  messages = current_messages
91
83
  brk, response = blk.call(params)
92
84
 
85
+ # Store the response for compaction check
86
+ @last_response = response
87
+
88
+ # Check and perform compaction if needed
89
+ compacted = check_and_compact?
90
+
91
+ # Skip tool processing if we just compacted or if messages were modified
93
92
  next unless current_messages.equal?(messages)
93
+ break if compacted
94
94
 
95
95
  mapped =
96
96
  response
@@ -134,21 +134,134 @@ module Anthropic
134
134
  messages << {role: :assistant, content:}
135
135
  messages << {role: :user, content: mapped}
136
136
 
137
+ @iteration_count += 1
138
+
137
139
  break if brk
138
140
  end
139
141
  # rubocop:enable Style/CaseEquality
140
142
  # rubocop:enable Metrics/BlockLength
141
143
  end
142
144
 
145
+ # Check token usage and compact messages if threshold exceeded
146
+ #
147
+ # @return [Boolean] true if compaction occurred, false otherwise
148
+ # @api private
149
+ private def check_and_compact?
150
+ return false unless @compaction_control&.[](:enabled)
151
+ return false unless @last_response
152
+
153
+ # Calculate total tokens used
154
+ usage = @last_response.usage
155
+ total_input_tokens = (
156
+ usage.input_tokens.to_i +
157
+ usage.cache_creation_input_tokens.to_i +
158
+ usage.cache_read_input_tokens.to_i
159
+ )
160
+ tokens_used = total_input_tokens + usage.output_tokens.to_i
161
+
162
+ # Check if we've exceeded the threshold
163
+ threshold = @compaction_control[:context_token_threshold] || DEFAULT_THRESHOLD
164
+ return false if tokens_used < threshold
165
+
166
+ # Warn once about compaction (only if no callback provided)
167
+ if @compaction_control[:on_compact].nil? && !@compaction_warned
168
+ warn(
169
+ "[anthropic-ruby] Context compaction triggered (#{tokens_used} tokens). " \
170
+ "Use compaction_control: { on_compact: ->(before, after) { ... } } for details."
171
+ )
172
+ @compaction_warned = true
173
+ end
174
+
175
+ # Prepare compaction request
176
+ model = @compaction_control[:model] || params[:model]
177
+ summary_prompt = @compaction_control[:summary_prompt] || DEFAULT_SUMMARY_PROMPT
178
+
179
+ # Prepare messages for compaction - handle tool_use blocks to avoid 400 errors
180
+ messages_for_compaction = current_messages.dup
181
+
182
+ # If last message is from assistant with tool_use blocks, we need to filter them out
183
+ # because tool_use blocks require corresponding tool_result blocks
184
+ if messages_for_compaction.last&.[](:role) == :assistant
185
+ last_msg = messages_for_compaction.last
186
+ content = last_msg[:content]
187
+
188
+ if content.is_a?(Array)
189
+ # Filter out tool_use blocks, keep text/thinking blocks
190
+ non_tool_blocks = content.reject do |block|
191
+ (block.is_a?(Hash) && block[:type] == :tool_use) ||
192
+ block.is_a?(Anthropic::Beta::BetaToolUseBlock)
193
+ end
194
+
195
+ if non_tool_blocks.empty?
196
+ # If no content remains after filtering, remove the entire message
197
+ messages_for_compaction.pop
198
+ else
199
+ # Keep the message but with filtered content
200
+ last_msg[:content] = non_tool_blocks
201
+ end
202
+ end
203
+ end
204
+
205
+ messages = [
206
+ *messages_for_compaction,
207
+ {role: :user, content: summary_prompt}
208
+ ]
209
+
210
+ # Get summary from Claude
211
+ response = @client.beta.messages.create(
212
+ with_helper_header(
213
+ {model: model, messages: messages, max_tokens: params[:max_tokens]},
214
+ "compaction"
215
+ )
216
+ )
217
+
218
+ # Validate that compaction response is text
219
+ first_content = response.content.first
220
+ unless first_content.is_a?(Anthropic::Beta::BetaTextBlock)
221
+ raise "Compaction response content is not of type 'text', got: #{first_content.class}"
222
+ end
223
+
224
+ tokens_after = response.usage.output_tokens.to_i
225
+
226
+ # Invoke callback if provided
227
+ @compaction_control[:on_compact]&.call(tokens_used, tokens_after)
228
+
229
+ # Replace message history with just the summary
230
+ self.params = {
231
+ **params,
232
+ messages: [
233
+ {role: :user, content: response.content}
234
+ ]
235
+ }
236
+
237
+ true
238
+ end
239
+
143
240
  # @api private
144
241
  #
145
242
  # @param client [Anthropic::Client]
146
243
  #
147
244
  # @param params [Anthropic::Models::Beta::MessageCreateParams]
148
- def initialize(client, params:)
245
+ #
246
+ # @param max_iterations [Integer, nil]
247
+ #
248
+ # @param compaction_control [Hash, nil]
249
+ def initialize(client, params:, max_iterations: nil, compaction_control: nil)
149
250
  @client = client
150
251
  @params = params.to_h
151
252
  @finished = false
253
+ @max_iterations = max_iterations
254
+ @iteration_count = 0
255
+ @compaction_control = compaction_control
256
+ @compaction_warned = false
257
+ @last_response = nil
258
+ end
259
+
260
+ private def with_helper_header(params, helper)
261
+ options = params[:request_options] || {}
262
+ headers = options[:extra_headers] || {}
263
+
264
+ {**params, request_options: {**options, extra_headers: {**headers, "x-stainless-helper" => helper}}}
152
265
  end
153
266
  end
154
267
  end
@@ -27,6 +27,8 @@ module Anthropic
27
27
 
28
28
  variant const: -> { Anthropic::Models::AnthropicBeta::MCP_CLIENT_2025_04_04 }
29
29
 
30
+ variant const: -> { Anthropic::Models::AnthropicBeta::MCP_CLIENT_2025_11_20 }
31
+
30
32
  variant const: -> { Anthropic::Models::AnthropicBeta::DEV_FULL_THINKING_2025_05_14 }
31
33
 
32
34
  variant const: -> { Anthropic::Models::AnthropicBeta::INTERLEAVED_THINKING_2025_05_14 }
@@ -62,6 +64,7 @@ module Anthropic
62
64
  OUTPUT_128K_2025_02_19 = :"output-128k-2025-02-19"
63
65
  FILES_API_2025_04_14 = :"files-api-2025-04-14"
64
66
  MCP_CLIENT_2025_04_04 = :"mcp-client-2025-04-04"
67
+ MCP_CLIENT_2025_11_20 = :"mcp-client-2025-11-20"
65
68
  DEV_FULL_THINKING_2025_05_14 = :"dev-full-thinking-2025-05-14"
66
69
  INTERLEAVED_THINKING_2025_05_14 = :"interleaved-thinking-2025-05-14"
67
70
  CODE_EXECUTION_2025_05_22 = :"code-execution-2025-05-22"
@@ -17,28 +17,55 @@ module Anthropic
17
17
  # @return [Symbol, :code_execution_20250522]
18
18
  required :type, const: :code_execution_20250522
19
19
 
20
+ # @!attribute allowed_callers
21
+ #
22
+ # @return [Array<Symbol, Anthropic::Models::Beta::BetaCodeExecutionTool20250522::AllowedCaller>, nil]
23
+ optional :allowed_callers,
24
+ -> { Anthropic::Internal::Type::ArrayOf[enum: Anthropic::Beta::BetaCodeExecutionTool20250522::AllowedCaller] }
25
+
20
26
  # @!attribute cache_control
21
27
  # Create a cache control breakpoint at this content block.
22
28
  #
23
29
  # @return [Anthropic::Models::Beta::BetaCacheControlEphemeral, nil]
24
30
  optional :cache_control, -> { Anthropic::Beta::BetaCacheControlEphemeral }, nil?: true
25
31
 
32
+ # @!attribute defer_loading
33
+ # If true, tool will not be included in initial system prompt. Only loaded when
34
+ # returned via tool_reference from tool search.
35
+ #
36
+ # @return [Boolean, nil]
37
+ optional :defer_loading, Anthropic::Internal::Type::Boolean
38
+
26
39
  # @!attribute strict
27
40
  #
28
41
  # @return [Boolean, nil]
29
42
  optional :strict, Anthropic::Internal::Type::Boolean
30
43
 
31
- # @!method initialize(cache_control: nil, strict: nil, name: :code_execution, type: :code_execution_20250522)
44
+ # @!method initialize(allowed_callers: nil, cache_control: nil, defer_loading: nil, strict: nil, name: :code_execution, type: :code_execution_20250522)
32
45
  # Some parameter documentations has been truncated, see
33
46
  # {Anthropic::Models::Beta::BetaCodeExecutionTool20250522} for more details.
34
47
  #
48
+ # @param allowed_callers [Array<Symbol, Anthropic::Models::Beta::BetaCodeExecutionTool20250522::AllowedCaller>]
49
+ #
35
50
  # @param cache_control [Anthropic::Models::Beta::BetaCacheControlEphemeral, nil] Create a cache control breakpoint at this content block.
36
51
  #
52
+ # @param defer_loading [Boolean] If true, tool will not be included in initial system prompt. Only loaded when re
53
+ #
37
54
  # @param strict [Boolean]
38
55
  #
39
56
  # @param name [Symbol, :code_execution] Name of the tool.
40
57
  #
41
58
  # @param type [Symbol, :code_execution_20250522]
59
+
60
+ module AllowedCaller
61
+ extend Anthropic::Internal::Type::Enum
62
+
63
+ DIRECT = :direct
64
+ CODE_EXECUTION_20250825 = :code_execution_20250825
65
+
66
+ # @!method self.values
67
+ # @return [Array<Symbol>]
68
+ end
42
69
  end
43
70
  end
44
71
 
@@ -17,28 +17,55 @@ module Anthropic
17
17
  # @return [Symbol, :code_execution_20250825]
18
18
  required :type, const: :code_execution_20250825
19
19
 
20
+ # @!attribute allowed_callers
21
+ #
22
+ # @return [Array<Symbol, Anthropic::Models::Beta::BetaCodeExecutionTool20250825::AllowedCaller>, nil]
23
+ optional :allowed_callers,
24
+ -> { Anthropic::Internal::Type::ArrayOf[enum: Anthropic::Beta::BetaCodeExecutionTool20250825::AllowedCaller] }
25
+
20
26
  # @!attribute cache_control
21
27
  # Create a cache control breakpoint at this content block.
22
28
  #
23
29
  # @return [Anthropic::Models::Beta::BetaCacheControlEphemeral, nil]
24
30
  optional :cache_control, -> { Anthropic::Beta::BetaCacheControlEphemeral }, nil?: true
25
31
 
32
+ # @!attribute defer_loading
33
+ # If true, tool will not be included in initial system prompt. Only loaded when
34
+ # returned via tool_reference from tool search.
35
+ #
36
+ # @return [Boolean, nil]
37
+ optional :defer_loading, Anthropic::Internal::Type::Boolean
38
+
26
39
  # @!attribute strict
27
40
  #
28
41
  # @return [Boolean, nil]
29
42
  optional :strict, Anthropic::Internal::Type::Boolean
30
43
 
31
- # @!method initialize(cache_control: nil, strict: nil, name: :code_execution, type: :code_execution_20250825)
44
+ # @!method initialize(allowed_callers: nil, cache_control: nil, defer_loading: nil, strict: nil, name: :code_execution, type: :code_execution_20250825)
32
45
  # Some parameter documentations has been truncated, see
33
46
  # {Anthropic::Models::Beta::BetaCodeExecutionTool20250825} for more details.
34
47
  #
48
+ # @param allowed_callers [Array<Symbol, Anthropic::Models::Beta::BetaCodeExecutionTool20250825::AllowedCaller>]
49
+ #
35
50
  # @param cache_control [Anthropic::Models::Beta::BetaCacheControlEphemeral, nil] Create a cache control breakpoint at this content block.
36
51
  #
52
+ # @param defer_loading [Boolean] If true, tool will not be included in initial system prompt. Only loaded when re
53
+ #
37
54
  # @param strict [Boolean]
38
55
  #
39
56
  # @param name [Symbol, :code_execution] Name of the tool.
40
57
  #
41
58
  # @param type [Symbol, :code_execution_20250825]
59
+
60
+ module AllowedCaller
61
+ extend Anthropic::Internal::Type::Enum
62
+
63
+ DIRECT = :direct
64
+ CODE_EXECUTION_20250825 = :code_execution_20250825
65
+
66
+ # @!method self.values
67
+ # @return [Array<Symbol>]
68
+ end
42
69
  end
43
70
  end
44
71
 
@@ -30,6 +30,8 @@ module Anthropic
30
30
  variant :text_editor_code_execution_tool_result,
31
31
  -> { Anthropic::Beta::BetaTextEditorCodeExecutionToolResultBlock }
32
32
 
33
+ variant :tool_search_tool_result, -> { Anthropic::Beta::BetaToolSearchToolResultBlock }
34
+
33
35
  variant :mcp_tool_use, -> { Anthropic::Beta::BetaMCPToolUseBlock }
34
36
 
35
37
  variant :mcp_tool_result, -> { Anthropic::Beta::BetaMCPToolResultBlock }
@@ -38,7 +40,7 @@ module Anthropic
38
40
  variant :container_upload, -> { Anthropic::Beta::BetaContainerUploadBlock }
39
41
 
40
42
  # @!method self.variants
41
- # @return [Array(Anthropic::Models::Beta::BetaTextBlock, Anthropic::Models::Beta::BetaThinkingBlock, Anthropic::Models::Beta::BetaRedactedThinkingBlock, Anthropic::Models::Beta::BetaToolUseBlock, Anthropic::Models::Beta::BetaServerToolUseBlock, Anthropic::Models::Beta::BetaWebSearchToolResultBlock, Anthropic::Models::Beta::BetaWebFetchToolResultBlock, Anthropic::Models::Beta::BetaCodeExecutionToolResultBlock, Anthropic::Models::Beta::BetaBashCodeExecutionToolResultBlock, Anthropic::Models::Beta::BetaTextEditorCodeExecutionToolResultBlock, Anthropic::Models::Beta::BetaMCPToolUseBlock, Anthropic::Models::Beta::BetaMCPToolResultBlock, Anthropic::Models::Beta::BetaContainerUploadBlock)]
43
+ # @return [Array(Anthropic::Models::Beta::BetaTextBlock, Anthropic::Models::Beta::BetaThinkingBlock, Anthropic::Models::Beta::BetaRedactedThinkingBlock, Anthropic::Models::Beta::BetaToolUseBlock, Anthropic::Models::Beta::BetaServerToolUseBlock, Anthropic::Models::Beta::BetaWebSearchToolResultBlock, Anthropic::Models::Beta::BetaWebFetchToolResultBlock, Anthropic::Models::Beta::BetaCodeExecutionToolResultBlock, Anthropic::Models::Beta::BetaBashCodeExecutionToolResultBlock, Anthropic::Models::Beta::BetaTextEditorCodeExecutionToolResultBlock, Anthropic::Models::Beta::BetaToolSearchToolResultBlock, Anthropic::Models::Beta::BetaMCPToolUseBlock, Anthropic::Models::Beta::BetaMCPToolResultBlock, Anthropic::Models::Beta::BetaContainerUploadBlock)]
42
44
  end
43
45
  end
44
46
 
@@ -47,6 +47,8 @@ module Anthropic
47
47
  variant :text_editor_code_execution_tool_result,
48
48
  -> { Anthropic::Beta::BetaTextEditorCodeExecutionToolResultBlockParam }
49
49
 
50
+ variant :tool_search_tool_result, -> { Anthropic::Beta::BetaToolSearchToolResultBlockParam }
51
+
50
52
  variant :mcp_tool_use, -> { Anthropic::Beta::BetaMCPToolUseBlockParam }
51
53
 
52
54
  variant :mcp_tool_result, -> { Anthropic::Beta::BetaRequestMCPToolResultBlockParam }
@@ -56,7 +58,7 @@ module Anthropic
56
58
  variant :container_upload, -> { Anthropic::Beta::BetaContainerUploadBlockParam }
57
59
 
58
60
  # @!method self.variants
59
- # @return [Array(Anthropic::Models::Beta::BetaTextBlockParam, Anthropic::Models::Beta::BetaImageBlockParam, Anthropic::Models::Beta::BetaRequestDocumentBlock, Anthropic::Models::Beta::BetaSearchResultBlockParam, Anthropic::Models::Beta::BetaThinkingBlockParam, Anthropic::Models::Beta::BetaRedactedThinkingBlockParam, Anthropic::Models::Beta::BetaToolUseBlockParam, Anthropic::Models::Beta::BetaToolResultBlockParam, Anthropic::Models::Beta::BetaServerToolUseBlockParam, Anthropic::Models::Beta::BetaWebSearchToolResultBlockParam, Anthropic::Models::Beta::BetaWebFetchToolResultBlockParam, Anthropic::Models::Beta::BetaCodeExecutionToolResultBlockParam, Anthropic::Models::Beta::BetaBashCodeExecutionToolResultBlockParam, Anthropic::Models::Beta::BetaTextEditorCodeExecutionToolResultBlockParam, Anthropic::Models::Beta::BetaMCPToolUseBlockParam, Anthropic::Models::Beta::BetaRequestMCPToolResultBlockParam, Anthropic::Models::Beta::BetaContainerUploadBlockParam)]
61
+ # @return [Array(Anthropic::Models::Beta::BetaTextBlockParam, Anthropic::Models::Beta::BetaImageBlockParam, Anthropic::Models::Beta::BetaRequestDocumentBlock, Anthropic::Models::Beta::BetaSearchResultBlockParam, Anthropic::Models::Beta::BetaThinkingBlockParam, Anthropic::Models::Beta::BetaRedactedThinkingBlockParam, Anthropic::Models::Beta::BetaToolUseBlockParam, Anthropic::Models::Beta::BetaToolResultBlockParam, Anthropic::Models::Beta::BetaServerToolUseBlockParam, Anthropic::Models::Beta::BetaWebSearchToolResultBlockParam, Anthropic::Models::Beta::BetaWebFetchToolResultBlockParam, Anthropic::Models::Beta::BetaCodeExecutionToolResultBlockParam, Anthropic::Models::Beta::BetaBashCodeExecutionToolResultBlockParam, Anthropic::Models::Beta::BetaTextEditorCodeExecutionToolResultBlockParam, Anthropic::Models::Beta::BetaToolSearchToolResultBlockParam, Anthropic::Models::Beta::BetaMCPToolUseBlockParam, Anthropic::Models::Beta::BetaRequestMCPToolResultBlockParam, Anthropic::Models::Beta::BetaContainerUploadBlockParam)]
60
62
  end
61
63
  end
62
64
 
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Anthropic
4
+ module Models
5
+ module Beta
6
+ class BetaDirectCaller < Anthropic::Internal::Type::BaseModel
7
+ # @!attribute type
8
+ #
9
+ # @return [Symbol, :direct]
10
+ required :type, const: :direct
11
+
12
+ # @!method initialize(type: :direct)
13
+ # Tool invocation directly from the model.
14
+ #
15
+ # @param type [Symbol, :direct]
16
+ end
17
+ end
18
+
19
+ BetaDirectCaller = Beta::BetaDirectCaller
20
+ end
21
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Anthropic
4
+ module Models
5
+ module Beta
6
+ class BetaMCPToolConfig < Anthropic::Internal::Type::BaseModel
7
+ # @!attribute defer_loading
8
+ #
9
+ # @return [Boolean, nil]
10
+ optional :defer_loading, Anthropic::Internal::Type::Boolean
11
+
12
+ # @!attribute enabled
13
+ #
14
+ # @return [Boolean, nil]
15
+ optional :enabled, Anthropic::Internal::Type::Boolean
16
+
17
+ # @!method initialize(defer_loading: nil, enabled: nil)
18
+ # Configuration for a specific tool in an MCP toolset.
19
+ #
20
+ # @param defer_loading [Boolean]
21
+ # @param enabled [Boolean]
22
+ end
23
+ end
24
+
25
+ BetaMCPToolConfig = Beta::BetaMCPToolConfig
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Anthropic
4
+ module Models
5
+ module Beta
6
+ class BetaMCPToolDefaultConfig < Anthropic::Internal::Type::BaseModel
7
+ # @!attribute defer_loading
8
+ #
9
+ # @return [Boolean, nil]
10
+ optional :defer_loading, Anthropic::Internal::Type::Boolean
11
+
12
+ # @!attribute enabled
13
+ #
14
+ # @return [Boolean, nil]
15
+ optional :enabled, Anthropic::Internal::Type::Boolean
16
+
17
+ # @!method initialize(defer_loading: nil, enabled: nil)
18
+ # Default configuration for tools in an MCP toolset.
19
+ #
20
+ # @param defer_loading [Boolean]
21
+ # @param enabled [Boolean]
22
+ end
23
+ end
24
+
25
+ BetaMCPToolDefaultConfig = Beta::BetaMCPToolDefaultConfig
26
+ end
27
+ end