> 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 — LangGraph Integration

LangGraph enables complex, stateful agentic workflows. Paygent's `PaygentLangGraphCallback` is engineered to provide deep observability into these graphs, automatically tracking every node across disparate cloud providers and model families.

## Zero-Touch Node Tracking

The most efficient way to instrument a LangGraph is by passing the Paygent callback directly into the graph's execution configuration. This **automatically monitors every node**, regardless of the underlying LLM provider, providing a unified view of cost and performance.

**`langgraph_global_tracking.py`**

```python title="langgraph_global_tracking.py"
import os
import paygent_sdk
from paygent_sdk import PaygentLangGraphCallback
from langgraph.graph import StateGraph

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

# 2. Create a unified tracker for the entire graph
# This single callback instruments ALL nodes automatically
paygent_tracker = PaygentLangGraphCallback(
    paygent_client=paygent_sdk._global_client,
    external_agent_id="enterprise-support-agent",
    external_customer_id="customer_id_123",
    external_customer_name="Company X" # Optional: Auto-creates customer in dashboard
)

# 3. Define your execution config with the callback
config = {
    "configurable": {"thread_id": "session_abc_789"},
    "recursion_limit": 50,
    "callbacks": [paygent_tracker] # The magic happens here!
}

# 4. Invoke your graph - every node is now tracked in real-time
# No code changes required inside your node functions.
app.invoke(initial_state, config)
```

**Scenario:** Company X, a high-growth AI startup, deployed a multi-agent LangGraph system for automated customer support. With nodes spanning OpenAI (GPT-4o), Anthropic (Claude), and local Ollama instances, they were flying blind on unit economics and couldn't accurately bill their enterprise clients.

**Solution:**

* Implemented `PaygentLangGraphCallback` at the graph configuration level.
* Enabled automatic customer onboarding for over 500 enterprise seats.
* Correlated node-level latency with token costs to identify high-overhead bottlenecks.

**Result:** Within 48 hours, Company X achieved 100% cost transparency. They discovered that 40% of their LLM spend was coming from a redundant 'Clarification' node. By consolidating it, they slashed their operational costs by \$6,500/month while maintaining 98% CSAT.

### Step 1 — Global Instrumenting

Drop the Paygent tracker into your LangGraph config to instantly enable observability across the entire graph.

### Step 2 — Multi-Provider Normalization

Automatically normalize usage data from OpenAI, Bedrock, VertexAI, and more into a single dashboard.

> **Success**
>
> **Enterprise Ready.** Paygent's LangGraph integration is built for high-throughput production environments. It handles callback buffering and asynchronous reporting to ensure that tracking never adds latency to your user's experience.

## Alternative: Model-Level Tracking

If you prefer not to use the global `config` object, you can attach the tracker directly to your LLM instances. This is useful when you want to use the same models across different graphs while maintaining a single tracking context.

**`langgraph_model_level.py`**

```python title="langgraph_model_level.py"
import os
from langchain_openai import ChatOpenAI
from langchain_anthropic import ChatAnthropic
from paygent_sdk import PaygentLangGraphCallback
import paygent_sdk

# 1. Initialize Tracker
tracker = PaygentLangGraphCallback(
    paygent_client=paygent_sdk.init(os.getenv("PAYGENT_API_KEY")),
    external_agent_id="multi-agent-system",
    external_customer_id="customer-789"
)

# 2. Attach tracker to individual models
# All nodes using these models will be tracked automatically
model_a = ChatOpenAI(model="gpt-4o", callbacks=[tracker])
model_b = ChatAnthropic(model="claude-3-5-sonnet", callbacks=[tracker])

# 3. Use models in your nodes as usual
def research_node(state):
    res = model_a.invoke("Research topic...")
    return {"data": res.content}

# Paygent automatically attributes usage to the correct node!
```