[FEAT] Implemented conversation caching and invalidating on new messages#1480
Merged
kyegomez merged 5 commits intokyegomez:masterfrom Mar 23, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds caching to Conversation.get_str() to avoid rebuilding the full conversation string on repeated calls, plus a few missing convenience APIs expected by tests.
Changes:
- Implemented a
_str_cacheforConversation.get_str()with invalidation on conversation mutations. - Added cache statistics (
get_cache_stats()), YAML serialization (to_yaml()), and a classmethod to list saved conversation names (list_cached_conversations()). - Added an interactive example script to manually observe cache behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
swarms/structs/conversation.py |
Adds cached get_str() with invalidation hooks and new helper APIs (cache stats, YAML export, listing saved convs). |
examples/conversation_cache_interactive.py |
New interactive script to manually exercise and observe the caching behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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.
Description
Conversation.get_str() was rebuilding the full conversation string on every call by iterating over all messages from scratch — even when nothing had changed between calls. In AgentRearrange, this is called once per agent step, meaning with 10 concurrent agents sharing the same conversation, the same string gets rebuilt 10 times redundantly per step.
I implemented a string cache (_str_cache) on the Conversation class that stores the result of get_str() and returns it instantly on subsequent calls. The cache is invalidated only when the conversation is actually mutated — via add, delete, update, clear, batch_add, load_from_json, load_from_yaml, or truncate_memory_with_tokenizer.
Additionally, I added get_cache_stats() which returns live hit/miss counts, hit rate, and cached token count — and included an interactive example (examples/conversation_cache_interactive.py) that spins up a real agent so you can chat and observe the cache stats update in real time after every response.
Additionally I added multiple fixes to ensure all 31 conversation tests now pass, up from 29:
Explanation Video
https://drive.google.com/file/d/12Jr5trKBu8NSdoMdRpt4fqultO3-5wCZ/view?usp=sharing
Issue
#1460
Dependencies
None
Tag maintainer
@kyegomez
Twitter handle
@akc__2025
📚 Documentation preview 📚: https://swarms--1480.org.readthedocs.build/en/1480/