Skip to content

Commit d1a6fe9

Browse files
committed
tag last tool w anthropic prompt cache
1 parent 6514498 commit d1a6fe9

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

node/providers/anthropic.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,28 @@ export class AnthropicProvider implements Provider {
105105
};
106106
});
107107

108+
const tools: Anthropic.Tool[] = ToolManager.TOOL_SPECS.map(
109+
(t, idx): Anthropic.Tool => {
110+
if (idx == ToolManager.TOOL_SPECS.length - 1) {
111+
/** Assuming that the ordering when the prompt is sent to anthropic will be:
112+
* system
113+
* tools
114+
*
115+
* So marking the last tool with cache_control should use the first cache breakpoint to tag the end of this opening section.
116+
*/
117+
return {
118+
...t,
119+
input_schema: t.input_schema as Anthropic.Messages.Tool.InputSchema,
120+
cache_control: { type: "ephemeral" },
121+
};
122+
}
123+
return {
124+
...t,
125+
input_schema: t.input_schema as Anthropic.Messages.Tool.InputSchema,
126+
};
127+
},
128+
);
129+
108130
try {
109131
this.request = this.client.messages
110132
.stream({
@@ -116,7 +138,7 @@ export class AnthropicProvider implements Provider {
116138
type: "auto",
117139
disable_parallel_tool_use: false,
118140
},
119-
tools: ToolManager.TOOL_SPECS as Anthropic.Tool[],
141+
tools,
120142
})
121143
.on("text", (text: string) => {
122144
buf.push(text);

0 commit comments

Comments
 (0)