How to Integrate LangGraph for payments with LangSmith for AI agents
Combining LangGraph for payments with LangSmith gives you a clean way to build AI agents that can initiate payment flows, route exceptions, and still give you full traceability across every decision. The practical win is simple: you get a graph-based payment workflow with observability, debugging, and evaluation in the same system.
Prerequisites
- •Python 3.10+
- •A LangGraph setup that includes your payment workflow nodes
- •A LangSmith account and project
- •API keys configured in your environment:
- •
LANGSMITH_API_KEY - •
LANGSMITH_PROJECT - •
LANGSMITH_TRACING=true
- •
- •Installed packages:
- •
langgraph - •
langsmith - •
langchain-coreor compatible dependencies
- •
- •A payment provider sandbox or mock payment service for testing
Integration Steps
- •
Install the dependencies
Start by installing the core packages you need for graph orchestration and tracing.
pip install langgraph langsmith langchain-coreIf your payment flow uses a provider SDK, install that too. Keep the payment API in sandbox mode until tracing is verified.
- •
Configure LangSmith tracing
LangSmith works best when tracing is enabled at the environment level. This lets every graph run, node execution, and tool call show up in your project automatically.
import os os.environ["LANGSMITH_API_KEY"] = "lsv2-your-key" os.environ["LANGSMITH_PROJECT"] = "payments-agent" os.environ["LANGSMITH_TRACING"] = "true"If you prefer explicit client configuration, use the SDK directly:
from langsmith import Client client = Client( api_key="lsv2-your-key", api_url="https://api.smith.langchain.com", workspace_id=None, ) - •
Build the LangGraph payment workflow
Model payments as a state machine. That gives you deterministic routing for approval, retry, failure handling, and audit logging.
from typing import TypedDict, Literal from langgraph.graph import StateGraph, END class PaymentState(TypedDict): user_id: str amount: float currency: str status: str payment_id: str | None def validate_payment(state: PaymentState) -> PaymentState: if state["amount"] <= 0: return {**state, "status": "rejected"} return {**state, "status": "validated"} def create_payment(state: PaymentState) -> PaymentState: # Replace with your real PSP call payment_id = f"pay_{state['user_id']}_{int(state['amount'] * 100)}" return {**state, "payment_id": payment_id, "status": "created"} def route_payment(state: PaymentState) -> Literal["create_payment", "__end__"]: return "create_payment" if state["status"] == "validated" else "__end__" graph = StateGraph(PaymentState) graph.add_node("validate_payment", validate_payment) graph.add_node("create_payment", create_payment) graph.set_entry_point("validate_payment") graph.add_conditional_edges("validate_payment", route_payment) graph.add_edge("create_payment", END) app = graph.compile() - •
Attach LangSmith tracing to each run
The easiest path is to wrap execution in a traced context so the full transaction appears in LangSmith. For production systems, this is where you add metadata like customer segment, environment, or payment channel.
from langsmith.run_helpers import traceable @traceable(name="payment_agent_run") def run_payment_flow(user_id: str, amount: float, currency: str): initial_state = { "user_id": user_id, "amount": amount, "currency": currency, "status": "pending", "payment_id": None, } return app.invoke(initial_state) result = run_payment_flow("cust_123", 49.99, "USD") print(result) - •
Log important events as traced operations
For banking and insurance workflows, node-level visibility matters more than just final output. Use explicit traced functions around sensitive operations like authorization checks or provider calls.
from langsmith.run_helpers import traceable @traceable(name="authorize_payment") def authorize_payment(amount: float) -> bool: return amount < 1000 def create_payment(state: PaymentState) -> PaymentState: authorized = authorize_payment(state["amount"]) if not authorized: return {**state, "status": "declined", "payment_id": None} payment_id = f"pay_{state['user_id']}_{int(state['amount'] * 100)}" return {**state, "payment_id": payment_id, "status": "created"}
Testing the Integration
Run a sample transaction and verify two things:
- •The graph returns the expected payment state
- •The trace appears in your LangSmith project with the correct run name and metadata
if __name__ == "__main__":
output = run_payment_flow("cust_456", 25.50, "USD")
print("Final output:", output)
Expected output:
Final output: {'user_id': 'cust_456', 'amount': 25.5, 'currency': 'USD', 'status': 'created', 'payment_id': 'pay_cust_456_2550'}
In LangSmith, you should see:
- •One top-level run named
payment_agent_run - •Child traces for traced functions like
authorize_payment - •Input/output payloads for each step
- •Execution timing for each node
Real-World Use Cases
- •
Payment approval agents
- •Route low-risk payments automatically.
- •Escalate high-value transactions to human review.
- •Use LangSmith traces to debug false declines.
- •
Refund orchestration
- •Build a graph that checks eligibility, verifies original charge data, and triggers refund creation.
- •Trace every branch so ops teams can audit why a refund was approved or denied.
- •
Insurance disbursement workflows
- •Let an agent validate claim payout conditions before initiating a payout.
- •Use LangGraph for deterministic branching and LangSmith for post-incident analysis when a payout fails.
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