Skip to content

The agent engineering platform.

fix(deepseek): map prompt_cache_hit_tokens to cache_read (#39668)

Nazrul Ansarib28d8c4630d4
diff --git a/libs/partners/deepseek/langchain_deepseek/chat_models.py b/libs/partners/deepseek/langchain_deepseek/chat_models.py
index 91596fb9f0..1f3162ecbc 100644
--- a/libs/partners/deepseek/langchain_deepseek/chat_models.py
+++ b/libs/partners/deepseek/langchain_deepseek/chat_models.py
@@ -19,6 +19,7 @@ from langchain_core.language_models import (
ModelProfileRegistry,
)
from langchain_core.messages import AIMessage, AIMessageChunk, BaseMessage
+from langchain_core.messages.ai import InputTokenDetails, UsageMetadata
from langchain_core.outputs import ChatGenerationChunk, ChatResult
from langchain_core.runnables import Runnable
from langchain_core.tools import BaseTool
@@ -45,6 +46,67 @@ def _get_default_model_profile(model_name: str) -> ModelProfile:
return default.copy()
+def _get_prompt_cache_hit_tokens(response: dict | openai.BaseModel) -> int | None:
+ """Read DeepSeek's top-level `prompt_cache_hit_tokens` usage field.
+
+ DeepSeek reports context-cache usage as top-level `prompt_cache_hit_tokens`
+ and `prompt_cache_miss_tokens` fields rather than OpenAI's nested
+ `prompt_tokens_details.cached_tokens`, so the count is not visible to the
+ base class.
+
+ Args:
+ response: A chat completion response or a streaming chunk.
+
+ Returns:
+ The number of prompt tokens served from the context cache, or `None` if
+ the field is absent.
+ """
+ usage: Any = (
+ response.get("usage")
+ if isinstance(response, dict)
+ else getattr(response, "usage", None)
+ )
+ if isinstance(usage, openai.BaseModel):
+ # Fields DeepSeek adds beyond OpenAI's schema are preserved as extras,
+ # which `model_dump` includes.
+ usage = usage.model_dump()
+ if not isinstance(usage, dict):
+ return None
+ cache_hit_tokens = usage.get("prompt_cache_hit_tokens")
+ if not isinstance(cache_hit_tokens, int):
+ return None
+ return cache_hit_tokens
+
+
+def _add_cache_read_tokens(message: BaseMessage, cache_hit_tokens: int) -> None:
+ """Record cached prompt tokens as `cache_read` in a message's usage metadata.
+
+ Only cache hits are recorded. DeepSeek defines `prompt_tokens` as
+ `prompt_cache_hit_tokens + prompt_cache_miss_tokens`, so a miss is an
+ ordinary uncached input token rather than a cache write, and mapping it to
+ `cache_creation` would misreport it.
+
+ An existing `cache_read` count is left untouched, since DeepSeek served
+ through an OpenAI-compatible gateway may report the nested form that the
+ base class already handles.
+
+ Args:
+ message: The message whose `usage_metadata` should be updated.
+ cache_hit_tokens: Prompt tokens served from DeepSeek's context cache.
+ """
+ if not isinstance(message, AIMessage) or message.usage_metadata is None:
+ return
+ input_token_details = message.usage_metadata.get("input_token_details") or {}
+ if "cache_read" in input_token_details:
+ return
+ usage_metadata: dict[str, Any] = dict(message.usage_metadata)
+ usage_metadata["input_token_details"] = cast(
+ "InputTokenDetails",
+ {**input_token_details, "cache_read": cache_hit_tokens},
+ )
+ message.usage_metadata = cast("UsageMetadata", usage_metadata)
+
+
class ChatDeepSeek(BaseChatOpenAI):
"""DeepSeek chat model integration to access models hosted in DeepSeek's API.
@@ -314,6 +376,11 @@ class ChatDeepSeek(BaseChatOpenAI):
) -> ChatResult:
rtn = super()._create_chat_result(response, generation_info)
+ cache_hit_tokens = _get_prompt_cache_hit_tokens(response)
+ if cache_hit_tokens is not None:
+ for generation in rtn.generations:
+ _add_cache_read_tokens(generation.message, cache_hit_tokens)
+
if not isinstance(response, openai.BaseModel):
return rtn
@@ -350,6 +417,13 @@ class ChatDeepSeek(BaseChatOpenAI):
default_chunk_class,
base_generation_info,
)
+ # Usage arrives in a trailing chunk that carries no choices, so this
+ # cannot be folded into the choices branch below.
+ if generation_chunk:
+ cache_hit_tokens = _get_prompt_cache_hit_tokens(chunk)
+ if cache_hit_tokens is not None:
+ _add_cache_read_tokens(generation_chunk.message, cache_hit_tokens)
+
if (choices := chunk.get("choices")) and generation_chunk:
top = choices[0]
if isinstance(generation_chunk.message, AIMessageChunk):
diff --git a/libs/partners/deepseek/tests/unit_tests/test_chat_models.py b/libs/partners/deepseek/tests/unit_tests/test_chat_models.py
index b5110629e1..e57fd28031 100644
--- a/libs/partners/deepseek/tests/unit_tests/test_chat_models.py
+++ b/libs/partners/deepseek/tests/unit_tests/test_chat_models.py
@@ -5,10 +5,12 @@ from __future__ import annotations
from typing import Any, Literal
from unittest.mock import MagicMock
-from langchain_core.messages import AIMessageChunk, ToolMessage
+from langchain_core.messages import AIMessage, AIMessageChunk, ToolMessage
from langchain_tests.unit_tests import ChatModelUnitTests
from openai import BaseModel
-from openai.types.chat import ChatCompletionMessage
+from openai.types import CompletionUsage
+from openai.types.chat import ChatCompletion, ChatCompletionMessage
+from openai.types.chat.chat_completion import Choice
from pydantic import BaseModel as PydanticBaseModel
from pydantic import Field, SecretStr
@@ -432,6 +434,204 @@ class TestChatDeepSeekAzureToolChoice:
assert bound_model is not None
+PROMPT_TOKENS = 100
+COMPLETION_TOKENS = 10
+TOTAL_TOKENS = 110
+CACHE_HIT_TOKENS = 64
+CACHE_MISS_TOKENS = 36
+GATEWAY_CACHED_TOKENS = 50
+
+
+class TestChatDeepSeekPromptCacheUsage:
+ """Tests for DeepSeek's top-level prompt-cache token counts.
+
+ DeepSeek reports context-cache usage as top-level `prompt_cache_hit_tokens`
+ and `prompt_cache_miss_tokens` fields on `usage`, rather than OpenAI's nested
+ `prompt_tokens_details.cached_tokens`. The base class reads only the nested
+ form, so the counts are dropped unless `ChatDeepSeek` maps them explicitly.
+
+ Only cache hits are mapped: DeepSeek defines
+ `prompt_tokens = prompt_cache_hit_tokens + prompt_cache_miss_tokens`, so a
+ miss is an ordinary uncached input token rather than a cache write.
+ """
+
+ def _get_model(self) -> ChatDeepSeek:
+ """Build a model instance with credentials that are never used."""
+ return ChatDeepSeek(model=MODEL_NAME, api_key=SecretStr("api_key"))
+
+ @staticmethod
+ def _usage(**overrides: Any) -> dict[str, Any]:
+ """Build a usage payload mirroring DeepSeek's documented response."""
+ return {
+ "prompt_tokens": PROMPT_TOKENS,
+ "completion_tokens": COMPLETION_TOKENS,
+ "total_tokens": TOTAL_TOKENS,
+ "prompt_cache_hit_tokens": CACHE_HIT_TOKENS,
+ "prompt_cache_miss_tokens": CACHE_MISS_TOKENS,
+ **overrides,
+ }
+
+ @staticmethod
+ def _completion(usage: dict[str, Any]) -> ChatCompletion:
+ """Wrap a usage payload in an otherwise ordinary completion."""
+ return ChatCompletion(
+ id="chatcmpl-test",
+ created=0,
+ model=MODEL_NAME,
+ object="chat.completion",
+ choices=[
+ Choice(
+ finish_reason="stop",
+ index=0,
+ message=ChatCompletionMessage(role="assistant", content="Hi"),
+ ),
+ ],
+ usage=CompletionUsage(**usage),
+ )
+
+ def test_cache_hit_tokens_mapped_to_cache_read(self) -> None:
+ """Test that `prompt_cache_hit_tokens` populates `cache_read`."""
+ response = self._completion(self._usage())
+
+ result = self._get_model()._create_chat_result(response)
+
+ message = result.generations[0].message
+ assert isinstance(message, AIMessage)
+ assert message.usage_metadata is not None
+ assert message.usage_metadata["input_tokens"] == PROMPT_TOKENS
+ assert (
+ message.usage_metadata["input_token_details"]["cache_read"]
+ == CACHE_HIT_TOKENS
+ )
+
+ def test_full_cache_miss_reports_zero_cache_read(self) -> None:
+ """Test that a total cache miss is reported as zero, not omitted."""
+ response = self._completion(
+ self._usage(
+ prompt_cache_hit_tokens=0,
+ prompt_cache_miss_tokens=PROMPT_TOKENS,
+ ),
+ )
+
+ result = self._get_model()._create_chat_result(response)
+
+ message = result.generations[0].message
+ assert isinstance(message, AIMessage)
+ assert message.usage_metadata is not None
+ assert message.usage_metadata["input_token_details"]["cache_read"] == 0
+
+ def test_cache_miss_tokens_not_mapped_to_cache_creation(self) -> None:
+ """Test that misses are not counted as cache writes."""
+ response = self._completion(self._usage())
+
+ result = self._get_model()._create_chat_result(response)
+
+ message = result.generations[0].message
+ assert isinstance(message, AIMessage)
+ assert message.usage_metadata is not None
+ assert "cache_creation" not in message.usage_metadata["input_token_details"]
+
+ def test_usage_without_cache_fields_is_unaffected(self) -> None:
+ """Test that responses lacking the DeepSeek cache fields still work."""
+ response = self._completion(
+ {
+ "prompt_tokens": PROMPT_TOKENS,
+ "completion_tokens": COMPLETION_TOKENS,
+ "total_tokens": TOTAL_TOKENS,
+ },
+ )
+
+ result = self._get_model()._create_chat_result(response)
+
+ message = result.generations[0].message
+ assert isinstance(message, AIMessage)
+ assert message.usage_metadata is not None
+ assert message.usage_metadata["input_tokens"] == PROMPT_TOKENS
+ assert "cache_read" not in message.usage_metadata["input_token_details"]
+
+ def test_nested_cached_tokens_take_precedence(self) -> None:
+ """Test that an OpenAI-style nested count is not overwritten.
+
+ DeepSeek served through an OpenAI-compatible gateway may report the
+ nested form instead, which the base class already handles correctly.
+ """
+ response = self._completion(
+ self._usage(
+ prompt_tokens_details={"cached_tokens": GATEWAY_CACHED_TOKENS},
+ ),
+ )
+
+ result = self._get_model()._create_chat_result(response)
+
+ message = result.generations[0].message
+ assert isinstance(message, AIMessage)
+ assert message.usage_metadata is not None
+ assert (
+ message.usage_metadata["input_token_details"]["cache_read"]
+ == GATEWAY_CACHED_TOKENS
+ )
+
+ def test_streaming_usage_only_chunk_maps_cache_read(self) -> None:
+ """Test that the trailing usage-only chunk carries `cache_read`.
+
+ DeepSeek sends token usage in a final chunk with no choices, so the
+ mapping cannot depend on a choices entry being present.
+ """
+ chunk: dict[str, Any] = {"choices": [], "usage": self._usage()}
+
+ generation_chunk = self._get_model()._convert_chunk_to_generation_chunk(
+ chunk,
+ AIMessageChunk,
+ None,
+ )
+
+ assert generation_chunk is not None
+ message = generation_chunk.message
+ assert isinstance(message, AIMessageChunk)
+ assert message.usage_metadata is not None
+ assert (
+ message.usage_metadata["input_token_details"]["cache_read"]
+ == CACHE_HIT_TOKENS
+ )
+
+ def test_streaming_usage_alongside_choices_maps_cache_read(self) -> None:
+ """Test that usage delivered with a content delta is also mapped."""
+ chunk: dict[str, Any] = {
+ "choices": [{"delta": {"content": "Hi"}}],
+ "usage": self._usage(),
+ }
+
+ generation_chunk = self._get_model()._convert_chunk_to_generation_chunk(
+ chunk,
+ AIMessageChunk,
+ None,
+ )
+
+ assert generation_chunk is not None
+ message = generation_chunk.message
+ assert isinstance(message, AIMessageChunk)
+ assert message.usage_metadata is not None
+ assert (
+ message.usage_metadata["input_token_details"]["cache_read"]
+ == CACHE_HIT_TOKENS
+ )
+
+ def test_streaming_content_chunk_without_usage_is_unaffected(self) -> None:
+ """Test that ordinary content chunks carry no usage metadata."""
+ chunk: dict[str, Any] = {"choices": [{"delta": {"content": "Hi"}}]}
+
+ generation_chunk = self._get_model()._convert_chunk_to_generation_chunk(
+ chunk,
+ AIMessageChunk,
+ None,
+ )
+
+ assert generation_chunk is not None
+ message = generation_chunk.message
+ assert isinstance(message, AIMessageChunk)
+ assert message.usage_metadata is None
+
+
def test_profile() -> None:
"""Test that model profile is loaded correctly."""
model = ChatDeepSeek(model="deepseek-reasoner", api_key=SecretStr("test_key"))