> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.withpaygent.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.withpaygent.com/_mcp/server.

# Python SDK — LangChain Integration

The recommended way to track LangChain pipelines in production is using the explicit `PaygentLangChainCallback`. This approach provides fine-grained control over tracking context and enables seamless multi-model pipeline monitoring.

## Production Callback Integration

Attach the `PaygentLangChainCallback` to your model instances to automatically track tokens, costs, and performance across every step of your chain.

**`langchain_production.py`**

```python title="langchain_production.py"
import os
import paygent_sdk
from paygent_sdk import PaygentLangChainCallback
from langchain_openai import ChatOpenAI
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from dotenv import load_dotenv

load_dotenv()

# 1. Initialize Paygent
paygent_sdk.init(os.getenv("PAYGENT_API_KEY"))

# 2. Create the production-ready callback
# Use this single tracker for the entire pipeline
paygent_cb = PaygentLangChainCallback(
    paygent_client=paygent_sdk._global_client,
    indicator="article-generator-pipeline",
    external_agent_id="writer-agent",     # Get this from your Agent settings in Paygent
    external_customer_id="customer-99",
    external_customer_name="Global Media" # Optional: Auto-creates customer in Paygent
)

# 3. Attach to your models
llm = ChatOpenAI(
    model="gpt-4o",
    temperature=0,
    callbacks=[paygent_cb]
)

# 4. Use in a standard LangChain Expression Language (LCEL) chain
prompt = ChatPromptTemplate.from_template("Summarize this topic: {topic}")
chain = prompt | llm | StrOutputParser()

if __name__ == "__main__":
    result = chain.invoke({"topic": "Quantum Computing"})
    print(f"Summary: {result}")
    # ✅ Every invocation is now tracked in your Paygent dashboard
```

> **Tip**
>
> **Why use callbacks?**
>
> * **Named Context:** Group different parts of your application under distinct indicators.
> * **Auto-Onboarding:** Automatically create customers in Paygent when they first interact with your AI.
> * **Multi-Model:** Track usage across OpenAI, Anthropic, Gemini, and Mistral in a single unified view.

## Multi-Model Pipeline Example

Complex workflows often involve multiple LLM providers. By passing the same callback instance to every model, you maintain a consistent billing context.

**`multi_model_pipeline.py`**

```python title="multi_model_pipeline.py"
# Attach the same callback to different providers
openai_llm  = ChatOpenAI(model="gpt-4o", callbacks=[paygent_cb])
gemini_llm  = ChatGoogleGenerativeAI(model="gemini-2.5-flash", callbacks=[paygent_cb])

# Both models will now contribute usage data to the same 'article-generator-pipeline' indicator
```