Archivemylife on X: speed should be the upstream's usage over the local time, with no split into reasoning, tools and answer, since reasoning and tool calls are tokens too. Speed is now every output token the vendor's usage counts (reasoning and tool-call arguments included) over the time from the first output of any kind to the end of the reply. The first output is the earlier of the first content (TTFT) and the first sign of reasoning, firstToken.reasoningMs in ttft.go, which works the same in every protocol: - a Responses reasoning item's start; - an Anthropic thinking or redacted_thinking block's start; - sealed (hidden) thinking; - reasoning deltas spread over 100 ms or more. Deltas that came at once are a buffered burst and tell none. A summary is no sign (summaryPart: a Responses reasoning summary, a Gemini thought, or a summary translated from another protocol via firstToken.summarized), because it is written after some of the reasoning it sums up (#1463). A reply that reasoned with no sign of when it began counts only its answer, its output less the reasoning, from its first text, and none with no text. Before this, a reply that reasoned always counted only its answer, and a reply that wrote tool calls after reasoning told no speed. Burst and held-content checks judge the answer's part. The same definition holds everywhere: - gateway: speed.go, trace.go, codex_backend.go; - usage.DecodeOf, for the Usage cards, rows and `magpie usage`; - the Usage page's ledDecode; - the Routing page's decodeOf and its ranking (fast routing reads noteTrySpeed, which uses DecodeOf). settledCountVersion goes to 8, so settled heat totals are counted again. The rule text is in en, zh, zh-TW, ja and de. Docs: reference.md, and gui-shell.md under "Streamed output speed". Red on the old code (scratch worktree at589e4ba6d): - TestCodexPreambleThenReasoningTellsSpeed: "reasoning seen from 0 ms; want 3166". - TestVisibleReasoningSpeed: 100/1000 and 0/0 where 1500/14000 was wanted. Run at this head (rebased ondc507aefd), on mote: - go test -tags nogui -count=1 -timeout 45m ./... - gateway ok (1132s), usage ok, routes ok. - Red, filed, and caused by mote's own gateway on 127.0.0.1:3425: internal/cli TestHealthcheckFindsTheRunningGatewayWithoutMagpieAddr, TestUpdateRestartsTheGatewayItFindsWithoutMagpieAddr and TestUpdateFindsAMagpieThatWroteNoRecord (#6223, #6471); internal/gui TestLaneLimitRoutes (#5150). - go vet for gateway and usage on darwin, GOOS=linux -tags nogui and GOOS=windows: ok. gofmt -l: clean. - GUI, 18 files in Chromium and WebKit (reasoning-speed, usage-speed-*, usage-ledger, routing and the i18n files): 242 of 270 pass. The rest are usage-ledger "newest live route needs no return-to-live button" (#6003) and usage-speed-all-models "asked for 7d" (#5834). Both are red the same way on origin/maindc507aefd, in a run of its own. Rebased again on70ad7cf30, which touches gateway.go and usage/page.js. At that head, on mote: go vet for gateway and usage; go test -run "Reasoning|Speed|Decode|Ultra|Uncounted" for gateway and usage, ok; reasoning-speed, usage-reads-note, usage-speed-every-row and i18n-keys in both engines, 66 of 66. Not tried: a real account per vendor. The timing is taken from recorded stream shapes in the tests: Responses, Anthropic, Chat and Gemini, native and translated.
117 lines
3.5 KiB
Go
117 lines
3.5 KiB
Go
package gateway
|
|
|
|
import (
|
|
"math"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
// Speed lately, for Speed first (routes.Fastest): of each account or key
|
|
// and model (restID), a decayed average of its streamed answers' time to
|
|
// first token and of how fast they wrote. Kept in memory, a few numbers
|
|
// each, written once as an answer ends (noteSpeed) and read with a map
|
|
// lookup per candidate as a pool routed by speed is weighed: the usage
|
|
// history is never read for it. A restart forgets them, and each is
|
|
// measured again as Fastest's probes give it a turn.
|
|
|
|
// speedHalfLife: a sample counts half after this, against those since.
|
|
// speedStale: this long after its last answer a seat counts as not
|
|
// measured lately, and Fastest gives it a turn now and then to be
|
|
// measured again, so one that was slow once isn't kept last for good.
|
|
const (
|
|
speedHalfLife = 20 * time.Minute
|
|
speedStale = time.Hour
|
|
)
|
|
|
|
// speedMost bounds the records: past it, those not measured lately go.
|
|
const speedMost = 4096
|
|
|
|
type speedRec struct {
|
|
ttft, ttftW float64 // the average ms to the first token, and its samples' weight
|
|
tps, tpsW float64 // the average output tokens a second, and its samples' weight
|
|
at, tpsAt time.Time // the last sample of each
|
|
}
|
|
|
|
var speeds = struct {
|
|
sync.Mutex
|
|
m map[string]speedRec // candidate restID → its speed lately
|
|
}{m: map[string]speedRec{}}
|
|
|
|
func decayed(w float64, since time.Duration) float64 {
|
|
if since <= 0 {
|
|
return w
|
|
}
|
|
return w * math.Exp2(-since.Seconds()/speedHalfLife.Seconds())
|
|
}
|
|
|
|
// fold is avg of weight w, decayed by since, with x added at weight 1.
|
|
func fold(avg, w float64, since time.Duration, x float64) (float64, float64) {
|
|
w = decayed(w, since)
|
|
return (avg*w + x) / (w + 1), w + 1
|
|
}
|
|
|
|
// noteSpeed records an answer of c's: ttft ms from its try's start to its
|
|
// first content, and, when its pace was told, tokens written in w ms
|
|
// after it (usage.DecodeOf). A reply that wasn't streamed has no ttft and
|
|
// tells nothing.
|
|
func noteSpeed(c candidate, ttft int64, tokens int, w int64, now time.Time) {
|
|
if ttft <= 0 {
|
|
return
|
|
}
|
|
id := c.restID()
|
|
speeds.Lock()
|
|
defer speeds.Unlock()
|
|
r := speeds.m[id]
|
|
r.ttft, r.ttftW = fold(r.ttft, r.ttftW, now.Sub(r.at), float64(ttft))
|
|
r.at = now
|
|
if tokens > 0 && w > 0 {
|
|
r.tps, r.tpsW = fold(r.tps, r.tpsW, now.Sub(r.tpsAt), float64(tokens)*1000/float64(w))
|
|
r.tpsAt = now
|
|
}
|
|
if _, had := speeds.m[id]; !had && len(speeds.m) >= speedMost {
|
|
for k, o := range speeds.m {
|
|
if now.Sub(o.at) > speedStale {
|
|
delete(speeds.m, k)
|
|
}
|
|
}
|
|
}
|
|
speeds.m[id] = r
|
|
}
|
|
|
|
// noteTrySpeed is noteSpeed of a try that answered, its pace as
|
|
// usage.DecodeOf counts it for the Requests and Usage pages.
|
|
func noteTrySpeed(c candidate, t Try, out, reasoning int, now time.Time) {
|
|
tokens, w := 0, int64(0)
|
|
if !t.ReasoningHidden || t.FirstText > 0 || t.FirstReasoning > 0 {
|
|
tokens, w = usage.DecodeOf(out, reasoning, t.Millis, t.TTFT, t.FirstText, t.Flow, t.FirstReasoning)
|
|
}
|
|
noteSpeed(c, t.TTFT, tokens, w, now)
|
|
}
|
|
|
|
// speedOf is c's speed lately: its average ms to the first token and
|
|
// tokens a second, 0 for what wasn't measured lately.
|
|
func speedOf(c candidate, now time.Time) (ttft, tps float64) {
|
|
speeds.Lock()
|
|
r, ok := speeds.m[c.restID()]
|
|
speeds.Unlock()
|
|
if !ok {
|
|
return 0, 0
|
|
}
|
|
if now.Sub(r.at) > speedStale {
|
|
return 0, 0
|
|
}
|
|
if !r.tpsAt.IsZero() && now.Sub(r.tpsAt) <= speedStale {
|
|
tps = r.tps
|
|
}
|
|
return r.ttft, tps
|
|
}
|
|
|
|
// forgetSpeeds drops every speed: a test starts anew.
|
|
func forgetSpeeds() {
|
|
speeds.Lock()
|
|
speeds.m = map[string]speedRec{}
|
|
speeds.Unlock()
|
|
}
|