Skip to content

Conversation

@PederHP
Copy link
Contributor

@PederHP PederHP commented Dec 14, 2025

For agentic loops the cost difference between caching and not caching is massive, and it's really awkward to use caching with the extensions. And not really possible at all at the system-level (because of how it converts without using the raw representation factory). Using raw representation factory also means one has to re-implement the mapping from MEAI to Anthropic content types. Instead this PR adds a new extension method that uses AdditionalProperties to store the CacheControl and then uses it while mapping in the existing extension code.

@PederHP PederHP requested a review from a team as a code owner December 14, 2025 11:52
@PederHP PederHP force-pushed the feature/meai-cache-control-extension branch 3 times, most recently from b38ea33 to 6a6e29c Compare December 14, 2025 12:49
@PederHP
Copy link
Contributor Author

PederHP commented Dec 14, 2025

Let me know if you want a separate extension for beta and non-beta cache control (currently the outer one is shared). I also added a new file. I can fold it into the extensions file, but it's getting massive, so I think if it's not ok to have more than one top-level MEAI related file - then maybe there should be a folder? Otherwise the file is just going to keep growing and get more and more unwieldy.

@stainless-app stainless-app bot force-pushed the next branch 2 times, most recently from a6ccfa7 to 007b0c0 Compare December 18, 2025 22:39
Add AIContentCacheExtensions with WithCacheControl() methods that allow
setting Anthropic prompt caching on AIContent instances via
AdditionalProperties.

Changes:
- New AIContentCacheExtensions.cs with WithCacheControl(CacheControlEphemeral)
  and WithCacheControl(TTL) extension methods
- Update CreateMessageParams in both AnthropicClientExtensions and
  AnthropicBetaClientExtensions to apply cache control when mapping AIContent
  to Anthropic content block params (TextBlockParam, ImageBlockParam,
  DocumentBlockParam, ToolUseBlockParam, ToolResultBlockParam)
- Fix system message handling in both clients to support cache control
- Add unit tests for extension methods and request serialization

The Beta client converts from CacheControlEphemeral to BetaCacheControlEphemeral
internally, so users can use the same extension methods with both clients.

Usage:
```csharp
var systemContent = new TextContent(prompt).WithCacheControl(TTL.TTL1h);
var lastContent = messages[^1].Contents.Last().WithCacheControl(TTL.TTL5m);
```

Note: ThinkingBlockParam/BetaThinkingBlockParam and RedactedThinkingBlockParam/
BetaRedactedThinkingBlockParam do not support cache control in the Anthropic API.
@PederHP PederHP force-pushed the feature/meai-cache-control-extension branch from 6a6e29c to f88b66b Compare December 19, 2025 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant