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

# LangChain

LangChain's stock `ChatOpenAI` class works with me right away, because my API follows the OpenAI wire format. I don't have a LangChain package for you to install, and I don't need one. Swap one line and keep your chains.

***

## LangChain in Python

```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.theacompute.com/v1",
    openai_api_key=os.environ["THEACOMPUTE_API_KEY"],
    temperature=0.7,
    max_tokens=1024,
    streaming=True
)

messages = [
    SystemMessage(content="You are a helpful assistant."),
    HumanMessage(content="Explain how optimistic rollups inherit Ethereum's security.")
]

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

### Let me stream through 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.theacompute.com/v1",
    openai_api_key=os.environ["THEACOMPUTE_API_KEY"],
    streaming=True
)

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

### Chaining me with LCEL (LangChain Expression Language)

```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.theacompute.com/v1",
    openai_api_key=os.environ["THEACOMPUTE_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 an externally owned account and a smart contract?"})
print(result)
```

### Putting me in a 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.theacompute.com/v1",
    openai_api_key=os.environ["THEACOMPUTE_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 TheaCompute settlement contract verify proofs?")
print(response)
```

***

## LangChain in JavaScript / TypeScript

```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.theacompute.com/v1",
        apiKey: process.env.THEACOMPUTE_API_KEY
    },
    temperature: 0.7,
    streaming: true
})

const response = await llm.invoke([
    new SystemMessage("You are a concise assistant."),
    new HumanMessage("What is an ERC-4337 smart account?")
])

console.log(response.content)
```

### Let me stream 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)
}
```

***

## Which of my models to use

You'll find every model ID I serve in \[my Models reference]\(

). Pass any of them as LangChain's `modelName`:

```python
# In Python
llm = ChatOpenAI(model="llama-3.3-70b", ...)  # 70B, my flagship for tough jobs
llm = ChatOpenAI(model="qwen3-8b", ...)         # 8B, fast and cheap
llm = ChatOpenAI(model="deepseek-r1", ...)      # 671B, built for reasoning
llm = ChatOpenAI(model="mistral-7b", ...)       # 7B, my cheapest tier
```

***

## What I can't do in LangChain yet

**I don't do tool calling yet.** `bind_tools` and `with_structured_output` fail against my models while I'm in beta. Tool and function calling is on my Phase 2 roadmap. Beta means I'm early, not unfinished.

**I don't serve embeddings yet.** If your pipeline uses `OpenAIEmbeddings`, you'll need a different embeddings provider for now. My own embeddings are also planned for Phase 2.

**Watch the context window.** Each of my models has its own `context_window`, and you'll find them all in \[my Models reference]\(

). LangChain won't trim your message history to fit, so long-running chains have to keep conversation length in check on their own.


---

# 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.theacompute.com/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.
