Enable prompt caching for agent calls (closes #611)#612
Open
kiranandcode wants to merge 3 commits intomasterfrom
Open
Enable prompt caching for agent calls (closes #611)#612kiranandcode wants to merge 3 commits intomasterfrom
kiranandcode wants to merge 3 commits intomasterfrom
Conversation
d2bafd3 to
06f075f
Compare
06f075f to
7348da4
Compare
Contributor
Author
|
@naiimic can you try running the MARA agent with this branch? it should be a lot faster. |
eb8680
reviewed
Mar 13, 2026
Contributor
eb8680
left a comment
There was a problem hiding this comment.
What happens if we add cache_control to each message at the time it is constructed? Is that incorrect vs the final-message-only behavior here?
eb8680
reviewed
Mar 13, 2026
| if msg["role"] not in ("user", "tool", "assistant"): | ||
| continue | ||
| content = msg.get("content") | ||
| if isinstance(content, list) and content: |
Contributor
There was a problem hiding this comment.
Ah, I see this applies to all Messages. Would it be easier to have this live in _make_message, our Message constructor? Or maybe in completions?
Contributor
Author
There was a problem hiding this comment.
I think that makes more sense, will update.
Contributor
Author
There was a problem hiding this comment.
Ah, no I guess the key difference is that we only apply this if the template has a history so we don't cache every template, only agent ones.
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.
Updates
completion.pysuch that system messages (call_system): content is now a list with cache_control: {"type": "ephemeral"} — cached across all turns.Agent user messages (LiteLLMProvider._call): _add_cache_control_to_history() annotates the last user/tool message with cache_control before each call_assistant round — only for templates with history (i.e. Agent subclasses)
Non-agent template calls are unaffected.
OpenAI calls are unaffected (OpenAI enables caching by default),
litellmstrips cache_control from OpenAI requests automatically.Cost impact: Anthropic charges 25% more for cache writes but cached reads cost 90% less.