Skip to content

Commit a940123

Browse files
feat(api): Add top-level cache control (automatic caching)
1 parent 6ab469e commit a940123

File tree

13 files changed

+192
-6
lines changed

13 files changed

+192
-6
lines changed

.stats.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
configured_endpoints: 34
2-
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/anthropic%2Fanthropic-51af8e63ed8396461bff3a89a65124496f905d2f8ac211f0b4c9a6588f6cf20f.yml
3-
openapi_spec_hash: 13d3d0a8e62a955b8b4df99c18d387d0
4-
config_hash: 5662eb02a2b78e86e8254f0934d1a870
2+
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/anthropic%2Fanthropic-29a6b7ba51942cd606e5bf4b533e5aac1bef42f6d4b1f7f45f756304cf676782.yml
3+
openapi_spec_hash: 58021ab18daccd5c45a930ffd7d6ab4d
4+
config_hash: 4e204fead5f0af80eb9effa1d1e34dca

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,9 +482,9 @@ message = client.messages.create(
482482
}
483483
],
484484
model="claude-opus-4-6",
485-
metadata={},
485+
cache_control={"type": "ephemeral"},
486486
)
487-
print(message.metadata)
487+
print(message.cache_control)
488488
```
489489

490490
## File uploads

src/anthropic/resources/beta/messages/messages.py

Lines changed: 57 additions & 0 deletions
Large diffs are not rendered by default.

src/anthropic/resources/messages/messages.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
from ...types.thinking_config_param import ThinkingConfigParam
5151
from ...types.json_output_format_param import JSONOutputFormatParam
5252
from ...types.raw_message_stream_event import RawMessageStreamEvent
53+
from ...types.cache_control_ephemeral_param import CacheControlEphemeralParam
5354
from ...types.message_count_tokens_tool_param import MessageCountTokensToolParam
5455

5556
__all__ = ["Messages", "AsyncMessages"]
@@ -105,6 +106,7 @@ def create(
105106
max_tokens: int,
106107
messages: Iterable[MessageParam],
107108
model: ModelParam,
109+
cache_control: Optional[CacheControlEphemeralParam] | Omit = omit,
108110
container: Optional[str] | Omit = omit,
109111
inference_geo: Optional[str] | Omit = omit,
110112
metadata: MetadataParam | Omit = omit,
@@ -215,6 +217,9 @@ def create(
215217
[models](https://docs.anthropic.com/en/docs/models-overview) for additional
216218
details and options.
217219
220+
cache_control: Top-level cache control automatically applies a cache_control marker to the last
221+
cacheable block in the request.
222+
218223
container: Container identifier for reuse across requests.
219224
220225
inference_geo: Specifies the geographic region for inference processing. If not specified, the
@@ -384,6 +389,7 @@ def create(
384389
messages: Iterable[MessageParam],
385390
model: ModelParam,
386391
stream: Literal[True],
392+
cache_control: Optional[CacheControlEphemeralParam] | Omit = omit,
387393
container: Optional[str] | Omit = omit,
388394
inference_geo: Optional[str] | Omit = omit,
389395
metadata: MetadataParam | Omit = omit,
@@ -497,6 +503,9 @@ def create(
497503
498504
See [streaming](https://docs.claude.com/en/api/messages-streaming) for details.
499505
506+
cache_control: Top-level cache control automatically applies a cache_control marker to the last
507+
cacheable block in the request.
508+
500509
container: Container identifier for reuse across requests.
501510
502511
inference_geo: Specifies the geographic region for inference processing. If not specified, the
@@ -662,6 +671,7 @@ def create(
662671
messages: Iterable[MessageParam],
663672
model: ModelParam,
664673
stream: bool,
674+
cache_control: Optional[CacheControlEphemeralParam] | Omit = omit,
665675
container: Optional[str] | Omit = omit,
666676
inference_geo: Optional[str] | Omit = omit,
667677
metadata: MetadataParam | Omit = omit,
@@ -775,6 +785,9 @@ def create(
775785
776786
See [streaming](https://docs.claude.com/en/api/messages-streaming) for details.
777787
788+
cache_control: Top-level cache control automatically applies a cache_control marker to the last
789+
cacheable block in the request.
790+
778791
container: Container identifier for reuse across requests.
779792
780793
inference_geo: Specifies the geographic region for inference processing. If not specified, the
@@ -939,6 +952,7 @@ def create(
939952
max_tokens: int,
940953
messages: Iterable[MessageParam],
941954
model: ModelParam,
955+
cache_control: Optional[CacheControlEphemeralParam] | Omit = omit,
942956
container: Optional[str] | Omit = omit,
943957
inference_geo: Optional[str] | Omit = omit,
944958
metadata: MetadataParam | Omit = omit,
@@ -986,6 +1000,7 @@ def create(
9861000
"max_tokens": max_tokens,
9871001
"messages": messages,
9881002
"model": model,
1003+
"cache_control": cache_control,
9891004
"container": container,
9901005
"inference_geo": inference_geo,
9911006
"metadata": metadata,
@@ -1019,6 +1034,7 @@ def stream(
10191034
max_tokens: int,
10201035
messages: Iterable[MessageParam],
10211036
model: ModelParam,
1037+
cache_control: Optional[CacheControlEphemeralParam] | Omit = omit,
10221038
inference_geo: Optional[str] | Omit = omit,
10231039
metadata: MetadataParam | Omit = omit,
10241040
output_config: OutputConfigParam | Omit = omit,
@@ -1097,6 +1113,7 @@ def stream(
10971113
"max_tokens": max_tokens,
10981114
"messages": messages,
10991115
"model": model,
1116+
"cache_control": cache_control,
11001117
"inference_geo": inference_geo,
11011118
"metadata": metadata,
11021119
"output_config": merged_output_config,
@@ -1249,6 +1266,7 @@ def count_tokens(
12491266
*,
12501267
messages: Iterable[MessageParam],
12511268
model: ModelParam,
1269+
cache_control: Optional[CacheControlEphemeralParam] | Omit = omit,
12521270
output_config: OutputConfigParam | Omit = omit,
12531271
output_format: None | JSONOutputFormatParam | type | Omit = omit,
12541272
system: Union[str, Iterable[TextBlockParam]] | Omit = omit,
@@ -1342,6 +1360,9 @@ def count_tokens(
13421360
[models](https://docs.anthropic.com/en/docs/models-overview) for additional
13431361
details and options.
13441362
1363+
cache_control: Top-level cache control automatically applies a cache_control marker to the last
1364+
cacheable block in the request.
1365+
13451366
output_config: Configuration options for the model's output, such as the output format.
13461367
13471368
@@ -1485,6 +1506,7 @@ def count_tokens(
14851506
"model": model,
14861507
"messages": messages,
14871508
"model": model,
1509+
"cache_control": cache_control,
14881510
"output_config": merged_output_config,
14891511
"system": system,
14901512
"thinking": thinking,
@@ -1531,6 +1553,7 @@ async def create(
15311553
max_tokens: int,
15321554
messages: Iterable[MessageParam],
15331555
model: ModelParam,
1556+
cache_control: Optional[CacheControlEphemeralParam] | Omit = omit,
15341557
container: Optional[str] | Omit = omit,
15351558
inference_geo: Optional[str] | Omit = omit,
15361559
metadata: MetadataParam | Omit = omit,
@@ -1641,6 +1664,9 @@ async def create(
16411664
[models](https://docs.anthropic.com/en/docs/models-overview) for additional
16421665
details and options.
16431666
1667+
cache_control: Top-level cache control automatically applies a cache_control marker to the last
1668+
cacheable block in the request.
1669+
16441670
container: Container identifier for reuse across requests.
16451671
16461672
inference_geo: Specifies the geographic region for inference processing. If not specified, the
@@ -1810,6 +1836,7 @@ async def create(
18101836
messages: Iterable[MessageParam],
18111837
model: ModelParam,
18121838
stream: Literal[True],
1839+
cache_control: Optional[CacheControlEphemeralParam] | Omit = omit,
18131840
container: Optional[str] | Omit = omit,
18141841
inference_geo: Optional[str] | Omit = omit,
18151842
metadata: MetadataParam | Omit = omit,
@@ -1923,6 +1950,9 @@ async def create(
19231950
19241951
See [streaming](https://docs.claude.com/en/api/messages-streaming) for details.
19251952
1953+
cache_control: Top-level cache control automatically applies a cache_control marker to the last
1954+
cacheable block in the request.
1955+
19261956
container: Container identifier for reuse across requests.
19271957
19281958
inference_geo: Specifies the geographic region for inference processing. If not specified, the
@@ -2088,6 +2118,7 @@ async def create(
20882118
messages: Iterable[MessageParam],
20892119
model: ModelParam,
20902120
stream: bool,
2121+
cache_control: Optional[CacheControlEphemeralParam] | Omit = omit,
20912122
container: Optional[str] | Omit = omit,
20922123
inference_geo: Optional[str] | Omit = omit,
20932124
metadata: MetadataParam | Omit = omit,
@@ -2201,6 +2232,9 @@ async def create(
22012232
22022233
See [streaming](https://docs.claude.com/en/api/messages-streaming) for details.
22032234
2235+
cache_control: Top-level cache control automatically applies a cache_control marker to the last
2236+
cacheable block in the request.
2237+
22042238
container: Container identifier for reuse across requests.
22052239
22062240
inference_geo: Specifies the geographic region for inference processing. If not specified, the
@@ -2365,6 +2399,7 @@ async def create(
23652399
max_tokens: int,
23662400
messages: Iterable[MessageParam],
23672401
model: ModelParam,
2402+
cache_control: Optional[CacheControlEphemeralParam] | Omit = omit,
23682403
container: Optional[str] | Omit = omit,
23692404
inference_geo: Optional[str] | Omit = omit,
23702405
metadata: MetadataParam | Omit = omit,
@@ -2412,6 +2447,7 @@ async def create(
24122447
"max_tokens": max_tokens,
24132448
"messages": messages,
24142449
"model": model,
2450+
"cache_control": cache_control,
24152451
"container": container,
24162452
"inference_geo": inference_geo,
24172453
"metadata": metadata,
@@ -2445,6 +2481,7 @@ def stream(
24452481
max_tokens: int,
24462482
messages: Iterable[MessageParam],
24472483
model: ModelParam,
2484+
cache_control: Optional[CacheControlEphemeralParam] | Omit = omit,
24482485
inference_geo: Optional[str] | Omit = omit,
24492486
metadata: MetadataParam | Omit = omit,
24502487
output_config: OutputConfigParam | Omit = omit,
@@ -2522,6 +2559,7 @@ def stream(
25222559
"max_tokens": max_tokens,
25232560
"messages": messages,
25242561
"model": model,
2562+
"cache_control": cache_control,
25252563
"inference_geo": inference_geo,
25262564
"metadata": metadata,
25272565
"output_config": merged_output_config,
@@ -2674,6 +2712,7 @@ async def count_tokens(
26742712
*,
26752713
messages: Iterable[MessageParam],
26762714
model: ModelParam,
2715+
cache_control: Optional[CacheControlEphemeralParam] | Omit = omit,
26772716
output_config: OutputConfigParam | Omit = omit,
26782717
output_format: None | JSONOutputFormatParam | type | Omit = omit,
26792718
system: Union[str, Iterable[TextBlockParam]] | Omit = omit,
@@ -2767,6 +2806,9 @@ async def count_tokens(
27672806
[models](https://docs.anthropic.com/en/docs/models-overview) for additional
27682807
details and options.
27692808
2809+
cache_control: Top-level cache control automatically applies a cache_control marker to the last
2810+
cacheable block in the request.
2811+
27702812
output_config: Configuration options for the model's output, such as the output format.
27712813
27722814
@@ -2910,6 +2952,7 @@ async def count_tokens(
29102952
"model": model,
29112953
"messages": messages,
29122954
"model": model,
2955+
"cache_control": cache_control,
29132956
"output_config": merged_output_config,
29142957
"system": system,
29152958
"thinking": thinking,

src/anthropic/types/beta/message_count_tokens_params.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from .beta_tool_bash_20241022_param import BetaToolBash20241022Param
2020
from .beta_tool_bash_20250124_param import BetaToolBash20250124Param
2121
from .beta_memory_tool_20250818_param import BetaMemoryTool20250818Param
22+
from .beta_cache_control_ephemeral_param import BetaCacheControlEphemeralParam
2223
from .beta_web_fetch_tool_20250910_param import BetaWebFetchTool20250910Param
2324
from .beta_web_fetch_tool_20260209_param import BetaWebFetchTool20260209Param
2425
from .beta_web_search_tool_20250305_param import BetaWebSearchTool20250305Param
@@ -117,6 +118,12 @@ class MessageCountTokensParams(TypedDict, total=False):
117118
details and options.
118119
"""
119120

121+
cache_control: Optional[BetaCacheControlEphemeralParam]
122+
"""
123+
Top-level cache control automatically applies a cache_control marker to the last
124+
cacheable block in the request.
125+
"""
126+
120127
context_management: Optional[BetaContextManagementConfigParam]
121128
"""Context management configuration.
122129

src/anthropic/types/beta/message_create_params.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from .beta_output_config_param import BetaOutputConfigParam
2020
from .beta_thinking_config_param import BetaThinkingConfigParam
2121
from .beta_json_output_format_param import BetaJSONOutputFormatParam
22+
from .beta_cache_control_ephemeral_param import BetaCacheControlEphemeralParam
2223
from .beta_context_management_config_param import BetaContextManagementConfigParam
2324
from .beta_request_mcp_server_url_definition_param import BetaRequestMCPServerURLDefinitionParam
2425

@@ -117,6 +118,12 @@ class MessageCreateParamsBase(TypedDict, total=False):
117118
details and options.
118119
"""
119120

121+
cache_control: Optional[BetaCacheControlEphemeralParam]
122+
"""
123+
Top-level cache control automatically applies a cache_control marker to the last
124+
cacheable block in the request.
125+
"""
126+
120127
container: Optional[Container]
121128
"""Container identifier for reuse across requests."""
122129

src/anthropic/types/beta/messages/batch_create_params.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class BatchCreateParams(TypedDict, total=False):
2323
"""Optional header to specify the beta version(s) you want to use."""
2424

2525

26+
2627
class Request(TypedDict, total=False):
2728
custom_id: Required[str]
2829
"""Developer-provided ID created for each request in a Message Batch.

src/anthropic/types/message_count_tokens_params.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from typing import Union, Iterable
5+
from typing import Union, Iterable, Optional
66
from typing_extensions import Required, TypedDict
77

88
from .model_param import ModelParam
@@ -11,6 +11,7 @@
1111
from .tool_choice_param import ToolChoiceParam
1212
from .output_config_param import OutputConfigParam
1313
from .thinking_config_param import ThinkingConfigParam
14+
from .cache_control_ephemeral_param import CacheControlEphemeralParam
1415
from .message_count_tokens_tool_param import MessageCountTokensToolParam
1516

1617
__all__ = ["MessageCountTokensParams"]
@@ -92,6 +93,12 @@ class MessageCountTokensParams(TypedDict, total=False):
9293
details and options.
9394
"""
9495

96+
cache_control: Optional[CacheControlEphemeralParam]
97+
"""
98+
Top-level cache control automatically applies a cache_control marker to the last
99+
cacheable block in the request.
100+
"""
101+
95102
output_config: OutputConfigParam
96103
"""Configuration options for the model's output, such as the output format."""
97104

src/anthropic/types/message_create_params.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from .tool_choice_any_param import ToolChoiceAnyParam
1818
from .tool_choice_auto_param import ToolChoiceAutoParam
1919
from .tool_choice_tool_param import ToolChoiceToolParam
20+
from .cache_control_ephemeral_param import CacheControlEphemeralParam
2021

2122
__all__ = [
2223
"MessageCreateParamsBase",
@@ -116,6 +117,12 @@ class MessageCreateParamsBase(TypedDict, total=False):
116117
details and options.
117118
"""
118119

120+
cache_control: Optional[CacheControlEphemeralParam]
121+
"""
122+
Top-level cache control automatically applies a cache_control marker to the last
123+
cacheable block in the request.
124+
"""
125+
119126
container: Optional[str]
120127
"""Container identifier for reuse across requests."""
121128

src/anthropic/types/messages/batch_create_params.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class BatchCreateParams(TypedDict, total=False):
1818
"""
1919

2020

21+
2122
class Request(TypedDict, total=False):
2223
custom_id: Required[str]
2324
"""Developer-provided ID created for each request in a Message Batch.

0 commit comments

Comments
 (0)