LangChainpylive · you pay per call

Gate one LangChain tool with @requires_payment

The minimal case: a single LangChain/LangGraph tool gated by Nevermined payments. No HTTP layer, no 402 round-trip — the buyer threads a token in-process.

langchain-paid-agent-py/
1 What you'll learn

The payment flow with everything else stripped away, so it's the only signal.

  • Protect a LangChain @tool by wrapping it with @requires_payment
  • Acquire an x402 access token with payments.x402.get_x402_access_token(plan_id=...)
  • Thread the token through agent.invoke(..., config={"configurable": {"payment_token": ...}})
  • Read the settlement receipt back from configurable["payment_settlement"]
2 How it works

It mirrors the x402 HTTP discovery pattern, in-process: the buyer invokes the agent with no token, the protected tool raises PaymentRequiredError carrying the full accepts block (scheme, network, plan id), and the buyer uses that to acquire a token before retrying.

No plan id, scheme, or provider has to be configured on the buyer up front — the error tells it everything it needs.

invokeno token
PaymentRequiredErroraccepts block
get tokenfor that plan
invokewith token
3 Under the hood
create_react_agentLangGraphpayments-py[langchain]OpenAI gpt-4o-mini
# the buyer retries with the token in configurable
agent.invoke(
    {"messages": [...]},
    config={"configurable": {"payment_token": token}},
)
src/agent.pythe agent + the single @requires_payment-protected tool
src/buyer.pybuyer that hits the no-token path, then pays and retries
4 See it run
Gate one LangChain tool with @requires_payment

Sandbox agent — real payment round-trips (402 → authorize → settle) with a per-session credit balance, no external service and no real money. This tutorial runs entirely in-process (no server). The live panel mirrors the buyer script's two-phase call. Point it at your own account by following the README.