Python - 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.
1import os
2import paygent_sdk
3from paygent_sdk import PaygentLangGraphCallback
4from langgraph.graph import StateGraph
5
6# 1. Initialize the global Paygent context
7paygent_sdk.init(os.getenv("PAYGENT_API_KEY"))
8
9# 2. Create a unified tracker for the entire graph
10# This single callback instruments ALL nodes automatically
11paygent_tracker = PaygentLangGraphCallback(
12 paygent_client=paygent_sdk._global_client,
13 external_agent_id="enterprise-support-agent",
14 external_customer_id="customer_id_123",
15 external_customer_name="Company X" # Optional: Auto-creates customer in dashboard
16)
17
18# 3. Define your execution config with the callback
19config = {
20 "configurable": {"thread_id": "session_abc_789"},
21 "recursion_limit": 50,
22 "callbacks": [paygent_tracker] # The magic happens here!
23}
24
25# 4. Invoke your graph - every node is now tracked in real-time
26# No code changes required inside your node functions.
27app.invoke(initial_state, config)📖 Real-World 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
PaygentLangGraphCallbackat 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.
Global Instrumenting
Drop the Paygent tracker into your LangGraph config to instantly enable observability across the entire graph.
Multi-Provider Normalization
Automatically normalize usage data from OpenAI, Bedrock, VertexAI, and more into a single dashboard.
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.
1import os
2from langchain_openai import ChatOpenAI
3from langchain_anthropic import ChatAnthropic
4from paygent_sdk import PaygentLangGraphCallback
5import paygent_sdk
6
7# 1. Initialize Tracker
8tracker = PaygentLangGraphCallback(
9 paygent_client=paygent_sdk.init(os.getenv("PAYGENT_API_KEY")),
10 external_agent_id="multi-agent-system",
11 external_customer_id="customer-789"
12)
13
14# 2. Attach tracker to individual models
15# All nodes using these models will be tracked automatically
16model_a = ChatOpenAI(model="gpt-4o", callbacks=[tracker])
17model_b = ChatAnthropic(model="claude-3-5-sonnet", callbacks=[tracker])
18
19# 3. Use models in your nodes as usual
20def research_node(state):
21 res = model_a.invoke("Research topic...")
22 return {"data": res.content}
23
24# Paygent automatically attributes usage to the correct node!Was this page helpful?
Need help? Contact us at support@withpaygent.com