Integrations

Integrating with LangChain

You can integrate the Fastino Personalization API into LangChain to give your chains and agents persistent, user-aware memory.

This lets your LangChain agents recall who a user is, what they care about, and how they act — all through the same standardized API endpoints that power the Fastino personalization layer.

Overview

Fastino works as a drop-in Retriever and Memory backend for LangChain.
By wiring Fastino into LangChain’s ecosystem, your apps gain:

  • Personalized retrieval — fetch relevant memories per user

  • Contextual reasoning — ground prompts on user history

  • Continuous learning — log new interactions for long-term memory

  • Tool integration — agents can call Fastino endpoints dynamically

Architecture

LangChain provides abstractions for retrievers, memories, and tools.
Fastino can serve as all three:

Component

Integration

Purpose

Retriever

FastinoRetriever

Retrieve user memories for RAG

Memory

FastinoMemory

Maintain persistent user context

Tool

FastinoSearchTool

Query user data during conversations

Prerequisites

  • Python 3.9+

  • Installed dependencies:

  • A valid Fastino API key

  • At least one registered user in your Fastino workspace:

Example: Fastino as a Custom Retriever

This retriever queries Fastino’s /chunks endpoint for top-k memory snippets.

from langchain.schema import Document
from langchain.retrievers import BaseRetriever
import requests

FASTINO_API = "https://api.fastino.ai/"
FASTINO_KEY = "sk_live_123"
HEADERS = {"Authorization": f"x-api-key {FASTINO_KEY}", "Content-Type": "application/json"}

class FastinoRetriever(BaseRetriever):
    """Retrieve personalized memory snippets for a given user."""
    def __init__(self, user_id: str, top_k: int = 5):
        self.user_id = user_id
        self.top_k = top_k

    def _get_relevant_documents(self, query: str):
        payload = {
            "user_id": self.user_id,
            "conversation": [
                {"role": "system", "content": "You are a helpful assistant."},
                {"role": "user", "content": query}
            ],
            "top_k": self.top_k
        }
        r = requests.post(f"{FASTINO_API}/chunks", json=payload, headers=HEADERS)
        results = r.json().get("results", [])
        return [Document(page_content=res["excerpt"]) for res in results]

    async def _aget_relevant_documents(self, query: str):
        return self._get_relevant_documents(query)

Usage:

retriever = FastinoRetriever(user_id="usr_42af7c", top_k=3)
docs = retriever.get_relevant_documents("When does Ash prefer meetings?")
for d in docs:
    print(d.page_content)

Example: Fastino as a Memory Module

You can create a persistent memory that reads and writes user context to Fastino automatically.

from langchain.memory import BaseMemory
from typing import Dict, Any
import requests

class FastinoMemory(BaseMemory):
    """Persistent memory that stores and retrieves from Fastino."""
    def __init__(self, user_id: str):
        self.user_id = user_id

    def load_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, Any]:
        payload = {
            "user_id": self.user_id,
            "question": "What are the latest user preferences and focus windows?"
        }
        r = requests.post(f"{FASTINO_API}/personalization/profile/query", json=payload, headers=HEADERS)
        answer = r.json().get("answer", "")
        return {"history": answer}

    def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, Any]) -> None:
        note = f"User asked: {inputs.get('input')} → Assistant replied: {outputs.get('output')}"
        payload = {
            "user_id": self.user_id,
            "source": "langchain",
            "documents": [
                {"doc_id": "auto_update", "kind": "note", "title": "LangChain session", "content": note}
            ]
        }
        requests.post(f"{FASTINO_API}/personalization/ingest", json=payload, headers=HEADERS)

    def clear(self):
        pass  # optional deletion if needed

Attach it to a chain:

from langchain.chains import ConversationChain
from langchain.chat_models import ChatOpenAI

llm = ChatOpenAI(model="gpt-4o-mini")
memory = FastinoMemory(user_id="usr_42af7c")

chain = ConversationChain(llm=llm, memory=memory, verbose=True)
chain.run("When should I schedule meetings tomorrow?")

Example: Fastino as a LangChain Tool

Expose Fastino’s query endpoint to your agent for dynamic calls during conversation.

from langchain.tools import BaseTool

class FastinoSearchTool(BaseTool):
    name = "fastino_search"
    description = "Ask personalized questions about a user."

    def _run(self, query: str):
        payload = {"user_id": "usr_42af7c", "question": query}
        r = requests.post(f"{FASTINO_API}/query", json=payload, headers=HEADERS)
        return r.json().get("answer")

    async def _arun(self, query: str):
        return self._run(query)

Register this tool in your LangChain agent:

from langchain.agents import initialize_agent, AgentType
from langchain.chat_models import ChatOpenAI

llm = ChatOpenAI(model="gpt-4o-mini")
tools = [FastinoSearchTool()]
agent = initialize_agent(tools, llm, agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION)

agent.run("Who does Ash collaborate with most often?")

Use Cases

Use Case

Description

RAG with personalization

Retrieve user-specific snippets to guide answers

Long-term user memory

Persist conversation history and reasoning context

Adaptive agents

Agents that tailor their responses to the user’s tone and habits

Cross-tool memory sync

Keep Fastino memory aligned with LangChain sessions

Feedback learning

Store corrections or edits to refine personalization

Authentication

As with other integrations, include your bearer token in every request:


Environment variable setup:

Error Handling

Fastino returns standardized JSON errors for easy integration with LangChain error traces.

Example:

{
  "error": {
    "code": "USER_NOT_FOUND",
    "message": "No user found with ID usr_42af7c"
  }
}

Handle these within your retriever or memory class to fall back gracefully when user data is missing.

Best Practices

  • Use a unique user_id for each LangChain user session.

  • Combine Fastino retrievers with internal document indexes for hybrid reasoning.

On this page