Memory Integration

chevron-rightRelevant source fileshashtag

Purpose and Scope

This document covers the integration of Mem0 memory retrieval and storage within the chat and recipe execution systems. Memory integration enables agents to access relevant context from previous interactions, improving coherence and task continuity across conversations and workflow executions.

For information about the broader chat streaming system, see Streaming Chat Service. For recipe execution details, see Recipe Execution.


Architecture Overview

Memory integration operates at two primary touch points: the streaming chat service and the recipe executor. Both systems use a shared memory injector to retrieve and format context from Mem0.

spinner

Sources: orchestrator/consumers/chatbot/service.py:601-614, orchestrator/api/recipe_executor.py:637-652


The Memory Retrieval Gate

To avoid unnecessary token overhead and API calls, memory retrieval is gated by the should_retrieve_memories function. This optimization determines whether the current user query warrants memory context injection.

Gate Logic

The gate evaluates the query against heuristics to detect when memories are likely irrelevant:

Query Pattern
Example
Should Retrieve?

Simple greetings

"hello", "hi there"

No

Meta questions

"what can you do?"

No

Continuation queries

"continue", "go on"

No

Complex task queries

"summarize last week's PRs"

Yes

Referential queries

"what did I ask about earlier?"

Yes

Implementation

The gate is checked before retrieval. If it returns False, memory injection is skipped entirely, saving tokens and reducing latency.

Sources: orchestrator/consumers/chatbot/service.py:602-604


Chat Memory Integration

Memory injection in the streaming chat service occurs during the prompt assembly phase, before the LLM generate call.

Injection Flow

spinner

Code Path

The memory injection occurs in stream_response_aisdk:

The memory message is inserted at position 1 (after the system message, before user context). This placement ensures memories inform the agent's response without overriding core instructions.

Sources: orchestrator/consumers/chatbot/service.py:492-615


Recipe Memory Integration

Recipe execution integrates memories at two stages: pre-execution (loading) and post-execution (storage).

Pre-Execution: Memory Loading

Memories are retrieved before recipe execution begins and injected into Step 1 only. This prevents redundant memory context from accumulating across all steps.

spinner

Code Path

Memory retrieval occurs in execute_recipe_direct:

Injection into Step 1:

Sources: orchestrator/api/recipe_executor.py:151-162, orchestrator/api/recipe_executor.py:637-652

Post-Execution: Memory Storage

After recipe execution completes, learning data is stored back to Mem0 for future runs. This enables continuous improvement through self-learning.

Sources: orchestrator/api/recipe_executor.py:762-774


Memory Context Formatting

Memory context is formatted into a structured system message that the LLM can parse naturally. The format varies slightly between chat and recipe contexts.

Chat Memory Format

For chat interactions, the memory injector builds a context summary:

The build_memory_injection_message method (from modules.memory.operations) typically produces:

Recipe Memory Format

For recipe execution, memories are formatted as learnings:

This simpler format reflects that recipe memories are typically higher-level patterns (e.g., "JIRA_GET_ISSUE requires issue_id_or_key param") rather than conversational context.

Sources: orchestrator/api/recipe_executor.py:151-162, orchestrator/consumers/chatbot/service.py:614


Component Reference

Component
Location
Purpose

MemoryInjector

modules.memory.operations

Core memory retrieval/formatting logic

RecipeMemoryService

core.services.recipe_memory_service

Recipe-specific memory operations

should_retrieve_memories

MemoryInjector method

Gate function to optimize retrieval

retrieve_relevant_memories

MemoryInjector method

Fetch memories from Mem0

build_memory_injection_message

MemoryInjector method

Format memories for LLM context

store_recipe_memory

RecipeMemoryService method

Persist learning data post-execution

Sources: orchestrator/consumers/chatbot/service.py:37, orchestrator/consumers/chatbot/service.py:466, orchestrator/api/recipe_executor.py:640


Memory Injection Points Summary

spinner

Sources: orchestrator/consumers/chatbot/service.py:492-615, orchestrator/api/recipe_executor.py:637-774


Last updated