How to Integrate LangGraph for payments with LangSmith for startups

By Cyprian AaronsUpdated 2026-04-22
langgraph-for-paymentslangsmithstartups

Combining LangGraph for payments with LangSmith gives you a practical setup for building payment-aware AI agents that you can actually debug in production. LangGraph handles the stateful payment workflow, while LangSmith gives you tracing, observability, and evaluation so you can see exactly where an agent approved, declined, or retried a transaction.

Prerequisites

  • Python 3.10+
  • A LangGraph project set up with a payment workflow
  • A LangSmith account and API key
  • Environment variables configured:
    • LANGSMITH_API_KEY
    • LANGSMITH_TRACING=true
    • LANGCHAIN_PROJECT=payments-agent or similar
  • Installed packages:
    • langgraph
    • langchain
    • langsmith
  • A payment provider SDK or mock service if you want to simulate charges

Integration Steps

  1. Install the required packages

    Start by installing the core libraries for graph execution and tracing.

    pip install langgraph langchain langsmith
    

    If your payment flow calls an external processor, install that SDK too. For example:

    pip install stripe
    
  2. Configure LangSmith tracing

    LangSmith works best when tracing is enabled at the process level. Set your environment variables before running the agent.

    import os
    
    os.environ["LANGSMITH_API_KEY"] = "lsv2_your_api_key"
    os.environ["LANGSMITH_TRACING"] = "true"
    os.environ["LANGCHAIN_PROJECT"] = "startup-payments-agent"
    

    If you prefer .env, load it early in your app entrypoint:

    from dotenv import load_dotenv
    
    load_dotenv()
    
  3. Build a LangGraph payment workflow

    Model payments as state transitions. That lets you keep authorization, validation, and confirmation separate, which is what you want in a startup system that may need retries and human review.

    from typing import TypedDict, Literal
    from langgraph.graph import StateGraph, START, END
    
    class PaymentState(TypedDict):
        user_id: str
        amount: float
        currency: str
        status: Literal["pending", "authorized", "declined", "captured"]
        reason: str
    
    def validate_payment(state: PaymentState) -> PaymentState:
        if state["amount"] <= 0:
            return {**state, "status": "declined", "reason": "Invalid amount"}
        return {**state, "status": "authorized", "reason": ""}
    
    def capture_payment(state: PaymentState) -> PaymentState:
        # Replace with real payment provider call
        return {**state, "status": "captured", "reason": ""}
    
    graph = StateGraph(PaymentState)
    graph.add_node("validate_payment", validate_payment)
    graph.add_node("capture_payment", capture_payment)
    
    graph.add_edge(START, "validate_payment")
    graph.add_conditional_edges(
        "validate_payment",
        lambda s: END if s["status"] == "declined" else "capture_payment",
        {
            END: END,
            "capture_payment": "capture_payment",
        },
    )
    graph.add_edge("capture_payment", END)
    
    app = graph.compile()
    
  4. Attach LangSmith tracing to the graph run

    LangGraph runs can be traced through LangSmith automatically when the environment is set correctly. For explicit control, use Client and create runs around critical steps like payment authorization and capture.

    from langsmith import Client
    
    client = Client()
    
    run = client.create_run(
        name="payment-workflow",
        run_type="chain",
        inputs={
            "user_id": "user_123",
            "amount": 49.99,
            "currency": "USD",
        },
        project_name="startup-payments-agent",
    )
    
     # execute the graph after creating the trace context
     result = app.invoke(
         {
             "user_id": "user_123",
             "amount": 49.99,
             "currency": "USD",
             "status": "pending",
             "reason": "",
         }
     )
    
     client.update_run(
         run_id=run.id,
         outputs=result,
         end_time=None,
     )
    
  5. Trace individual tool calls inside the payment flow

    This is where LangSmith becomes useful. Don’t just trace the whole graph; trace each external call so failures are easy to isolate.

    from langsmith import traceable
    
    @traceable(name="capture_with_provider")
    def capture_with_provider(amount: float, currency: str) -> dict:
        # Example placeholder for Stripe or another PSP
        return {
            "provider_status": "succeeded",
            "provider_reference": f"pay_{int(amount * 100)}_{currency.lower()}",
        }
    
    def capture_payment(state: PaymentState) -> PaymentState:
        response = capture_with_provider(state["amount"], state["currency"])
        if response["provider_status"] != "succeeded":
            return {**state, "status": "declined", "reason": "Provider rejected charge"}
        return {**state, "status": "captured", "reason": ""}
    

Testing the Integration

Run a local invocation and confirm that both the state transition and the trace show up.

result = app.invoke(
    {
        "user_id": "user_123",
        "amount": 19.99,
        "currency": "USD",
        "status": "pending",
        "reason": "",
    }
)

print(result)

Expected output:

{
  'user_id': 'user_123',
  'amount': 19.99,
  'currency': 'USD',
  'status': 'captured',
  'reason': ''
}

In LangSmith, you should see a run named payment-workflow, plus child traces for any decorated tool calls like capture_with_provider.

Real-World Use Cases

  • Subscription checkout agents
    Build an agent that validates plan selection, checks billing rules, captures payment, and records every decision in LangSmith.

  • Refund approval workflows
    Route refund requests through a LangGraph state machine with policy checks, then trace each approval step for auditability.

  • Invoice collection assistants
    Let an AI agent negotiate reminders or partial payments while keeping every external API call and decision path visible in LangSmith.


Keep learning

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

Related Guides