Skip to content

Commit d85b934

Browse files
committed
fix long context anthropic provider cache_control
1 parent a95075d commit d85b934

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

node/providers/anthropic.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ export class AnthropicProvider implements Provider {
8181
content = m.content.map((c): Anthropic.ContentBlockParam => {
8282
switch (c.type) {
8383
case "text":
84-
return c;
84+
// important to create a new object here so when we attach ephemeral
85+
// cache_control markers we won't mutate the content.
86+
return {
87+
...c,
88+
};
8589
case "tool_use":
8690
return {
8791
id: c.request.id,
@@ -630,15 +634,13 @@ export function placeCacheBreakpoints(messages: MessageParam[]): number {
630634
// however, since we are not accounting for tools or the system prompt, and generally code and technical writing
631635
// tend to have a lower coefficient of string length to tokens (about 3.5 average sting length per token), this means
632636
// that the first cache control should be past the 1024 mark and should be cached.
633-
const powers = highestPowersOfTwo(tokens, 3).filter((n) => n >= 1024);
634-
if (powers.length) {
635-
for (const power of powers) {
636-
const targetLength = power * STR_CHARS_PER_TOKEN; // power is in tokens, but we want string chars instead
637-
// find the first block where we are past the target power
638-
const blockEntry = blocks.find((b) => b.acc > targetLength);
639-
if (blockEntry) {
640-
blockEntry.block.cache_control = { type: "ephemeral" };
641-
}
637+
const powers = highestPowersOfTwo(tokens, 4).filter((n) => n >= 1024);
638+
for (const power of powers) {
639+
const targetLength = power * STR_CHARS_PER_TOKEN; // power is in tokens, but we want string chars instead
640+
// find the first block where we are past the target power
641+
const blockEntry = blocks.find((b) => b.acc > targetLength);
642+
if (blockEntry) {
643+
blockEntry.block.cache_control = { type: "ephemeral" };
642644
}
643645
}
644646

0 commit comments

Comments
 (0)