Usage → Requests: T3 Code calls are counted twice — the gateway row (agent `t3code`) never pairs with its `codex` session-file row
Summary
On Usage → Requests, every call made by T3 Code (which drives codex app-server) is listed twice: once as the gateway request (agent: t3code, served model + cost + TTFT filled) and once as a Codex local-session row (agent: codex, provider: session-unknown, only the requested model name, with served model / cost / TTFT shown as —). Both rows carry the same response_id, so they are the same physical call.
The client asks for a magpie routing group, so the requested name is a group alias the gateway resolves into a real model; the local-session row never sees that resolution and can only repeat the alias.
This is the same symptom as #3674, but the blocker here is not a missing id — both sides have it. The id pairing is gated on both sides being codex (internal/usage/codex_identity.go:65), and a T3 Code request is recorded on the gateway side as agent: t3code, so the pair is refused and the local-session row stays in the ledger.
Environment
- magpie version: 0.1.4762 (
magpie version), bare daemon:magpie serve --web=127.0.0.1:3430, systemd user unit, Linux - Where: GUI — Usage → Requests
- OS: Linux (Ubuntu, x86_64)
- Agent, provider and model: T3 Code →
codex app-server→ a magpie routing group → two third-party relays plus one ChatGPT subscription account - Code pointers below are read at tag v0.1.4762 (commit
161baac4); the same gate is still present onmainateab4b00.
Steps to reproduce
- Point a Codex-based client whose
User-Agent/Originatoris notcodex*— T3 Code runscodex app-serverthis way — at magpie, and select a routing group as the model. - Send a few normal turns.
- Open Usage → Requests. Each call appears as two rows: one
T3 Coderow with the served model and a cost, and oneCodexrow tagged Local session carrying the ChatGPT account, the raw group alias only, and—for the served model, cost and TTFT.
What happened, and what was expected
Happened: the page lists the same call twice. The Local session row cannot resolve anything beyond the requested group alias, so the served-model and cost columns stay —, while its gateway twin shows the real model and cost a few rows away.
Expected: one row per call — the same way a request from an agent recorded as codex on the gateway side pairs with its session-file row and the Local session duplicate is dropped (internal/usage/ledger.go:526 skips matched local rows).
Evidence
Two concrete pairs (times are Asia/Shanghai; the local-session rows carry the same response_id as their gateway twin):
| Row | time | agent | model column | provider side | in | cache_read | out | cost | ms |
|---|---|---|---|---|---|---|---|---|---|
| gateway | 2026-11-25 20:05:48.385 | t3code |
served model, resolved | relay A | 837 | 242944 | 267 | 8.116656e-05 | 29104 |
| local session | 2026-11-25 20:06:23.924 | codex |
group alias, unresolved | session-unknown |
837 | 242944 | 267 | 0 | 44563 |
| gateway | 2026-11-25 20:04:54.889 | t3code |
served model, resolved | relay B | 261523 | 0 | 99 | 0.03928785 | 84086 |
| local session | 2026-11-25 20:06:24.946 | codex |
group alias, unresolved | session-unknown |
261523 | 0 | 99 | 0 | 95371 |
response_ids are identical inside each pair (both sides of pair 1 end …b7244, both sides of pair 2 end …0b547).
Scale in this instance, window 2026-11-25 00:05:37 → 20:53:42 (+08), 30,000 rows of GET /api/usage/requests?period=30d&tz=Asia/Shanghai&limit=500&offset=N:
- 4,229 duplicate pairs, every one of them
(agent: codex, source: log)+(agent: t3code, source: null). No duplicate pair involves a gateway row recorded ascodex. - Local-session side of those pairs: input 36,657,402, cache_read 602,773,248, output 3,369,086 tokens.
- Gateway side in the same window: input 239,995,428, cache_read 3,323,084,465, output 22,593,460.
- ⇒ page totals read +17.9% tokens and +12.0% cost on this instance.
- Of the 4,229 duplicated local-session rows, 3,885 requested an unpriced group alias (they only inflate row count and tokens) and 344 requested an alias that does carry a price, so those inflate cost as well.
- 23,176 of the 25,232 gateway rows in the window are
agent: t3code, so this is not a rare edge case on a T3-Code-only instance.
Counting method: page GET /api/usage/requests with limit=500 until the window is covered, then group the rows by response_id and keep ids that appear exactly twice with one source: "log" row and one gateway row.
Root cause
internal/usage/codex_identity.go:65— the id pairing accepts a pair only whenrecords[record].Agent == "codex"andcalls[call].Agent == "codex". The gateway record of a T3 Code request hasagent: t3code, so an id that matches on both sides is still refused.internal/usage/ledger.go:406-409—logRecorddoes carryResponseIDfor Codex session files, so unlike #3674 the local row does have the id here.internal/usage/ledger.go:526— local-session rows that are not inmatchedare appended to the ledger, i.e. the page shows them next to their gateway twin.- The non-id fallback (same native session, equal token counts, near-equal end) does not rescue these rows either: the two rows' end times differ by ~6 s (gateway end 20:06:18.98 vs local row 20:06:24.95), and the recorded durations differ as well (84,086 ms vs 95,371 ms; 29,104 ms vs 44,563 ms).
Related
- #3674 — same page, same double counting, reported for
dsh; there the response id never reaches the ledger. Here the id survives and the agent label is what blocks correlation.
Note
This report describes the observed behaviour and the code path that produces it; no patch is proposed. Happy to re-measure on a build that changes the pairing rule.
Fixed. The diagnosis was right and the fix is the minimal change to the pairing rule: the gateway side no longer needs Agent == "codex" — it needs a ResponseID (which is the vendor's and names only a Codex answer), not to be rejected and not to be reported. The session file the same call wrote is always Codex's, so the call pairs whatever the driving client called itself.
Both gates that ask the question moved together: codexResponseMatches and codexGatewayCall (the packed-row path the ledger uses on large histories). A T3-Code gateway row and the Codex session file of the same call now pair by the shared response id, and the local-session duplicate is dropped.
The test reproduces the exact two-row shape from the report (a t3code/maru-code gateway row and a codex/session-unknown local row, one response_id, no Session on the gateway side so only the id can pair them): red on main with the call listed twice, green with the fix once.
Branch: https://github.com/yetone/magpie/compare/main...joei4cm:magpie:fix/7746-codex-client-pairs-by-response-id (internal/usage only, eab4b006d + one commit; go test -tags nogui ./internal/usage ok, vet/gofmt clean). The fix keeps the local side Codex-only — it can't widen matching to a non-Codex session file.
Independent maintenance check: L0NE-6/magpie-panel now carries the T3 Code response-id pairing fix as 8ef1bd0e. A gateway call with a matching response_id pairs with its Codex session-file row even when another client drove Codex. The focused usage regression and go vet pass. The full Windows internal/usage package still has an unrelated account-metadata failure. This is a fork integration, not an upstream merge.
@L0NE-6 thanks for the independent maintenance check — good to see the T3 Code response-id pairing fix already integrated and verified in L0NE-6/magpie-panel as 8ef1bd0e.
Since this account cannot open a pull request against yetone/magpie (not a collaborator, no push access), may I ask a favour: would you be willing to open the upstream PR for this one? Your fork branch already carries the change with the focused usage regression and go vet passing, so it should be a straightforward upstream PR.
For reference, our fix branch is joei4cm/magpie → fix/7746-codex-client-pairs-by-response-id (fee024765): both gates that ask the question moved together — codexResponseMatches and codexGatewayCall — and the pairing rule now requires a ResponseID rather than Agent == "codex". Happy to provide the patch as a clean diff, or to answer any review questions, if that makes it easier. Co-authorship however you prefer.
Maintenance verification: branch L0NE-6/magpie-panel:fix/7746-codex-response-id at da9bd4db pairs the gateway row by response_id without requiring the driving client to be named codex, while the session-file side stays Codex-only. The T3-Code-shaped regression test is red before the change and green after; focused usage test and go vet -tags nogui ./internal/usage pass. Upstream PR creation is currently disabled, so the branch is available for a maintainer to cherry-pick.
@yetone sorry for the direct ping — this one has two independently written and independently verified fix branches ready to pick, so it may be worth a look.
Cherry-pickable branches (T3 Code response-id pairing):
joei4cm/magpie:fix/7746-codex-client-pairs-by-response-id(fee024765)L0NE-6/magpie-panel:fix/7746-codex-response-id(da9bd4db)
Both make the gateway side pair a call by ResponseID instead of requiring Agent == "codex", so a T3 Code gateway call pairs with its Codex session-file row even when another client drove Codex. Both report a regression test that is red before the change and green after, plus focused internal/usage tests and go vet -tags nogui ./internal/usage passing. Our account cannot open an upstream PR (no collaborator access), and L0NE-6 reports upstream PR creation is currently disabled — so either branch is available for you to cherry-pick as-is.