> For the complete documentation index, see [llms.txt](https://docs.usedecentral.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.usedecentral.org/integrations/langchain.md).

# LangChain

Decentral works with LangChain via the `ChatOpenAI` class. Since the Decentral API is OpenAI-compatible, no additional package is required.

***

## Python (LangChain)

```bash
pip install langchain-openai
```

```python
import os
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage

llm = ChatOpenAI(
    model="qwen3-8b",
    openai_api_base="https://api.usedecentral.org/v1",
    openai_api_key=os.environ["DECENTRAL_API_KEY"],
    temperature=0.7,
    max_tokens=1024,
    streaming=True
)

messages = [
    SystemMessage(content="You are a helpful assistant."),
    HumanMessage(content="Explain how Solana's tower BFT consensus works.")
]

response = llm.invoke(messages)
print(response.content)
```

### Streaming with LangChain

```python
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage

llm = ChatOpenAI(
    model="llama-3.3-70b",
    openai_api_base="https://api.usedecentral.org/v1",
    openai_api_key=os.environ["DECENTRAL_API_KEY"],
    streaming=True
)

for chunk in llm.stream([HumanMessage(content="Write a short essay on decentralization.")]):
    print(chunk.content, end="", flush=True)
```

### LangChain Expression Language (LCEL)

```python
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

llm = ChatOpenAI(
    model="qwen3-8b",
    openai_api_base="https://api.usedecentral.org/v1",
    openai_api_key=os.environ["DECENTRAL_API_KEY"]
)

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a blockchain expert. Be concise."),
    ("human", "{question}")
])

chain = prompt | llm | StrOutputParser()

result = chain.invoke({"question": "What is the difference between a Solana account and a program?"})
print(result)
```

### RAG pipeline

```python
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser

llm = ChatOpenAI(
    model="llama-3.3-70b",
    openai_api_base="https://api.usedecentral.org/v1",
    openai_api_key=os.environ["DECENTRAL_API_KEY"]
)

template = """Answer the question based on the following context.

Context:
{context}

Question:
{question}
"""

prompt = ChatPromptTemplate.from_template(template)

def format_docs(docs):
    return "\n\n".join(doc.page_content for doc in docs)

rag_chain = (
    {"context": retriever | format_docs, "question": RunnablePassthrough()}
    | prompt
    | llm
    | StrOutputParser()
)

response = rag_chain.invoke("How does the Decentral settlement program verify proofs?")
print(response)
```

***

## JavaScript / TypeScript (LangChain)

```bash
npm install @langchain/openai
```

```typescript
import { ChatOpenAI } from "@langchain/openai"
import { HumanMessage, SystemMessage } from "@langchain/core/messages"

const llm = new ChatOpenAI({
    modelName: "qwen3-8b",
    configuration: {
        baseURL: "https://api.usedecentral.org/v1",
        apiKey: process.env.DECENTRAL_API_KEY
    },
    temperature: 0.7,
    streaming: true
})

const response = await llm.invoke([
    new SystemMessage("You are a concise assistant."),
    new HumanMessage("What is a Solana PDA?")
])

console.log(response.content)
```

### Streaming in TypeScript

```typescript
const stream = await llm.stream([
    new HumanMessage("Explain WebGPU and how it enables browser-based inference.")
])

for await (const chunk of stream) {
    process.stdout.write(chunk.content as string)
}
```

***

## Available models

Use the model IDs from the \[Models reference]\(

). For LangChain's `modelName` parameter:

```python
# Python
llm = ChatOpenAI(model="llama-3.3-70b", ...)  # 70B, highest capability
llm = ChatOpenAI(model="qwen3-8b", ...)         # 8B, fast and cost-effective
llm = ChatOpenAI(model="deepseek-r1", ...)      # 70B, strong at reasoning tasks
llm = ChatOpenAI(model="mistral-7b", ...)       # 7B, lowest cost
```

***

## Notes on LangChain compatibility

**Tool calling is not yet supported.** LangChain's tool/function calling features (`bind_tools`, `with_structured_output`) will not work with Decentral models during the beta. Tool calling support is on the Phase 2 roadmap.

**Embeddings are not yet supported.** If your LangChain pipeline uses `OpenAIEmbeddings`, you will need to use a separate embeddings provider. Decentral embeddings are on the Phase 2 roadmap.

**Context window.** Each Decentral model has its own context window. Check the \[Models reference]\(

) for `context_window` values. LangChain does not automatically truncate messages to fit, so manage conversation length yourself if you are building long-running chains.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.usedecentral.org/integrations/langchain.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
