trae 0.2.31: a token_usage counting 0 read and 0 written is unknown, not a 0% miss

feng2787 on magpie's Discord: Trae through magpie shows cache hits always
zero. The plugin passes on what Trae's token_usage says. Since 10-27 Trae
answers cache_read_input_tokens 0 (and cache_creation_input_tokens 0) on
every turn while it still bills the turn at its cache price: a real Trae CN
account on 10-29, deepseek-v4.1-flash, a 146k prompt, 5.84 credits cold and
0.12 warm, cache_read 0 on both (dingminhua/dsh-connect-trae,
docs/ISSUE69_DIAGNOSIS.md; the ratio matches Trae's cache-read/input price,
0.006/0.30). 0.2.26 made a token_usage with no cache field unknown, but a
named 0 still went on as a counted miss, so magpie showed 0%.

Now a token_usage whose cache read and write are both 0 goes on with
magpie_cache_unknown: true, which magpie (v0.1.2396 and later) shows as
unknown, not 0%. A nonzero read or a write alone (a first turn that
wrote the cache) goes as before. The cache itself was never missed; magpie
cannot make Trae report it again.

Run: bun test in packages/trae on mote, 240 pass. Broken by hand: without
the new line, "a cache read and write of 0 is unknown" fails (8 pass, 1
fail in cache.test.mjs).
Not tried: a real Trae account (none here); the 0/0 shape and the billing
come from the third-party measurement above. A real cold, short turn that
Trae caches nothing of also shows unknown now, not 0%.

Reported-by: feng2787
This commit is contained in:
yetone
2026-11-06 15:02:08 +08:00
parent 39f5427420
commit 4578b11de2
4 changed files with 28 additions and 7 deletions
+3 -1
View File
@@ -118,7 +118,9 @@ agent's `POST https://trae-api-cn.mchost.guru/api/agent/v3/llm_utils_chat`:
`prompt_cache_hit_tokens` and `cached_tokens` are read as cache reads
too. A `token_usage` that names no cache count at all goes on with
`magpie_cache_unknown: true`, so magpie shows the cache as unknown,
not as a 0% miss (0.2.26).
not as a 0% miss (0.2.26). So does one that counts 0 read and 0
written: Trae answers that while it bills the turn at its cache price
(0.2.31).
`done`'s `finish_reason` `length`/`max_tokens` is OpenAI's `length`.
- Queue events are ignored.
- A `{"reasoning_content": …}` object written into the text is reasoning,
+16 -4
View File
@@ -60,11 +60,23 @@ test("OpenAI's own cache count is kept as it came", async () => {
expect((await res.json()).usage).toEqual({ prompt_tokens: 100, completion_tokens: 5, total_tokens: 105, prompt_tokens_details: { cached_tokens: 64 } })
})
test("no cache count adds no field", async () => {
// feng2787 on magpie's Discord: every Trae hit 0. Trae answers 0 read and
// 0 written while it bills the turn at its cache price, so a count of 0
// says nothing: the cache is unknown, not a 0% miss. A write alone, the
// first turn of a prefix, is still a miss Trae counted.
test("a cache read and write of 0 is unknown", async () => {
for (const counts of [{ cache_read_input_tokens: 0 }, { cache_read_input_tokens: 0, cache_creation_input_tokens: 0 }]) {
f?.close()
f = fakeTrae()
f.route("POST /api/agent/v3/llm_utils_chat", () => sse([["output", { response: "ok" }], ["token_usage", { prompt_tokens: 10, completion_tokens: 5, ...counts }], ["done", {}]]))
const res = await ask({ model: "glm-5", messages: [{ role: "user", content: "hi" }] })
expect((await res.json()).usage).toEqual({ prompt_tokens: 10, completion_tokens: 5, total_tokens: 15, magpie_cache_unknown: true })
}
f.close()
f = fakeTrae()
f.route("POST /api/agent/v3/llm_utils_chat", () => sse([["output", { response: "ok" }], ["token_usage", { prompt_tokens: 10, completion_tokens: 5, cache_read_input_tokens: 0 }], ["done", {}]]))
f.route("POST /api/agent/v3/llm_utils_chat", () => sse([["output", { response: "ok" }], ["token_usage", { prompt_tokens: 10, completion_tokens: 5, cache_read_input_tokens: 0, cache_creation_input_tokens: 8 }], ["done", {}]]))
const res = await ask({ model: "glm-5", messages: [{ role: "user", content: "hi" }] })
expect((await res.json()).usage).toEqual({ prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 })
expect((await res.json()).usage).toEqual({ prompt_tokens: 10, completion_tokens: 5, total_tokens: 15, cache_creation_input_tokens: 8 })
})
// plugins#455 (chansee97): TRAE CN's own client's llm_utils_chat answer,
@@ -101,7 +113,7 @@ for (const stream of [true, false]) {
// A token_usage that names no cache at all — Trae's own bytes, as
// plugins#455 captured one — is told to magpie as unknown
// (magpie_cache_unknown), so it shows no hit rate, not a 0% Trae never
// said; a counted 0 above stays a miss.
// said; nor is a count of 0 (above).
const UNCOUNTED = 'event: token_usage\ndata: {"prompt_tokens":1717,"completion_tokens":2,"total_tokens":1719}\n\n'
for (const stream of [true, false]) {
+8 -1
View File
@@ -1562,7 +1562,13 @@ const eventName = (s) =>
// went on with no cache field, which magpie can only read as a miss, so
// since 10-28 every chat showed 0 (yetone/magpie#3979, #3991; plugins#434
// says Trae stopped sending cache_read_input_tokens then). A count naming
// the cache, a 0 included, is Trae's own word and goes as before.
// the cache with nothing read and nothing written is unknown too: Trae
// answers 0 and 0 while it bills the turn at its cache price (a real
// Trae CN account on 10-29, deepseek-v4.1-flash, a 146k prompt: 5.84
// credits cold, 0.12 warm, cache_read 0 on both; dingminhua/dsh-connect-
// trae's docs/ISSUE69_DIAGNOSIS.md), so a 0 says nothing of the cache
// (feng2787 on magpie's Discord: every hit 0). A nonzero read or write is
// Trae's word, as before.
// DeepSeek's prompt_cache_hit_tokens, a bare cached_tokens and the
// Responses-style input_tokens_details are read too, the largest of them,
// should Trae pass a model's own usage on.
@@ -1585,6 +1591,7 @@ function tokensOf(u) {
const w = n(u.cache_creation_input_tokens, u.cacheCreationInputTokens)
if (r) out.cache_read_input_tokens = r
if (w) out.cache_creation_input_tokens = w
if (!r && !w) out.magpie_cache_unknown = true
}
const thought = n(u.reasoning_tokens, u.completion_tokens_details?.reasoning_tokens)
if (thought) out.completion_tokens_details = { reasoning_tokens: thought }
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-trae-auth",
"version": "0.2.30",
"version": "0.2.31",
"description": "OpenCode provider plugins for Trae: Trae CN (trae.cn) and Trae Global (trae.ai), free tier included, with Trae's own browser sign-in",
"type": "module",
"main": "./opencode.mjs",