Repository navigation
Conversation
gtokman
force-pushed
the
sort-request-body-keys
branch
from
November 19, 2026 12:23
b133b51 to
ca63241
Compare
Requests were encoded with a plain JSONEncoder, so the same tools and messages came out in a different key order each time. Providers whose prompt cache matches an exact prefix (OpenAI's, Azure's) never hit: every call was billed as a cache write. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
gtokman
force-pushed
the
sort-request-body-keys
branch
from
November 19, 2026 12:24
ca63241 to
8117244
Compare
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.
Requests are encoded with a plain
JSONEncoder, so the same tools and messages can come out with their keys in a different order on each request. OpenAI's prompt cache, and Azure's, only match an exact byte prefix. When the order changes, no request ever reads from the cache, and each one is billed as a fresh cache write.This sorts the keys (
.sortedKeys) when encoding request bodies, in all three places they're encoded: Chat Completions, Responses, and streaming. It also adds a test.What we measured: we ran a 15-turn tool-using benchmark on
gpt-6.2-solthrough Azure:The key-order differences were easy to see in a diff of two consecutive raw requests: one showed up at character 16 of the system message, and another at character 190 of the tools.