[Front-end] [Messages] Populate num_cache_creation_tokens (#48535)

Signed-off-by: Yifan Zong <yzong@redhat.com>
This commit is contained in:
yzong-rh
2026-07-18 13:04:35 -04:00
committed by GitHub
parent e94243893d
commit a287eb163f
15 changed files with 201 additions and 127 deletions
@@ -90,15 +90,21 @@ def _get_mm_token_counts(engine_input: EngineInput) -> dict[str, int]:
def _make_prompt_tokens_details(
enable_prompt_tokens_details: bool,
num_cached_tokens: int | None,
num_cache_creation_tokens: int | None,
mm_token_counts: dict[str, int] | None,
) -> PromptTokenUsageInfo | None:
"""Build ``prompt_tokens_details`` from cached + multimodal token counts."""
if not enable_prompt_tokens_details:
return None
if num_cached_tokens is None and not mm_token_counts:
if (
num_cached_tokens is None
and num_cache_creation_tokens is None
and not mm_token_counts
):
return None
return PromptTokenUsageInfo(
cached_tokens=num_cached_tokens,
created_cache_tokens=num_cache_creation_tokens,
multimodal_tokens=mm_token_counts or None,
)
@@ -427,6 +433,7 @@ class OpenAIServingChat(GenerateBaseServing):
finish_reason_sent = [False] * num_choices
num_prompt_tokens = 0
num_cached_tokens = None
num_cache_creation_tokens = None
tools_streamed = [False] * num_choices
if isinstance(request.tool_choice, ChatCompletionNamedToolChoiceParam):
@@ -479,6 +486,7 @@ class OpenAIServingChat(GenerateBaseServing):
# response (by the try...catch).
if first_iteration:
num_cached_tokens = res.num_cached_tokens
num_cache_creation_tokens = res.num_cache_creation_tokens
# Send first response for each request.n (index) with
# the role
role = self.get_chat_request_role(request)
@@ -756,6 +764,7 @@ class OpenAIServingChat(GenerateBaseServing):
final_usage.prompt_tokens_details = _make_prompt_tokens_details(
self.enable_prompt_tokens_details,
num_cached_tokens,
num_cache_creation_tokens,
mm_token_counts,
)
@@ -1030,6 +1039,7 @@ class OpenAIServingChat(GenerateBaseServing):
usage.prompt_tokens_details = _make_prompt_tokens_details(
self.enable_prompt_tokens_details,
final_res.num_cached_tokens,
final_res.num_cache_creation_tokens,
mm_token_counts,
)