Repository navigation
fix(openai): cache system prompts on GPT-6.2 Astra and Sol - #201094
Conversation
…-astra gpt-6.2-astra never caches the Responses `instructions` field, so the whole system prompt was billed as plain input on every request. A new model capability, `compat.cachesInstructions: false`, makes the transport send the same prompt as a developer input item, which the provider caches. The item goes directly after the last compaction item (OpenAI drops input that comes before it). It stays in the stored prefix, so continuation deltas do not repeat it and a changed prompt forces a full replay with only the new item. The compact endpoint still receives the prompt as `instructions`, and context-usage accounting finds the item by a marker instead of position. Co-authored-by: obviyus <[email protected]>
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
The Responses image sanitizer rebuilt every mutable input item from its string keys, which dropped the symbol markers that later request stages read. The system-prompt marker was lost before the compact call and context-usage accounting, so the compact call sent the prompt in input. Copy the item with a spread so the markers stay on the copy, and remove the V2 compaction step that restored only the retained-user marker. Co-authored-by: obviyus <[email protected]>
|
Codex review: blocked before merge. What this changesThe PR adds a model capability that moves uncached system instructions into cacheable Responses input while preserving compaction and continuation behavior. Example: A repeated Gateway turn uses openai/gpt-6.2-astra with roughly 9,500 tokens of system instructions.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete Merge readiness⛔ Blocked before merge - 2 items remain Keep this PR open: it addresses measured excess input billing, retains meaningful unique work, and is maintainer-authored; no definite introduced correctness bug was found. Priority: P2 Before merge
FindingsNone. Tests
Agent review detailsHow this fits togetherThe shared Responses transport converts agent prompts and conversation checkpoints into provider requests, then records replies, continuation state, and measured context usage. flowchart LR
A[Agent context] --> B[Catalog compatibility]
B --> C[Responses payload policy]
A --> D[Replay serializer]
C --> D
D --> E[OpenAI Responses transport]
E --> F[Reply and usage]
F --> A
Technical reviewBest possible solution: Retain the shared-policy repair and establish stable-upgrade compatibility before landing it. Do we have a high-confidence way to reproduce the issue? Current main demonstrably sends this model's system prompt through instructions, and the supplied real-provider baseline/head measurements establish the billing consequence; this review ran no target code. Is this the best way to solve the issue? The capability belongs in the existing catalog and payload-policy owners, with checkpoint ordering, compact-endpoint extraction, and symbol preservation forming one coherent repair. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 1fc5296cba84. Provenance checked
TestingProof path: shipped entry point. SecurityNone. EvidenceWhat I checked:
PR surfaceSource +17, Tests +237, Docs +2, Generated +6. Total +262 across 25 files. Added test files: 2. View PR surface stats
Preflight sourceSource unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified. Review metrics
LabelsLabel changes:
Label justifications:
Rating scale6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior. WorkflowClawSweeper edits this one comment on every review. Comment Reviewed November 18, 2026, 4:23 AM ET / 09:23 UTC. |
Co-authored-by: steipete <[email protected]>
Baseline-Refresh: 5f89d3995ea22e5416ab75d4e7d61b66b0019132 Co-authored-by: steipete <[email protected]>
|
Measurement for the Same body sent twice, 3 s apart. Direct Columns:
Result: This comment adds evidence only. No commits on this PR were changed. |
Baseline-Refresh: 17108c014ffaccf5fab1bc16f30e00ba24b4fde9 Co-authored-by: steipete <[email protected]>
|
Merged via squash.
|
* fix(openai): send the system prompt as a cached input item on gpt-6.2-astra gpt-6.2-astra never caches the Responses `instructions` field, so the whole system prompt was billed as plain input on every request. A new model capability, `compat.cachesInstructions: false`, makes the transport send the same prompt as a developer input item, which the provider caches. The item goes directly after the last compaction item (OpenAI drops input that comes before it). It stays in the stored prefix, so continuation deltas do not repeat it and a changed prompt forces a full replay with only the new item. The compact endpoint still receives the prompt as `instructions`, and context-usage accounting finds the item by a marker instead of position. Co-authored-by: obviyus <[email protected]> * fix(ai): keep request provenance markers through image sanitation The Responses image sanitizer rebuilt every mutable input item from its string keys, which dropped the symbol markers that later request stages read. The system-prompt marker was lost before the compact call and context-usage accounting, so the compact call sent the prompt in input. Copy the item with a spread so the markers stay on the copy, and remove the V2 compaction step that restored only the retained-user marker. Co-authored-by: obviyus <[email protected]> * fix(openai): cache native Responses prompts on Astra and Sol only Co-authored-by: steipete <[email protected]> --------- Co-authored-by: obviyus <[email protected]> Co-authored-by: steipete <[email protected]> (cherry picked from commit c5d0e00)
…201094) * fix(openai): send the system prompt as a cached input item on gpt-6.2-astra gpt-6.2-astra never caches the Responses `instructions` field, so the whole system prompt was billed as plain input on every request. A new model capability, `compat.cachesInstructions: false`, makes the transport send the same prompt as a developer input item, which the provider caches. The item goes directly after the last compaction item (OpenAI drops input that comes before it). It stays in the stored prefix, so continuation deltas do not repeat it and a changed prompt forces a full replay with only the new item. The compact endpoint still receives the prompt as `instructions`, and context-usage accounting finds the item by a marker instead of position. Co-authored-by: obviyus <[email protected]> * fix(ai): keep request provenance markers through image sanitation The Responses image sanitizer rebuilt every mutable input item from its string keys, which dropped the symbol markers that later request stages read. The system-prompt marker was lost before the compact call and context-usage accounting, so the compact call sent the prompt in input. Copy the item with a spread so the markers stay on the copy, and remove the V2 compaction step that restored only the retained-user marker. Co-authored-by: obviyus <[email protected]> * fix(openai): cache native Responses prompts on Astra and Sol only Co-authored-by: steipete <[email protected]> --------- Co-authored-by: obviyus <[email protected]> Co-authored-by: steipete <[email protected]>
…ncached (#202372) (#202720) * fix(openai): embed system prompt in input on ChatGPT Responses when uncached (#202372) * test(openai): claim provider state for cached continuation Share the conversation provider-state claim across the three fixture turns so the regression exercises reusable WebSocket continuation and changed-prompt replay. Co-authored-by: RomneyDa <[email protected]> * test(openai): align compaction prompt placement by transport Keep the managed transport instructions contract and assert native ChatGPT developer input when cachesInstructions is false. Preserve full normal-versus-compaction request equality. Co-authored-by: RomneyDa <[email protected]> --------- Co-authored-by: tideclaw-bot <[email protected]> Co-authored-by: RomneyDa <[email protected]> (cherry picked from commit 8b17822) Release adaptation: use stable's existing sessionId-keyed WebSocket continuation in the regression fixture. The main-only #203568 writer scope is unnecessary on this branch; the three-turn continuation/replay assertions and the upstream production request builder are unchanged. Companion disposition: #201094 is present-equivalent at stable 7750bb3 (compat type, catalog and managed Responses transport). #203568 is not applicable to stable's transport ownership and is not imported. Existing cache/replay sibling tests require no changes. No later same-path or issue-reference follow-ups. Prior beta.3 deferral does not apply to this expressly requested stable pick. Co-authored-by: RomneyDa <[email protected]>
Problem
gpt-6.2-astranever caches the Responsesinstructionsfield. Input items and tools cache normally, andgpt-6-astra/gpt-6.1-solcacheinstructions. On native OpenAI Responses, OpenClaw sends the system prompt ininstructions(usesInstructionsFieldinopenai-responses-payload-policy.ts). So ongpt-6.2-astrathe full system prompt is billed as plain input on every request. On a real Gateway turn that is about 9.5k plain tokens per request (measured below).Controlled API replays with the same body:
instructions: 0 written, 0 read on every try.Solution
compat.cachesInstructions(defaulttrue). The OpenAI catalog sets it tofalseongpt-6.2-astra.supportsInstructionsstill means "the route accepts the field". The new capability means "the model caches it". The payload policy usesinstructionsonly when both are true. In all other cases the prompt goes intoinput, which is the path that already exists for routes withoutinstructions.openai-responses-replay-messages-internal.ts): the system item now goes directly after the compaction item (or compacted window), not before it. OpenAI drops input that comes before a compaction item, and server compaction absorbs a developer item placed before it. Layout:[compaction, developer(system), tail…]. Without compaction:[developer(system), …]. Provider-style and Azure routes, which always embed the prompt, use the same rule.previous_response_idcarries it.history_changed, and the request is a full replay with noprevious_response_id. It carries only the new item, so the model never sees two system prompts./responses/compacton native OpenAI still receives the prompt asinstructions, which keeps the prompt out of the retained window. Without this, the next replay would send the old prompt from the window and the current one. The item carries a symbol marker (RESPONSES_SYSTEM_PROMPT). The marker never reaches the wire.input[0]. After compaction,input[0]is the compaction item, so accounting now uses the marker.RESPONSES_RETAINED_USERafter sanitation is removed.cachesInstructionsrow in custom-providers, plus one paragraph in prompt-caching (OpenAI section).Evidence
Red/green (
ocx test)openai-responses-uncached-instructions.integration.test.ts(real HTTP/SSE loopback, unmocked SDK, native route):instructions(green on base and head, default unchanged)false→ noinstructions, developer item first, delta has no developer item, changed prompt → full replay with only the new item (red on base, green on head)history_changed(a missing prompt never matches a present one)openai-responses-compaction-replay.test.ts: provider-style replay puts the prompt after the compaction item (red on base, green on head)openai-responses-client.compact.test.ts: compact body keeps the prompt ininstructionsfor both modelsReal route, isolated scratch Gateway (
env -i, random port, realopenai/gpt-6.2-astra,responsesCompactThreshold: 28000)The same 6-turn chat ran on the merge base (
6a8eb5a93234) and on this head (3a600ca; rerun on18cb195matched: plain 119–220, cacheRead 17,949 / 40,421 on turns 5/6, rules held). Turn 4 pastes about 15k tokens, which crosses server compaction. The workspace rules are: end every reply withZEBRA-17, and answerREFUSEDwhen asked for the secret word. Provider usage per request:OPENCLAW_DEBUG_MODEL_PAYLOAD=summary). Base:instructionspresent on every request. Head: noinstructionsfield, and the input begins withmessage:developer. After compaction, base sendscompaction,message:user,…and head sendscompaction,message:developer,message:user,….REFUSED+ZEBRA-17, all turns end withZEBRA-17).Real
previous_response_idchain with a prompt change (openai-responses-uncached-instructions.live.test.ts, realgpt-6.2-astra)Hi! ALPHA-17. ALPHA-14. BRAVO-2The rule that exists only in B is followed. The rule from A, which B removed, is no longer applied.
Gates
pnpm tsgo:core,pnpm check:test-typesocx testonpackages/ai/src/transports,packages/ai/src/providers,packages/model-catalog-core,extensions/openai, config/model-registry schema testsgit diff --check18cb195: complete, "patch is correct", zero findingspnpm static:validators:gen,pnpm config:schema:gen,pnpm config:docs:gen(regenerated outputs committed)Impact / risks
cachesInstructions: falsechange wire shape (today onlygpt-6.2-astra).input, and context-usage falls back to the conservative local estimate. Both are visible and recoverable.instructionswas equal on a small probe (12/12 vs 12/12). This is not a proof for every prompt.