Add documentation for provider-side prompt caching with Anthropic and OpenAI - #8970
Merged
Conversation
Co-authored-by: TomeHirata <33407409+TomeHirata@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add support for prompt caching in dspy
Add documentation for provider-side prompt caching with Anthropic and OpenAI
Oct 25, 2025
TomeHirata
reviewed
Oct 25, 2025
|
|
||
| In addition to DSPy's built-in caching mechanism, you can leverage provider-side prompt caching offered by LLM providers like Anthropic and OpenAI. This feature is particularly useful when working with modules like `dspy.ReAct()` that send similar prompts repeatedly, as it reduces both latency and costs by caching prompt prefixes on the provider's servers. | ||
|
|
||
| DSPy seamlessly passes configuration parameters to LiteLLM, which in turn supports various provider-specific caching mechanisms. You can enable prompt caching by passing the appropriate parameters directly to `dspy.LM()`. |
Co-authored-by: TomeHirata <33407409+TomeHirata@users.noreply.github.com>
TomeHirata
reviewed
Oct 25, 2025
|
|
||
| DSPy seamlessly passes configuration parameters to LiteLLM, which in turn supports various provider-specific caching mechanisms. You can enable prompt caching by passing the appropriate parameters directly to `dspy.LM()`. | ||
|
|
||
| ### Anthropic Prompt Caching |
Collaborator
There was a problem hiding this comment.
@copilot What's the difference between Anthropic and OpenAI? If the configuration strategy on DSPy is the same between providers, can we simplify this subsection?
Contributor
Author
There was a problem hiding this comment.
Since the configuration is identical between providers, I've consolidated the separate Anthropic and OpenAI subsections into a single unified section showing both examples. This simplifies the documentation while making it clear that the same approach works for both providers. Changes in commit dc73d6d.
Co-authored-by: TomeHirata <33407409+TomeHirata@users.noreply.github.com>
TomeHirata
reviewed
Oct 25, 2025
| - Working with long system prompts that remain constant | ||
| - Making multiple requests with similar context | ||
|
|
||
| ### Additional Configuration Options |
TomeHirata
reviewed
Oct 25, 2025
TomeHirata
reviewed
Oct 25, 2025
|
|
||
| This configuration tells LiteLLM to automatically inject cache control markers at system messages, allowing the provider to cache the system prompt across multiple requests. This is especially beneficial when: | ||
|
|
||
| - Using `dspy.ReAct()` with the same instructions |
Co-authored-by: TomeHirata <33407409+TomeHirata@users.noreply.github.com>
Co-authored-by: TomeHirata <33407409+TomeHirata@users.noreply.github.com>
TomeHirata
reviewed
Oct 25, 2025
|
|
||
| In addition to DSPy's built-in caching mechanism, you can leverage provider-side prompt caching offered by LLM providers like Anthropic and OpenAI. This feature is particularly useful when working with modules like `dspy.ReAct()` that send similar prompts repeatedly, as it reduces both latency and costs by caching prompt prefixes on the provider's servers. | ||
|
|
||
| You can enable prompt caching by passing the `cache_control_injection_points` parameter to `dspy.LM()`. This works with supported providers like Anthropic and OpenAI: |
Co-authored-by: TomeHirata <33407409+TomeHirata@users.noreply.github.com>
TomeHirata
approved these changes
Oct 25, 2025
TomeHirata
marked this pull request as ready for review
October 25, 2025 08:59
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.
Summary
Added comprehensive documentation for using provider-side prompt caching with DSPy. The documentation covers:
cache_control_injection_pointswith supported providers like Anthropic and OpenAIdspy.ReAct()that send similar prompts repeatedlyOriginal prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.