[GH-ISSUE #4897] [Engine] OpenAI endpoint: usage never reports cache_creation_input_tokens (OpenAI-side counterpart of #2290) #2248

Open
opened 2026-11-04 01:57:55 +07:00 by bsakaguchi · 3 comments
Owner

Originally created by @wan0791 on GitHub (Nov 1, 2026).
Original GitHub issue: https://github.com/Niko1221/Strata/issues/4897

中文

Area

[Engine] (speed, kernels, models, the server and API)

Desktop app, Terminal or source?

Terminal(预编译引擎 0.1.55,通过 new-api 网关走 OpenAI 端点)

What happened(发生了什么)

#2290(xrip,2026-10-13 合入)修复了 Anthropic 端点:serve/server.py 现在在 /v1/messages 的 message_delta.usage 里上报 cache_creation_input_tokens,于是"本次读入并保留给下一次请求的 token"不再被静默计入 input_tokens。

但 #2290 没有动 OpenAI 端点(/v1/chat/completions)。它的 usage 仍然只报:

"usage": {
  "prompt_tokens": <N>,
  "completion_tokens": <M>,
  "prompt_tokens_details": { "cached_tokens": <K> }
}

也就是说:缓存读取量(cached_tokens)有上报,但缓存写入量——本次读入并保留给下一次的 prompt token 数——没有上报。而这个写入量正是 #2290 在 Anthropic 侧算的 read_now - 1。

我预期:OpenAI 端点的 usage 也应带上 cache_creation_input_tokens(对称于 #2290),这样下游网关能区分"读缓存"与"写缓存"。

Smallest steps or command that reproduce it(最小复现)

  1. 用任意客户端(或 new-api)打 OpenAI 端点 /v1/chat/completions。
  2. 同一会话内,用同一段 prompt 连发两次。
  3. 看第二次响应的 usage:cached_tokens 有值(读缓存生效),但没有 cache_creation_input_tokens(写入不可见)。
  4. 对照组:同一段 prompt 打 Anthropic 端点 /v1/messages,第二次响应有 cache_creation_input_tokens(#2290 之后)。

Reproduction status

Reproduced locally(已在生产复现:19 万 token 多轮 agent 会话,第 2 轮起 cached_tokens 基本 99%+ 命中,但 OpenAI 端点始终无 cache_creation_input_tokens)

Strata version

Strata 0.2.9 · engine 0.1.55(strata --version = strata 0.1.55,预编译 0.1.55-linux-x86_64-hip-rx7900)

Graphics card, memory and system

  • 显卡:AMD Radeon RX 7900 XTX · 24 GB VRAM(gfx1100 / RDNA3)
  • 内存:125 GiB DDR4(HP DL360p Gen9 裸机)
  • 系统:Ubuntu 24.04.5 LTS(裸机,非 Docker)

Model, size and settings

Qwen3.8-Flash-Next · IQ3_S GGUF(SC117 abliterated)· 上下文按请求(实测最大 409,194 tokens)· 图片关闭 · 通过 new-api(calciumion rc.47)网关走 OpenAI 端点

影响(Why it matters)

new-api 的"模型调用分析"面板(new-api PR #7773 加入)读上游 usage 建两个计数器:

  • 缓存读取 ← usage.prompt_tokens_details.cached_tokens → 正常
  • 缓存写入 ← usage.cache_creation_input_tokens(或归一化后的 cache_write_tokens)→ 恒 0,因为 OpenAI 端点从不发这个字段

于是对任何走 OpenAI 协议的客户端,"缓存写入"格子永远是 0——不是没写缓存(每个新前缀确实被保留),而是 OpenAI 端点不上报写入量。

建议改法(Suggested fix)

把 #2290 在 Anthropic 侧做的事镜像到 OpenAI chat/completions 的 usage:

  1. 算 read_now = len(ids) - reused(#2290 在 serve/server.py 用的同一表达式)。
  2. 在 OpenAI usage(顶层或 prompt_tokens_details 下)加 cache_creation_input_tokens,值 = 本次读入并保留给下一次的 prompt token 数(即 read_now - 1,与 #2290 的 input_tokens: 1 约定一致,保证 prompt_tokens == cache_read + cache_creation + 1 对账)。

OpenAI 官方 schema 没定义 cache_creation_input_tokens,但 usage 里的额外字段被常见客户端容忍,而且这正是网关已经会读的字段。如果你们希望 OpenAI 负载严格贴合官方 schema,也可以改为在文档里说明等价算法(cache-write = prompt_tokens - cached_tokens - 1)让网关自己算;直接发字段对下游工具更省事。


English

Area

[Engine] (speed, kernels, models, the server and API)

Desktop app, Terminal or source?

Terminal (prebuilt engine 0.1.55, served over the OpenAI endpoint through new-api)

What happened

#2290 (xrip, merged 2026-10-13) fixed the Anthropic endpoint: serve/server.py now reports cache_creation_input_tokens in the /v1/messages message_delta.usage, so "prompt tokens read this request and retained for the next" are no longer silently folded into input_tokens.

The OpenAI endpoint (/v1/chat/completions) was not touched by #2290. Its usage still only reports:

"usage": {
  "prompt_tokens": <N>,
  "completion_tokens": <M>,
  "prompt_tokens_details": { "cached_tokens": <K> }
}

i.e. the cache-read amount (cached_tokens) is reported, but the cache-write amount — the prompt tokens read this request and retained for the next — is not. That write amount is exactly what #2290 computes as read_now - 1 on the Anthropic side.

Expected: the OpenAI usage should also carry cache_creation_input_tokens (symmetric to #2290) so downstream gateways can distinguish "cache read" from "cache write".

Smallest steps or command that reproduce it

  1. Hit the OpenAI endpoint /v1/chat/completions with any client (or new-api).
  2. In one session, send the same prompt twice.
  3. Inspect the second response's usage: cached_tokens is populated (read works) but there is no cache_creation_input_tokens (write is invisible).
  4. Control: the same prompt against the Anthropic endpoint /v1/messages does report cache_creation_input_tokens on the second call (since #2290).

Reproduction status

Reproduced locally (production: ~190k-token multi-turn agent session, ~99%+ cached_tokens hit from turn 2 on, but the OpenAI endpoint never emits cache_creation_input_tokens)

Strata version

Strata 0.2.9 · engine 0.1.55 (strata --version = strata 0.1.55, prebuilt 0.1.55-linux-x86_64-hip-rx7900)

Graphics card, memory and system

  • GPU: AMD Radeon RX 7900 XTX · 24 GB VRAM (gfx1100 / RDNA3)
  • RAM: 125 GiB DDR4 (HP DL360p Gen9 bare metal)
  • OS: Ubuntu 24.04.5 LTS (bare metal, not Docker)

Model, size and settings

Qwen3.8-Flash-Next · IQ3_S GGUF (SC117 abliterated) · context per request (max seen 409,194 tokens) · images off · served over the OpenAI endpoint through new-api (calciumion rc.47)

Why it matters

new-api's model-analysis dashboard (added in new-api PR #7773) reads the upstream usage to build two counters:

  • cache read ← usage.prompt_tokens_details.cached_tokens → works
  • cache write ← usage.cache_creation_input_tokens (or the normalized cache_write_tokens) → always 0, because the OpenAI endpoint never emits that field

So for any OpenAI-protocol client, the "cache write" tile is permanently 0 — not because nothing is written to the cache (every fresh prefix is retained), but because the OpenAI endpoint doesn't report the write count.

Suggested fix

Mirror what #2290 did on the Anthropic path for the OpenAI chat/completions usage:

  1. Compute read_now = len(ids) - reused (the same expression #2290 uses in serve/server.py).
  2. Add cache_creation_input_tokens to the OpenAI usage (top level, or under prompt_tokens_details), valued at the prompt tokens read this request and retained for the next — i.e. read_now - 1, consistent with #2290's input_tokens: 1 convention so the totals still reconcile (prompt_tokens == cache_read + cache_creation + 1).

OpenAI's own schema doesn't define cache_creation_input_tokens, but extra keys in usage are tolerated by common clients, and it is the exact field the gateways already know how to read. If you prefer to keep the OpenAI payload strictly spec-shaped, an alternative is to document the equivalent so gateways can derive it (cache-write = prompt_tokens - cached_tokens - 1); emitting the field directly is lower-friction for downstream tooling.

Originally created by @wan0791 on GitHub (Nov 1, 2026). Original GitHub issue: https://github.com/Niko1221/Strata/issues/4897 ## 中文 ### Area [Engine] (speed, kernels, models, the server and API) ### Desktop app, Terminal or source? Terminal(预编译引擎 0.1.55,通过 new-api 网关走 OpenAI 端点) ### What happened(发生了什么) #2290(xrip,2026-10-13 合入)修复了 **Anthropic** 端点:`serve/server.py` 现在在 `/v1/messages` 的 `message_delta.usage` 里上报 `cache_creation_input_tokens`,于是"本次读入并保留给下一次请求的 token"不再被静默计入 `input_tokens`。 但 **#2290 没有动 OpenAI 端点**(`/v1/chat/completions`)。它的 `usage` 仍然只报: ```json "usage": { "prompt_tokens": <N>, "completion_tokens": <M>, "prompt_tokens_details": { "cached_tokens": <K> } } ``` 也就是说:缓存**读取**量(`cached_tokens`)有上报,但缓存**写入**量——本次读入并保留给下一次的 prompt token 数——没有上报。而这个写入量正是 #2290 在 Anthropic 侧算的 `read_now - 1`。 我预期:OpenAI 端点的 `usage` 也应带上 `cache_creation_input_tokens`(对称于 #2290),这样下游网关能区分"读缓存"与"写缓存"。 ### Smallest steps or command that reproduce it(最小复现) 1. 用任意客户端(或 new-api)打 OpenAI 端点 `/v1/chat/completions`。 2. 同一会话内,用同一段 prompt 连发两次。 3. 看第二次响应的 `usage`:`cached_tokens` 有值(读缓存生效),但**没有** `cache_creation_input_tokens`(写入不可见)。 4. 对照组:同一段 prompt 打 Anthropic 端点 `/v1/messages`,第二次响应**有** `cache_creation_input_tokens`(#2290 之后)。 ### Reproduction status Reproduced locally(已在生产复现:19 万 token 多轮 agent 会话,第 2 轮起 `cached_tokens` 基本 99%+ 命中,但 OpenAI 端点始终无 `cache_creation_input_tokens`) ### Strata version Strata 0.2.9 · engine 0.1.55(`strata --version` = strata 0.1.55,预编译 `0.1.55-linux-x86_64-hip-rx7900`) ### Graphics card, memory and system - 显卡:AMD Radeon RX 7900 XTX · 24 GB VRAM(gfx1100 / RDNA3) - 内存:125 GiB DDR4(HP DL360p Gen9 裸机) - 系统:Ubuntu 24.04.5 LTS(裸机,非 Docker) ### Model, size and settings Qwen3.8-Flash-Next · IQ3_S GGUF(SC117 abliterated)· 上下文按请求(实测最大 409,194 tokens)· 图片关闭 · 通过 new-api(calciumion rc.47)网关走 OpenAI 端点 ### 影响(Why it matters) new-api 的"模型调用分析"面板(new-api PR #7773 加入)读上游 usage 建两个计数器: - 缓存**读取** ← `usage.prompt_tokens_details.cached_tokens` → 正常 - 缓存**写入** ← `usage.cache_creation_input_tokens`(或归一化后的 `cache_write_tokens`)→ **恒 0**,因为 OpenAI 端点从不发这个字段 于是对任何走 OpenAI 协议的客户端,"缓存写入"格子永远是 0——不是没写缓存(每个新前缀*确实*被保留),而是 OpenAI 端点不上报写入量。 ### 建议改法(Suggested fix) 把 #2290 在 Anthropic 侧做的事镜像到 OpenAI `chat/completions` 的 usage: 1. 算 `read_now = len(ids) - reused`(#2290 在 `serve/server.py` 用的同一表达式)。 2. 在 OpenAI `usage`(顶层或 `prompt_tokens_details` 下)加 `cache_creation_input_tokens`,值 = 本次读入并保留给下一次的 prompt token 数(即 `read_now - 1`,与 #2290 的 `input_tokens: 1` 约定一致,保证 `prompt_tokens == cache_read + cache_creation + 1` 对账)。 OpenAI 官方 schema 没定义 `cache_creation_input_tokens`,但 `usage` 里的额外字段被常见客户端容忍,而且这正是网关已经会读的字段。如果你们希望 OpenAI 负载严格贴合官方 schema,也可以改为在文档里说明等价算法(cache-write = `prompt_tokens - cached_tokens - 1`)让网关自己算;直接发字段对下游工具更省事。 --- ## English ### Area [Engine] (speed, kernels, models, the server and API) ### Desktop app, Terminal or source? Terminal (prebuilt engine 0.1.55, served over the OpenAI endpoint through new-api) ### What happened #2290 (xrip, merged 2026-10-13) fixed the **Anthropic** endpoint: `serve/server.py` now reports `cache_creation_input_tokens` in the `/v1/messages` `message_delta.usage`, so "prompt tokens read this request and retained for the next" are no longer silently folded into `input_tokens`. The **OpenAI** endpoint (`/v1/chat/completions`) was not touched by #2290. Its `usage` still only reports: ```json "usage": { "prompt_tokens": <N>, "completion_tokens": <M>, "prompt_tokens_details": { "cached_tokens": <K> } } ``` i.e. the cache-**read** amount (`cached_tokens`) is reported, but the cache-**write** amount — the prompt tokens read this request and retained for the next — is not. That write amount is exactly what #2290 computes as `read_now - 1` on the Anthropic side. Expected: the OpenAI `usage` should also carry `cache_creation_input_tokens` (symmetric to #2290) so downstream gateways can distinguish "cache read" from "cache write". ### Smallest steps or command that reproduce it 1. Hit the OpenAI endpoint `/v1/chat/completions` with any client (or new-api). 2. In one session, send the same prompt twice. 3. Inspect the second response's `usage`: `cached_tokens` is populated (read works) but there is **no** `cache_creation_input_tokens` (write is invisible). 4. Control: the same prompt against the Anthropic endpoint `/v1/messages` **does** report `cache_creation_input_tokens` on the second call (since #2290). ### Reproduction status Reproduced locally (production: ~190k-token multi-turn agent session, ~99%+ `cached_tokens` hit from turn 2 on, but the OpenAI endpoint never emits `cache_creation_input_tokens`) ### Strata version Strata 0.2.9 · engine 0.1.55 (`strata --version` = strata 0.1.55, prebuilt `0.1.55-linux-x86_64-hip-rx7900`) ### Graphics card, memory and system - GPU: AMD Radeon RX 7900 XTX · 24 GB VRAM (gfx1100 / RDNA3) - RAM: 125 GiB DDR4 (HP DL360p Gen9 bare metal) - OS: Ubuntu 24.04.5 LTS (bare metal, not Docker) ### Model, size and settings Qwen3.8-Flash-Next · IQ3_S GGUF (SC117 abliterated) · context per request (max seen 409,194 tokens) · images off · served over the OpenAI endpoint through new-api (calciumion rc.47) ### Why it matters new-api's model-analysis dashboard (added in new-api PR #7773) reads the upstream usage to build two counters: - **cache read** ← `usage.prompt_tokens_details.cached_tokens` → works - **cache write** ← `usage.cache_creation_input_tokens` (or the normalized `cache_write_tokens`) → **always 0**, because the OpenAI endpoint never emits that field So for any OpenAI-protocol client, the "cache write" tile is permanently 0 — not because nothing is written to the cache (every fresh prefix *is* retained), but because the OpenAI endpoint doesn't report the write count. ### Suggested fix Mirror what #2290 did on the Anthropic path for the OpenAI `chat/completions` usage: 1. Compute `read_now = len(ids) - reused` (the same expression #2290 uses in `serve/server.py`). 2. Add `cache_creation_input_tokens` to the OpenAI `usage` (top level, or under `prompt_tokens_details`), valued at the prompt tokens read this request and retained for the next — i.e. `read_now - 1`, consistent with #2290's `input_tokens: 1` convention so the totals still reconcile (`prompt_tokens == cache_read + cache_creation + 1`). OpenAI's own schema doesn't define `cache_creation_input_tokens`, but extra keys in `usage` are tolerated by common clients, and it is the exact field the gateways already know how to read. If you prefer to keep the OpenAI payload strictly spec-shaped, an alternative is to document the equivalent so gateways can derive it (cache-write = `prompt_tokens - cached_tokens - 1`); emitting the field directly is lower-friction for downstream tooling.
Author
Owner

@github-actions[bot] commented on GitHub (Nov 1, 2026):

Thanks for the issue. To keep the tracker readable, every item needs an area prefix in the title and the parts of the issue form filled in. This one is missing:

  • Describe it in the issue body: what happened, what you expected, and the exact error if there is one. The issue forms (New issue > Bug report) list what to include.
  • Add the Strata version (Settings > About, or strata --version).
  • Add your graphics card with its VRAM, your RAM and your operating system.

Edit the issue (title and description) and this note goes away on its own. If it is still missing parts in 5 days it will be closed, which is not a rejection: fix it and comment /reopen.

Details: CONTRIBUTING.md

<!-- gh-comment-id:6556453166 --> @github-actions[bot] commented on GitHub (Nov 1, 2026): Thanks for the issue. To keep the tracker readable, every item needs an area prefix in the title and the parts of the issue form filled in. This one is missing: - [ ] Describe it in the issue body: what happened, what you expected, and the exact error if there is one. The issue forms (New issue > Bug report) list what to include. - [ ] Add the Strata version (Settings > About, or `strata --version`). - [ ] Add your graphics card with its VRAM, your RAM and your operating system. Edit the issue (title and description) and this note goes away on its own. If it is still missing parts in 5 days it will be closed, which is not a rejection: fix it and comment `/reopen`. Details: [CONTRIBUTING.md](https://github.com/Niko1221/Strata/blob/main/CONTRIBUTING.md)
Author
Owner

@github-actions[bot] commented on GitHub (Nov 1, 2026):

Thanks, this follows the format now.

<!-- gh-comment-id:6556610161 --> @github-actions[bot] commented on GitHub (Nov 1, 2026): Thanks, this follows the format now.
Author
Owner

@wan0791 commented on GitHub (Nov 1, 2026):

中文

感谢格式确认。

补充一点上下文:我方场景是通过 new-api(OpenAI 协议)网关使用 Strata,多轮 agent 会话 ~19 万 token,第 2 轮起 cached_tokens 基本 99%+ 命中(读缓存正常),但 OpenAI 端点始终不上报 cache_creation_input_tokens,导致 new-api 面板"缓存写入"恒 0。

等上游对"OpenAI 端点是否也补这个字段"的判断。如果你们认为 OpenAI 负载应严格贴合官方 schema(不额外加字段),那在文档里说明等价算法(cache-write = prompt_tokens - cached_tokens - 1)也够用,我们可以在 new-api 侧适配。


English

Thanks for the format check.

A bit of context on our side: we use Strata through new-api (OpenAI protocol). Multi-turn agent sessions at ~190k tokens, cached_tokens is ~99%+ hit from turn 2 on (cache read works), but the OpenAI endpoint never reports cache_creation_input_tokens, so new-api's "cache write" tile stays at 0.

We'll wait for your call on whether the OpenAI endpoint should also emit this field. If you prefer to keep the OpenAI payload strictly spec-shaped (no extra field), then documenting the equivalent (cache-write = prompt_tokens - cached_tokens - 1) is sufficient and we can adapt on the new-api side.

<!-- gh-comment-id:6556647241 --> @wan0791 commented on GitHub (Nov 1, 2026): ## 中文 感谢格式确认。 补充一点上下文:我方场景是通过 new-api(OpenAI 协议)网关使用 Strata,多轮 agent 会话 ~19 万 token,第 2 轮起 `cached_tokens` 基本 99%+ 命中(读缓存正常),但 OpenAI 端点始终不上报 `cache_creation_input_tokens`,导致 new-api 面板"缓存写入"恒 0。 等上游对"OpenAI 端点是否也补这个字段"的判断。如果你们认为 OpenAI 负载应严格贴合官方 schema(不额外加字段),那在文档里说明等价算法(cache-write = `prompt_tokens - cached_tokens - 1`)也够用,我们可以在 new-api 侧适配。 --- ## English Thanks for the format check. A bit of context on our side: we use Strata through new-api (OpenAI protocol). Multi-turn agent sessions at ~190k tokens, `cached_tokens` is ~99%+ hit from turn 2 on (cache read works), but the OpenAI endpoint never reports `cache_creation_input_tokens`, so new-api's "cache write" tile stays at 0. We'll wait for your call on whether the OpenAI endpoint should also emit this field. If you prefer to keep the OpenAI payload strictly spec-shaped (no extra field), then documenting the equivalent (cache-write = `prompt_tokens - cached_tokens - 1`) is sufficient and we can adapt on the new-api side.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#2248
No description provided.