cd ../projects
demo

AgentCore Payments for LangChain

Launch sample with AWS for LangChain agents that pay for x402 APIs — budgets enforced by Bedrock AgentCore, every purchase traced and evaluated in LangSmith

$ apt list --installed

Bedrock AgentCoreLangChainLangSmithx402Python
metrics.sh
$ metrics --project=agentcore-payments
> Budget Enforcement: AgentCore infrastructure, not the prompt
> Policy Cases: $1.00, $0.50, and too-small limits
> Test Network: Base Sepolia (test USDC)

The LangChain sample for the Amazon Bedrock AgentCore Payments launch: an agent that pays for x402-paywalled APIs through the AgentCore Payments middleware, plus the LangSmith tracing and evaluation workflow that answers "what did this agent buy, and was it worth it?" It lives in `examples/agentcore-payments`.

How Payments Flow

  • •Detect — a tool hits a paid API that returns HTTP 402 with an x402 payload
  • •Validate — the middleware checks the amount against the session budget; over the limit, nothing is signed
  • •Sign — within budget, AgentCore signs the payment
  • •Retry — the request goes out again with payment proof, and the agent continues as if the API had returned 200 the first time

Because AgentCore enforces the spending limit rather than the prompt, it holds even if the agent is compromised.

What the Sample Covers

  • •A setup notebook that creates the AWS resources and a Coinbase test wallet funded with free test USDC on Base Sepolia, so nothing spends real money; other payment providers are documented as drop-in options
  • •An agent notebook that traces one paid request end to end, then stores three policy cases — a $1.00 limit, a $0.50 limit, and a limit that's intentionally too small — as a LangSmith dataset
  • •Policy evaluators and an LLM judge score each run, and two prompt variants are compared like production candidates, following the build-and-test stages of LangChain's Agent Development Lifecycle
  • •Every run traced and tagged with its network, scenario, and budget, so the paid call — the 402, then the retry with payment attached — sits next to the reasoning behind it, context a payment ledger never has

Writing