Skip to content

Commit 79dabaa

Browse files
authored
feat!: Add support for prompt caching in anthropic_sdk_dart (#587)
1 parent 36c4a3e commit 79dabaa

File tree

14 files changed

+1870
-487
lines changed

14 files changed

+1870
-487
lines changed

packages/anthropic_sdk_dart/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,45 @@ const request = CreateMessageRequest(
284284
final res = await client.createMessage(request: request);
285285
```
286286

287+
### Prompt caching
288+
289+
Prompt caching is a powerful feature that optimizes your API usage by allowing resuming from specific prefixes in your prompts. This approach significantly reduces processing time and costs for repetitive tasks or prompts with consistent elements.
290+
291+
Refer to the [official documentation](https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching) for more information.
292+
293+
Example:
294+
```dart
295+
final request = CreateMessageRequest(
296+
model: Model.model(Models.claude35Sonnet20241022),
297+
system: CreateMessageRequestSystem.blocks([
298+
Block.text(
299+
text:
300+
'You are an AI assistant tasked with analyzing literary works. '
301+
'Your goal is to provide insightful commentary on themes, characters, and writing style.',
302+
),
303+
Block.text(
304+
cacheControl: CacheControlEphemeral(),
305+
text: '<The whole text of the book>',
306+
),
307+
]),
308+
messages: [
309+
Message(
310+
role: MessageRole.user,
311+
content: MessageContent.text("What's the theme of the work?"),
312+
),
313+
],
314+
maxTokens: 1024,
315+
);
316+
317+
final res1 = await client.createMessage(request: request);
318+
print(res1.usage?.cacheCreationInputTokens); // 5054
319+
print(res1.usage?.cacheReadInputTokens); // 0
320+
321+
final res2 = await client.createMessage(request: request);
322+
print(res2.usage?.cacheCreationInputTokens); // 0
323+
print(res2.usage?.cacheReadInputTokens); // 5054
324+
```
325+
287326
### Message Batches
288327

289328
The Message Batches API is a powerful, cost-effective way to asynchronously process large volumes of Messages requests. This approach is well-suited to tasks that do not require immediate responses, reducing costs by 50% while increasing throughput.

packages/anthropic_sdk_dart/lib/src/generated/schema/block.dart

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/anthropic_sdk_dart/lib/src/generated/schema/cache_control_ephemeral.dart

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/anthropic_sdk_dart/lib/src/generated/schema/create_message_request.dart

Lines changed: 64 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/anthropic_sdk_dart/lib/src/generated/schema/schema.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)