Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/server-compaction-model-settings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@openai/agents-core': patch
'@openai/agents-openai': patch
---

feat: add model settings support for context management
1 change: 1 addition & 0 deletions packages/agents-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export {
ModelRequest,
ModelResponse,
ModelSettings,
ModelSettingsContextManagement,
ModelSettingsToolChoice,
RetryDecision,
RetryPolicy,
Expand Down
26 changes: 26 additions & 0 deletions packages/agents-core/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@ export interface ModelSettingsText {
verbosity?: 'low' | 'medium' | 'high' | null;
}

export type ModelSettingsContextManagement = Array<{
/**
* The context-management strategy to apply.
*/
type: 'compaction' | (string & {});

/**
* Rendered-token threshold that triggers server-side compaction.
*/
compactThreshold?: number;

/**
* Rendered-token threshold that triggers server-side compaction.
*/
compact_threshold?: number;

[key: string]: unknown;
}>;

export type RetryDecision =
| boolean
| {
Expand Down Expand Up @@ -281,6 +300,13 @@ export type ModelSettings = {
*/
promptCacheRetention?: 'in-memory' | '24h' | null;

/**
* Context-management strategies to apply when calling the model.
* This setting is available on OpenAI Responses requests, including server-side compaction.
* See https://developers.openai.com/api/docs/guides/compaction.
*/
contextManagement?: ModelSettingsContextManagement;

/**
* The reasoning settings to use when calling the model.
*/
Expand Down
14 changes: 14 additions & 0 deletions packages/agents-openai/src/openaiResponsesModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
SerializedTool,
ModelRequest,
ModelResponse,
ModelSettingsContextManagement,
ModelSettingsToolChoice,
ResponseStreamEvent,
SerializedOutputType,
Expand Down Expand Up @@ -700,6 +701,16 @@ function getResponseFormat(
};
}

function getContextManagement(
contextManagement: ModelSettingsContextManagement | undefined,
): unknown {
if (!contextManagement) {
return undefined;
}

return contextManagement.map((entry) => camelOrSnakeToSnakeCase(entry));
}

function normalizeFunctionCallOutputForRequest(
output: protocol.FunctionCallResultItem['output'],
): string | ResponseFunctionCallOutputListItem[] {
Expand Down Expand Up @@ -3026,6 +3037,9 @@ export class OpenAIResponsesModel implements Model {
text: responseFormat,
store: request.modelSettings.store,
prompt_cache_retention: request.modelSettings.promptCacheRetention,
context_management: getContextManagement(
request.modelSettings.contextManagement,
),
...restOfProviderData,
};

Expand Down
34 changes: 34 additions & 0 deletions packages/agents-openai/test/openaiResponsesModel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,40 @@ describe('OpenAIResponsesModel', () => {
});
});

it('sends context management settings to the Responses API', async () => {
await withTrace('test', async () => {
const fakeResponse = { id: 'res-context', usage: {}, output: [] };
const createMock = vi.fn().mockResolvedValue(fakeResponse);
const fakeClient = {
responses: { create: createMock },
} as unknown as OpenAI;
const model = new OpenAIResponsesModel(fakeClient, 'gpt-context');

const request = {
systemInstructions: undefined,
input: 'hello',
modelSettings: {
contextManagement: [{ type: 'compaction', compactThreshold: 200000 }],
},
tools: [],
outputType: 'text',
handoffs: [],
tracing: false,
signal: undefined,
};

await model.getResponse(request as any);

const [args] = createMock.mock.calls[0];
expect(args.context_management).toEqual([
{
type: 'compaction',
compact_threshold: 200000,
},
]);
});
});

it('still sends an empty tools array when no prompt is provided', async () => {
await withTrace('test', async () => {
const fakeResponse = { id: 'res-no-prompt', usage: {}, output: [] };
Expand Down
Loading