Files
magpie/internal/gateway/tier_record.go
T
yetone 30869e35a7 usage, gateway: each request's log row says the service tier asked, the one each try was sent at, and the one the vendor said it ran at (#1784, zhenghaowen0967)
What changed
- usage.Record has tier_asked, tier_sent and tier_served, and the CSV gets
  them as its last three columns.
  - tier_asked is the client body's service_tier (Claude's speed fast reads
    "fast").
  - tier_sent is the tier the try's last request actually went out at. It
    is noted in forwardOnce after every rewrite (a fast pick's priority,
    Ultrafast turned to priority on a plan without it, buildResponses
    dropping a tier the host can't take) onto the per-attempt wireAt. So a
    handed-on try's own row has its own tier.
  - tier_served is only what the reply said: service_tier at the top level,
    in response.service_tier, or in an Anthropic usage. It is read by the
    relay sniffer and by both translated readers, and is empty when the
    reply says nothing. It is never filled in from the ask.
- Codex's own relayed sign-in (codex_backend) records the same three.
- /v1/magpie/route: Route.tier (asked), and each Try has tier and
  tierServed. The session route has asked_tier, tier, tier_served and each
  try's tier and tier_served.
- docs/reference.md (CSV columns) and gateway-routing.md describe them.

Why
zhenghaowen0967 asked that a request whose Ultrafast was sent as priority
to an account without it show that in usage, per try, without calling
the served tier confirmed unless the reply says so.

What was run
- TestTierRecordedPerTry. With Pro out, the first try is recorded
  ultrafast, the second priority, and the final record has asked=ultrafast,
  sent=priority and served empty while the fake doesn't echo. With an
  echoing fake, served is what it said ("ultrafast", and "default" when
  none was asked). It also checks the CSV columns, and the Codex relay path
  with priority.
- TestUpstreamTierShapes covers the Chat chunk, Responses
  response.completed, a Responses body, an Anthropic message_start and
  message_delta usage, null, an over-long tier and non-JSON, as well as
  bodyTier per protocol.
- Broke it by hand:
  - with forwardOnce's noteWireTier removed, TestTierRecordedPerTry goes
    red (route tries' tier empty, TierSent empty);
  - with the sniffer's Tier line removed, TestUpstreamTierShapes goes red,
    and so does the Codex relay case of TestTierRecordedPerTry.
- go test -tags nogui ./internal/gateway ./internal/usage ./internal/cli;
  ledger_test's CSV expectations gain the three empty columns.
- gofmt -l; go vet on darwin; GOOS=linux and GOOS=windows go vet -tags
  nogui.

Not done
- The served-tier fixtures are built from the shapes OpenAI and Anthropic
  document, not from saved replies. Whether chatgpt.com's Codex backend
  echoes service_tier wasn't tried against a real account.
- Bridges that don't go through forwardOnce (the Claude subscription,
  Cursor's built-in) leave tier_sent empty.
- There is no price multiplier for priority/ultrafast and no GUI column.
2026-10-13 14:22:53 +08:00

93 lines
2.7 KiB
Go

package gateway
// The service tier a request asked for, what went out on each try and
// what the vendor said it ran at, for the usage log, its CSV and the
// route (#1784). Fast's and Ultrafast's priority processing bills at
// more than the default, and the tier a request went at could change on
// the way: a fast pick asks for priority, and an account whose plan
// doesn't offer Ultrafast is asked for priority in its place.
import (
"bytes"
"context"
"encoding/json"
"strings"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/provider"
)
// bodyTier is the service tier a request body in proto asks for, in
// OpenAI's words (service_tier: "priority", "ultrafast", "flex" …);
// Claude's speed "fast" reads as "fast". "" when it asks for none.
func bodyTier(proto provider.Protocol, body []byte) string {
switch proto {
case provider.Anthropic:
if gjson.GetBytes(body, "speed").String() == "fast" {
return "fast"
}
fallthrough
case provider.Chat, provider.Responses:
if t := gjson.GetBytes(body, "service_tier"); t.Type == gjson.String {
return tierWord(t.String())
}
}
return ""
}
// tierWord is a tier as the log keeps it: short, and nothing else.
func tierWord(t string) string {
t = strings.TrimSpace(t)
if len(t) > 32 {
return ""
}
return t
}
// noteWireTier leaves, where the request's context has a place for it
// (wireAt), the tier body goes out to the vendor at: the last request a
// try sent is the one that answered or failed.
func noteWireTier(ctx context.Context, to provider.Protocol, body []byte) {
if w, _ := ctx.Value(wireAtKey{}).(*wireAt); w != nil {
w.tier = bodyTier(to, body)
}
}
// upstreamTier is the service tier a reply (a JSON body, or one event of
// its stream) says it ran at: a top-level service_tier (a Chat body and
// its chunks, a Responses body), a Responses stream's response.service_tier,
// an Anthropic usage's service_tier. "" when it says none, which is never
// read as the tier that was asked.
func upstreamTier(b []byte) string {
if !bytes.Contains(b, []byte(`"service_tier"`)) {
return ""
}
var v struct {
Tier json.RawMessage `json:"service_tier"`
Response struct {
Tier json.RawMessage `json:"service_tier"`
} `json:"response"`
Usage struct {
Tier json.RawMessage `json:"service_tier"`
} `json:"usage"`
Message struct {
Usage struct {
Tier json.RawMessage `json:"service_tier"`
} `json:"usage"`
} `json:"message"`
}
if json.Unmarshal(b, &v) != nil {
return ""
}
for _, raw := range []json.RawMessage{v.Tier, v.Response.Tier, v.Usage.Tier, v.Message.Usage.Tier} {
var s string
if json.Unmarshal(raw, &s) == nil {
if s = tierWord(s); s != "" {
return s
}
}
}
return ""
}