How to Integrate LangGraph for payments with LangSmith for production AI
Integrating LangGraph for payments with LangSmith gives you two things most agent systems lack: a controlled execution path for payment workflows, and trace-level visibility into what the agent actually did. That combination is what you need when an AI agent is allowed to initiate billing, confirm charges, or route payment exceptions in production.
The practical use case is simple: LangGraph handles the stateful payment flow, while LangSmith records runs, prompts, tool calls, and failures so you can debug and audit every step. If you’re building anything that touches money, that observability layer is not optional.
Prerequisites
- •Python 3.10+
- •A LangChain/LangGraph-compatible environment
- •
langgraphinstalled - •
langsmithinstalled - •API key for LangSmith set in your environment
- •Access to your payment provider SDK or a mocked payment service
- •Basic familiarity with LangGraph state graphs and tool nodes
Install the packages:
pip install langgraph langsmith langchain-core
Set your environment variables:
export LANGSMITH_API_KEY="lsv2_..."
export LANGSMITH_TRACING="true"
export LANGSMITH_PROJECT="payments-agent-prod"
Integration Steps
1. Define the payment workflow state
Start by modeling the data that moves through the graph. For payments, keep the state explicit: amount, currency, customer ID, authorization status, and final result.
from typing import TypedDict, Optional
class PaymentState(TypedDict):
customer_id: str
amount: float
currency: str
authorized: bool
payment_id: Optional[str]
status: str
This matters because LangGraph is built around deterministic state transitions. You want every branch in the payment flow to be inspectable later in LangSmith.
2. Create the payment tools as callable functions
Wrap your payment actions as plain Python functions. In production, these would call Stripe, Adyen, or your internal payments API.
import uuid
def authorize_payment(customer_id: str, amount: float, currency: str) -> dict:
# Replace with real PSP call
return {
"authorized": True,
"payment_id": f"pay_{uuid.uuid4().hex[:12]}",
"status": "authorized",
}
def capture_payment(payment_id: str) -> dict:
# Replace with real PSP capture call
return {
"payment_id": payment_id,
"status": "captured",
}
If you’re using LangChain tools elsewhere in the stack, you can also wrap these with @tool, but plain functions are enough for graph nodes.
3. Build the LangGraph workflow
Use StateGraph to define the payment path. Add one node for authorization and one for capture.
from langgraph.graph import StateGraph, END
def authorize_node(state: PaymentState) -> PaymentState:
result = authorize_payment(
customer_id=state["customer_id"],
amount=state["amount"],
currency=state["currency"],
)
return {
**state,
**result,
}
def capture_node(state: PaymentState) -> PaymentState:
if not state["authorized"] or not state["payment_id"]:
return {**state, "status": "capture_skipped"}
result = capture_payment(state["payment_id"])
return {
**state,
**result,
}
graph = StateGraph(PaymentState)
graph.add_node("authorize", authorize_node)
graph.add_node("capture", capture_node)
graph.set_entry_point("authorize")
graph.add_edge("authorize", "capture")
graph.add_edge("capture", END)
app = graph.compile()
At this point you have a deterministic payment pipeline. The next step is making sure every execution gets traced in LangSmith.
4. Enable LangSmith tracing for the graph run
LangSmith traces are usually enabled through environment variables, but you can also attach metadata directly to runs so production debugging is easier.
from langsmith import Client
client = Client()
input_state = {
"customer_id": "cus_123",
"amount": 49.99,
"currency": "USD",
"authorized": False,
"payment_id": None,
"status": "pending",
}
result = app.invoke(
input_state,
config={
"run_name": "payment_flow_v1",
"tags": ["payments", "production"],
"metadata": {
"service": "checkout-agent",
"environment": "prod",
"workflow": "authorize_and_capture",
},
},
)
That config object is what makes this useful in production. When something fails later, you can search by tags or metadata in LangSmith and inspect exactly which node broke.
5. Send custom events or feedback to LangSmith
For production systems, tracing alone is not enough. Record feedback or annotations when a human reviews a transaction outcome.
run = client.create_run(
name="manual_review_payment",
run_type="chain",
inputs={"payment_id": result["payment_id"]},
)
client.update_run(
run.id,
outputs={"review_status": "approved"},
)
If you already have an approval workflow outside the graph, this pattern keeps audit data attached to the same operational timeline as your agent runs.
Testing the Integration
Run a local invocation and verify that both the workflow and tracing behave correctly.
test_state = {
"customer_id": "cus_test_001",
"amount": 19.95,
"currency": "USD",
"authorized": False,
"payment_id": None,
"status": "pending",
}
output = app.invoke(
test_state,
config={
"run_name": "integration_test_payment_flow",
"tags": ["test"],
"metadata": {"env": "local"},
},
)
print(output)
Expected output:
{
'customer_id': 'cus_test_001',
'amount': 19.95,
'currency': 'USD',
'authorized': True,
'payment_id': 'pay_8f3a12b4c91d',
'status': 'captured'
}
In LangSmith, you should see one trace for the graph run with child steps for authorize and capture.
Real-World Use Cases
- •
Payment exception handling agents
Build an agent that authorizes a charge, detects failures like card declines or timeout errors, then routes to retry logic or human review while keeping full traces in LangSmith. - •
Subscription billing assistants
Use LangGraph to manage recurring billing flows and cancellation logic, then use LangSmith to monitor prompt behavior and tool reliability across thousands of runs. - •
Chargeback investigation workflows
Combine graph-based decisioning with traced tool calls so support teams can inspect why a transaction was flagged and what data influenced the outcome.
The main pattern here is straightforward: keep business logic in LangGraph nodes and keep observability in LangSmith. That separation gives you controllable payment automation without losing the audit trail you need in production.
Keep learning
- •The complete AI Agents Roadmap — my full 8-step breakdown
- •Free: The AI Agent Starter Kit — PDF checklist + starter code
- •Work with me — I build AI for banks and insurance companies
By Cyprian Aarons, AI Consultant at Topiax.
Want the complete 8-step roadmap?
Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.
Get the Starter Kit