Skip to content

fix(relay): convert only tool-bearing chat calls to Responses, and keep their system prompt cacheable - #297

Merged
Yuchengw merged 1 commit into
mainfrom
fix/converted-chat-keeps-prompt-cache
Nov 18, 2026
Merged

Yuchengw merged 1 commit into
mainfrom
fix/converted-chat-keeps-prompt-cache

Conversation

@Yuchengw

Copy link
Copy Markdown

⚠️ 提交说明 / PR Notice

AI-assisted: written by an agent session (Claude) for the operator.

📝 变更描述 / Description

Follow-up to #296, found by switching the chat→Responses policy on for gpt-6.2-sol / gpt-6.2-astra in production (now switched back off for them).

Those two models answer 400 to any tool-bearing /v1/chat/completions call and work on /v1/responses. The policy converts such calls, but as upstream ships it, it does two things that cost customers:

  1. It converts every matching call, not just the ones that fail. Plain chat works natively. The Responses API has no field for stop, n, logprobs or the penalties, so a converted plain call can only lose something.
  2. It moves the system prompt into instructions, which gpt-6.2 never caches. Measured against the supplier directly, 8.7K-token prompt, four identical calls each:
model prompt in instructions prompt as leading developer message
gpt-6.2-sol write 0, read 0, every call write 8,716 then read 8,716
gpt-6.2-astra write 0, read 0, every call write 8,713 then read 8,713
gpt-6-astra write 8,718 then read 8,718 write 8,715 then read 8,715

So with the policy on, a 6.2 customer with a system prompt paid full input price on every call where native chat bills a tenth.

The change. Two options on global.chat_completions_to_responses_policy, both false by default (upstream behaviour unchanged until a row sets them):

  • only_with_tools: a matching call with no tools stays on the native path;
  • instructions_as_input: a converted call sends its system prompt as the first input message (role developer) and omits instructions.

New files hold the logic (service/unifyapi_chat_responses_policy.go, relay/unifyapi_converted_instructions.go). Upstream files gain two struct fields and three one-line hooks, each marked UNIFYAPI-FORK. No relaykit change.

Why code and not configuration. param_override runs on the chat body before conversion, so it cannot reach instructions; the policy cannot see the request body, so "only when tools are present" is not expressible. The fix is shaped as configuration: both behaviours are switched from the option row and revert within one SyncOptions tick.

What number moves. None from this PR alone. No price, ratio or billing code is touched. Once the row sets the options, converted gpt-6.2 calls get cache reads on their system prompt (down), and plain chat stays as it is today.

No existing test is changed.

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix)

🔗 关联任务 / Related Issue

✅ 提交前检查项 / Checklist

  • 变更理解 / 范围聚焦 / 本地验证 / 安全合规

📸 运行证明 / Proof of Work

Local build of this branch, fresh SQLite, fake OpenAI upstream, production's own billing_setting rows, policy with both options:

billing: 16 ok, 0 bad
/v1/chat/completions gpt-6.2-sol  tools=0 ...                       <- plain chat stays native (5 sol + 2 astra cases)
/v1/responses gpt-6.2-sol  tools=1 instructions=False input[0].role=developer
/v1/responses gpt-6.2-sol stream tools=1 instructions=False input[0].role=developer
/v1/responses gpt-6.2-astra  tools=1 instructions=False input[0].role=developer

go test ./service/... ./relay/... ./setting/... ./model/... all ok; cd relaykit && GOWORK=off go build ./... ok.

For the release (staging): the same harness, exact image. Expect billing: 16 ok, 0 bad, the three lines above, plain 6.2 chat on /v1/chat/completions, and the control model unchanged. No migration, seed, option or env var; the option row is changed by the coding session after the release.

🤖 Generated with Claude Code

…ep their system prompt cacheable

gpt-6.2-sol and gpt-6.2-astra refuse function tools on
/v1/chat/completions ("use /v1/responses"). The chat-to-Responses policy is
the way to serve those calls, and with #296 it bills cache correctly. Turning
it on for 6.2 in production showed two more reasons it could not stay on:

1. The policy converts every matching call. A plain chat call already works
   natively; converting it gains nothing and loses whatever the Responses API
   has no field for (stop, n, logprobs, the penalties).
2. The conversion lifts system messages into `instructions`, and gpt-6.2
   never caches `instructions`: four identical 8.7K-token prompts in a row
   reported no cache write and no cache read, against a write then reads for
   the same text sent as a leading developer message. gpt-6-astra caches
   both. Measured against the supplier directly on 2026-11-18. A converted
   call paid the full input price for a prompt native chat bills at a tenth.

Two options on the policy row, both false by default so upstream's behaviour
is unchanged until a row asks for them:

- only_with_tools: a matching call that carries no tools stays native.
- instructions_as_input: a converted call sends its system prompt as the
  first input message (role developer) instead of `instructions`.

The logic lives in two new files; the upstream files gain one struct field
pair and three one-line hooks, each marked UNIFYAPI-FORK.

Configuration was checked first: param_override runs on the chat body before
conversion, so it cannot reach `instructions`, and nothing in the policy can
see the request body.

Verified with a local build against a fake upstream, production's own
billing rows and the policy set to both options: 16 billed cases exact;
plain gpt-6.2 chat reached /v1/chat/completions, tool calls reached
/v1/responses with input[0].role=developer and no instructions.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@Yuchengw
Yuchengw merged commit 21f1339 into main Nov 18, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant