Skip to content

Instantly share code, notes, and snippets.

@jclyne
Last active January 27, 2026 19:53
Show Gist options
  • Select an option

  • Save jclyne/fee76fcb56020f6c21ac5af025aa1cf7 to your computer and use it in GitHub Desktop.

Select an option

Save jclyne/fee76fcb56020f6c21ac5af025aa1cf7 to your computer and use it in GitHub Desktop.
Memory System for Cash App Agentic Loop

Memory System for Cash App Agentic Loop

Overview

The memory system enables the Cash App advisor to remember information about users across conversations. When memory extraction is triggered, the system processes conversation history, extracts meaningful memory items using an LLM, and stores them for retrieval in future conversations.

┌─────────────────────────────────────────────────────────────────────────────┐
│                           MEMORY LIFECYCLE                                   │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│   CONVERSATION                    EXTRACTION                   RETRIEVAL    │
│                                                                             │
│   ┌─────────┐                    ┌─────────┐                 ┌─────────┐   │
│   │  User   │                    │  LLM    │                 │ Memory  │   │
│   │  Chat   │ ──► Trigger ──►    │ Analysis│ ──► Store ──►  │ Recall  │   │
│   │ Session │    (manual)        │         │                 │         │   │
│   └─────────┘                    └─────────┘                 └─────────┘   │
│                                                                             │
│   "I prefer weekly          "User prefers weekly         "Based on your    │
│    spending summaries"       spending summaries"          preference for   │
│                              (confidence: 0.9)            weekly summaries" │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘

Memory Extraction Flow

Memory extraction creates a new ChatWorkflow configured with the memory MCP extension, allowing the LLM to use the upsert_memories tool to save memories directly.

Note: Extraction is currently triggered manually via admin API. Automatic triggers (e.g., on conversation end) may be added in the future.

Flow Diagram

                                    ADMIN TRIGGERS EXTRACTION
                                          │
                                          ▼
┌──────────────────────────────────────────────────────────────────────────────┐
│              TriggerMemoryExtractionForChatWorkflowV2GrpcAction              │
│              POST /_admin/trigger-memory-extraction-v2                       │
│                                                                              │
│    Input: session_id (the source conversation to extract memories from)      │
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘
                                          │
                                          ▼
┌──────────────────────────────────────────────────────────────────────────────┐
│                         Step 1: Fetch Source Session                         │
│                                                                              │
│    • SessionStore.getSession(sessionId)                                      │
│    • MessageStore.getMessages(sessionId)                                     │
│    • Format conversation history as structured text:                         │
│                                                                              │
│      [USER]: I prefer weekly spending summaries                              │
│      [ASSISTANT]: I'll remember that preference...                           │
│      [USER]: Also, I run a small bakery                                      │
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘
                                          │
                                          ▼
┌──────────────────────────────────────────────────────────────────────────────┐
│                      Step 2: Build Extraction Request                        │
│                                                                              │
│    Create PushMessagesRequest with:                                          │
│                                                                              │
│    ┌────────────────────────────────────────────────────────────────────┐    │
│    │ InputMessage (role: USER)                                          │    │
│    │                                                                    │    │
│    │ "Please analyze the following conversation history and extract     │    │
│    │  any memorable information about the user.                         │    │
│    │  YOU MUST use the upsert_memories tool to save each memory.        │    │
│    │                                                                    │    │
│    │  <conversation_history>                                            │    │
│    │  [USER]: I prefer weekly spending summaries                        │    │
│    │  [ASSISTANT]: I'll remember that preference...                     │    │
│    │  </conversation_history>                                           │    │
│    │                                                                    │    │
│    │  Based on this conversation, please:                               │    │
│    │  1. Identify user preferences, facts, behavioral patterns          │    │
│    │  2. Call upsert_memories tool with type, content, confidence, tags │    │
│    │  3. If no memorable info found, respond accordingly"               │    │
│    └────────────────────────────────────────────────────────────────────┘    │
│                                                                              │
│    ProfileConfig:                                                            │
│    ┌────────────────────────────────────────────────────────────────────┐    │
│    │ system_preamble: MEMORY_EXTRACTION_PREAMBLE                        │    │
│    │ preferred_backend_tools: [memory extension]                        │    │
│    └────────────────────────────────────────────────────────────────────┘    │
│                                                                              │
│    ChatContext:                                                              │
│    ┌────────────────────────────────────────────────────────────────────┐    │
│    │ moneybot_context.source_session_id: <original session ID>          │    │
│    │ (Links extraction session to source conversation for tracking)     │    │
│    └────────────────────────────────────────────────────────────────────┘    │
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘
                                          │
                                          ▼
┌──────────────────────────────────────────────────────────────────────────────┐
│                    Step 3: Start New ChatWorkflow                            │
│                                                                              │
│    PushMessagesHandler.handlePushMessages(request, isAdminActionCaller=true) │
│                                                                              │
│    Creates a NEW session dedicated to memory extraction                      │
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘
                                          │
                                          ▼
┌──────────────────────────────────────────────────────────────────────────────┐
│                         NEW ChatWorkflow Session                             │
│                                                                              │
│    ┌────────────────────────────────────────────────────────────────────┐    │
│    │                        LLM Processing                              │    │
│    │                                                                    │    │
│    │  System Preamble: MEMORY_EXTRACTION_PREAMBLE                       │    │
│    │  • What to extract (preferences, facts, patterns, summaries)       │    │
│    │  • Guidelines (confidence scoring, topic tags)                     │    │
│    │  • What NOT to extract (PII, temporary states)                     │    │
│    │                                                                    │    │
│    │  Available Tools: memory MCP extension                             │    │
│    │  • upsert_memories(type, content, confidence, topic_tags)          │    │
│    │                                                                    │    │
│    └────────────────────────────────────────────────────────────────────┘    │
│                                          │                                   │
│                                          ▼                                   │
│    ┌────────────────────────────────────────────────────────────────────┐    │
│    │                     LLM Tool Calls                                 │    │
│    │                                                                    │    │
│    │  LLM analyzes conversation and calls upsert_memories:              │    │
│    │                                                                    │    │
│    │  upsert_memories({                                                 │    │
│    │    type: "USER_PREFERENCE",                                        │    │
│    │    content: "Prefers weekly spending summaries",                   │    │
│    │    confidence: 0.9,                                                │    │
│    │    topic_tags: ["COMMUNICATION_PREFERENCES"]                       │    │
│    │  })                                                                │    │
│    │                                                                    │    │
│    │  upsert_memories({                                                 │    │
│    │    type: "FACTUAL_INFO",                                           │    │
│    │    content: "Runs a small bakery business",                        │    │
│    │    confidence: 0.95,                                               │    │
│    │    topic_tags: ["HOUSEHOLD_AND_CONTEXT"]                           │    │
│    │  })                                                                │    │
│    │                                                                    │    │
│    └────────────────────────────────────────────────────────────────────┘    │
│                                          │                                   │
│                                          ▼                                   │
│    ┌────────────────────────────────────────────────────────────────────┐    │
│    │                  Memory MCP Extension                              │    │
│    │                                                                    │    │
│    │  Receives tool calls and persists memories via MemoryService       │    │
│    │                                                                    │    │
│    └────────────────────────────────────────────────────────────────────┘    │
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘
                                          │
                                          ▼
┌──────────────────────────────────────────────────────────────────────────────┐
│                              Response                                        │
│                                                                              │
│    TriggerMemoryExtractionResponse {                                         │
│      triggered: true,                                                        │
│      message: "Memory extraction triggered, session ID: <new_session_id>"    │
│    }                                                                         │
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘

Memory Types

Type Description Examples
USER_PREFERENCE Explicit user preferences "Prefers concise responses", "Wants weekly spending summaries"
FACTUAL_INFO Facts about the user "Runs a small bakery", "Has 3 employees"
BEHAVIORAL_PATTERN Observed user behaviors "Frequently checks weekend sales", "Often asks about tax deductions"
CONVERSATION_SUMMARY Key outcomes from conversations "Discussed setting up automatic transfers"
OPEN_LOOP Unresolved intentions or plans "User intends to set up automatic savings next week"

Topic Tags

Memories are categorized with topic tags for better organization and retrieval:

Tag Description
HOUSEHOLD_AND_CONTEXT Living situation, family context, or personal background
GOALS_AND_TIMELINES Stated objectives, milestones, or time-bound plans
RISK_TOLERANCE Comfort level with uncertainty, tradeoffs, or potential downside
CASHFLOW_AND_SPENDING Spending habits, budgeting behavior, or cashflow-related patterns
COMMUNICATION_PREFERENCES How the user prefers information to be communicated
FINANCIAL_PLANNING Longer-term planning related to finances, savings, or strategy
OTHER Relevant persistent information that doesn't fit other topics

Memory Retrieval

When a new conversation starts, memories are injected into the system preamble:

┌──────────────────────────────────────────────────────────────────────────────┐
│                         RETRIEVAL: SystemPreambleBuilder                     │
│                                                                              │
│    1. Fetch memories: MemoryService.getMemories(creator)                     │
│    2. Format into system preamble section                                    │
│    3. Inject into LLM context                                                │
│                                                                              │
│    System prompt includes:                                                   │
│    "## Information about this user from past conversations:                  │
│     - Prefers weekly spending summaries                                      │
│     - Runs a small bakery business                                           │
│     - Often asks about tax deductions"                                       │
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘

Component Architecture

┌─────────────────────────────────────────────────────────────────────────────┐
│                              KGOOSE SERVICE                                  │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│  ┌─────────────────────────────────────────────────────────────────────┐    │
│  │                        EXTRACTION TRIGGER                           │    │
│  │                                                                     │    │
│  │  TriggerMemoryExtractionForChatWorkflowV2GrpcAction                 │    │
│  │  POST /_admin/trigger-memory-extraction-v2                          │    │
│  │                                                                     │    │
│  │  (Manual trigger - automatic triggers TBD)                          │    │
│  │                                                                     │    │
│  └──────────────────────────────────┬──────────────────────────────────┘    │
│                                     │                                       │
│                                     ▼                                       │
│  ┌─────────────────────────────────────────────────────────────────────┐    │
│  │                      EXTRACTION PROCESSING                          │    │
│  │                                                                     │    │
│  │  PushMessagesHandler                                                │    │
│  │  └─► Creates new ChatWorkflow with memory MCP extension             │    │
│  │                                                                     │    │
│  │  ChatWorkflow                                                       │    │
│  │  └─► LLM processes conversation, calls upsert_memories tool         │    │
│  │                                                                     │    │
│  │  Memory MCP Extension                                               │    │
│  │  └─► Handles upsert_memories tool calls                             │    │
│  │                                                                     │    │
│  └──────────────────────────────────┬──────────────────────────────────┘    │
│                                     │                                       │
│                                     ▼                                       │
│  ┌─────────────────────────────────────────────────────────────────────┐    │
│  │                        MEMORY SERVICE                               │    │
│  │                                                                     │    │
│  │  saveMemories(creator, memories)                                    │    │
│  │  getMemories(creator) ──► used in future conversations              │    │
│  │                                                                     │    │
│  └─────────────────────────────────────────────────────────────────────┘    │
│                                                                             │
│  ┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐         │
│  │  MessageStore   │    │ SessionStore    │    │ GooseWrapper    │         │
│  │  (DynamoDB)     │    │ (DynamoDB)      │    │ (LLM calls)     │         │
│  └─────────────────┘    └─────────────────┘    └─────────────────┘         │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘

Key Components

Component Purpose
TriggerMemoryExtractionForChatWorkflowV2GrpcAction Admin API endpoint to trigger extraction
PushMessagesHandler Creates and starts the extraction ChatWorkflow
MemoryExtractionPrompts.MEMORY_EXTRACTION_PREAMBLE System prompt guiding the LLM on what/how to extract
Memory MCP Extension Provides upsert_memories tool for the LLM
MemoryService Persists and retrieves memories
SystemPreambleBuilder Injects memories into new conversation contexts

What NOT to Extract

The system avoids extracting:

  • Sensitive PII: SSN, credit card numbers, passwords, full account numbers
  • Temporary states: Current balance, recent transactions
  • Identifiable info: Email, phone number, address, date of birth
  • Session-specific info: Information only relevant to the current conversation
  • Low confidence: Speculative or uncertain information (confidence < 0.5)

Key Design Decisions

Decision Choice Rationale
Trigger Manual admin API Controlled rollout, on-demand extraction
Processing ChatWorkflow + MCP Reuses existing infrastructure, tool-based approach
LLM Integration upsert_memories tool LLM decides what to extract and save
Session Tracking source_session_id in context Links extraction session to source conversation
Storage External MemoryService Flexible backend, separation of concerns
Retrieval System preamble injection Seamless integration with conversation flow
Deduplication Handled by memory MCP extension Prevents duplicate memories
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment