Add `openai_cache_instructions` to place a prompt cache breakpoint on the instructions
- Closes #7128
Adds openai_cache_instructions to OpenAIChatModelSettings / OpenAIResponsesModelSettings (bool, opt-in, ignored unless the model profile has openai_supports_prompt_cache_breakpoints, i.e. GPT-5.6 and GPT-6). It places a prompt_cache_breakpoint after the last static instruction, matching anthropic_cache_instructions / bedrock_cache_instructions: dynamic instructions (@agent.instructions, toolsets) stay after the breakpoint so they don't invalidate it.
Chat Completions
Instructions are already leading system/developer messages; the breakpoint is added to the last static one. Skipped when the system prompt role is 'user', when the profile merges leading system messages, or when a dynamic system prompt precedes the instructions.
Responses
The top-level instructions field can't carry a breakpoint, so the instructions are moved into leading input messages (role from openai_system_prompt_role) with the breakpoint on the last static one:
instructions: omitted
input: [
{role: "system", content: [{type: "input_text", text: "Support policies...", prompt_cache_breakpoint: {mode: "explicit"}}]},
{role: "user", content: "Where is order 1234?"},
]
That only happens on requests that don't continue server-side state. When openai_previous_response_id (including 'auto') or openai_conversation_id is set, or the history has been compacted, the instructions stay in the top-level field and no breakpoint is added. That is decided by the settings, not by what 'auto' resolves to, so with 'auto' no request gets an instruction breakpoint, not even the first. The reason: OpenAI replays a stored response's input to the request that continues it, but not its top-level instructions. Relocated instructions would therefore be replayed next to the next request's own instructions, and in a handoff that means a later agent keeps running under the earlier agent's policy.
Cost of that rule: agent runs send the full message history by default, so it only affects runs that opt into server-side state. Those requests still get OpenAI's implicit breakpoint with the default mode='implicit'. What they give up is an instruction breakpoint that separate conversations can share.
Known limitation (documented): a response created with openai_cache_instructions and without openai_previous_response_id stores the relocated instructions in its input. If a later request continues it through openai_previous_response_id, those instructions are replayed. It's the same thing that already happens with system_prompt parts and with prompted-output instructions on main.
An earlier revision of this PR reconciled chains instead: it stamped instructions_relocated on responses and compared instruction text against the chained request. That layer kept producing edge-case bugs (background resume, prompted output, handoffs to non-caching agents), so it was removed in favor of the rule above.
Verification
Mocked-request tests in tests/models/test_openai_prompt_cache.py pin the wire shape for both APIs, including:
test_openai_responses_cache_instructions_skipped_with_previous_response_id:'auto'chaining keeps instructions top-level on every requesttest_openai_responses_cache_instructions_handoff_to_chained_agent: a later agent without the setting chains and runs under its own instructions onlytest_openai_responses_cache_instructions_skipped_with_explicit_previous_response_id/_skipped_with_conversation_id/_not_relocated_after_compactiontest_openai_responses_cache_instructions_with_prompted_output: the JSON-mode instruction trick doesn't duplicate relocated instructionstest_openai_responses_cache_instructions_relocated_once_per_request_in_tool_loop- static/dynamic split, all-dynamic fallback, developer role,
'user'role, merged system messages, dynamic system prompt, unsupported models test_openai_cache_instructions_unsorted_parts_keep_dynamic_out_of_prefix: a directModel.requestwith unsorted parts still puts the breakpoint after the leading static ones (Chat and Responses)test_openai_responses_cache_instructions_with_leading_tool_reveal: a leadingadditional_toolsitem (also'developer'role) isn't treated as a system prompt
What changes for existing users
Nothing unless they opt into openai_cache_instructions=True.
Checklist
- Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it.
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
- PR title is fit for the release changelog.