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

# LangChain & LangGraph

> Auto-instrument LangChain and LangGraph agent frameworks

Automatically capture agent steps, tool calls, and LLM invocations within LangChain chains and LangGraph graphs.

## Install

> [tab] Python
>
> ```bash theme={null}
> pip install traceroot langchain-core langchain-openai langgraph
> ```

> [tab] TypeScript
>
> ```bash theme={null}
> npm install @traceroot-ai/traceroot @langchain/core @langchain/openai @langchain/langgraph
> ```

## Set up

> [tab] Python
>
> ```python theme={null}
> import traceroot
> from traceroot import Integration
>
> traceroot.initialize(integrations=[Integration.LANGCHAIN])
> ```

> [tab] TypeScript
>
> ```typescript theme={null}
> import * as lcCallbackManager from '@langchain/core/callbacks/manager';
> import { TraceRoot } from '@traceroot-ai/traceroot';
>
> TraceRoot.initialize({
>   instrumentModules: { langchain: lcCallbackManager },
> });
> ```

## Usage

### Usage with LangGraph

> [tab] Python
>
> ```python theme={null}
> from langchain_openai import ChatOpenAI
> from langgraph.prebuilt import create_react_agent
>
> llm = ChatOpenAI(model="gpt-4o")
>
> tools = [search_tool, calculator_tool]
> agent = create_react_agent(llm, tools=tools)
>
> # All agent steps are automatically traced
> result = agent.invoke({
>     "messages": [{"role": "user", "content": "What is 2 + 2?"}]
> })
> ```

> [tab] TypeScript
>
> ```typescript theme={null}
> import { ChatOpenAI } from '@langchain/openai';
> import { HumanMessage } from '@langchain/core/messages';
> import { END, START, StateGraph, Annotation } from '@langchain/langgraph';
>
> const llm = new ChatOpenAI({ model: 'gpt-4o', temperature: 0 });
>
> const AgentState = Annotation.Root({
>   query: Annotation<string>({ reducer: (_, v) => v, default: () => '' }),
>   answer: Annotation<string>({ reducer: (_, v) => v, default: () => '' }),
> });
>
> // Build graph — all nodes are automatically traced
> const app = new StateGraph(AgentState)
>   .addNode('answer', async (state) => {
>     const response = await llm.invoke([new HumanMessage(state.query)]);
>     return { answer: response.content as string };
>   })
>   .addEdge(START, 'answer')
>   .addEdge('answer', END)
>   .compile();
>
> const result = await app.invoke({ query: 'What is 2 + 2?' });
> console.log(result.answer);
> ```

### Usage with LangChain

> [tab] Python
>
> ```python theme={null}
> from langchain_openai import ChatOpenAI
> from langchain_core.prompts import ChatPromptTemplate
>
> llm = ChatOpenAI(model="gpt-4o")
> prompt = ChatPromptTemplate.from_messages([
>     ("system", "You are a helpful assistant."),
>     ("user", "{input}"),
> ])
>
> chain = prompt | llm
>
> # Chain execution is automatically traced
> result = chain.invoke({"input": "Hello!"})
> ```

> [tab] TypeScript
>
> ```typescript theme={null}
> import { ChatOpenAI } from '@langchain/openai';
> import { ChatPromptTemplate } from '@langchain/core/prompts';
>
> const llm = new ChatOpenAI({ model: 'gpt-4o' });
> const prompt = ChatPromptTemplate.fromMessages([
>   ['system', 'You are a helpful assistant.'],
>   ['user', '{input}'],
> ]);
>
> const chain = prompt.pipe(llm);
>
> // Chain execution is automatically traced
> const result = await chain.invoke({ input: 'Hello!' });
> ```

## Verify

Run your code and confirm the trace arrives:

1. **In the traceroot UI**: Navigate to **Tracing > Traces** in [traceroot Cloud](https://app.traceroot.ai). You should see a trace with root span `RunnableSequence` or `CompiledStateGraph`.
2. **Via the CLI**: Run `traceroot traces list` to inspect your recent traces from the terminal.

## What gets captured

| Attribute | Description |
| - | - |
| Agent steps | Each agent iteration as a span |
| Tool calls | Each tool invocation with input/output |
| LLM calls | All LLM calls within the chain/graph |
| Graph structure | Parent-child relationships between steps |
| Token usage | Aggregated across all LLM calls |
| Cost | Aggregated when the underlying model IDs match traceroot's pricing table |

## Limitations

* For TypeScript, `instrumentModules` must be configured with `@langchain/core/callbacks/manager` to enable automatic callback tracing.

## Run the example

Clone the repo and run a complete agent end-to-end.

> [card] [Python](https://github.com/traceroot-ai/traceroot/tree/main/examples/python/langgraph-code-agent)
>
> Run the Python example

> [card] [TypeScript](https://github.com/traceroot-ai/traceroot/tree/main/examples/typescript/langchain)
>
> Run the TypeScript example

## Next steps

> [card] [Python SDK Reference](https://traceroot.ai/docs/tracing/python-sdk.md)
>
> Explore the full Python SDK reference and instrumentation options.

> [card] [TypeScript SDK Reference](https://traceroot.ai/docs/tracing/typescript-sdk.md)
>
> Explore the full TypeScript SDK reference and configuration options.


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