diff --git a/docs/reference.md b/docs/reference.md index 6fd8141fa..30f289367 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -338,12 +338,20 @@ item to its last, never from the output of a tool the call itself ran, which an older rollout writes before the call's token count. OpenCode's rows show none. A call whose session file timed it (a Codex turn of one call) shows its own -speed as before. When that call reasoned, the file has its first token but -not its first text, so no speed is measured; it shows the least its answer -wrote at instead: its output less the reasoning, all written after the first -token, over the time from the first token to its end. Its whole output over -that time would count reasoning written before the first token and read -high. A floor never mixes into a measured speed: the Speed trend and +speed when the time after its first token is a second or more and it wrote +100 tokens or more in it (`usage.CodexWindowMs`, `usage.CodexBodyTokens`). +That time can be one network push rather than the writing: a reply whose +first token came at 6.8 s and the rest 0.2 s later read 1,900 tok/s. When +that call reasoned, the file has its first token but not its first text, so +no speed is measured; it shows the least its answer wrote at instead: its +output less the reasoning, all written after the first token, over the time +from the first token to its end, held to the same second and 100 tokens. Its +whole output over that time would count reasoning written before the first +token and read high. A Codex turn of one call that tells neither shows the +least it wrote at over the whole turn, the wait for the first token in it +(`≥54 tok/s` for 380 tokens in 7 s), of any size, as everything it wrote +was written in that time. Gateway rows and other agents' keep their own +speed. A floor never mixes into a measured speed: the Speed trend and `magpie usage` count timed calls only. A group's speed (a row of the Usage page's ranking, by model, provider or diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 9865a3bbc..330b8e99c 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -18190,8 +18190,17 @@ const ledDecode = (r) => { if (!(n > 0 && from > 0 && w >= 100 && n * 1000 <= 10000 * w)) return null; if (r.flow_ms > 0 && r.flow_ms * 4 < w) return null; if (think && n * (r.first_text_ms - r.ttft_ms) > 20 * r.reasoning * w) return null; + if (ledCodexTurn(r) && (w < LED_CODEX_WINDOW_MS || n < LED_CODEX_BODY)) return null; return { n, w }; }; +// a Codex turn of one call read from its session file, its first token +// known: its window after the first token can be one network push, not the +// writing (Fallmonkey on Discord: a first token at 6.8 s, the rest 0.2 s +// later), so it tells a speed only over a second or more and 100 tokens or +// more (usage.CodexWindowMs, CodexBodyTokens); else ledFloor_ tells the +// least it wrote at over the whole turn +const LED_CODEX_WINDOW_MS = 1000, LED_CODEX_BODY = 100; +const ledCodexTurn = (r) => r.source === "log" && r.agent === "codex" && r.ttft_ms > 0; // how fast a reply wrote, in tokens a second: 0 when it can't tell const ledRowSpeed = (r) => { const d = ledDecode(r); return d ? d.n / (d.w / 1000) : 0; }; // the least a reply read from a session file wrote at, in tokens a second: @@ -18209,14 +18218,18 @@ const ledRowSpeed = (r) => { const d = ledDecode(r); return d ? d.n / (d.w / 100 // A Codex turn of one call that reasoned has its first token but not its // first text (Fallmonkey on Discord): its answer, its output less the // reasoning, all came after the first token, so the answer over the time -// after it is a floor of the answer's speed; any size tells one, as the wait -// isn't in it. ledFloor_ says the tokens, the ms, and whether it is that. +// after it is a floor of the answer's speed, when that time is a second or +// more and the answer 100 tokens or more, as a measured one is. A Codex turn +// of one call that tells neither tells its output over the whole turn, the +// wait in it: all of it was written in that time. ledFloor_ says the tokens, +// the ms, and which it is (answer, turn). const LED_FLOOR_AGENTS = new Set(["claude", "claude-desktop", "codex"]); const ledFloor_ = (r) => { if (r.source !== "log" || !LED_FLOOR_AGENTS.has(r.agent) || ledFailed_(r) || ledRowSpeed(r)) return null; - if (r.agent === "codex" && r.ttft_ms > 0) { - const n = r.out - r.reasoning, w = r.ms - r.ttft_ms; - return r.reasoning > 0 && n > 0 && w >= 100 && n * 1000 <= 10000 * w ? { n, w, answer: true } : null; + if (ledCodexTurn(r)) { + const n = r.out - (r.reasoning || 0), w = r.ms - r.ttft_ms; + if (r.reasoning > 0 && n >= LED_CODEX_BODY && w >= LED_CODEX_WINDOW_MS && n * 1000 <= 10000 * w) return { n, w, answer: true }; + return r.out > 0 && r.ms >= 100 && r.out * 1000 <= 10000 * r.ms ? { n: r.out, w: r.ms, turn: true } : null; } return r.out >= 1000 && r.ms >= 1000 && r.out * 1000 <= 10000 * r.ms ? { n: r.out, w: r.ms } : null; }; @@ -18497,6 +18510,7 @@ function renderLedger() { v ? (r.reasoning > 0 ? t("{n} answer tokens in {ms} after the first text, the {r} reasoning tokens before it left out", { n: ledNum(ledDecode(r).n), ms: ledTook(ledDecode(r).w), r: ledNum(r.reasoning) }) : t("{n} output tokens in {ms} after the first", { n: ledNum(r.out), ms: ledTook(r.ms - r.ttft_ms) })) + " · " + t("TTFT {ms}", { ms: ledTook(r.ttft_ms) }) : floor && ledFloor_(r).answer ? t("At least: {n} answer tokens in {ms} after the first token, the {r} reasoning tokens left out. The session file has no first text, so reasoning after the first token is counted too", { n: ledNum(ledFloor_(r).n), ms: ledTook(ledFloor_(r).w), r: ledNum(r.reasoning) }) + " · " + t("TTFT {ms}", { ms: ledTook(r.ttft_ms) }) + : floor && ledFloor_(r).turn ? t("At least: {n} output tokens in {ms}, the whole turn. What came after the first token ({after}) is too little to time a speed by: under a second or under 100 tokens, so the wait for it is counted too", { n: ledNum(r.out), ms: ledTook(r.ms), after: ledTook(r.ms - r.ttft_ms) }) + " · " + t("TTFT {ms}", { ms: ledTook(r.ttft_ms) }) : floor ? t("At least: {n} output tokens in {ms}, from the line that asked for it to its last. The session file has no first token, so the wait for it is counted too", { n: ledNum(r.out), ms: ledTook(r.ms) }) : r.ttft_ms || untimed || ledFailed_(r) ? "" : timed ? t("The session file has no first token to time a speed from") : t("Not streamed: no first token to time a speed from")); const st = el("span", "st"); diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.js index 9c80b2541..fbd2dca24 100644 --- a/internal/gui/assets/i18n.js +++ b/internal/gui/assets/i18n.js @@ -3633,6 +3633,7 @@ const I18N = { "{n} output tokens in {ms} after the first": "首字后 {ms} 输出 {n} 个 token", "At least: {n} output tokens in {ms}, from the line that asked for it to its last. The session file has no first token, so the wait for it is counted too": "下限:从发出请求的那一行到最后一行,{ms} 输出 {n} 个 token。会话文件没有首字时间,等首字的时间也算在里面", "At least: {n} answer tokens in {ms} after the first token, the {r} reasoning tokens left out. The session file has no first text, so reasoning after the first token is counted too": "下限:首字后 {ms} 输出 {n} 个正文 token,{r} 个推理 token 不计。会话文件没有首个正文的时间,首字后的推理时间也算在里面", + "At least: {n} output tokens in {ms}, the whole turn. What came after the first token ({after}) is too little to time a speed by: under a second or under 100 tokens, so the wait for it is counted too": "下限:整轮 {ms} 输出 {n} 个 token。首字后只有 {after},不足 1 秒或不足 100 个 token,测不出速度,等首字的时间也算在里面", "The session file has no first token to time a speed from": "会话文件没有首字时间,算不出速度", "A reply that reasoned counts only its answer, from its first text: the reasoning was written before the stream showed it": "有推理的回复只算正文部分,从首个正文起计:推理在流里出现前就已写完", "{n} answer tokens in {ms} after the first text, the {r} reasoning tokens before it left out": "首个正文后 {ms} 输出 {n} 个正文 token,之前的 {r} 个推理 token 不计", @@ -8098,6 +8099,7 @@ const I18N = { "{n} output tokens in {ms} after the first": "首字後 {ms} 輸出 {n} 個 token", "At least: {n} output tokens in {ms}, from the line that asked for it to its last. The session file has no first token, so the wait for it is counted too": "下限:從發出請求的那一行到最後一行,{ms} 輸出 {n} 個 token。會話檔案沒有首字時間,等首字的時間也算在裡面", "At least: {n} answer tokens in {ms} after the first token, the {r} reasoning tokens left out. The session file has no first text, so reasoning after the first token is counted too": "下限:首字後 {ms} 輸出 {n} 個正文 token,{r} 個推理 token 不計。會話檔案沒有首個正文的時間,首字後的推理時間也算在裡面", + "At least: {n} output tokens in {ms}, the whole turn. What came after the first token ({after}) is too little to time a speed by: under a second or under 100 tokens, so the wait for it is counted too": "下限:整輪 {ms} 輸出 {n} 個 token。首字後只有 {after},不足 1 秒或不足 100 個 token,測不出速度,等首字的時間也算在裡面", "The session file has no first token to time a speed from": "會話檔案沒有首字時間,算不出速度", "A reply that reasoned counts only its answer, from its first text: the reasoning was written before the stream showed it": "有推理的回覆只算正文部分,從首個正文起計:推理在流裡出現前就已寫完", "{n} answer tokens in {ms} after the first text, the {r} reasoning tokens before it left out": "首個正文後 {ms} 輸出 {n} 個正文 token,之前的 {r} 個推理 token 不計", @@ -12577,6 +12579,7 @@ const I18N = { "{n} output tokens in {ms} after the first": "最初のトークン後 {ms} で {n} トークンを出力", "At least: {n} output tokens in {ms}, from the line that asked for it to its last. The session file has no first token, so the wait for it is counted too": "下限:要求した行から最後の行まで {ms} で {n} トークンを出力。セッションファイルには最初のトークンの時刻がないため、それを待つ時間も含まれます", "At least: {n} answer tokens in {ms} after the first token, the {r} reasoning tokens left out. The session file has no first text, so reasoning after the first token is counted too": "下限:最初のトークン後 {ms} で回答 {n} トークン(推論 {r} トークンは含めず)。セッションファイルには最初の本文の時刻がないため、最初のトークン後の推論の時間も含まれます", + "At least: {n} output tokens in {ms}, the whole turn. What came after the first token ({after}) is too little to time a speed by: under a second or under 100 tokens, so the wait for it is counted too": "下限:ターン全体の {ms} で {n} トークンを出力。最初のトークン後は {after} だけで、1 秒未満か 100 トークン未満のため速度を計れず、それを待つ時間も含まれます", "The session file has no first token to time a speed from": "セッションファイルには、速度を計れる最初のトークンの時刻がありません", "A reply that reasoned counts only its answer, from its first text: the reasoning was written before the stream showed it": "推論した応答は、最初の本文からの回答部分のみ数えます。推論はストリームに出る前に書き終わっています", "{n} answer tokens in {ms} after the first text, the {r} reasoning tokens before it left out": "最初の本文後 {ms} で回答 {n} トークン(その前の推論 {r} トークンは含めず)", @@ -17059,6 +17062,7 @@ const I18N = { "{n} output tokens in {ms} after the first": "{n} Ausgabe-Token in {ms} nach dem ersten", "At least: {n} output tokens in {ms}, from the line that asked for it to its last. The session file has no first token, so the wait for it is counted too": "Mindestens: {n} Ausgabe-Token in {ms}, von der Zeile, die sie anforderte, bis zu ihrer letzten. Die Sitzungsdatei hat kein erstes Token, also zählt das Warten darauf mit", "At least: {n} answer tokens in {ms} after the first token, the {r} reasoning tokens left out. The session file has no first text, so reasoning after the first token is counted too": "Mindestens: {n} Antwort-Token in {ms} nach dem ersten Token, ohne die {r} Reasoning-Token. Die Sitzungsdatei hat keinen ersten Text, also zählt Reasoning nach dem ersten Token mit", + "At least: {n} output tokens in {ms}, the whole turn. What came after the first token ({after}) is too little to time a speed by: under a second or under 100 tokens, so the wait for it is counted too": "Mindestens: {n} Ausgabe-Token in {ms}, der ganze Turn. Nach dem ersten Token kam nur {after}, unter einer Sekunde oder unter 100 Token, zu wenig für ein Tempo, also zählt das Warten darauf mit", "The session file has no first token to time a speed from": "Die Sitzungsdatei hat kein erstes Token, an dem sich ein Tempo messen ließe", "A reply that reasoned counts only its answer, from its first text: the reasoning was written before the stream showed it": "Eine Antwort mit Reasoning zählt nur ihre Antwort ab dem ersten Text: Das Reasoning entstand, bevor der Stream es zeigte", "{n} answer tokens in {ms} after the first text, the {r} reasoning tokens before it left out": "{n} Antwort-Token in {ms} nach dem ersten Text, ohne die {r} Reasoning-Token davor", diff --git a/internal/gui/tests/usage-speed-count.test.cjs b/internal/gui/tests/usage-speed-count.test.cjs index 169d45016..ec17914dc 100644 --- a/internal/gui/tests/usage-speed-count.test.cjs +++ b/internal/gui/tests/usage-speed-count.test.cjs @@ -12,8 +12,12 @@ // rows of the Requests tab say each one's the same way. Codex's rows tell a // floor too: a call in a turn of several, timed from what asked for it, its // output over that time; a turn of one call that reasoned, its answer over -// the time after its first token. Every language, a wide window and a narrow -// one; no backend, the API is faked. +// the time after its first token. A Codex turn of one call is measured, and +// its answer's floor told, only over a second or more after its first token +// and 100 tokens or more; else it tells the least it wrote at over the whole +// turn (Fallmonkey on Discord: a first token at 6.8 s, the model working, +// and the rest 0.2 s later in one push, read as 1,900 tok/s). Every +// language, a wide window and a narrow one; no backend, the API is faked. const assert = require("node:assert/strict"); const fs = require("node:fs/promises"); const path = require("node:path"); @@ -38,8 +42,15 @@ const ROWS = [ // a short one tells nothing { ...CC, t: at(3), out: 300, ms: 20000 }, // a sol turn of one call that reasoned: its 272 answer tokens came in the - // 827 ms after its first token, at least 329 tok/s (not its 496 tokens, 600) + // 827 ms after its first token, under a second, so the whole turn: at + // least 496 tokens in 11,055 ms, 45 tok/s (not 329 from the answer alone) { ...SOL, t: at(4), out: 496, reasoning: 224, ms: 11055, ttft_ms: 10228 }, + // one that reasoned and wrote its 1,000-token answer over the 10 s after + // its first token: at least 100 tok/s (not its 1,200 tokens, 120) + { ...SOL, t: at(5), out: 1200, reasoning: 200, ms: 12000, ttft_ms: 2000 }, + // Fallmonkey's: a first token at 6.8 s and its 380 tokens 0.2 s later, one + // push, not 1,900 tok/s: at least 380 in the whole 7 s, 54 tok/s + { ...SOL, t: at(6), out: 380, ms: 7000, ttft_ms: 6800 }, ]; // the groups as usage.Breakdown sums them (TestGroupSpeedSaysHowManyRepliesItIsOf) const G = { @@ -103,27 +114,27 @@ const L = { tok: (n, k) => `${n} tok/s (${k})`, measured: (k, others) => (k === 1 ? `Measured on ${k} reply, from its first token to its end` : `Measured on ${k} replies, from their first token to their end`) + (others ? "; the others tell no speed" : ""), floorTip: (k) => `At least: ${k} replies read from session files, which don't say when their answers began, so time before that is counted too`, row: "125 tok/s", rowFloor: "≥100 tok/s", - cx: "≥50 tok/s", luna: "≥329 tok/s", lunaTip: "At least: 272 answer tokens in " }, + cx: "≥50 tok/s", luna: "≥45 tok/s", answer: "≥100 tok/s", fall: "≥54 tok/s", turnTip: "the whole turn. What came after the first token (", answerTip: "At least: 1,000 answer tokens in " }, zh: { speed: "速度", kpi: "输出速度", tok: (n, k) => `${n} token/秒(${k} 次)`, measured: (k, others) => `实测 ${k} 次回复:从首字到结束` + (others ? ";其余请求测不出速度" : ""), floorTip: (k) => `下限:${k} 次读自会话文件的回复。会话文件不记录正文何时开始,之前的时间也算在里面`, row: "125 token/秒", rowFloor: "≥100 token/秒", - cx: "≥50 token/秒", luna: "≥329 token/秒", lunaTip: "输出 272 个正文 token,224 个推理 token 不计" }, + cx: "≥50 token/秒", luna: "≥45 token/秒", answer: "≥100 token/秒", fall: "≥54 token/秒", turnTip: "首字后只有 ", answerTip: "输出 1,000 个正文 token,200 个推理 token 不计" }, "zh-TW": { speed: "速度", kpi: null, tok: (n, k) => `${n} token/秒(${k} 次)`, measured: (k, others) => `實測 ${k} 次回覆:從首字到結束` + (others ? ";其餘請求測不出速度" : ""), floorTip: (k) => `下限:${k} 次讀自會話檔案的回覆。會話檔案不記錄正文何時開始,之前的時間也算在裡面`, row: "125 token/秒", rowFloor: "≥100 token/秒", - cx: "≥50 token/秒", luna: "≥329 token/秒", lunaTip: "輸出 272 個正文 token,224 個推理 token 不計" }, + cx: "≥50 token/秒", luna: "≥45 token/秒", answer: "≥100 token/秒", fall: "≥54 token/秒", turnTip: "首字後只有 ", answerTip: "輸出 1,000 個正文 token,200 個推理 token 不計" }, ja: { speed: null, kpi: null, tok: (n, k) => `${n} トークン/秒(${k} 件)`, measured: (k, others) => `${k} 件の応答で実測:最初のトークンから終わりまで` + (others ? "。ほかのリクエストは速度を計れません" : ""), floorTip: (k) => `下限:セッションファイルから読んだ ${k} 件の応答。回答が始まった時刻がないため、その前の時間も含まれます`, row: "125 トークン/秒", rowFloor: "≥100 トークン/秒", - cx: "≥50 トークン/秒", luna: "≥329 トークン/秒", lunaTip: "で回答 272 トークン(推論 224 トークンは含めず)" }, + cx: "≥50 トークン/秒", luna: "≥45 トークン/秒", answer: "≥100 トークン/秒", fall: "≥54 トークン/秒", turnTip: "最初のトークン後は ", answerTip: "で回答 1,000 トークン(推論 200 トークンは含めず)" }, de: { speed: null, kpi: null, tok: (n, k) => `${n} Token/s (${k})`, measured: (k, others) => `Gemessen an ${k} ${k === 1 ? "Antwort" : "Antworten"}, vom ersten Token bis zum Ende` + (others ? "; die übrigen Anfragen zeigen kein Tempo" : ""), floorTip: (k) => `Mindestens: ${k} Antworten aus Sitzungsdateien. Sie sagen nicht, wann die Antworten begannen, also zählt die Zeit davor mit`, row: "125 Token/s", rowFloor: "≥100 Token/s", - cx: "≥50 Token/s", luna: "≥329 Token/s", lunaTip: "Mindestens: 272 Antwort-Token in " }, + cx: "≥50 Token/s", luna: "≥45 Token/s", answer: "≥100 Token/s", fall: "≥54 Token/s", turnTip: "der ganze Turn. Nach dem ersten Token kam nur ", answerTip: "Mindestens: 1.000 Antwort-Token in " }, }; for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { @@ -169,7 +180,12 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", assert.equal((await cell(2).textContent()).trim(), w.rowFloor); assert.equal((await cell(3).textContent()).trim(), "—"); assert.equal((await cell(4).textContent()).trim(), w.luna); - assert((await cell(4).getAttribute("title")).includes(w.lunaTip), "the answer's floor says so: " + (await cell(4).getAttribute("title"))); + assert((await cell(4).getAttribute("title")).includes(w.turnTip), "the whole turn's floor says so: " + (await cell(4).getAttribute("title"))); + assert.equal((await cell(5).textContent()).trim(), w.answer); + assert((await cell(5).getAttribute("title")).includes(w.answerTip), "the answer's floor says so: " + (await cell(5).getAttribute("title"))); + assert.equal((await cell(6).textContent()).trim(), w.fall); + const fallTip = await cell(6).getAttribute("title"); + assert(fallTip.includes(w.turnTip) && fallTip.includes("380"), "Fallmonkey's says it is the whole turn: " + fallTip); // the ranking, by tokens: each group's speed beside its requests const rk = (id) => p.locator("#ledRank .rk").filter({ has: p.locator(".rk-nm", { hasText: id }) }); diff --git a/internal/usage/ledger.go b/internal/usage/ledger.go index 33b6655dc..45a2e8d67 100644 --- a/internal/usage/ledger.go +++ b/internal/usage/ledger.go @@ -379,9 +379,6 @@ func ledgerMatched(since, until time.Time, f Filter, recs []Record, logs []sessi if !f.keeps(r) { return } - if !r.IsRejected() { - sum.add(r, pr) - } sent := r.Model if source == "" { sent = provider.SentNameIn(wires, r.Provider, r.Model, r.Effort) @@ -391,6 +388,9 @@ func ledgerMatched(since, until time.Time, f Filter, recs []Record, logs []sessi row.Cost, row.Priced = r.CostAt(*pr), true } row.Agent = AgentOf(r.Agent) + if !r.IsRejected() { + sum.addFrom(row, pr) + } rows = append(rows, row) } for i := len(recs) - 1; i >= 0; i-- { @@ -654,6 +654,36 @@ func (t *Totals) addRow(r Row) { // app.js's LED_FLOOR_AGENTS has the same. var FloorAgents = map[string]bool{"claude": true, "claude-desktop": true, "codex": true} +// A Codex turn of one call read from its session file is timed by the +// turn's time_to_first_token_ms and its end, and that window can be one +// network push, not the writing: Fallmonkey on Discord had a reply whose +// first token came at 6.8 s, the model working, and the rest 0.2 s later +// in one go, read as thousands of tok/s. Such a row's window after the first +// token tells a speed only when it is CodexWindowMs or more and its tokens +// (its answer, for one that reasoned) CodexBodyTokens or more; else it tells +// the least it wrote at over the whole turn (Row.Floor). app.js's ledDecode +// and ledFloor_ have the same. +const ( + CodexWindowMs = 1000 + CodexBodyTokens = 100 +) + +// codexTurn is whether r is a Codex turn of one call read from its session +// file, the turn's first token known. +func (r Row) codexTurn() bool { + return r.Source == "log" && r.Agent == "codex" && r.TTFT > 0 +} + +// Decode is the row's DecodeOf, a Codex turn of one call from its session +// file held to CodexWindowMs and CodexBodyTokens. +func (r Row) Decode() (tokens int, w int64) { + n, w := r.Record.Decode() + if w > 0 && r.codexTurn() && (w < CodexWindowMs || n < CodexBodyTokens) { + return 0, 0 + } + return n, w +} + // Floor is the tokens and ms a row read from a session file tells the least // its reply wrote at by, when its speed wasn't measured. 0, 0 when it tells // none. app.js's ledFloorSpeed is the same rule for one row. @@ -674,7 +704,14 @@ var FloorAgents = map[string]bool{"claude": true, "claude-desktop": true, "codex // its time. Its output over that time, as Fallmonkey asked, would count the // reasoning written before the first token too and read high: a gateway // reply of 496 tokens, 224 of them reasoning, 827 ms after a 10.2 s first -// token, reads 600 tok/s so, its measured answer 415, and this 329. +// token, reads 600 tok/s so, its measured answer 415, and this 329. That +// window is held to what a measured one is (CodexWindowMs, CodexBodyTokens): +// 827 ms can be one push as well. +// +// A Codex turn of one call that tells neither, its window after the first +// token too short or its body too small, tells its output over the whole +// turn, the wait for the first token in it: everything it wrote, reasoning +// too, was written in that time, so it is a true floor of any size. func (r Row) Floor() (tokens int, ms int64) { if r.Source != "log" || !FloorAgents[r.Agent] || r.Failed() { return 0, 0 @@ -682,15 +719,16 @@ func (r Row) Floor() (tokens int, ms int64) { if _, w := r.Decode(); w > 0 { return 0, 0 } - if r.Agent == "codex" && r.TTFT > 0 { - if r.Reasoning <= 0 { + if r.codexTurn() { + if n := r.Output - r.Reasoning; r.Reasoning > 0 && n >= CodexBodyTokens { + if w := DecodeWindow(n, r.Millis, r.TTFT); w >= CodexWindowMs { + return n, w + } + } + if r.Output <= 0 || r.Millis < MinDecodeMs || int64(r.Output)*1000 > MaxDecodeSpeed*r.Millis { return 0, 0 } - n := r.Output - r.Reasoning - if w := DecodeWindow(n, r.Millis, r.TTFT); w > 0 { - return n, w - } - return 0, 0 + return r.Output, r.Millis } if r.Output < 1000 || r.Millis < 1000 || int64(r.Output)*1000 > MaxDecodeSpeed*r.Millis { return 0, 0 diff --git a/internal/usage/speed_count_test.go b/internal/usage/speed_count_test.go index 416b707c7..f305c209f 100644 --- a/internal/usage/speed_count_test.go +++ b/internal/usage/speed_count_test.go @@ -3,6 +3,8 @@ package usage import ( "testing" "time" + + "github.com/yetone/magpie/internal/sessions" ) // A group's speed says how many replies it is of, and a group none of whose @@ -79,7 +81,8 @@ func TestGroupSpeedSaysHowManyRepliesItIsOf(t *testing.T) { // several, timed from what asked for it, by the same rule as Claude Code's; // a turn of one call that reasoned, whose first text isn't in the file, by // its answer over the time after its first token, never its whole output -// over it, which counts reasoning written before the first token. +// over it, which counts reasoning written before the first token, when that +// is a second or more and 100 tokens or more; else over the whole turn. func TestCodexRowsTellAFloor(t *testing.T) { row := func(out, reasoning int, ms, ttft int64) Row { return Row{Source: "log", Record: Record{Agent: "codex", Model: "gpt-6-luna", Output: out, Reasoning: reasoning, Millis: ms, TTFT: ttft}} @@ -90,12 +93,14 @@ func TestCodexRowsTellAFloor(t *testing.T) { n int ms int64 }{ - // the gateway measured 415 tok/s on this one's answer; 272 over 827 ms is 329 - {"one call that reasoned", row(496, 224, 11055, 10228), 272, 827}, + // 272 answer tokens 827 ms after a 10.2 s first token could be one + // push: the whole turn, 496 over 11,055 ms, 45 tok/s + {"one call that reasoned, its answer under a second", row(496, 224, 11055, 10228), 496, 11055}, {"one call that reasoned, all of it after its first token", row(1200, 200, 12000, 2000), 1000, 10000}, + {"one call that reasoned, an answer of 60 over 3 s", row(260, 200, 5000, 2000), 260, 5000}, {"one call that didn't reason tells its own speed", row(500, 0, 5000, 1000), 0, 0}, - {"one call whose answer came too fast to tell", row(500, 100, 2050, 2000), 0, 0}, - {"one call that only reasoned", row(300, 300, 9000, 2000), 0, 0}, + {"one call whose answer came too fast to tell", row(500, 100, 2050, 2000), 500, 2050}, + {"one call that only reasoned", row(300, 300, 9000, 2000), 300, 9000}, {"a call of several, 1,500 over 30 s", row(1500, 600, 30000, 0), 1500, 30000}, {"a call of several too short to tell", row(800, 100, 20000, 0), 0, 0}, } { @@ -111,3 +116,63 @@ func TestCodexRowsTellAFloor(t *testing.T) { t.Errorf("gateway row: floor %d/%d", n, ms) } } + +// Fallmonkey on Discord: a Codex turn of one call whose first token came at +// 6.8 s, the model working, and its 380 tokens 0.2 s later in one push read +// 1,900 tok/s. A Codex turn of one call from its session file is measured +// only when the window after its first token is a second or more and its +// body 100 tokens or more; else it tells the least it wrote at over the +// whole turn. The gateway's own rows, and other agents', keep their speed. +func TestCodexTurnMeasuredOnlyOverASecondAndAHundredTokens(t *testing.T) { + row := func(out int, ms, ttft int64) Row { + return Row{Source: "log", Record: Record{Agent: "codex", Model: "gpt-6.1-sol", Output: out, Millis: ms, TTFT: ttft}} + } + for _, c := range []struct { + name string + r Row + dn int + dw int64 + floorN int + floorMs int64 + }{ + {"one push 0.2 s after a 6.8 s first token", row(380, 7000, 6800), 0, 0, 380, 7000}, + {"60 tokens over 2 s", row(60, 3000, 1000), 0, 0, 60, 3000}, + {"500 tokens over 4 s", row(500, 5000, 1000), 500, 4000, 0, 0}, + {"100 tokens over exactly a second", row(100, 7800, 6800), 100, 1000, 0, 0}, + {"99 tokens over a second", row(99, 7800, 6800), 0, 0, 99, 7800}, + {"400 tokens over 999 ms", row(400, 7799, 6800), 0, 0, 400, 7799}, + } { + if n, w := c.r.Decode(); n != c.dn || w != c.dw { + t.Errorf("%s: measured %d/%d ms, want %d/%d", c.name, n, w, c.dn, c.dw) + } + if n, ms := c.r.Floor(); n != c.floorN || ms != c.floorMs { + t.Errorf("%s: floor %d/%d ms, want %d/%d", c.name, n, ms, c.floorN, c.floorMs) + } + } + // the same reply through the gateway, or Claude Code's, keeps its speed + for _, r := range []Row{ + {Record: Record{Agent: "codex", Output: 380, Millis: 7000, TTFT: 6800}}, + {Source: "log", Record: Record{Agent: "claude", Output: 380, Millis: 7000, TTFT: 6800}}, + } { + if n, w := r.Decode(); n != 380 || w != 200 { + t.Errorf("%s/%q: measured %d/%d ms, want 380/200", r.Agent, r.Source, n, w) + } + } + // a group and the ledger's sum count it as a floor, not 1,900 tok/s + var g Totals + g.addRow(row(380, 7000, 6800)) + g.addRow(row(500, 5000, 1000)) + if g.DecodeN != 1 || g.Speed() != 125 || g.FloorN != 1 || g.FloorOut != 380 || g.FloorMs != 7000 { + t.Fatalf("group: %+v", g) + } + at := time.Date(2026, 10, 11, 9, 0, 0, 0, time.UTC) + _, sum, _, _ := ledgerWith(time.Time{}, Filter{}, nil, []sessions.Call{{Time: at, Agent: "codex", Model: "gpt-6.1-sol", Tokens: sessions.Tokens{Output: 380}, Millis: 7000, TTFT: 6800}}) + if sum.DecodeN != 0 || sum.DecodeMs != 0 || sum.Timed != 1 || sum.TTFT != 6800 { + t.Fatalf("sum: %+v", sum) + } + var p Part + p.add(row(380, 7000, 6800)) + if p.DecodeMs != 0 || p.Timed != 1 { + t.Fatalf("part: %+v", p) + } +} diff --git a/internal/usage/usage.go b/internal/usage/usage.go index c80924241..49e5e0d21 100644 --- a/internal/usage/usage.go +++ b/internal/usage/usage.go @@ -519,7 +519,12 @@ func FormatCost(amountUSD float64, currency string, rate float64) string { } } -func (t *Totals) add(r Record, price *catalog.Price) { +func (t *Totals) add(r Record, price *catalog.Price) { t.addFrom(Row{Record: r}, price) } + +// addFrom is add for a ledger row, whose speed is told as Row.Decode tells +// it (a Codex turn of one call read from its session file, by its own rule). +func (t *Totals) addFrom(row Row, price *catalog.Price) { + r := row.Record t.count(r) t.Input += r.Input t.Output += r.Output @@ -530,7 +535,7 @@ func (t *Totals) add(r Record, price *catalog.Price) { if r.TTFT > 0 && !r.Failed() { t.Timed++ t.TTFT += r.TTFT - if n, w := r.Decode(); w > 0 { + if n, w := row.Decode(); w > 0 { t.DecodeMs += w t.DecodeOut += n t.DecodeN++