Skip to content

feat: Add cache control support in langchain orchestration client#1950

Open
davidkna-sap wants to merge 6 commits into
mainfrom
davidkna-sap_lc-o-cache
Open

feat: Add cache control support in langchain orchestration client#1950
davidkna-sap wants to merge 6 commits into
mainfrom
davidkna-sap_lc-o-cache

Conversation

@davidkna-sap

Copy link
Copy Markdown
Member

Context

Closes SAP/ai-sdk-js-backlog#575.

What this PR does and why it is needed

@davidkna-sap davidkna-sap changed the title feat: Add cache control support for prompt caching in orchestration feat: Add cache control support for prompt caching in langchain orchestration Jun 22, 2026
@davidkna-sap davidkna-sap force-pushed the davidkna-sap_lc-o-cache branch 2 times, most recently from c72ccdd to 071eec1 Compare June 22, 2026 13:48
@davidkna-sap davidkna-sap changed the title feat: Add cache control support for prompt caching in langchain orchestration feat: Add cache control support in langchain orchestration client Jun 22, 2026
@davidkna-sap davidkna-sap marked this pull request as ready for review June 22, 2026 14:33
@hyperspace-insights

Copy link
Copy Markdown
Contributor

Summary

The following content is AI-generated and provides a summary of the pull request:


feat: Add Cache Control Support in LangChain Orchestration Client

New Features

✨ Introduces prompt caching support for the @sap-ai-sdk/langchain OrchestrationClient. This enables automatic cache breakpoint injection into orchestration requests, reducing latency and token costs for repeated context in multi-turn conversations. The feature targets Anthropic Claude and Amazon Nova model families served through SAP AI Core Orchestration.

Two new capabilities are added:

  1. cache_control call option — Directly pass a CacheControl directive when invoking the OrchestrationClient. A breakpoint is automatically applied to the last cacheable text block of the last message.
  2. orchestrationPromptCachingMiddleware() — A new LangChain AgentMiddleware exported from @sap-ai-sdk/langchain/orchestration/prompt-caching-middleware that wraps langchain's anthropicPromptCachingMiddleware and integrates it with OrchestrationClient. Configurable with TTL (5m / 1h), message thresholds, and unsupported-model behavior (warn, ignore, raise).

Additionally, cached_tokens and cache_creation_tokens are now exposed in usage_metadata.input_token_details on LangChain orchestration responses.

Changes

  • packages/langchain/src/orchestration/types.ts: Added cache_control?: CacheControl to the LangChain orchestration call options type with full JSDoc.
  • packages/langchain/src/orchestration/client.ts: Applied cache_control option in both _generate and _streamResponseChunks by calling applyCacheControlToLastMessage.
  • packages/langchain/src/orchestration/util.ts: Added applyCacheControlToLastMessage() which mutates the last message in a ChatMessage[] array to attach a cache breakpoint to the last cacheable content block. Also updated mapOutputToChatResult to include response_metadata.tokenUsage, input_token_details (cache read/creation), and output_token_details (reasoning tokens).
  • packages/langchain/src/orchestration/prompt-caching-middleware.ts (new): Implements orchestrationPromptCachingMiddleware() by proxying the OrchestrationClient as ChatAnthropic so the upstream middleware's class-identity check passes, then delegating to anthropicPromptCachingMiddleware.
  • packages/langchain/src/orchestration/prompt-caching-middleware.test.ts (new): Unit tests covering threshold logic, enableCaching flag, system prompt counting, and unsupported model behavior.
  • packages/langchain/src/orchestration/util.test.ts: Added tests for applyCacheControlToLastMessage across all message roles and content types, and for prompt token details in response metadata.
  • packages/langchain/package.json: Added langchain ^1.4.4 as an optional peer dependency and dev dependency; exported new ./orchestration/prompt-caching-middleware entry point.
  • sample-code/src/langchain-orchestration.ts & sample-code/src/server.ts: Added invokePromptCachingAgent() sample demonstrating two sequential agent calls with cache write/read, exposed via GET /langchain/invoke-prompt-caching-agent.
  • .github/workflows/build.yml: Excluded prompt-caching-middleware from the public API check path pattern.
  • tsconfig.typedoc.json: Included the new middleware file in API documentation generation.
  • styles/config/vocabularies/SAP/accept.txt: Added breakpoints? to the spell-check vocabulary.
  • packages/core/src/stream/line-decoder.ts: Minor cleanup — replaced text[text.length - 1] with text.at(-1).
  • pnpm-lock.yaml: Updated lockfile with new langchain dependency and bumped canary versions of SAP AI SDK packages.

  • 🔄 Regenerate and Update Summary
  • ✏️ Insert as PR Description (deletes this comment)
  • 🗑️ Delete comment
PR Bot Information

Version: 1.26.5

@davidkna-sap davidkna-sap force-pushed the davidkna-sap_lc-o-cache branch from 8c956db to 812bb62 Compare June 22, 2026 14:37
@davidkna-sap davidkna-sap force-pushed the davidkna-sap_lc-o-cache branch from 812bb62 to f97b34d Compare June 22, 2026 15:01
run: echo "NODE_PATH=$(pwd)/node_modules" >> "$GITHUB_ENV"
- name: Check public API
uses: sap/cloud-sdk-js/.github/actions/check-public-api@main
uses: sap/cloud-sdk-js/.github/actions/check-public-api@extend-generator-ts-options

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be reverted after merge of SAP/cloud-sdk-js#6694

"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./orchestration/prompt-caching-middleware": {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this as explicit export to avoid having to dynamically import langchain as this would have also required some manual type definitions.

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