From c4d4ea6c710450042afa4abe262e8fefab4c5358 Mon Sep 17 00:00:00 2001 From: Joao Moura Date: Thu, 26 Feb 2026 00:01:09 -0800 Subject: [PATCH] refactor: update memory recall limit and formatting in Agent class - Reduced the memory recall limit from 10 to 5 in multiple locations within the Agent class. - Updated the memory formatting to use a new `format` method in the MemoryMatch class for improved readability and metadata inclusion. --- lib/crewai/src/crewai/agent/core.py | 12 ++++++------ lib/crewai/src/crewai/memory/types.py | 16 ++++++++++++++++ lib/crewai/src/crewai/tools/memory_tools.py | 17 ++--------------- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/lib/crewai/src/crewai/agent/core.py b/lib/crewai/src/crewai/agent/core.py index 22b5a24ca..e121a9771 100644 --- a/lib/crewai/src/crewai/agent/core.py +++ b/lib/crewai/src/crewai/agent/core.py @@ -384,10 +384,10 @@ class Agent(BaseAgent): ) if unified_memory is not None: query = task.description - matches = unified_memory.recall(query, limit=10) + matches = unified_memory.recall(query, limit=5) if matches: memory = "Relevant memories:\n" + "\n".join( - f"- {m.record.content}" for m in matches + m.format() for m in matches ) if memory.strip() != "": task_prompt += self.i18n.slice("memory").format(memory=memory) @@ -622,10 +622,10 @@ class Agent(BaseAgent): ) if unified_memory is not None: query = task.description - matches = unified_memory.recall(query, limit=10) + matches = unified_memory.recall(query, limit=5) if matches: memory = "Relevant memories:\n" + "\n".join( - f"- {m.record.content}" for m in matches + m.format() for m in matches ) if memory.strip() != "": task_prompt += self.i18n.slice("memory").format(memory=memory) @@ -1811,11 +1811,11 @@ class Agent(BaseAgent): ), ) start_time = time.time() - matches = agent_memory.recall(formatted_messages, limit=10) + matches = agent_memory.recall(formatted_messages, limit=5) memory_block = "" if matches: memory_block = "Relevant memories:\n" + "\n".join( - f"- {m.record.content}" for m in matches + m.format() for m in matches ) if memory_block: formatted_messages += "\n\n" + self.i18n.slice("memory").format( diff --git a/lib/crewai/src/crewai/memory/types.py b/lib/crewai/src/crewai/memory/types.py index e67ad163f..e3d02e70d 100644 --- a/lib/crewai/src/crewai/memory/types.py +++ b/lib/crewai/src/crewai/memory/types.py @@ -87,6 +87,22 @@ class MemoryMatch(BaseModel): description="Information the system looked for but could not find.", ) + def format(self) -> str: + """Format this match as a human-readable string including metadata. + + Returns: + A multi-line string with score, content, categories, and non-empty + metadata fields. + """ + lines = [f"- (score={self.score:.2f}) {self.record.content}"] + if self.record.categories: + lines.append(f" categories: {', '.join(self.record.categories)}") + if self.record.metadata: + for key, value in self.record.metadata.items(): + if value: + lines.append(f" {key}: {value}") + return "\n".join(lines) + class ScopeInfo(BaseModel): """Information about a scope in the memory hierarchy.""" diff --git a/lib/crewai/src/crewai/tools/memory_tools.py b/lib/crewai/src/crewai/tools/memory_tools.py index 5c98a9892..c7e04db39 100644 --- a/lib/crewai/src/crewai/tools/memory_tools.py +++ b/lib/crewai/src/crewai/tools/memory_tools.py @@ -20,14 +20,6 @@ class RecallMemorySchema(BaseModel): "or multiple items to search for several things at once." ), ) - scope: str | None = Field( - default=None, - description="Optional scope to narrow the search (e.g. /project/alpha)", - ) - depth: str = Field( - default="shallow", - description="'shallow' for fast vector search, 'deep' for LLM-analyzed retrieval", - ) class RecallMemoryTool(BaseTool): @@ -41,32 +33,27 @@ class RecallMemoryTool(BaseTool): def _run( self, queries: list[str] | str, - scope: str | None = None, - depth: str = "shallow", **kwargs: Any, ) -> str: """Search memory for relevant information. Args: queries: One or more search queries (string or list of strings). - scope: Optional scope prefix to narrow the search. - depth: "shallow" for fast vector search, "deep" for LLM-analyzed retrieval. Returns: Formatted string of matching memories, or a message if none found. """ if isinstance(queries, str): queries = [queries] - actual_depth = depth if depth in ("shallow", "deep") else "shallow" all_lines: list[str] = [] seen_ids: set[str] = set() for query in queries: - matches = self.memory.recall(query, scope=scope, limit=5, depth=actual_depth) + matches = self.memory.recall(query) for m in matches: if m.record.id not in seen_ids: seen_ids.add(m.record.id) - all_lines.append(f"- (score={m.score:.2f}) {m.record.content}") + all_lines.append(m.format()) if not all_lines: return "No relevant memories found."