LangChain Integration
Drop Synapse into any LangChain agent as a BaseMemory adapter. No changes to your chain code — just swap the memory backend.
Install
pip install langchain requestsMemory adapter
Copy this class into your project. It implements save_context (write after each turn) and load_memory_variables (query before each turn).
from langchain.memory import BaseMemory
import requests
from typing import Any, Dict, List
class SynapseMemory(BaseMemory):
api_key: str
agent_id: str
base_url: str = "https://synapse.by-kit.com/api/v1"
@property
def memory_variables(self) -> List[str]:
return ["history"]
def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, str]) -> None:
"""Save a conversation exchange to Synapse."""
body = f"Input: {inputs}\nOutput: {outputs}"
requests.post(
f"{self.base_url}/record",
headers={"Authorization": f"Bearer {self.api_key}"},
json={
"agent_id": self.agent_id,
"type": "exchange",
"title": "Conversation turn",
"body": body
}
)
def load_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, Any]:
"""Retrieve relevant memories for the current query."""
query = inputs.get("input", "") or inputs.get("question", "")
resp = requests.get(
f"{self.base_url}/query",
headers={"Authorization": f"Bearer {self.api_key}"},
params={
"q": query,
"agent_id": self.agent_id,
"limit": 5
}
)
memories = resp.json() if resp.ok else []
history = "\n".join(
f"[{m['type']}] {m['title']}: {m['body']}"
for m in memories
)
return {"history": history}
def clear(self) -> None:
pass # Records are permanent — use agent_id namespacing for isolationUsage
from langchain.chains import ConversationChain
from langchain.llms import OpenAI
memory = SynapseMemory(
api_key="sk_syn_your_key_here",
agent_id="customer-support-bot"
)
chain = ConversationChain(
llm=OpenAI(),
memory=memory,
verbose=True
)
# Every exchange is now persisted to Synapse
chain.run("What's our return policy for software licenses?")Tips
- Use a unique
agent_idper user or conversation — this namespaces all queries. - For multi-session persistence, use a stable
agent_id(e.g., user ID or customer account ID). - Increase
limitinload_memory_variablesfor broader context window loading. - Add
type="open_item"filters to surface unresolved tasks at session start.