> ## 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.

# 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](/skills/code-prompts#instrument-my-code) into your coding agent and it will set this up for you automatically.
</Tip>

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

## Installation

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

  ```bash uv theme={null}
  uv add langwatch langchain langchain-openai
  ```
</CodeGroup>

## Usage

<Info>
  The LangWatch API key is configured by default via the `LANGWATCH_API_KEY` environment variable.
</Info>

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(
    model="openai:gpt-5",
    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.

## Related

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