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

# LangChain and LangGraph

> Record LangChain and LangGraph model and tool calls in Sovara and add lessons to model requests.

```bash Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
pip install "sovara-langchain[agent]"   # without [agent] if you only use wrap_chat_model
```

```bash TypeScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
npm install @sovara/langchain @sovara/sdk
```

Inside a trace, use the middleware for `create_agent`, or wrap the chat model for anything else
(LangGraph nodes, prebuilt agents, structured output). Use one per model. Put the middleware
last, so it records the model and prompt that other middleware settled on, and each retry by
other middleware as its own step.

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import sovara_langchain
from sovara_langchain import SovaraMiddleware, wrap_chat_model

agent = create_agent(model, tools, middleware=[*other_middleware, SovaraMiddleware()])

sovara_langchain.instrument_tools()  # tool calls of agents that don't use the middleware
model = wrap_chat_model(ChatOpenAI(model="gpt-5.4")).bind_tools(tools)
```

```ts theme={"theme":{"light":"github-light","dark":"github-dark"}}
import { sovaraMiddleware } from "@sovara/langchain";
import { wrapChatModel } from "@sovara/langchain/chat-model";
import { instrumentTools } from "@sovara/langchain/tools";

const agent = createAgent({ model, tools, middleware: [...otherMiddleware, sovaraMiddleware()] });
instrumentTools(); // tool calls outside the agent middleware
const wrapped = wrapChatModel(new ChatOpenAI({ model: "gpt-5.4" })).bindTools(tools);
```

Lessons are one temporary user message at the end of the request; the agent's messages are
never changed. Outside a trace, calls pass through unrecorded, and if Sovara is unavailable they
still run.

Requires `langchain-core` 1.x (`langchain` 1.1+ for the middleware) in Python, or
`@langchain/core` 1.2+ (`langchain` 1.5+ for the middleware) in TypeScript.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.