Skip to content

LangChain Plugin: Session

The FlotorchLangChainSession provides comprehensive session management for LangChain agents, enabling persistent storage of conversation history and session data through FloTorch Gateway. It implements LangChain’s BaseMemory interface and stores chat turns as FloTorch session events, exposing a single history string for prompts.

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

Configure your session service with the following parameters:

FlotorchLangChainSession(
api_key: str, # FloTorch API key for authentication (required)
base_url: str # FloTorch Gateway endpoint URL (required)
)

Parameter Details:

  • api_key - Authentication key for accessing FloTorch Gateway (can be set via environment variable)
  • base_url - The FloTorch Gateway endpoint URL (can be set via environment variable)

Fully implements LangChain’s BaseMemory interface:

  • Memory Key - Uses history as the memory key
  • Event Persistence - Stores chat turns as FloTorch session events
  • History Formatting - Exposes a single history string for prompts
  • LangChain Compatibility - Works seamlessly with LangChain’s memory framework

Provides comprehensive session persistence capabilities:

  • Gateway Storage - Stores session data persistently in FloTorch Gateway
  • Event History - Preserves complete event history across service restarts
  • State Synchronization - Automatically synchronizes state management
  • Format Conversion - Seamlessly converts events between LangChain and FloTorch formats

Serves as short-term memory for LangChain agents:

  • Conversation History - Maintains conversation history for the current session
  • History String - Provides formatted history string for prompt injection
  • Automatic Updates - Automatically updates history as conversations progress
from flotorch.langchain.agent import FlotorchLangChainAgent
from flotorch.langchain.session import FlotorchLangChainSession
from langchain.agents import AgentExecutor
# Initialize session service
short_term_memory = FlotorchLangChainSession(
api_key="your_api_key",
base_url="https://gateway.flotorch.cloud"
)
# 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=short_term_memory,
verbose=False,
handle_parsing_errors=True
)
from flotorch.langchain.session import FlotorchLangChainSession
# Initialize session service
short_term_memory = FlotorchLangChainSession(
api_key="your_api_key",
base_url="https://gateway.flotorch.cloud"
)
# Access memory key
memory_key = short_term_memory.memory_key # Returns "history"
# Load memory variables
memory_vars = short_term_memory.load_memory_variables({})
print(memory_vars["history"])
  1. Environment Variables - Use environment variables for credentials to enhance security
  2. Session Management - Implement proper session lifecycle management for long-running applications
  3. Memory Key - Remember that the memory key is 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 robust error handling for network and storage operations
  6. Session Cleanup - Consider implementing session cleanup strategies for production environments