usage: a reported call takes first_text_ms and flow_ms, so a reasoning reply tells its speed and a burst is held (#2637)
POST /v1/magpie/usage/requests turned away first_text_ms and flow_ms as
"not a field a reported record takes", so a reported reasoning reply was
timed from its first reasoning token and a burst-at-the-end reply was
never held. The ingest now takes both, validated like ttft_ms (whole
milliseconds, 0..ingestMaxMillis, null = unset), and checks:
- first_text_ms <= ms, and first_text_ms >= ttft_ms when both are set
(the first text is content too);
- flow_ms <= ms*10/8+1, since the gateway's flow is the tenth-to-nine-
tenths span scaled by 10/8 and can exceed ms.
The fields reach usage.jsonl through the Record, so DecodeOf, Row.Decode
(ThinkPace, HeldShare) and the Reported totals read them as they do the
gateway's own rows. docs/reference.md and gateway-sessions.md say so.
Tests: TestUsageIngestTellsSpeed posts the reporter's literal repro
(grok-4.7, out 307, reasoning 129, ms 5035, ttft 4632, first_text 4700:
178 tokens over 335ms), a burst held by flow_ms, an even reply told, and
a ThinkPace hold, and checks the totals. TestUsageIngestTimesChecked
covers each bound, its edge, null and absent fields.
Broken by hand: old code fails both tests (field refused); dropping
FirstText tells repro-1 0; dropping Flow tells the burst 601/4035;
removing the ttft check accepts first_text 299 < ttft 300; bounding flow
by ms rejects the valid 1501.
Run at 8bb9375e7 + this diff: gofmt -l clean; go vet ./... on darwin,
GOOS=linux and GOOS=windows -tags nogui clean; set -o pipefail; go test
-timeout 45m ./... exit 0 (66 ok, gateway 1192s) under a sandbox HOME and
private GOCACHE (an earlier run against the shared cache had build
errors from cache files removed mid-run, not code). Machine load ~250.
Not tried with a real reporting client.
This commit is contained in:
+12
-2
@@ -1863,8 +1863,18 @@ curl -s http://127.0.0.1:3425/v1/magpie/usage/requests \
|
||||
|
||||
- **Fields:** `t` (RFC 3339), `agent`, `provider`, `model` and `rid` are
|
||||
required. `in`, `out`, `cache_read`, `cache_write`, `cache_write_1h`,
|
||||
`reasoning`, `ms`, `ttft_ms`, `status`, `cost` (USD), `effort`,
|
||||
`session` and `err` may be left out.
|
||||
`reasoning`, `ms`, `ttft_ms`, `first_text_ms`, `flow_ms`, `status`,
|
||||
`cost` (USD), `effort`, `session` and `err` may be left out.
|
||||
- **Speed:** the Requests page tells a reply's speed from `out`, `ms` and
|
||||
`ttft_ms` (ms from the request to the reply's first content: text,
|
||||
reasoning or a tool call). A reply that reasoned (`reasoning` above 0)
|
||||
is timed from its first text, so it tells a speed only with
|
||||
`first_text_ms` (ms from the request to its first text), which may not
|
||||
be before `ttft_ms` or after `ms`. `flow_ms` is how long the content
|
||||
took to come, from where the speed is timed: the time from a tenth of
|
||||
it to nine tenths, times 10/8 (at most `ms` × 10/8). A reply whose
|
||||
content came in under a quarter of its window was held and let go in a
|
||||
burst, and tells no speed. Leave out what you don't know.
|
||||
- **Pricing:** a record without `cost` is priced at magpie's prices for
|
||||
its provider and model.
|
||||
- **Past times:** `t` is taken as given, however far back, so a history
|
||||
|
||||
@@ -337,7 +337,8 @@ them straight to its vendor, so the Usage page covers them too. The body is
|
||||
one record or an array of up to 5000 (32 MiB at most), in `usage.jsonl`'s
|
||||
shape: `t`, `agent`, `provider`, `model` and `rid` are required; `in`,
|
||||
`out`, `cache_read`, `cache_write`, `cache_write_1h`, `reasoning`, `ms`,
|
||||
`ttft_ms`, `status`, `cost`, `effort`, `session` and `err` are optional.
|
||||
`ttft_ms`, `first_text_ms`, `flow_ms`, `status`, `cost`, `effort`,
|
||||
`session` and `err` are optional.
|
||||
Any other field, a quoted number, a negative or out-of-range count, a time
|
||||
more than an hour ahead or before 2000-01-01T00:00:00Z refuses the whole
|
||||
post with a 400 that names the record (`error.index`) and the field
|
||||
@@ -363,6 +364,16 @@ however old, so a client's history can be backfilled.
|
||||
- **Pricing:** a record with `cost` keeps it (`Record.OwnCost`, the
|
||||
`rowOwnCost` flag on a packed row). Without one it is priced at magpie's
|
||||
own prices, like a gateway row.
|
||||
- **Speed:** `first_text_ms` and `flow_ms` (#2637) mean what they do in
|
||||
`usage.jsonl` (`Record.FirstText`, `Record.Flow`), so a reported row's
|
||||
speed is `usage.DecodeOf`'s, as a gateway row's is, on the server
|
||||
(`Row.Decode`, the totals' `DecodeN`) and in `page.js`'s `ledDecode`. A
|
||||
row that reasoned tells none without `first_text_ms`; `ThinkPace` and,
|
||||
with `flow_ms`, `HeldShare` tell a reply let go in a burst. `first_text_ms`
|
||||
may not be before `ttft_ms` (the first text is content too) or after `ms`;
|
||||
`flow_ms` is the middle 80% of the content's time scaled by 10/8, as
|
||||
`firstToken.flowMs` measures it, so it is bounded by `ms` × 10/8, not
|
||||
`ms`. `TestUsageIngestTellsSpeed` (the reporter's record), `TestUsageIngestTimesChecked`.
|
||||
- **Showing:** reported rows count in every total. They also have a source
|
||||
of their own (`SourceReported`): `RequestPage.Reported`, `?via=reported`,
|
||||
and a fourth cell and a "Reported" badge on the Requests tab.
|
||||
|
||||
@@ -70,7 +70,11 @@ var ingestEarliest = time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
|
||||
// ingestFields are the fields a reported record may carry, of the ones
|
||||
// usage.jsonl has: what a client can know of a call it made itself.
|
||||
var ingestFields = []string{"t", "agent", "provider", "model", "in", "out", "cache_read", "cache_write", "cache_write_1h", "reasoning", "ms", "ttft_ms", "status", "rid", "cost", "effort", "session", "err"}
|
||||
// first_text_ms and flow_ms (#2637) are what a reply's speed is told by
|
||||
// (usage.DecodeOf): without first_text_ms a reply that reasoned tells
|
||||
// none, and without flow_ms one its vendor held and let go in a burst
|
||||
// isn't told apart from one that streamed.
|
||||
var ingestFields = []string{"t", "agent", "provider", "model", "in", "out", "cache_read", "cache_write", "cache_write_1h", "reasoning", "ms", "ttft_ms", "first_text_ms", "flow_ms", "status", "rid", "cost", "effort", "session", "err"}
|
||||
|
||||
// ingestFault is what is wrong with one record of a post.
|
||||
type ingestFault struct {
|
||||
@@ -321,6 +325,33 @@ func parseReported(i int, raw json.RawMessage, now time.Time) (usage.Record, *in
|
||||
if rec.TTFT, f = whole("ttft_ms", ingestMaxMillis, false); f != nil {
|
||||
return rec, f
|
||||
}
|
||||
// first_text_ms is when the reply's first text came, counted as
|
||||
// ttft_ms is: within the call, and never before its first content,
|
||||
// which ttft_ms is (text, reasoning or a tool call). One before it
|
||||
// would read the reasoning as written in no time, and ThinkPace would
|
||||
// never tell an answer let go in a burst.
|
||||
if rec.FirstText, f = whole("first_text_ms", ingestMaxMillis, false); f != nil {
|
||||
return rec, f
|
||||
}
|
||||
hasMs := fields["ms"] != nil && string(fields["ms"]) != "null"
|
||||
if hasMs && rec.FirstText > rec.Millis {
|
||||
return rec, &ingestFault{i, "first_text_ms", "after ms, the end of the call it is in"}
|
||||
}
|
||||
if rec.TTFT > 0 && rec.FirstText > 0 && rec.FirstText < rec.TTFT {
|
||||
return rec, &ingestFault{i, "first_text_ms", "before ttft_ms: the first text is content too, so it can't come before the first content"}
|
||||
}
|
||||
// flow_ms is how long the content took to come, from where the speed
|
||||
// is counted (the first text when it reasoned, else the first
|
||||
// content): the time from a tenth of it to nine tenths, scaled to the
|
||||
// whole (×10/8), as the gateway measures it. Scaled so, a reply that
|
||||
// came evenly over nearly all of the call can read a little over ms,
|
||||
// so the bound is ms scaled the same way.
|
||||
if rec.Flow, f = whole("flow_ms", ingestMaxMillis, false); f != nil {
|
||||
return rec, f
|
||||
}
|
||||
if hasMs && rec.Flow > rec.Millis*10/8+1 {
|
||||
return rec, &ingestFault{i, "flow_ms", "over ms×10/8: the content took longer to come than the call took"}
|
||||
}
|
||||
st, f := whole("status", 599, true)
|
||||
if f != nil {
|
||||
return rec, f
|
||||
|
||||
@@ -342,3 +342,92 @@ func TestUsageIngestConcurrent(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// #2637 (tsy0716): a reported call carries first_text_ms and flow_ms, and
|
||||
// its speed is told as a gateway call's is. The reporter's own record, a
|
||||
// grok-4.7 reply that reasoned, tells its answer's speed from its first
|
||||
// text; without first_text_ms it told none. A reply let go in a burst
|
||||
// tells none, whether its flow_ms says so (HeldShare) or its answer came
|
||||
// over 20 times faster than its reasoning before it (ThinkPace).
|
||||
func TestUsageIngestTellsSpeed(t *testing.T) {
|
||||
do := ingestServer(t)
|
||||
repro := `{
|
||||
"t":"2026-10-17T00:24:29Z","agent":"my-app","provider":"xai","model":"grok-4.7","rid":"repro-1",
|
||||
"in":10696,"out":307,"reasoning":129,"ms":5035,"ttft_ms":4632,"first_text_ms":4700,"status":200}`
|
||||
at := time.Now().Add(-time.Hour).UTC().Format(time.RFC3339)
|
||||
// a reply that didn't reason, its 601 tokens let go in the last 109 ms
|
||||
// (its flow, scaled as the gateway scales it, 136 ms) of a 4 s window
|
||||
burst := `{"t":"` + at + `","agent":"my-app","provider":"xai","model":"grok-4.7","rid":"burst-1","in":1000,"out":601,"ms":5035,"ttft_ms":1000,"flow_ms":136,"status":200}`
|
||||
// the same reply, streamed as it was written
|
||||
even := strings.Replace(strings.Replace(burst, `"flow_ms":136`, `"flow_ms":4000`, 1), "burst-1", "even-1", 1)
|
||||
// 22 reasoning tokens over 4.4 s, then 528 answer tokens in 335 ms
|
||||
held := `{"t":"` + at + `","agent":"my-app","provider":"xai","model":"grok-4.7","rid":"held-1","in":1000,"out":550,"reasoning":22,"ms":5035,"ttft_ms":300,"first_text_ms":4700,"status":200}`
|
||||
c, out, b := do("127.0.0.1:5000", "["+repro+","+burst+","+even+","+held+"]")
|
||||
if c != 200 || out.Accepted != 4 {
|
||||
t.Fatal(c, b)
|
||||
}
|
||||
|
||||
h := lanGuard(New().Handler())
|
||||
r := httptest.NewRequest("GET", "/v1/magpie/usage/requests?period=all&via=reported", nil)
|
||||
r.RemoteAddr = "127.0.0.1:5000"
|
||||
w := httptest.NewRecorder()
|
||||
h.ServeHTTP(w, r)
|
||||
var l usageapi.Ledger
|
||||
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &l) != nil {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
speed := map[string][2]int64{}
|
||||
for _, row := range l.Rows {
|
||||
n, w := row.Row.Decode()
|
||||
speed[row.RequestID] = [2]int64{int64(n), w}
|
||||
}
|
||||
want := map[string][2]int64{
|
||||
"repro-1": {307 - 129, 5035 - 4700}, // its answer, from its first text
|
||||
"burst-1": {0, 0}, // held: its content came in under a quarter of its window
|
||||
"even-1": {601, 5035 - 1000},
|
||||
"held-1": {0, 0}, // held: its answer came over 20 times its reasoning's pace
|
||||
}
|
||||
for rid, w := range want {
|
||||
if speed[rid] != w {
|
||||
t.Errorf("%s: told %d tokens over %d ms, want %d over %d", rid, speed[rid][0], speed[rid][1], w[0], w[1])
|
||||
}
|
||||
}
|
||||
// the page's totals count the two that tell a speed
|
||||
if got := l.Reported; got.DecodeN != 2 || got.DecodeOut != 178+601 || got.DecodeMs != 335+4035 {
|
||||
t.Errorf("reported totals: %d timed, %d tokens over %d ms", got.DecodeN, got.DecodeOut, got.DecodeMs)
|
||||
}
|
||||
}
|
||||
|
||||
// first_text_ms and flow_ms are checked as ttft_ms is, and against the
|
||||
// call they are in: first text within ms and never before the first
|
||||
// content; flow within ms as the gateway scales it.
|
||||
func TestUsageIngestTimesChecked(t *testing.T) {
|
||||
do := ingestServer(t)
|
||||
good := reportedRecord("ok-1", "")
|
||||
cases := []struct{ extra, field string }{
|
||||
{`,"first_text_ms":1201`, "first_text_ms"}, // after the call's 1200 ms
|
||||
{`,"first_text_ms":299`, "first_text_ms"}, // before its 300 ms first content
|
||||
{`,"first_text_ms":-1`, "first_text_ms"},
|
||||
{`,"first_text_ms":"400"`, "first_text_ms"},
|
||||
{`,"first_text_ms":400.5`, "first_text_ms"},
|
||||
{`,"flow_ms":1502`, "flow_ms"}, // over 1200×10/8
|
||||
{`,"flow_ms":-1`, "flow_ms"},
|
||||
{`,"flow_ms":1e12`, "flow_ms"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
code, out, b := do("127.0.0.1:5000", "["+good+","+reportedRecord("b", c.extra)+"]")
|
||||
if code != 400 || out.Error == nil || out.Error.Param != c.field {
|
||||
t.Errorf("%s: %d %s", c.extra, code, b)
|
||||
}
|
||||
}
|
||||
if n := reportedLines(t); n != 0 {
|
||||
t.Fatal("a refused batch wrote", n)
|
||||
}
|
||||
// the edges are taken, and so is either left out or null
|
||||
ok := []string{`,"first_text_ms":300,"flow_ms":1501`, `,"first_text_ms":1200,"flow_ms":0`, `,"first_text_ms":null,"flow_ms":null`, `,"reasoning":10`}
|
||||
for i, extra := range ok {
|
||||
if code, out, b := do("127.0.0.1:5000", reportedRecord(fmt.Sprintf("ok-%d", i), extra)); code != 200 || out.Accepted != 1 {
|
||||
t.Errorf("%s: %d %s", extra, code, b)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user