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"]
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.
agent.invoke(
{"messages": [...]},
config={"configurable": {"payment_token": token}},
)| src/agent.py | the agent + the single @requires_payment-protected tool |
| src/buyer.py | buyer that hits the no-token path, then pays and retries |
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.