Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: adding further support for cache control
  • Loading branch information
andyjmorgan committed Sep 25, 2025
commit a7c2fe985e97be5e8abf3493ee07c5dc9a346d00
19 changes: 16 additions & 3 deletions Anthropic.SDK.Tests/CacheControlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ public async Task TestCacheControlStreaming()
Assert.IsTrue(messageResponses.First().StreamStartMessage.Usage.CacheCreationInputTokens > 0 ||
messageResponses.First().StreamStartMessage.Usage.CacheReadInputTokens > 0);

if (messageResponses.First().StreamStartMessage.Usage.CacheCreationInputTokens > 0)
{
Assert.IsTrue(messageResponses.First().StreamStartMessage.Usage.CacheCreation.Ephemeral5mInputTokens > 0);
}

messages.Add(new Message(messageResponses));
messages.Add(new Message(RoleType.User, "Who is the main character and how old is he?"));

Expand Down Expand Up @@ -487,7 +492,11 @@ public async Task TestCacheControlOfAssistantMessages()
var systemMessages = new List<SystemMessage>()
{
new SystemMessage("You are an expert at analyzing literary texts."),
new SystemMessage(content, new CacheControl() { Type = CacheControlType.ephemeral })
new SystemMessage(content, new CacheControl()
{
Type = CacheControlType.ephemeral,
TTL = CacheControl.CacheDuration1Hour,
})
};
var parameters = new MessageParameters()
{
Expand All @@ -505,15 +514,19 @@ public async Task TestCacheControlOfAssistantMessages()
Assert.IsTrue(res.Usage.CacheCreationInputTokens > 0 || res.Usage.CacheReadInputTokens > 0);

//try caching an assistant message
res.Message.Content.First().CacheControl = new CacheControl() { Type = CacheControlType.ephemeral };
res.Message.Content.First().CacheControl = new CacheControl()
{
Type = CacheControlType.ephemeral,
TTL = CacheControl.CacheDuration1Hour,
};

messages.Add(res.Message);
messages.Add(new Message(RoleType.User, "Who is the main character and how old is he?"));

var res2 = await client.Messages.GetClaudeMessageAsync(parameters);

Assert.IsTrue(res2.Usage.CacheReadInputTokens > 0);

Assert.IsTrue(res2.Usage.CacheCreation.Ephemeral1hInputTokens > 0);
Assert.IsNotNull(res2.Message.ToString());

//message 3
Expand Down
18 changes: 8 additions & 10 deletions Anthropic.SDK.Tests/DocumentTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using Anthropic.SDK.Constants;
using Anthropic.SDK.Messaging;

Expand Down Expand Up @@ -41,7 +35,8 @@ public async Task TestPDF()
},
CacheControl = new CacheControl()
{
Type = CacheControlType.ephemeral
Type = CacheControlType.ephemeral,
TTL = CacheControl.CacheDuration5Minutes,
}
}),
new Message(RoleType.User, "Which model has the highest human preference win rates across each use-case?"),
Expand All @@ -60,8 +55,11 @@ public async Task TestPDF()

Assert.IsNotNull(res.FirstMessage.ToString());
Assert.IsTrue(res.Usage.CacheCreationInputTokens > 0 || res.Usage.CacheReadInputTokens > 0);


if (res.Usage.CacheCreationInputTokens > 0)
{
Assert.IsNotNull(res.Usage.CacheCreation);
Assert.IsTrue(res.Usage.CacheCreation.Ephemeral5mInputTokens > 0);
}
}

[TestMethod]
Expand Down
10 changes: 10 additions & 0 deletions Anthropic.SDK/Messaging/CacheControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@ namespace Anthropic.SDK.Messaging;

public class CacheControl
{
public static string CacheDuration5Minutes = "5m";
public static string CacheDuration1Hour = "1h";

[JsonPropertyName("type")]
public CacheControlType Type { get; set; }

/// <summary>
/// The duration to cache.
/// Supported values are <see cref="CacheDuration5Minutes"/> or <see cref="CacheDuration1Hour"/>
/// </summary>
[JsonPropertyName("ttl")]
public string TTL { get; set; }
}

[JsonConverter(typeof(JsonStringEnumConverter))]
Expand Down
15 changes: 15 additions & 0 deletions Anthropic.SDK/Messaging/MessageResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,26 @@ public class Usage

[JsonPropertyName("server_tool_use")]
public ServerToolUse ServerToolUse { get; set; }

[JsonPropertyName("cache_creation")]
public CacheCreation CacheCreation { get; set; }
}

public class CacheCreation
{
[JsonPropertyName("ephemeral_5m_input_tokens")]
public int? Ephemeral5mInputTokens { get; set; }

[JsonPropertyName("ephemeral_1h_input_tokens")]
public int? Ephemeral1hInputTokens { get; set; }
}

public class ServerToolUse
{
[JsonPropertyName("web_search_requests")]
public int? WebSearchRequests { get; set; }

[JsonPropertyName("web_fetch_requests")]
public int? WebFetchRequests { get; set; }
}
}