Forward cache_control from message-level placement in ChatDatabricks - #456
Open
adamgurary wants to merge 1 commit into
Open
Forward cache_control from message-level placement in ChatDatabricks#456adamgurary wants to merge 1 commit into
adamgurary wants to merge 1 commit into
Conversation
_convert_message_to_dict built the outbound message from message.content only, so an Anthropic cache_control breakpoint reached the endpoint solely when nested inside a typed content block. A message-level breakpoint (stored by LangChain in additional_kwargs) was dropped silently, so prompt caching never activated for that placement. Forward cache_control from additional_kwargs onto the outbound message dict. Add unit tests for the present and absent cases. Co-authored-by: Isaac
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
This issue is one finding from a hands-on evaluation (a Critical User Journey run) of building, tracing, and improving an agent that runs off Databricks. The agent is a LangGraph rebuild of a customer-notes workflow. It runs on a laptop and calls the Databricks Foundation Model API endpoint
databricks-claude-opus-4-8. Its MLflow traces are written to Unity Catalog.Full writeup:
Environment
ChatDatabricks drops cache_control unless it is inside a typed content block
Summary
The LangChain
ChatDatabricksclient (fromdatabricks-langchain) forwards an Anthropiccache_controlbreakpoint only when it sits inside a typed content block. A message-level key or anadditional_kwargsplacement is dropped silently, with no error, so prompt caching never activates.ChatDatabricksCAN cache. The breakpoint just has to be in the one shape the client preserves, and nothing tells the user that.Steps to reproduce
Run the attached repro (
repro_cache_control_and_autolog.py, Bug 1 section) against a Databricks-served Claude endpoint. It sends the same large static system block three ways through the sameChatDatabricksclient and reads the warm-call cache count each time.Actual behavior
Warm-call
cache_readby placement (from the repro):Isolation proof that
ChatDatabricksactually writes the cache, not just that the flag reaches the wire: a never-seen block sent ONLY throughChatDatabricksin the content-block form, then read once via the OpenAI client, reportedcache_read_input_tokens > 0.Root cause
databricks_langchain.chat_models._convert_message_to_dict(v0.17.0) builds the outbound message as{"content": message.content}and copies nothing else:So
cache_controlsurvives only when it is nested inside a content block that lives inmessage.content. A message-level sibling key andadditional_kwargssit outsidecontentand are dropped, with no warning.The fix in this PR
_convert_message_to_dictnow forwards a message-levelcache_controlfromadditional_kwargsonto the outbound message dict, so caching activates without nesting the flag inside a content block. The forwarding fires only whencache_controlis present, so the no-breakpoint path is unchanged.Two unit tests cover the present and absent cases. The full
test_chat_models.pyunit suite passes (108 passed, 2 skipped).This is a distinct bug from issue #446 (
ChatDatabricks: UsageMetadata inflates input_tokens when prompt caching is used), which concerns token-count accounting once caching is active. This PR is about the breakpoint being dropped so caching never activates for a message-level placement.Evidence and references
Full repro attached in the CUJ writeup (
mlflow-caching-repro-2026-07-23.zip): README Bug 1,repro-notes.md"Bug 1 mechanism", and the FORM matrix inexpected-output.txt. Warm-callcache_readwas 0 for the message-level andadditional_kwargsplacements and 8803 for the typed content block, through the same client. This is the corrected version of CUJ step 35, which first read this as "ChatDatabricks cannot cache at all." See the walkthrough linked above.