]> git.giacomolli.uk - onechat/commitdiff
Leave the turn state out of the usage records
authorAnderson A. Giacomolli <agiacomolli@gmail.com>
Fri, 6 Nov 2026 18:33:04 +0000 (15:33 -0300)
committerAnderson A. Giacomolli <agiacomolli@gmail.com>
Fri, 6 Nov 2026 18:33:04 +0000 (15:33 -0300)
The ChatGPT backend sends x-codex-turn-state on each response: an opaque
state of the backend of about 1 KB, not quota. The usage records keep
every other x-codex-* header, so each line holds the plan and the
windows without the noise.

Issue: b14d537

responses.go
responses_test.go

index ebffaa36aaa3e477cc9a722f2fbbfea16a8f5a52..cdc1558195e159cd111f0e4968f8e9f5d9aea941 100644 (file)
@@ -27,6 +27,10 @@ const (
        // backend, in canonical form.
        quotaPrefix = "X-Codex-"
 
+       // turnStateHeader is an x-codex-* header that holds an opaque state of
+       // the backend, not quota.
+       turnStateHeader = "X-Codex-Turn-State"
+
        // errorBytes is the most bytes of an error response that go into the
        // error.
        errorBytes = 4096
@@ -262,7 +266,8 @@ type usageLog struct {
 }
 
 // usageRecord is one line of usage.jsonl. Status is the HTTP status, or 0 when
-// no response came. Quota holds the x-codex-* headers of the response.
+// no response came. Quota holds the x-codex-* headers of the response, without
+// the turn state.
 type usageRecord struct {
        Date         string            `json:"date"`
        Model        string            `json:"model"`
@@ -331,11 +336,12 @@ func userMessage(texts ...string) any {
        }
 }
 
-// quotaHeaders returns the x-codex-* headers, with lowercase names.
+// quotaHeaders returns the x-codex-* headers, with lowercase names, without the
+// turn state.
 func quotaHeaders(header http.Header) map[string]string {
        var quota map[string]string
        for name, values := range header {
-               if !strings.HasPrefix(name, quotaPrefix) || len(values) == 0 {
+               if !strings.HasPrefix(name, quotaPrefix) || name == turnStateHeader || len(values) == 0 {
                        continue
                }
 
index c1a7b4da78421149cb67e1ba0fe938dac1baade5..577e6111e5b2c3e80135768237cc1c9ef59b0219 100644 (file)
@@ -102,6 +102,7 @@ func TestClientRecordsEachCall(t *testing.T) {
                w.Header().Set("X-Codex-Primary-Used-Percent", "12")
                w.Header().Set("X-Codex-Plan-Type", "plus")
                w.Header().Set("X-Other", "not quota")
+               w.Header().Set("X-Codex-Turn-State", "opaque state")
                if call.number == 1 {
                        w.WriteHeader(http.StatusTooManyRequests)
                        _, _ = io.WriteString(w, `{"detail": "slow down"}`)