[PR #1013] [CLOSED] serve: a continued batch request reports its own prompt's reuse; input_tokens never 0 #2966

Closed
opened 2026-11-04 02:04:32 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/1013
Author: @CurtisThe
Created: 10/6/2026
Status: ❌ Closed

Base: main ← Head: fix/batch-usage-input-tokens


📝 Commits (10+)

  • fa16567 setup: an earlier config that does not parse is skipped, not a crash; configs are written atomically (#459)
  • 749d957 setup (--backend hip): a system ROCm without the HIP development files falls back to the wheels (#446)
  • 5a30024 calibrate: the engine's arguments come from the server's engine_args; a failed tuning says why (#447)
  • 9f60d13 serve, setup: name the small split card that caps the prompt chunk; setup recommends the big card alone (#448)
  • eb8da1d /metrics: each request's speculative draft counts, and their sums in the totals (#457)
  • 6a09387 setup (--gguf-dir): GGUFs Strata cannot run are named as such, with the --family/--model to use (#444)
  • d6a91e2 docs: the Coder is weaker outside code, Chinese/CJK text included; the unpruned sizes for that (#438)
  • d8d4ad3 Merge branch 'f-135-eng' into rel-0.1.35
  • 7b600d5 Merge branch 'f-135-small' into rel-0.1.35
  • d9ab843 Engine 0.1.35: Windows AMD uses its bundled HIP runtime (#468 #461), the low-RAM resident mode on Windows 32 GB PCs (#467), fixes #460 #459 #446 #447 #457 #448 #444; setup requires it

📊 Changes

2 files changed (+51 additions, -7 deletions)

View changed files

📝 serve/server.py (+11 -2)
📝 serve/test_parallel.py (+40 -5)

📄 Description

With "parallel": 2, our Claude Code agents stopped with "Prompt is too long · automatic compaction failed" at 60–65k tokens of a 137k window. The engine never rejected a request: every turn was HTTP 200, with no SSE error event and no stream cut short.

Cause. A request that is STOPped on the solo path and continued in a batch slot (or sent back to the solo path) is sent again as its prompt plus what it generated. The DONE of that last leg counts this request's own output as reused. min(reused, len(ids)) then turns it into the whole prompt, so message_delta says input_tokens: 0 and cache_read_input_tokens = the whole prompt.

Claude Code keeps message_start's input_tokens when message_delta's is 0 and adds cache_read_input_tokens, so it counts the prompt twice and compacts or blocks at half the window.

Measured (RTX 5090 32 GB, Ryzen 9 9950X3D, 91 GB RAM, Qwen3.8-Flash-Next IQ3_S, v0.1.39 at 6f32ec0, Claude Code 2.1.290, a 137k window):

v0.1.39 agent runs responses input_tokens: 0 agent result
agent alone (parallel 1: 3 runs; parallel 2: 1 run) 4 204 0 no "Prompt is too long"
agent beside chats or a second agent (parallel 2 and 4) 5 177 2–14 per run 2 ended "Prompt is too long · compaction failed" at 60–65k
this patch, parallel 2, agent beside 8 chats 1 54 0 (smallest 57) passed, context up to 108k

Claude Code's side was measured against a stub that sends these exact usage events:

  • input_tokens: 0 with a 64k prompt: auto-compaction at preTokens: 128073, the same as a real 128k prompt.
  • input_tokens: 1 with the rest as cache_read_input_tokens: no compaction.

Change

  • generate_batched keeps the first leg's reused, which is the request's own prompt, across a continuation.
  • anthropic_events clamps the reported reuse to len(ids) - 1. The last prompt token is always read, so input_tokens is never 0 and input_tokens + cache_read_input_tokens is still the prompt.
  • test_parallel.py: the fake engine can report a held prefix (--reuse). The new test, test_a_continued_request_reports_its_own_prompt_reuse, fails without the fix (input_tokens: 0, cache_read_input_tokens: 260) and passes with it.
  • All 269 tests under serve/ pass (python -m unittest discover -s serve -t . -p 'test_*.py', Linux, Python 3.12, jsonschema installed). No engine change.

The OpenAI path (prompt_tokens_details.cached_tokens, timings cache_n) uses the same reused, so it now also reports the first leg's figure for a continued request. I left its clamp alone.

Related open PRs

  • #998 and #751 touch the same lines. #998 adds --fail-window to test_parallel's fake engine where this adds --reuse. #751 adds a line to generate_batched's cleanup block next to the one added here. In both cases the fix is to keep both changes; I can rebase onto whichever lands first.
  • #753 (draft, waiting on #559) reworks per-request accounting in the batch path, including continuation segments. If it lands, it would replace the reused0 part of this change. This PR is the minimal fix on current main.
  • #615 applies the same rule (report the first segment's input/cache counts) to reasoning_budget_tokens continuations, which use a different code path.

I wrote this with an AI coding assistant and checked the change and the test runs myself.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/1013 **Author:** [@CurtisThe](https://github.com/CurtisThe) **Created:** 10/6/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix/batch-usage-input-tokens` --- ### 📝 Commits (10+) - [`fa16567`](https://github.com/Niko1221/Strata/commit/fa165679f9860552ac97ebd07118bb8db286be4a) setup: an earlier config that does not parse is skipped, not a crash; configs are written atomically (#459) - [`749d957`](https://github.com/Niko1221/Strata/commit/749d957a1790b1a171dd4a01ab3e31b3882c9892) setup (--backend hip): a system ROCm without the HIP development files falls back to the wheels (#446) - [`5a30024`](https://github.com/Niko1221/Strata/commit/5a3002446dbb9dfcabb7d0bd9f113da90cc2f3c0) calibrate: the engine's arguments come from the server's engine_args; a failed tuning says why (#447) - [`9f60d13`](https://github.com/Niko1221/Strata/commit/9f60d13246e46b02b1dd0841980b4d18b0faaea7) serve, setup: name the small split card that caps the prompt chunk; setup recommends the big card alone (#448) - [`eb8da1d`](https://github.com/Niko1221/Strata/commit/eb8da1d8c26ec41e6ffb084fdf579dba7cad10bd) /metrics: each request's speculative draft counts, and their sums in the totals (#457) - [`6a09387`](https://github.com/Niko1221/Strata/commit/6a093874fe037020f49bd5cf053879ff6a133c21) setup (--gguf-dir): GGUFs Strata cannot run are named as such, with the --family/--model to use (#444) - [`d6a91e2`](https://github.com/Niko1221/Strata/commit/d6a91e2d1770d0f469d6ec15890acc7ee93e764c) docs: the Coder is weaker outside code, Chinese/CJK text included; the unpruned sizes for that (#438) - [`d8d4ad3`](https://github.com/Niko1221/Strata/commit/d8d4ad337d3c98ba21d3bd0af1a16e9177e112b6) Merge branch 'f-135-eng' into rel-0.1.35 - [`7b600d5`](https://github.com/Niko1221/Strata/commit/7b600d55ff801caaaa11542af1523050992e049b) Merge branch 'f-135-small' into rel-0.1.35 - [`d9ab843`](https://github.com/Niko1221/Strata/commit/d9ab8435f654c368c586340d490915f6addf56a3) Engine 0.1.35: Windows AMD uses its bundled HIP runtime (#468 #461), the low-RAM resident mode on Windows 32 GB PCs (#467), fixes #460 #459 #446 #447 #457 #448 #444; setup requires it ### 📊 Changes **2 files changed** (+51 additions, -7 deletions) <details> <summary>View changed files</summary> 📝 `serve/server.py` (+11 -2) 📝 `serve/test_parallel.py` (+40 -5) </details> ### 📄 Description With `"parallel": 2`, our Claude Code agents stopped with **"Prompt is too long · automatic compaction failed"** at 60–65k tokens of a 137k window. The engine never rejected a request: every turn was HTTP 200, with no SSE error event and no stream cut short. **Cause.** A request that is STOPped on the solo path and continued in a batch slot (or sent back to the solo path) is sent again as its prompt plus what it generated. The DONE of that last leg counts this request's own output as `reused`. `min(reused, len(ids))` then turns it into the whole prompt, so `message_delta` says `input_tokens: 0` and `cache_read_input_tokens` = the whole prompt. Claude Code keeps `message_start`'s `input_tokens` when `message_delta`'s is 0 and adds `cache_read_input_tokens`, so it counts the prompt twice and compacts or blocks at half the window. **Measured** (RTX 5090 32 GB, Ryzen 9 9950X3D, 91 GB RAM, Qwen3.8-Flash-Next IQ3_S, v0.1.39 at 6f32ec0, Claude Code 2.1.290, a 137k window): | v0.1.39 | agent runs | responses | `input_tokens: 0` | agent result | |---|---|---|---|---| | agent alone (parallel 1: 3 runs; parallel 2: 1 run) | 4 | 204 | 0 | no "Prompt is too long" | | agent beside chats or a second agent (parallel 2 and 4) | 5 | 177 | 2–14 per run | 2 ended "Prompt is too long · compaction failed" at 60–65k | | this patch, parallel 2, agent beside 8 chats | 1 | 54 | 0 (smallest 57) | passed, context up to 108k | **Claude Code's side was measured against a stub** that sends these exact usage events: - `input_tokens: 0` with a 64k prompt: auto-compaction at `preTokens: 128073`, the same as a real 128k prompt. - `input_tokens: 1` with the rest as `cache_read_input_tokens`: no compaction. **Change** - `generate_batched` keeps the first leg's `reused`, which is the request's own prompt, across a continuation. - `anthropic_events` clamps the reported reuse to `len(ids) - 1`. The last prompt token is always read, so `input_tokens` is never 0 and `input_tokens + cache_read_input_tokens` is still the prompt. - `test_parallel.py`: the fake engine can report a held prefix (`--reuse`). The new test, `test_a_continued_request_reports_its_own_prompt_reuse`, fails without the fix (`input_tokens: 0`, `cache_read_input_tokens: 260`) and passes with it. - All 269 tests under `serve/` pass (`python -m unittest discover -s serve -t . -p 'test_*.py'`, Linux, Python 3.12, `jsonschema` installed). No engine change. The OpenAI path (`prompt_tokens_details.cached_tokens`, timings `cache_n`) uses the same `reused`, so it now also reports the first leg's figure for a continued request. I left its clamp alone. **Related open PRs** - #998 and #751 touch the same lines. #998 adds `--fail-window` to `test_parallel`'s fake engine where this adds `--reuse`. #751 adds a line to `generate_batched`'s cleanup block next to the one added here. In both cases the fix is to keep both changes; I can rebase onto whichever lands first. - #753 (draft, waiting on #559) reworks per-request accounting in the batch path, including continuation segments. If it lands, it would replace the `reused0` part of this change. This PR is the minimal fix on current `main`. - #615 applies the same rule (report the first segment's input/cache counts) to `reasoning_budget_tokens` continuations, which use a different code path. I wrote this with an AI coding assistant and checked the change and the test runs myself. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-04 02:04:32 +07:00
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#2966
No description provided.