-
Notifications
You must be signed in to change notification settings - Fork 9
feat(chat-workflow): Anthropic prompt cache control (cutover Bundle A.6) #599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
lib/agent/contextManagement/__tests__/addCacheControlToMessages.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { addCacheControlToMessages } from "@/lib/agent/contextManagement/addCacheControlToMessages"; | ||
|
|
||
| const anthropicModel = { provider: "anthropic", modelId: "claude-haiku-4.5" } as never; | ||
| const openaiModel = { provider: "openai", modelId: "gpt-5" } as never; | ||
|
|
||
| const makeMsgs = () => [ | ||
| { role: "user", content: "first" }, | ||
| { role: "assistant", content: "ack" }, | ||
| { role: "user", content: "second" }, | ||
| ]; | ||
|
|
||
| describe("addCacheControlToMessages", () => { | ||
| it("returns messages unchanged for non-Anthropic models", () => { | ||
| const messages = makeMsgs(); | ||
| const result = addCacheControlToMessages({ messages: messages as never, model: openaiModel }); | ||
| expect(result).toEqual(messages); | ||
| }); | ||
|
|
||
| it("returns messages unchanged when the array is empty", () => { | ||
| const result = addCacheControlToMessages({ messages: [], model: anthropicModel }); | ||
| expect(result).toEqual([]); | ||
| }); | ||
|
|
||
| it("marks ONLY the last message with ephemeral cacheControl (per Anthropic guidance)", () => { | ||
| const messages = makeMsgs(); | ||
| const result = addCacheControlToMessages({ | ||
| messages: messages as never, | ||
| model: anthropicModel, | ||
| }) as Array<{ providerOptions?: { anthropic?: { cacheControl?: { type: string } } } }>; | ||
| expect(result[0]?.providerOptions).toBeUndefined(); | ||
| expect(result[1]?.providerOptions).toBeUndefined(); | ||
| expect(result[2]?.providerOptions?.anthropic?.cacheControl).toEqual({ type: "ephemeral" }); | ||
| }); | ||
|
|
||
| it("preserves existing providerOptions on the last message when merging the anthropic marker", () => { | ||
| const messages = [ | ||
| { role: "user", content: "first" }, | ||
| { | ||
| role: "user", | ||
| content: "second", | ||
| providerOptions: { openai: { foo: "bar" } }, | ||
| }, | ||
| ]; | ||
| const result = addCacheControlToMessages({ | ||
| messages: messages as never, | ||
| model: anthropicModel, | ||
| }) as Array<{ providerOptions?: Record<string, unknown> }>; | ||
| expect(result[1]?.providerOptions?.openai).toEqual({ foo: "bar" }); | ||
| expect(result[1]?.providerOptions?.anthropic).toEqual({ | ||
| cacheControl: { type: "ephemeral" }, | ||
| }); | ||
| }); | ||
|
|
||
| it("does NOT mutate the input messages array", () => { | ||
| const messages = makeMsgs(); | ||
| addCacheControlToMessages({ messages: messages as never, model: anthropicModel }); | ||
| expect((messages[2] as { providerOptions?: unknown }).providerOptions).toBeUndefined(); | ||
| }); | ||
| }); |
63 changes: 63 additions & 0 deletions
63
lib/agent/contextManagement/__tests__/addCacheControlToTools.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { addCacheControlToTools } from "@/lib/agent/contextManagement/addCacheControlToTools"; | ||
|
|
||
| const anthropicModel = { provider: "anthropic", modelId: "claude-haiku-4.5" } as never; | ||
| const openaiModel = { provider: "openai", modelId: "gpt-5" } as never; | ||
|
|
||
| const makeTools = () => ({ | ||
| bash: { description: "run bash", inputSchema: {} }, | ||
| read: { description: "read file", inputSchema: {} }, | ||
| write: { description: "write file", inputSchema: {} }, | ||
| }); | ||
|
|
||
| describe("addCacheControlToTools", () => { | ||
| it("returns tools unchanged for non-Anthropic models", () => { | ||
| const tools = makeTools(); | ||
| const result = addCacheControlToTools({ tools, model: openaiModel }); | ||
| expect(result).toEqual(tools); | ||
| }); | ||
|
|
||
| it("returns tools unchanged when the toolset is empty", () => { | ||
| const tools = {}; | ||
| const result = addCacheControlToTools({ tools, model: anthropicModel }); | ||
| expect(result).toEqual({}); | ||
| }); | ||
|
|
||
| it("marks ONLY the last tool with ephemeral cacheControl (Anthropic's 4-breakpoint limit)", () => { | ||
| const tools = makeTools(); | ||
| const result = addCacheControlToTools({ tools, model: anthropicModel }) as Record< | ||
| string, | ||
| { providerOptions?: { anthropic?: { cacheControl?: { type: string } } } } | ||
| >; | ||
| expect(result.bash?.providerOptions).toBeUndefined(); | ||
| expect(result.read?.providerOptions).toBeUndefined(); | ||
| expect(result.write?.providerOptions?.anthropic?.cacheControl).toEqual({ type: "ephemeral" }); | ||
| }); | ||
|
|
||
| it("preserves existing providerOptions on the last tool when merging the anthropic marker", () => { | ||
| const tools = { | ||
| a: { description: "a", inputSchema: {} }, | ||
| b: { | ||
| description: "b", | ||
| inputSchema: {}, | ||
| providerOptions: { openai: { foo: "bar" } }, | ||
| }, | ||
| } as never; | ||
| const result = addCacheControlToTools({ tools, model: anthropicModel }) as Record< | ||
| string, | ||
| { providerOptions?: Record<string, unknown> } | ||
| >; | ||
| expect(result.b?.providerOptions?.openai).toEqual({ foo: "bar" }); | ||
| expect(result.b?.providerOptions?.anthropic).toEqual({ cacheControl: { type: "ephemeral" } }); | ||
| }); | ||
|
|
||
| it("respects a custom providerOptions override", () => { | ||
| const tools = { only: { description: "x", inputSchema: {} } } as never; | ||
| const result = addCacheControlToTools({ | ||
| tools, | ||
| model: anthropicModel, | ||
| providerOptions: { anthropic: { cacheControl: { type: "ephemeral_1h" } } }, | ||
| }) as Record<string, { providerOptions?: { anthropic?: { cacheControl?: { type: string } } } }>; | ||
| expect(result.only?.providerOptions?.anthropic?.cacheControl).toEqual({ type: "ephemeral_1h" }); | ||
| }); | ||
| }); |
36 changes: 36 additions & 0 deletions
36
lib/agent/contextManagement/__tests__/isAnthropicModel.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { isAnthropicModel } from "@/lib/agent/contextManagement/isAnthropicModel"; | ||
|
|
||
| describe("isAnthropicModel", () => { | ||
| it("returns true for a string model id containing 'anthropic'", () => { | ||
| expect(isAnthropicModel("anthropic/claude-haiku-4.5" as never)).toBe(true); | ||
| }); | ||
|
|
||
| it("returns true for a string model id containing 'claude' (no provider prefix)", () => { | ||
| expect(isAnthropicModel("claude-3-5-haiku" as never)).toBe(true); | ||
| }); | ||
|
|
||
| it("returns false for non-Anthropic string model ids", () => { | ||
| expect(isAnthropicModel("openai/gpt-5.2" as never)).toBe(false); | ||
| expect(isAnthropicModel("google/gemini-3" as never)).toBe(false); | ||
| }); | ||
|
|
||
| it("returns true for a model object whose `provider` is 'anthropic'", () => { | ||
| expect(isAnthropicModel({ provider: "anthropic", modelId: "claude-haiku-4.5" } as never)).toBe( | ||
| true, | ||
| ); | ||
| }); | ||
|
|
||
| it("returns true for a model object whose `provider` contains 'anthropic' (gateway-prefixed)", () => { | ||
| expect(isAnthropicModel({ provider: "gateway.anthropic", modelId: "x" } as never)).toBe(true); | ||
| }); | ||
|
|
||
| it("returns true for a model object whose `modelId` contains 'anthropic' or 'claude'", () => { | ||
| expect(isAnthropicModel({ provider: "gateway", modelId: "anthropic/x" } as never)).toBe(true); | ||
| expect(isAnthropicModel({ provider: "gateway", modelId: "claude-x" } as never)).toBe(true); | ||
| }); | ||
|
|
||
| it("returns false for a model object with no anthropic / claude markers", () => { | ||
| expect(isAnthropicModel({ provider: "openai", modelId: "gpt-5" } as never)).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import type { JSONValue, LanguageModel, ModelMessage } from "ai"; | ||
| import { isAnthropicModel } from "@/lib/agent/contextManagement/isAnthropicModel"; | ||
|
|
||
| type ProviderOptions = Record<string, Record<string, JSONValue>>; | ||
|
|
||
| const DEFAULT_PROVIDER_OPTIONS: ProviderOptions = { | ||
| anthropic: { cacheControl: { type: "ephemeral" } }, | ||
| }; | ||
|
|
||
| /** | ||
| * Mark the LAST message with `cacheControl: { type: "ephemeral" }` so | ||
| * Anthropic incrementally caches the conversation prefix. Per | ||
| * Anthropic's docs: "Mark the final block of the final message with | ||
| * cache_control so the conversation can be incrementally cached." | ||
| * | ||
| * Port of open-agents' `addCacheControl({messages, model})` overload | ||
| * in `packages/agent/context-management/cache-control.ts`. | ||
| * | ||
| * For non-Anthropic models the input is returned unchanged. The input | ||
| * array is not mutated — a new array of message refs is returned. | ||
| */ | ||
| export function addCacheControlToMessages(opts: { | ||
| messages: ModelMessage[]; | ||
| model: LanguageModel; | ||
| providerOptions?: ProviderOptions; | ||
| }): ModelMessage[] { | ||
| const { messages, model, providerOptions = DEFAULT_PROVIDER_OPTIONS } = opts; | ||
|
|
||
| if (!isAnthropicModel(model)) return messages; | ||
| if (messages.length === 0) return messages; | ||
|
|
||
| const lastIndex = messages.length - 1; | ||
| return messages.map((message, index) => | ||
| index === lastIndex | ||
| ? { | ||
| ...message, | ||
| providerOptions: { | ||
| ...(message as { providerOptions?: ProviderOptions }).providerOptions, | ||
| ...providerOptions, | ||
| }, | ||
| } | ||
| : message, | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import type { JSONValue, LanguageModel, ToolSet } from "ai"; | ||
| import { isAnthropicModel } from "@/lib/agent/contextManagement/isAnthropicModel"; | ||
|
|
||
| type ProviderOptions = Record<string, Record<string, JSONValue>>; | ||
|
|
||
| const DEFAULT_PROVIDER_OPTIONS: ProviderOptions = { | ||
| anthropic: { cacheControl: { type: "ephemeral" } }, | ||
| }; | ||
|
|
||
| /** | ||
| * Mark the LAST tool in a toolset with `cacheControl: { type: "ephemeral" }` | ||
| * so Anthropic caches the tool-definitions block across the conversation. | ||
| * | ||
| * Port of open-agents' `addCacheControl({tools, model})` overload in | ||
| * `packages/agent/context-management/cache-control.ts`. Why only the | ||
| * last tool: Anthropic enforces a max of 4 cache breakpoints, and we | ||
| * spend one each on the system prompt + messages, so we conserve by | ||
| * marking just the trailing tool entry (the message's cumulative | ||
| * cache covers the rest). | ||
| * | ||
| * For non-Anthropic models the input is returned unchanged. | ||
| */ | ||
| export function addCacheControlToTools<T extends ToolSet>(opts: { | ||
| tools: T; | ||
| model: LanguageModel; | ||
| providerOptions?: ProviderOptions; | ||
| }): T { | ||
| const { tools, model, providerOptions = DEFAULT_PROVIDER_OPTIONS } = opts; | ||
|
|
||
| if (!isAnthropicModel(model)) return tools; | ||
|
|
||
| const entries = Object.entries(tools); | ||
| if (entries.length === 0) return tools; | ||
|
|
||
| const lastIndex = entries.length - 1; | ||
| return Object.fromEntries( | ||
| entries.map(([name, t], index) => [ | ||
| name, | ||
| index === lastIndex | ||
| ? { | ||
| ...t, | ||
| providerOptions: { | ||
| ...(t as { providerOptions?: ProviderOptions }).providerOptions, | ||
| ...providerOptions, | ||
| }, | ||
| } | ||
| : t, | ||
| ]), | ||
| ) as T; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import type { LanguageModel } from "ai"; | ||
|
|
||
| /** | ||
| * Predicate: is this a Claude / Anthropic model? Drives whether to | ||
| * attach `cacheControl: { type: "ephemeral" }` to messages + tools | ||
| * (Anthropic prompt caching) or leave them untouched. | ||
| * | ||
| * Byte-for-byte port of open-agents' `isAnthropicModel` | ||
| * (`packages/agent/context-management/cache-control.ts`). | ||
| * | ||
| * Accepts both string model ids (e.g. `"anthropic/claude-haiku-4.5"`) | ||
| * and `LanguageModel` instances (e.g. the value returned from | ||
| * `gateway("anthropic/claude-...")`, which carries `provider` and | ||
| * `modelId` properties). | ||
| */ | ||
| export function isAnthropicModel(model: LanguageModel): boolean { | ||
| if (typeof model === "string") { | ||
| return model.includes("anthropic") || model.includes("claude"); | ||
| } | ||
| return ( | ||
| model.provider === "anthropic" || | ||
| model.provider.includes("anthropic") || | ||
| model.modelId.includes("anthropic") || | ||
| model.modelId.includes("claude") | ||
| ); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Shallow-merging
providerOptionsoverwrites existinganthropicoptions on the last message, so enabling cache control can silently discard other Anthropic message-level settings.Prompt for AI agents