Skip to content

Commit 1684042

Browse files
committed
huge optimization for token usage with anthropic
1 parent 59f0004 commit 1684042

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed
Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
import type { CoreMessage } from "ai"
1+
import type { CoreMessage, LanguageModelV1Prompt } from "ai"
2+
import { unique } from "remeda"
23

34
export namespace ProviderTransform {
45
export function message(
5-
msg: CoreMessage,
6-
index: number,
6+
msgs: LanguageModelV1Prompt,
77
providerID: string,
88
modelID: string,
99
) {
10-
if (
11-
(providerID === "anthropic" || modelID.includes("anthropic")) &&
12-
index < 4
13-
) {
14-
msg.providerOptions = {
15-
...msg.providerOptions,
16-
anthropic: {
17-
cacheControl: { type: "ephemeral" },
18-
},
10+
if (providerID === "anthropic" || modelID.includes("anthropic")) {
11+
const system = msgs.filter((msg) => msg.role === "system").slice(0, 2)
12+
const final = msgs.filter((msg) => msg.role !== "system").slice(-2)
13+
14+
for (const msg of unique([...system, ...final])) {
15+
msg.providerMetadata = {
16+
...msg.providerMetadata,
17+
anthropic: {
18+
cacheControl: { type: "ephemeral" },
19+
},
20+
}
1921
}
2022
}
21-
22-
return msg
23+
return msgs
2324
}
2425
}

packages/opencode/src/session/index.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
type CoreMessage,
1515
type UIMessage,
1616
type ProviderMetadata,
17+
wrapLanguageModel,
1718
} from "ai"
1819
import { z, ZodSchema } from "zod"
1920
import { Decimal } from "decimal.js"
@@ -285,9 +286,7 @@ export namespace Session {
285286
parts: toParts(input.parts),
286287
},
287288
]),
288-
].map((msg, i) =>
289-
ProviderTransform.message(msg, i, input.providerID, input.modelID),
290-
),
289+
],
291290
model: model.language,
292291
})
293292
.then((result) => {
@@ -527,12 +526,26 @@ export namespace Session {
527526
...convertToCoreMessages(
528527
msgs.map(toUIMessage).filter((x) => x.parts.length > 0),
529528
),
530-
].map((msg, i) =>
531-
ProviderTransform.message(msg, i, input.providerID, input.modelID),
532-
),
529+
],
533530
temperature: model.info.temperature ? 0 : undefined,
534531
tools: model.info.tool_call === false ? undefined : tools,
535-
model: model.language,
532+
model: wrapLanguageModel({
533+
model: model.language,
534+
middleware: [
535+
{
536+
async transformParams(args) {
537+
if (args.type === "stream") {
538+
args.params.prompt = ProviderTransform.message(
539+
args.params.prompt,
540+
input.providerID,
541+
input.modelID,
542+
)
543+
}
544+
return args.params
545+
},
546+
},
547+
],
548+
}),
536549
})
537550
try {
538551
for await (const value of result.fullStream) {

0 commit comments

Comments
 (0)