AI 에이전트가 API 호출당 과금을 구현한 방법 (HTTP 402)

발행: (2026년 6월 18일 AM 03:07 GMT+9)
3 분 소요
원문: Dev.to

Source: Dev.to

If you’ve built an MCP server or any API that costs money to run (an LLM call, a paid data source, compute), you’ve probably hit the same wall I did: How do you get paid per call — when the caller is an AI agent, not a human with a credit card form? A human can sign up, enter a card, get an API key. An autonomous agent can’t fill out a Stripe checkout form mid-task. And you don’t want to hand an agent a raw API key with no spending limit — one runaway loop and your bill explodes. This post walks through the design I landed on. It’s not the only way, but the pieces are reusable even if you build your own. The core idea: HTTP 402 The flow: An agent calls your endpoint with no payment. { “accepts”: [ 예산 한도는 중요한 부분입니다 { “budget”: 5.00, JWT 토큰을 HS256 서명된 것으로 인코딩하여 핫 경로에서 DB 라운드‑트립 없이 게이트웨이가 검증할 수 있게 합니다. 그리고 PostgreSQL의 실시간 지출 카운터를 확인합니다. JWT에는 토큰 ID, 엔드포인트 ID, 소유자 ID가 담겨 있으며, 가변 예산은 DB에 저장됩니다. 게이트웨이 패턴 pay 토큰을 검증합니다. 돈을 정산합니다. 이 방법이 중요한 이유는 매 호출마다 수수료를 청arging하면 Stripe의 최소 거래 금액 때문에 미세 호출이 비용 효율성을 잃게 됩니다. 사전 지불 번들 + ledger 인출은 이를 완전히 피할 수 있습니다. What I’d tell you before you build your own Don’t put the fee on each call. Stripe’s minimum charge makes sub-cent per-call billing impossible. Prepay a budget, draw it down in your own ledger. https://www.lemoncake.xyz/demo But honestly, even if you never touch it, the pattern stands on its own: 402 to advertise the price → budget-capped token → gateway that verifies, forwards, and meters. I’m still genuinely unsure whether autonomous per-call payment is something agent builders need today or whether I’m a year or two early. If you’ve hit the “how do i charge an agent” problem from the other side, I’d love to hear how you solved it.

0 조회
Back to Blog

관련 글

더 보기 »