Skip to content

docs(langchain): document that bare-LLM cache hits are not scanned - #10

Merged
TARSDrakon merged 1 commit into
mainfrom
loop/langchain-llm-cache-scope
Oct 17, 2026
Merged

TARSDrakon merged 1 commit into
mainfrom
loop/langchain-llm-cache-scope

Conversation

@TARSDrakon

Copy link
Copy Markdown
Contributor

Limitation

A bare (non-chat) LangChain LLM call answered entirely from an LLM cache is not scanned. The handlers scan prompts in on_llm_start and outputs in on_llm_end, but langchain-core (1.6.12, language_models/llms.py) looks up the cache before firing on_llm_start. A full cache hit returns the cached generations without any start or end event. So for a repeated prompt on a cached LLM, neither the prompt nor the cached output reaches ShieldCortex, and no BLOCK or fail_closed error can stop the call.

Why document instead of fix

A callback handler can only act on events LangChain fires. On a full cache hit, no event fires, so the handlers cannot fix this without changing how they are used. This PR narrows the documented contract instead:

  • README: the coverage sentence now says scans happen "whenever LangChain fires the matching callback". A new LLM cache bullet in the limitations list tells users to set cache=False on LLMs whose calls must be scanned, or to scan outside the model call. It notes that the default cache=None falls back to any cache set with set_llm_cache.
  • CHANGELOG [Unreleased] → ### Known limitation: a new entry with the same guidance.
  • Module docstring of shieldcortex.integrations.langchain: the same paragraph.

The docs also say that chat models fire on_chat_model_start before their cache lookup, so a cached chat call still has its input scanned. The control test below shows this.

Tests added (tests/integrations/test_langchain.py)

These use a real FakeListLLM / FakeListChatModel with InMemoryCache and respx-mocked /v1/scan. Each test runs in sync (ShieldCortexSyncCallbackHandler) and async (ShieldCortexCallbackHandler) mode. Each makes two calls with the same handler and raise_on_block=True: ALLOW for the first call, then BLOCK for the second.

  • test_bare_llm_cache_hit_is_not_scanned: the first call scans llm_input and llm_output. The identical second call returns "cached reply" with zero scan requests and no raise. This pins the limitation, so a LangChain change that starts firing callbacks on cache hits will make the test fail and prompt a docs update.
  • test_bare_llm_without_cache_rescans_and_blocks: same setup with cache=False. The second call raises ContentBlockedError on langchain:llm_input.
  • test_chat_model_cache_hit_still_scans_input: a chat model with the same cache. The second call raises ContentBlockedError on langchain:chat_input.

No runtime code changed and no existing assertions changed.

Gates (langchain-core 1.6.12)

  • ruff check src/: exit 0, all checks passed
  • mypy src/shieldcortex/ --ignore-missing-imports: exit 0, no issues in 12 source files
  • python -m pytest -q: exit 0, 297 passed

🤖 Generated with Claude Code

LangChain (langchain-core 1.6.12) looks up the LLM cache before firing
on_llm_start, and a full cache hit returns the cached generations
without start or end events. A callback handler therefore cannot scan
the prompt or the cached output of such a call, and no BLOCK or
fail_closed error can stop it.

Narrow the documented contract in the README, CHANGELOG (Known
limitation) and the integration's module docstring: set cache=False on
LLMs whose calls must be scanned, or scan outside the model call. Chat
models fire on_chat_model_start before their cache lookup, so cached
chat calls still have their input scanned.

Add regression tests with real FakeListLLM / FakeListChatModel and
InMemoryCache, sync and async:
- bare LLM cache hit: first call scans llm_input and llm_output (ALLOW);
  the identical second call with BLOCK mocked returns the cached reply
  with zero scan requests and no raise
- bare LLM with cache=False: the second call raises ContentBlockedError
  on llm_input
- chat model with the same cache: the second call raises
  ContentBlockedError on chat_input

No runtime behaviour changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TARSDrakon

Copy link
Copy Markdown
Contributor Author

Independent review (GPT-6 Astra, round 1) at head 0df67822152e9e4361f4fa1abfd0599851cd8f0a: APPROVE.

The cache-hit exclusion accurately narrows the documented contract; the six new sync/async regressions exercise real LangChain cache paths with blocking controls, and executable SDK code is unchanged (AST-equal). Full suite 297 passed. One non-blocking nit: add a cross-reference near the historical coverage summaries (CHANGELOG.md:60-67, langchain.py module docstring). CI 3.9-3.13 green on this head. Merging via the improvement loop.

@TARSDrakon
TARSDrakon merged commit 5b73afe into main Oct 17, 2026
5 checks passed
@CASE-Drakon CASE-Drakon mentioned this pull request Oct 17, 2026
@TARSDrakon TARSDrakon mentioned this pull request Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant