Files
magpie/internal/gateway/responses_instructions_after_input_test.go
T
yetone f7e0f68ca2 codex: GPT-6.2's instructions go first in the input, where its cache keeps them
From GPT-6.2 on, OpenAI reads a Responses request's top-level
instructions after its input, not before it (prompt caching guide:
"instructions ... appended to the end of the input" for gpt-6.2 and
later), so they sit past the cached prefix and are billed uncached on
every turn. Codex itself sends no top-level instructions: client.rs puts
its base instructions first in the input as a developer message
(BaseInstructionsFragment, codex-rs/core/src/client.rs).

magpie sent Codex's instructions on top for every ChatGPT-account
request that is not Responses Lite: Claude Code, OpenCode, Pi and other
translated agents through a codex account, and Codex CLIs older than
Lite. Measured on a real ChatGPT account, third turn of a growing
conversation: gpt-6.2-sol cached 1,920 of 8,271 input tokens with the
instructions on top, 6,016 with them first in the input; gpt-6-sol
cached 5,632 of 6,843 with them on top (the old order still works
there). Reported on X by Anotiaww (Codex hit rate looked low).

- codexBody: for a GPT-6.2+ model Codex's instructions, and a client's
  own, go first in the input as developer messages, and the request
  carries no instructions field. Older models are unchanged, and Lite
  turns (no instructions at all) are unchanged.
- siwcBody (Sign in with ChatGPT API path): the client's instructions
  go first in the input for GPT-6.2+.
- buildResponses (Chat/Anthropic translated to Responses, any host):
  the system prompt goes first in the input for GPT-6.2+.
- InstructionsAfterInput names the models: gpt-N.M with N.M >= 6.2,
  gpt-7 and later, after a provider prefix; gpt-6, gpt-6.1, gpt-5.x,
  o-series and other vendors' models keep top-level instructions.

The instruction text itself is unchanged; only where it sits moves.

Run (mote, at this tree on origin/main 48fffb60c):
- go test -count=1 -timeout 45m ./...: all 103 packages ok
  (internal/gateway 1602s, internal/provider 463s).
- go vet ./internal/provider ./internal/gateway on darwin, linux and
  windows: ok. gofmt -l: clean.

Broken by hand, then put back: InstructionsAfterInput returning false
turns TestInstructionsAfterInput, TestCodexBodyInstructionsFirstIn-
InputOnGPT62, TestSiwcBodyInstructionsFirstInInputOnGPT62 and
TestResponsesSystemFirstInInputOnGPT62 red.

Not tried: a real Claude Code or OpenCode session through magpie on
gpt-6.2 (the live app lists no gpt-6.2 model here); the cache numbers
above are from direct requests to the ChatGPT backend in both orders.
2026-11-18 16:04:56 +08:00

36 lines
1.3 KiB
Go

package gateway
import (
"encoding/json"
"testing"
)
// A request built in Responses for GPT-6.2 carries its system prompt first
// in the input: the model reads top-level instructions after the input,
// uncached every turn. Another model's goes on top as before.
func TestResponsesSystemFirstInInputOnGPT62(t *testing.T) {
r := &Request{System: "You are Claude Code.", Messages: []Message{{Role: "user", Parts: []Part{{Kind: Text, Text: "hi"}}}}}
var m struct {
Instructions *string `json:"instructions"`
Input []map[string]any `json:"input"`
}
if err := json.Unmarshal(buildResponses(r, "gpt-6.2-sol", "api.openai.com", false), &m); err != nil {
t.Fatal(err)
}
if m.Instructions != nil || len(m.Input) != 2 || m.Input[0]["role"] != "developer" || m.Input[1]["role"] != "user" {
t.Fatalf("gpt-6.2-sol: %+v", m)
}
c, _ := m.Input[0]["content"].([]any)
if p, _ := c[0].(map[string]any); len(c) != 1 || p["text"] != "You are Claude Code." {
t.Fatalf("gpt-6.2-sol developer item: %+v", m.Input[0])
}
m.Instructions, m.Input = nil, nil
if err := json.Unmarshal(buildResponses(r, "gpt-6-sol", "api.openai.com", false), &m); err != nil {
t.Fatal(err)
}
if m.Instructions == nil || *m.Instructions != "You are Claude Code." || len(m.Input) != 1 {
t.Fatalf("gpt-6-sol: %+v", m)
}
}