Skip to content

Commit ef0a6d8

Browse files
authored
feat: Add prompt_cache_retention option to ModelSettings (#668)
1 parent 078ab38 commit ef0a6d8

File tree

6 files changed

+72
-0
lines changed

6 files changed

+72
-0
lines changed

.changeset/thirty-clubs-smell.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@openai/agents-openai': patch
3+
'@openai/agents-core': patch
4+
---
5+
6+
feat: Add prompt_cache_retention option to ModelSettings

packages/agents-core/src/model.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ export type ModelSettings = {
122122
*/
123123
store?: boolean;
124124

125+
/**
126+
* Enables prompt caching and controls how long cached content should be retained by the model provider.
127+
* See https://platform.openai.com/docs/guides/prompt-caching#prompt-cache-retention for the available options.
128+
*/
129+
promptCacheRetention?: 'in-memory' | '24h' | null;
130+
125131
/**
126132
* The reasoning settings to use when calling the model.
127133
*/

packages/agents-openai/src/openaiChatCompletionsModel.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ export class OpenAIChatCompletionsModel implements Model {
337337
parallel_tool_calls: parallelToolCalls,
338338
stream,
339339
store: request.modelSettings.store,
340+
prompt_cache_retention: request.modelSettings.promptCacheRetention,
340341
...providerData,
341342
};
342343

packages/agents-openai/src/openaiResponsesModel.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,6 +1622,7 @@ export class OpenAIResponsesModel implements Model {
16221622
stream,
16231623
text: responseFormat,
16241624
store: request.modelSettings.store,
1625+
prompt_cache_retention: request.modelSettings.promptCacheRetention,
16251626
...restOfProviderData,
16261627
};
16271628

packages/agents-openai/test/openaiChatCompletionsModel.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,37 @@ describe('OpenAIChatCompletionsModel', () => {
137137
]);
138138
});
139139

140+
it('sends prompt cache retention when provided', async () => {
141+
const client = new FakeClient();
142+
const response = {
143+
id: 'r',
144+
choices: [{ message: { content: 'cached' } }],
145+
usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 },
146+
} as any;
147+
client.chat.completions.create.mockResolvedValue(response);
148+
149+
const model = new OpenAIChatCompletionsModel(client as any, 'gpt');
150+
const req: any = {
151+
input: 'u',
152+
modelSettings: {
153+
promptCacheRetention: '24h',
154+
},
155+
tools: [],
156+
outputType: 'text',
157+
handoffs: [],
158+
tracing: false,
159+
};
160+
161+
await withTrace('t', () => model.getResponse(req));
162+
163+
expect(client.chat.completions.create).toHaveBeenCalledWith(
164+
expect.objectContaining({
165+
prompt_cache_retention: '24h',
166+
}),
167+
{ headers: HEADERS, signal: undefined },
168+
);
169+
});
170+
140171
it('handles refusal message', async () => {
141172
const client = new FakeClient();
142173
const response = {

packages/agents-openai/test/openaiResponsesModel.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,33 @@ describe('OpenAIResponsesModel', () => {
7474
});
7575
});
7676

77+
it('sends prompt cache retention setting to the Responses API', async () => {
78+
await withTrace('test', async () => {
79+
const fakeResponse = { id: 'res-cache', usage: {}, output: [] };
80+
const createMock = vi.fn().mockResolvedValue(fakeResponse);
81+
const fakeClient = {
82+
responses: { create: createMock },
83+
} as unknown as OpenAI;
84+
const model = new OpenAIResponsesModel(fakeClient, 'gpt-cache');
85+
86+
const request = {
87+
systemInstructions: undefined,
88+
input: 'hello',
89+
modelSettings: { promptCacheRetention: 'in-memory' },
90+
tools: [],
91+
outputType: 'text',
92+
handoffs: [],
93+
tracing: false,
94+
signal: undefined,
95+
};
96+
97+
await model.getResponse(request as any);
98+
99+
const [args] = createMock.mock.calls[0];
100+
expect(args.prompt_cache_retention).toBe('in-memory');
101+
});
102+
});
103+
77104
it('still sends an empty tools array when no prompt is provided', async () => {
78105
await withTrace('test', async () => {
79106
const fakeResponse = { id: 'res-no-prompt', usage: {}, output: [] };

0 commit comments

Comments
 (0)