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

# LangChain

> Use RedPill for LangChain chat, streaming, retrieval, and agents.

LangChain connects to RedPill through its OpenAI integration. Use the Chat Completions base URL and
an exact model ID from the live catalog.

```bash theme={null}
export REDPILL_AI_API_KEY="YOUR_API_KEY"
pip install -U langchain langchain-openai
npm install @langchain/openai @langchain/core
```

## Send a message

<CodeGroup>
  ```python Python theme={null}
  import os

  from langchain_openai import ChatOpenAI

  model = ChatOpenAI(
      model="z-ai/glm-5.2",
      base_url="https://api.redpill.ai/v1",
      api_key=os.environ["REDPILL_AI_API_KEY"],
  )
  response = model.invoke("Explain attestation in one sentence.")
  print(response.content)
  ```

  ```typescript TypeScript theme={null}
  import { ChatOpenAI } from "@langchain/openai";

  const apiKey = process.env.REDPILL_AI_API_KEY;
  if (!apiKey) throw new Error("REDPILL_AI_API_KEY is required");

  const model = new ChatOpenAI({
    model: "z-ai/glm-5.2",
    apiKey,
    configuration: { baseURL: "https://api.redpill.ai/v1" },
  });
  const response = await model.invoke("Explain attestation in one sentence.");
  console.log(response.content);
  ```
</CodeGroup>

Use `model.stream(...)` instead of `model.invoke(...)` to render tokens as they arrive.

## Build an agent

LangChain's `create_agent` runtime is built on LangGraph. Bound each run and validate tool input:

```python theme={null}
from langchain.agents import create_agent
from langchain.tools import tool


@tool
def lookup_service(name: str) -> str:
    """Return the owner of an internal service."""
    owners = {"gateway": "inference", "dashboard": "product"}
    return owners.get(name, "unknown")


agent = create_agent(
    model=model,
    tools=[lookup_service],
    system_prompt="Use tools when a service owner is requested.",
)
result = agent.invoke(
    {"messages": [{"role": "user", "content": "Who owns the gateway service?"}]},
    config={"recursion_limit": 4},
)
print(result["messages"][-1].content)
```

Choose a model whose `supported_features` includes `tools`.

## Add retrieval

Query the embedding catalog, then configure `OpenAIEmbeddings` with a current model:

```python theme={null}
from langchain_core.vectorstores import InMemoryVectorStore
from langchain_openai import OpenAIEmbeddings

embeddings = OpenAIEmbeddings(
    model="qwen/qwen3-embedding-8b",
    base_url="https://api.redpill.ai/v1",
    api_key=os.environ["REDPILL_AI_API_KEY"],
)
store = InMemoryVectorStore.from_texts(
    ["Receipts bind a request and response to the serving path."],
    embedding=embeddings,
)
print(store.similarity_search("What does a receipt prove?", k=1)[0].page_content)
```

## Verification boundary

This base-URL integration does not verify ACI locally. TypeScript applications that require an
attested, TLS-bound connection can inject `connectAci().fetch` through LangChain's `configuration`
hook and audit the retained receipt, using the same pattern as the
[OpenAI SDK](/guides/integrations/openai-sdk#verified-node-and-bun-transport).

## References

* [LangChain models](https://docs.langchain.com/oss/python/langchain/models)
* [LangChain agents](https://docs.langchain.com/oss/python/langchain/agents)
* [LangChain retrieval](https://docs.langchain.com/oss/python/langchain/retrieval)
* [RedPill embeddings API](/api-reference/embeddings)
