Reference for the AgentPayClient.consume() method, detailing parameters, return object, and idempotency for charging users.
consume()
method is the core function in the AgentPay Python SDK for charging a user for their interaction with your MCP service.
agentpay-sdk
package currently on PyPI is a placeholder to reserve the name during Early Access. To get the actual SDK now, join the Waitlist.api_key
(str, required):
amount_cents
(int, required):
50
.0
to track usage without charging (e.g., for free operations).usage_event_id
(str, required):
consume()
call multiple times with the same usage_event_id
(e.g., due to network retries on your end), AgentPay will only process the charge once, preventing duplicate billing for the same underlying user action.ConsumptionResult
)consume()
method returns a ConsumptionResult
object with the following attributes:
success
(bool):
True
if the consumption was successful and the user was charged.False
if the consumption failed (e.g., due to insufficient funds, invalid API key).remaining_balance_cents
(int | None):
success
is True
, this attribute contains the user’s remaining balance in cents after the current charge has been deducted.None
if success
is False
, or if balance information is not applicable to the type of failure.error_message
(str | None):
success
is False
, this provides a human-readable message describing the reason for failure (e.g., “Insufficient funds.”, “API key is invalid or expired.”).None
if success
is True
.error_code
(str | None):
success
is False
, this provides a short, machine-readable error code (e.g., INSUFFICIENT_FUNDS
, INVALID_API_KEY
, USAGE_EVENT_ID_CONFLICT
).None
if success
is True
.AgentPayError
: If there is an error communicating with the AgentPay serviceValueError
: If any required parameter is empty or Nonesuccess
is False
, the error_code
field will contain one of the following values:
"INVALID_API_KEY"
: The provided API key is not recognized or has been revoked/deactivated"OUTSTANDING_PAYMENT"
: The key is associated with an account that has an unpaid consumption attempt from a previous failed call"INSUFFICIENT_FUNDS"
: The key is valid but the associated account has no remaining balance (including free credits)"USAGE_EVENT_ID_CONFLICT"
: The same usage_event_id
was used in a previous successful consumption call