Skip to content

Commit 9ca650f

Browse files
authored
refactor(ai): promote prompt cache key (anomalyco#39965)
1 parent db3b54a commit 9ca650f

21 files changed

Lines changed: 38 additions & 38 deletions

‎packages/ai/README.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,11 +368,12 @@ Other provider exports listed above remain direct facades until they explicitly
368368

369369
## Provider options & HTTP overlays
370370

371-
Three escape hatches in order of stability:
371+
Request options in order of stability:
372372

373373
1. **`generation`** — portable knobs (`maxTokens`, `temperature`, `topP`, `topK`, penalties, seed, stop).
374-
2. **`providerOptions: { <provider>: {...} }`** — typed-at-the-facade provider-specific knobs (OpenAI `promptCacheKey`, Anthropic `thinking`, Gemini `thinkingConfig`, OpenRouter routing).
375-
3. **`http: { body, headers, query }`** — last-resort serializable overlays merged into the final HTTP request. Reach for this only when a stable typed path doesn't yet exist.
374+
2. **`promptCacheKey`** — stable cache affinity lowered by every protocol that supports it.
375+
3. **`providerOptions: { <provider>: {...} }`** — typed-at-the-facade provider-specific knobs (OpenAI `store`, Anthropic `thinking`, Gemini `thinkingConfig`, OpenRouter routing).
376+
4. **`http: { body, headers, query }`** — last-resort serializable overlays merged into the final HTTP request. Reach for this only when a stable typed path doesn't yet exist.
376377

377378
Route/provider defaults are overridden by request-level values for each axis.
378379

‎packages/ai/example/tutorial.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ const model = OpenAI.configure({
3333
//
3434
// - `generation`: common controls such as max tokens, temperature, topP/topK,
3535
// penalties, seed, and stop sequences.
36+
// - `promptCacheKey`: stable cache affinity for protocols that support it.
3637
// - `providerOptions`: namespaced provider-native behavior. For example,
37-
// OpenAI cache keys and store behavior, Anthropic thinking, Gemini thinking
38-
// config, or OpenRouter routing/reasoning.
38+
// OpenAI store behavior, Anthropic thinking, Gemini thinking config, or
39+
// OpenRouter routing/reasoning.
3940
// - `http`: last-resort serializable overlays for final request body, headers,
4041
// and query params. Prefer typed `providerOptions` when a field is stable.
4142
//
@@ -45,9 +46,7 @@ const request = LLM.request({
4546
system: "You are concise and practical.",
4647
prompt: "Tell me a joke",
4748
generation: { maxTokens: 80, temperature: 0.7 },
48-
providerOptions: {
49-
openai: { promptCacheKey: "tutorial-joke" },
50-
},
49+
promptCacheKey: "tutorial-joke",
5150
})
5251

5352
// 3. `generate` sends the request and collects the event stream into one

‎packages/ai/src/protocols/open-responses.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ const lowerOptions = (request: LLMRequest) => {
539539
return {
540540
...(options.instructions ? { instructions: options.instructions } : {}),
541541
...(options.store !== undefined ? { store: options.store } : {}),
542-
...(options.promptCacheKey ? { prompt_cache_key: options.promptCacheKey } : {}),
542+
...(request.promptCacheKey ? { prompt_cache_key: request.promptCacheKey } : {}),
543543
...(options.include ? { include: options.include } : {}),
544544
...(options.reasoningEffort || options.reasoningSummary
545545
? { reasoning: { effort: options.reasoningEffort, summary: options.reasoningSummary } }

‎packages/ai/src/protocols/utils/open-responses-options.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export const ServiceTierSchema = Schema.Literals(ServiceTiers)
3333
export interface Resolved {
3434
readonly instructions?: string
3535
readonly store?: boolean
36-
readonly promptCacheKey?: string
3736
readonly reasoningEffort?: string
3837
readonly reasoningSummary?: "auto" | "concise" | "detailed"
3938
readonly include?: ReadonlyArray<ResponseIncludable>
@@ -50,7 +49,6 @@ export const resolve = (request: LLMRequest): Resolved => {
5049
return {
5150
instructions: typeof input?.instructions === "string" ? input.instructions : undefined,
5251
store: typeof input?.store === "boolean" ? input.store : undefined,
53-
promptCacheKey: typeof input?.promptCacheKey === "string" ? input.promptCacheKey : undefined,
5452
reasoningEffort: typeof input?.reasoningEffort === "string" ? input.reasoningEffort : undefined,
5553
reasoningSummary:
5654
reasoningSummary === "auto" || reasoningSummary === "concise" || reasoningSummary === "detailed"

‎packages/ai/src/providers/open-responses-options.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ export interface OpenResponsesOptionsInput {
55
readonly [key: string]: unknown
66
readonly instructions?: string
77
readonly store?: boolean
8-
readonly promptCacheKey?: string
98
readonly reasoningEffort?: ReasoningEffort
109
readonly reasoningSummary?: "auto" | "concise" | "detailed"
1110
readonly include?: ReadonlyArray<ResponseIncludable>

‎packages/ai/src/providers/openai-options.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const openAIProviderOptions = (options: OpenAIOptionsInput | undefined): Provide
1717
const openai = Object.fromEntries(
1818
definedEntries({
1919
store: options?.store,
20-
promptCacheKey: options?.promptCacheKey,
2120
reasoningEffort: options?.reasoningEffort,
2221
reasoningSummary: options?.reasoningSummary,
2322
include: options?.include,

‎packages/ai/src/providers/openrouter.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ export interface OpenRouterOptions {
5555
readonly debug?: Readonly<{ echo_upstream_body?: boolean }>
5656
readonly models?: ReadonlyArray<string>
5757
readonly plugins?: ReadonlyArray<OpenRouterPlugin>
58-
readonly promptCacheKey?: string
5958
readonly provider?: OpenRouterProviderRouting
6059
readonly reasoning?: Readonly<{
6160
enabled?: boolean
@@ -122,6 +121,7 @@ export const protocol = Protocol.make({
122121
...body,
123122
messages,
124123
...bodyOptions(request.providerOptions?.openrouter),
124+
...(request.promptCacheKey ? { prompt_cache_key: request.promptCacheKey } : {}),
125125
} as OpenRouterBody
126126
}),
127127
),
@@ -161,7 +161,6 @@ const bodyOptions = (input: unknown) => {
161161
...(isRecord(debug) ? { debug } : {}),
162162
...(typeof user === "string" ? { user } : {}),
163163
...(isRecord(reasoning) ? { reasoning } : {}),
164-
...(typeof promptCacheKey === "string" ? { prompt_cache_key: promptCacheKey } : {}),
165164
}
166165
}
167166

‎packages/ai/src/schema/messages.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ export class LLMRequest extends Schema.Class<LLMRequest>("LLM.Request")({
272272
providerOptions: Schema.optional(ProviderOptions),
273273
http: Schema.optional(HttpOptions),
274274
cache: Schema.optional(CachePolicy),
275+
// Stable cache affinity for protocols that support provider-managed prompt caching.
276+
promptCacheKey: Schema.optional(Schema.String),
275277
metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
276278
}) {}
277279

@@ -289,6 +291,7 @@ export namespace LLMRequest {
289291
providerOptions: request.providerOptions,
290292
http: request.http,
291293
cache: request.cache,
294+
promptCacheKey: request.promptCacheKey,
292295
metadata: request.metadata,
293296
})
294297

‎packages/ai/test/provider-options/cloudflare.types.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { CloudflareWorkersAI } from "../../src/providers"
33

44
const model = CloudflareWorkersAI.configure({ accountId: "account", apiKey: "test" }).model("model")
55

6-
LLM.request({ model, prompt: "Hello", providerOptions: { openai: { promptCacheKey: "cache" } } })
6+
LLM.request({ model, prompt: "Hello", promptCacheKey: "cache" })
77

88
LLM.request({
99
model,
1010
prompt: "Hello",
11-
// @ts-expect-error Cloudflare's OpenAI-compatible prompt cache key must be a string.
12-
providerOptions: { openai: { promptCacheKey: 1 } },
11+
// @ts-expect-error Prompt cache keys must be strings.
12+
promptCacheKey: 1,
1313
})

‎packages/ai/test/provider/openai-responses-cache.recorded.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const cacheRequest = LLM.request({
2020
system: LARGE_CACHEABLE_SYSTEM,
2121
prompt: "Say hi.",
2222
generation: { maxTokens: 16, temperature: 0 },
23-
providerOptions: { openai: { promptCacheKey: "recorded-cache-test" } },
23+
promptCacheKey: "recorded-cache-test",
2424
})
2525

2626
const recorded = recordedTests({

0 commit comments

Comments
 (0)