-
-
Notifications
You must be signed in to change notification settings - Fork 796
[FEAT] Implemented conversation caching and invalidating on new messages #1480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kyegomez
merged 5 commits into
kyegomez:master
from
adichaudhary:fix/conversation-cache
Mar 23, 2026
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
76d87bd
conversation string caching implementation
adichaudhary 4a3aa33
Ensured all passing tests
adichaudhary 7f4363c
add interactive cache test example and fix black formatting
adichaudhary 3885bde
Apply suggestions from code review
adichaudhary a97681c
finalized code review feedback on conversation string caching
adichaudhary File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| """ | ||
| Interactive agent to manually test conversation string caching. | ||
| Run: python examples/conversation_cache_interactive.py | ||
| Type messages, see cache stats update after each response. | ||
| """ | ||
|
|
||
| from swarms import Agent | ||
|
|
||
| agent = Agent( | ||
| agent_name="CacheTestAgent", | ||
| model_name="claude-sonnet-4-5", | ||
| max_loops=1, | ||
| verbose=False, | ||
| temperature=1.0, | ||
| ) | ||
|
|
||
| print("\n=== Conversation Cache Interactive Test ===") | ||
| print("Type your messages. After each response, cache stats are shown.") | ||
| print("Type 'quit' to exit.\n") | ||
|
|
||
| while True: | ||
| user_input = input("You: ").strip() | ||
| if user_input.lower() in ("quit", "exit", "q"): | ||
| break | ||
| if not user_input: | ||
| continue | ||
|
|
||
| response = agent.run(user_input) | ||
| print(f"\nAgent: {response}\n") | ||
|
|
||
| # Call get_str() multiple times to exercise the cache | ||
| agent.short_memory.get_str() | ||
| agent.short_memory.get_str() | ||
| agent.short_memory.get_str() | ||
|
|
||
| stats = agent.short_memory.get_cache_stats() | ||
| print(f"--- Cache Stats ---") | ||
| print(f" Hits: {stats['hits']} (get_str() returned cached string)") | ||
| print(f" Misses: {stats['misses']} (get_str() rebuilt the string)") | ||
| print(f" Hit rate: {stats['hit_rate']:.0%}") | ||
| print(f" Cached tokens: {stats['cached_tokens']}") | ||
| print(f"-------------------\n") | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.