Skip to content

LangChain Plugin: Memory

FloTorch LangChain Memory services enable persistent memory capabilities for your agents, allowing them to store and retrieve information across sessions. The memory system integrates seamlessly with LangChain’s agent framework and provides long-term persistent storage that exposes a single history string for prompts.

Important: Memory services require prior setup in the FloTorch Console. Memory capabilities are not built-in and must be explicitly configured.

Before using FloTorch LangChain Memory services, ensure you have completed the general prerequisites outlined in the LangChain Plugin Overview, including installation and environment configuration.

from flotorch.langchain.memory import FlotorchLangChainMemory
# Initialize memory service (requires prior registration in FloTorch Console)
external_memory = FlotorchLangChainMemory(
name="your-memory-provider",
api_key="your_api_key",
base_url="https://gateway.flotorch.cloud",
user_id="user_123",
app_id="app_123"
)

Configure your memory service with the following parameters:

FlotorchLangChainMemory(
name: str, # Memory provider name (from FloTorch Console) - required
api_key: str, # FloTorch API key for authentication (required)
base_url: str, # FloTorch Gateway URL (required)
user_id: str, # User identifier for memory isolation (required)
app_id: str # Application identifier for memory isolation (required)
)

Parameter Details:

  • name - The name of the memory provider configured in FloTorch Console
  • api_key - Authentication key for accessing FloTorch Gateway (can be set via environment variable)
  • base_url - FloTorch Gateway endpoint URL (can be set via environment variable)
  • user_id - User identifier for isolating memory per user
  • app_id - Application identifier for isolating memory per application

Fully implements LangChain’s BaseMemory interface:

  • Memory Key - Uses longterm_history as the memory key
  • History Management - Persists turns to FloTorch Memory
  • Prompt Integration - Exposes a single history string for prompts
  • LangChain Compatibility - Works seamlessly with LangChain’s memory framework

Memory service provides automatic content management:

  • Content Extraction - Automatically extracts content from LangChain messages
  • Turn Persistence - Persists conversation turns to FloTorch Memory
  • Metadata Storage - Stores conversations with associated metadata
  • History Formatting - Formats history for prompt injection

Provides comprehensive memory search:

  • Keyword-Based Search - Performs traditional text-based searches
  • User/App Filtering - Filters results by user ID and application name
  • Metadata Filtering - Supports custom metadata filtering
  • Date Range Queries - Allows filtering by time ranges
from flotorch.langchain.agent import FlotorchLangChainAgent
from flotorch.langchain.memory import FlotorchLangChainMemory
from langchain.agents import AgentExecutor
# Initialize memory service
external_memory = FlotorchLangChainMemory(
name="your-memory-provider",
api_key="your_api_key",
base_url="https://gateway.flotorch.cloud",
user_id="user_123",
app_id="app_123"
)
# Initialize agent with memory enabled
agent_manager = FlotorchLangChainAgent(
agent_name="customer-support",
enable_memory=True, # Enables memory placeholders in prompt
base_url="https://gateway.flotorch.cloud",
api_key="your_api_key"
)
# Get agent and tools
agent = agent_manager.get_agent()
tools = agent_manager.get_tools()
# Use with AgentExecutor
executor = AgentExecutor(
agent=agent,
tools=tools,
memory=external_memory,
verbose=False,
handle_parsing_errors=True
)
from flotorch.langchain.memory import FlotorchLangChainMemory
# Initialize memory service
external_memory = FlotorchLangChainMemory(
name="your-memory-provider",
api_key="your_api_key",
base_url="https://gateway.flotorch.cloud",
user_id="user_123",
app_id="app_123"
)
# Access memory key
memory_key = external_memory.memory_key # Returns "longterm_history"
# Load memory variables
memory_vars = external_memory.load_memory_variables({})
print(memory_vars["longterm_history"])
  1. Memory Provider Setup - Always configure memory providers in FloTorch Console before using them in code
  2. User/App Isolation - Use appropriate user_id and app_id values to isolate memory per user and application
  3. Memory Key - Remember that the memory key is longterm_history when accessing memory variables
  4. Agent Memory Enablement - Set enable_memory=True in FlotorchLangChainAgent to inject memory placeholders in the prompt
  5. Error Handling - Implement proper error handling for memory operations in production environments
  6. Memory Cleanup - Consider implementing memory cleanup strategies for long-running applications