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

# LangChain

> Auto-instrument LangChain and LangGraph with tracing for chat models, chains, tools, retrievers, and graph nodes.

Valiqor provides the deepest integration with LangChain, automatically
tracing **chat models**, **chains**, **tools**, **retrievers**, and
**LangGraph** nodes. RAG pipelines get dedicated retrieval spans with
document scores and relevance data.

***

## Install

```bash theme={"system"}
pip install valiqor[langchain]
```

This installs `valiqor` plus `langchain>=0.1.0` and `langchain-core>=0.1.0`.

***

## Zero-Config (Recommended)

Add a single import at the top of your app — all LangChain components
are automatically traced:

```python theme={"system"}
import valiqor.auto  # ← Add this line

from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage

llm = ChatOpenAI(model="gpt-4o")
response = llm.invoke([HumanMessage(content="Explain quantum computing")])
print(response.content)
```

Every LLM call, chain invocation, tool execution, and retrieval is traced.

***

## Selective Instrumentation

If you only want LangChain tracing:

```python theme={"system"}
from valiqor.trace import langchain_autolog

langchain_autolog()

# Or using the namespace-style API:
from valiqor.trace import LangChain
LangChain.autolog()
```

***

## What Gets Instrumented

LangChain instrumentation covers 5 subsystems:

<CardGroup cols={2}>
  <Card title="Chat Models" icon="comments">
    `invoke()`, `ainvoke()`, `stream()`, `astream()` on any
    `BaseChatModel` subclass — captures model name, vendor, tokens, cost,
    and messages.
  </Card>

  <Card title="Chains" icon="link">
    `invoke()` and `ainvoke()` on `Runnable` chains — captures the full
    LCEL pipeline execution.
  </Card>

  <Card title="Tools" icon="wrench">
    `invoke()`, `ainvoke()`, `run()`, `arun()` on `BaseTool` — captures
    tool name, arguments, and results.
  </Card>

  <Card title="Retrievers" icon="magnifying-glass">
    `get_relevant_documents()` and `aget_relevant_documents()` on
    `BaseRetriever` — captures documents, scores, and retrieval metadata.
  </Card>
</CardGroup>

### LangGraph

LangGraph is also instrumented automatically:

* **Graph execution**: `invoke()` and `ainvoke()` on compiled graphs
* **Node execution**: Individual node functions are wrapped and traced
* **State tracking**: Graph state transitions are captured

***

## RAG Pipeline Tracing

When using retrievers, Valiqor captures rich RAG-specific data automatically:

```python theme={"system"}
import valiqor.auto
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough

# Build RAG chain
embeddings = OpenAIEmbeddings()
vectorstore = FAISS.from_texts(
    ["Paris is the capital of France", "Berlin is the capital of Germany"],
    embeddings
)
retriever = vectorstore.as_retriever()
llm = ChatOpenAI(model="gpt-4o")

prompt = ChatPromptTemplate.from_template(
    "Answer based on context: {context}\n\nQuestion: {question}"
)

chain = (
    {"context": retriever, "question": RunnablePassthrough()}
    | prompt
    | llm
)

# RAG retrieval is automatically traced with document scores
response = chain.invoke("What is the capital of France?")
```

Each retrieval span captures:

| Field           | Description                                |
| --------------- | ------------------------------------------ |
| Documents       | Retrieved documents with content snippets  |
| Scores          | Relevance scores per document              |
| Metadata        | Document metadata (source, page, etc.)     |
| Embedding model | Model used for embeddings (when available) |
| Latency         | Retrieval time in milliseconds             |
| Top-k           | Number of documents retrieved              |

***

## Async Support

All LangChain async methods are traced automatically:

```python theme={"system"}
import valiqor.auto
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage

llm = ChatOpenAI(model="gpt-4o")

async def main():
    response = await llm.ainvoke([HumanMessage(content="Hello!")])
    print(response.content)
```

***

## Streaming

LangChain streaming is supported — `stream()` and `astream()` calls
are traced:

```python theme={"system"}
import valiqor.auto
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage

llm = ChatOpenAI(model="gpt-4o")

for chunk in llm.stream([HumanMessage(content="Tell me a story")]):
    print(chunk.content, end="")
```

***

## Custom Retriever Detection

If you have custom retrieval tools that Valiqor doesn't detect
automatically, you can register them:

```python theme={"system"}
from valiqor.trace import configure_retriever_detection, autolog

configure_retriever_detection(
    name_patterns=["my_search_tool", "doc_finder"],
    class_patterns=["MyCustomRetriever"],
    module_patterns=["my_app.search"]
)

autolog(["langchain"])
```

***

## What Gets Captured

### Chat Model Spans

| Field               | Description                              |
| ------------------- | ---------------------------------------- |
| `model`             | Model name                               |
| `vendor`            | Provider (auto-detected from class name) |
| `prompt_tokens`     | Input tokens                             |
| `completion_tokens` | Output tokens                            |
| `total_tokens`      | Combined tokens                          |
| `cost`              | Estimated cost in USD                    |
| `messages`          | Full message history                     |
| `tool_calls`        | Tool call arguments and results          |

### Tool Spans

| Field         | Description      |
| ------------- | ---------------- |
| `tool_name`   | Name of the tool |
| `arguments`   | Input arguments  |
| `result`      | Tool output      |
| `duration_ms` | Execution time   |

***

## With Workflows

Group LangChain operations into a named trace:

```python theme={"system"}
import valiqor.auto
from valiqor.trace import trace_workflow
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4o")

with trace_workflow("customer-support-agent"):
    # All LangChain calls within this block are grouped
    response = llm.invoke("How can I help you?")
```

***

## Disabling

```python theme={"system"}
from valiqor.trace import disable_autolog

disable_autolog("langchain")    # Disable LangChain only
disable_autolog()               # Disable all providers
```

***

## Next Steps

<CardGroup cols={2}>
  <Card title="Tracing Guide" icon="route" href="/workflows/tracing">
    Learn about traces, spans, workflows, and exporters.
  </Card>

  <Card title="Traces & Spans" icon="diagram-project" href="/concepts/traces-and-spans">
    Understand the trace data model, span kinds, and RAG stages.
  </Card>
</CardGroup>
