fix(agents): keep Anthropic tool cache breakpoint advancing

This commit is contained in:
Ayaan Zaidi
2026-06-03 14:36:47 +05:30
parent 77c383d1e0
commit 2454952544
3 changed files with 20 additions and 2 deletions
+10 -2
View File
@@ -100,12 +100,13 @@ describe("anthropic payload policy", () => {
type: "tool_result",
tool_use_id: "tool_1",
content: "done",
cache_control: { type: "ephemeral", ttl: "1h" },
},
],
});
});
it("keeps the conversation cache marker on user text through trailing tool results", () => {
it("keeps a stable user marker while advancing through trailing tool results", () => {
const policy = resolveAnthropicPayloadPolicy({
provider: "anthropic",
api: "anthropic-messages",
@@ -157,7 +158,14 @@ describe("anthropic payload policy", () => {
});
expect(payload.messages[4]).toEqual({
role: "user",
content: [{ type: "tool_result", tool_use_id: "tool_2", content: "next chunk" }],
content: [
{
type: "tool_result",
tool_use_id: "tool_2",
content: "next chunk",
cache_control: { type: "ephemeral" },
},
],
});
});
+3
View File
@@ -185,6 +185,9 @@ function applyAnthropicCacheControlToMessages(
const blockRecord = block as Record<string, unknown>;
if (blockRecord.type === "text" || blockRecord.type === "image") {
blockRecord.cache_control = cacheControl;
if (fallbackToolResult) {
fallbackToolResult.cache_control = cacheControl;
}
return;
}
if (blockRecord.type === "tool_result" && fallbackToolResult === undefined) {
+7
View File
@@ -1193,6 +1193,13 @@ function convertMessages(
if (block.type === "text" || block.type === "image") {
(block as typeof block & { cache_control?: typeof cacheControl }).cache_control =
cacheControl;
if (fallbackToolResult) {
(
fallbackToolResult as typeof fallbackToolResult & {
cache_control?: typeof cacheControl;
}
).cache_control = cacheControl;
}
return params;
}
if (block.type === "tool_result" && fallbackToolResult === undefined) {