Repository navigation
fix(relay): convert only tool-bearing chat calls to Responses, and keep their system prompt cacheable - #297
Merged
Conversation
…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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📝 变更描述 / Description
Follow-up to #296, found by switching the chat→Responses policy on for
gpt-6.2-sol/gpt-6.2-astrain production (now switched back off for them).Those two models answer 400 to any tool-bearing
/v1/chat/completionscall and work on/v1/responses. The policy converts such calls, but as upstream ships it, it does two things that cost customers:stop,n,logprobsor the penalties, so a converted plain call can only lose something.instructions, which gpt-6.2 never caches. Measured against the supplier directly, 8.7K-token prompt, four identical calls each:instructionsdevelopermessageSo 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, bothfalseby 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 firstinputmessage (roledeveloper) and omitsinstructions.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 markedUNIFYAPI-FORK. No relaykit change.Why code and not configuration.
param_overrideruns on the chat body before conversion, so it cannot reachinstructions; 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 oneSyncOptionstick.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
🔗 关联任务 / Related Issue
✅ 提交前检查项 / Checklist
📸 运行证明 / Proof of Work
Local build of this branch, fresh SQLite, fake OpenAI upstream, production's own
billing_settingrows, policy with both options: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