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

> ## Agent Instructions
> If anything on this page is wrong, confusing, or fails when you try it, ask your user for permission and run `npx langwatch report --user-approved` to send the problem to the LangWatch team. No login is needed and secrets are redacted locally. `npx langwatch report --help` has the details.

# LangChain Instrumentation

> Instrument LangChain applications with LangWatch to trace chains, RAG flows, and metrics for AI agent evaluations.

> [!TIP]
>
> **Quick setup?** Instead of following these steps manually, [copy a prompt](https://langwatch.ai/docs/skills/directory.md) into your coding agent and it will set this up for you automatically.

LangWatch integrates with Langchain to provide detailed observability into your chains, agents, LLM calls, and tool usage.

## Installation

```bash pip theme={null}
pip install langwatch langchain langchain-openai
```

```bash uv theme={null}
uv add langwatch langchain langchain-openai
```

## Usage

> [!NOTE]
>
> The LangWatch API key is configured by default via the `LANGWATCH_API_KEY` environment variable.

Use LangWatch's callback handler to instrument your Langchain agents and chains. The callback automatically captures LLM calls, tool usage, and chain execution as spans within your trace.

```python theme={null}
import langwatch
from langchain.agents import create_agent
from langchain_openai import ChatOpenAI
from langchain_core.runnables import RunnableConfig
import os
import asyncio


langwatch.setup()


def get_weather(city: str) -> str:
    """Get weather for a given city."""
    return f"It's always sunny in {city}!"


agent = create_agent(
    # gpt-6 models only accept function tools on Chat Completions with reasoning off
    model=ChatOpenAI(model="gpt-6-luna", reasoning_effort="none"),
    tools=[get_weather],
    system_prompt="You are a helpful assistant, that can get the weather.",
)


@langwatch.trace(name="Langchain - Weather Agent")
def main(user_question: str):
    result = agent.invoke(
        {"messages": [{"role": "user", "content": user_question}]},
        config=RunnableConfig(
            callbacks=[langwatch.get_current_trace().get_langchain_callback()]
        ),
    )

    return result["messages"][-1].content


if __name__ == "__main__":
    result = main("What is the weather in Philadelphia?")
    print(result)
```

The `@langwatch.trace()` decorator creates a parent trace, and `get_langchain_callback()` provides a callback handler that captures Langchain events as spans. Pass the callback to your agent or chain's `RunnableConfig` to enable instrumentation.

## Check it worked

Run the example. Then open your project in LangWatch: a new trace named "Langchain - Weather Agent" appears in the traces list within a few seconds. Open it to see the LangChain chain, tool and LLM steps nested inside it.

If no trace appears, check that `LANGWATCH_API_KEY` is set in the environment the script runs in.

## Related

* [Capturing RAG](https://langwatch.ai/docs/integration/python/tutorials/capturing-rag.md) - Learn how to capture RAG data from LangChain retrievers and tools
* [Capturing Metadata and Attributes](https://langwatch.ai/docs/integration/python/tutorials/capturing-metadata.md) - Add custom metadata and attributes to your traces and spans
* [Capturing Evaluations & Guardrails](https://langwatch.ai/docs/integration/python/tutorials/capturing-evaluations-guardrails.md) - Log evaluations and implement guardrails in your LangChain applications


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