> ## Documentation Index
> Fetch the complete documentation index at: https://docs.thrindex.com/llms.txt
> Use this file to discover all available pages before exploring further.

# LangChain

> LangChain-compatible memory and retriever components backed by Thrindex.

The LangChain integration provides two components that plug directly into any chain or agent that uses the standard LangChain interfaces.

| Component            | Interface       | Use case                                                                      |
| -------------------- | --------------- | ----------------------------------------------------------------------------- |
| `ThrindexChatMemory` | `BaseMemory`    | Persist conversation turns and load relevant memories as chat history context |
| `ThrindexRetriever`  | `BaseRetriever` | Semantic memory search inside RAG chains, RetrievalQA, agents                 |

***

## Install

<Tabs>
  <Tab title="Python">
    ```bash theme={null}
    pip install 'thrindex[langchain]'
    ```
  </Tab>

  <Tab title="TypeScript">
    ```bash theme={null}
    npm install thrindex @langchain/core
    ```
  </Tab>
</Tabs>

***

## `ThrindexChatMemory`

<Tabs>
  <Tab title="Python">
    A `BaseMemory` implementation that saves conversation turns to Thrindex after each chain call and retrieves the most relevant memories before the next one.

    ```python theme={null}
    from thrindex import Thrindex
    from thrindex.integrations.langchain import ThrindexChatMemory

    client = Thrindex(api_key="th_live_...")

    memory = ThrindexChatMemory(
        thrindex_client=client,
        agent_id="support-agent",
        user_id="user-42",
        memory_key="history",   # key injected into chain inputs, default "history"
        k=10,                   # memories to retrieve per call, default 10
        return_messages=True,   # True = BaseMessage objects, False = plain string
    )
    ```

    Use with any chain that accepts a `memory` parameter:

    ```python theme={null}
    from langchain.chains import ConversationChain
    from langchain_openai import ChatOpenAI

    chain = ConversationChain(
        llm=ChatOpenAI(model="gpt-4o"),
        memory=memory,
    )
    response = chain.invoke({"input": "What notification style do I prefer?"})
    ```
  </Tab>
</Tabs>

***

## `ThrindexRetriever`

<Tabs>
  <Tab title="Python">
    A `BaseRetriever` that wraps Thrindex semantic search. Use it anywhere LangChain accepts a retriever — `RetrievalQA`, LCEL chains, agents, and more.

    ```python theme={null}
    from thrindex.integrations.langchain import ThrindexRetriever

    retriever = ThrindexRetriever(
        thrindex_client=client,
        agent_id="support-agent",
        user_id="user-42",
        k=5,                                            # results to return, default 5
        task_context="answering user preference query", # optional ranking hint
    )

    docs = retriever.invoke("user notification preferences")
    for doc in docs:
        print(doc.page_content, doc.metadata["score"])
    ```

    Each returned `Document` has `page_content` set to the memory text and `metadata` containing `memory_id`, `agent_id`, `user_id`, `score`, `importance`, `level`, and `created_at`.
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    import { ThrindexClient } from 'thrindex';
    import { ThrindexLangChainRetriever } from 'thrindex/integrations/langchain';

    const retriever = new ThrindexLangChainRetriever({
      thrindex: new ThrindexClient({ apiKey: 'th_live_...' }),
      agentId: 'support-agent',
      userId: 'user-42',
      k: 5,
    });

    // Use in any LangChain chain that accepts a BaseRetriever
    ```
  </Tab>
</Tabs>
