1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// SPDX-License-Identifier: MIT OR Apache-2.0
//! 2026-09-25: `usage.prompt_tokens_details.cached_tokens` accounting. Logical
//! child of `phase_promote_prefills` via `#[path]`.
//!
//! Owner: scheduler.
//! Invariants: none beyond the types.
/// 2026-09-25: `usage.prompt_tokens_details.cached_tokens` must report the
/// prompt tokens whose KV was reused, not the prefix-cache lookup length. The
/// lookup length lives on `cached_prefix_tokens` and is load-bearing for block
/// refcounting, so the report sites must read `reused_prefix_tokens` instead:
/// `cached_prefix_tokens` is non-zero even when the prefill recomputed every
/// token (no SSM snapshot / exact-leaf bypass).
#[test]
fn cached_tokens_is_reported_from_reused_prefix_tokens() {
for (name, src) in [
(
"phase_promote_prefills.rs",
include_str!("phase_promote_prefills.rs"),
),
("prefill_a_step.rs", include_str!("prefill_a_step.rs")),
("prefill_b_step.rs", include_str!("prefill_b_step.rs")),
(
"prefill_a_step/first_chunk.rs",
include_str!("prefill_a_step/first_chunk.rs"),
),
(
"prefill_b_step/first_token.rs",
include_str!("prefill_b_step/first_token.rs"),
),
] {
let compact: String = src.chars().filter(|c| !c.is_whitespace()).collect();
assert!(
!compact.contains("cached_prompt_tok=p.seq.cached_prefix_tokens")
&& !compact.contains("cached_prompt_tok=seq.cached_prefix_tokens"),
"{name} still reports the prefix-cache LOOKUP length as cached_tokens (#919)"
);
assert!(
compact.contains("cached_prompt_tok=p.seq.reused_prefix_tokens")
|| compact.contains("cached_prompt_tok=seq.reused_prefix_tokens"),
"{name} must report the tokens actually reused from the prefix cache"
);
}
}