X402 exact and upto Billing Schemes
Ace Data Cloud X402 currently uses two types of schemes: exact and upto. They solve different billing problems.
¶ exact
exact means that the price can be determined before this request enters the target API. The amount signed by the client is the final charged amount.
Suitable for:
- Fixed-price image generation;
- Fixed-price video task creation;
- Fixed-price search or tool APIs;
- Order payments.
EVM exact uses USDC EIP-3009 TransferWithAuthorization:
{
"x402Version": 2,
"accepted": {
"scheme": "exact",
"network": "eip155:8453"
},
"payload": {
"authorization": {
"from": "0x...",
"to": "0x...",
"value": "95215",
"validAfter": "1780237345",
"validBefore": "1780240945",
"nonce": "0x..."
},
"signature": "0x..."
}
}
The Facilitator verifies the signature and amount during the /verify stage, and submits this authorization on-chain during the /settle stage.
¶ upto
upto means that the client authorizes a maximum ceiling, and Ace Data Cloud settles according to actual usage after the request is completed. The actual charge cannot exceed the ceiling.
Suitable for:
- Chat completions: the final price depends on prompt tokens and completion tokens;
- Streaming responses: the actual output length is only known after it ends;
- Future post-metering APIs.
upto uses Permit2 PermitWitnessTransferFrom. What the client signs is not a fixed transfer, but a ceiling authorization with a witness:
{
"x402Version": 2,
"accepted": {
"scheme": "upto",
"network": "eip155:8453"
},
"payload": {
"permit2Authorization": {
"from": "0x...",
"spender": "0x4020A4f3b7b90ccA423B9fabCc0CE57C6C240002",
"nonce": "123456789",
"deadline": "1780240945",
"permitted": {
"token": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
"amount": "95215"
},
"witness": {
"to": "0x...",
"facilitator": "0x...",
"validAfter": "1780237345"
}
},
"signature": "0x..."
}
}
permitted.amount is the ceiling and is not necessarily the final charge. During the /record stage, the Gateway converts actual usage into amount and passes it to the Facilitator. The Facilitator only allows settlement when amount <= permitted.amount.
The program execution result for Base upto:
payer 0x5d4f08D5c2bb60703284bc06671Eb680fA41B105
elapsed_ms 5104
content ADC_BASE_UPTO_OK
id chatcmpl-DlcbyS4IT8kUAMo4Ri97HiIHc9T8V
tx 0x4b0b836ce1cd1171cdbc37df1637150b024214ec28e7f6f2d09122f15cbfc036
block 46726437
explorer https://basescan.org/tx/0x4b0b836ce1cd1171cdbc37df1637150b024214ec28e7f6f2d09122f15cbfc036
signed ceiling 95215 atomic USDC
transfer value 3 atomic USDC
Explanation:
- The authorization ceiling returned in 402 is
95215atomic USDC, and the client signs according to this ceiling. - Only
3atomic USDC is settled after the actual model response, and the on-chain transaction can be found on BaseScan. - This result demonstrates the key difference of
upto: the signed amount is a ceiling, and the on-chain settlement can be less than the ceiling. - If actual usage exceeds the ceiling, the Facilitator should reject settlement, and the client needs to authorize again with a higher ceiling.
upto is currently available only on Base. When post-metering is required, please use Base.
¶ Why Permit2 approve is required
upto is ultimately pulled from the payer wallet in USDC by the x402 proxy through Permit2. Before the first use, the payer wallet needs to give Permit2 a one-time ERC-20 allowance.
Python CLI:
pip install 'acedatacloud-x402[cli]'
X402_PRIVATE_KEY=0x... acedatacloud-x402 approve-permit2 --network base
Programmatic approach:
from acedatacloud_x402 import EVMAccountSigner, approve_permit2
approve_permit2(
rpc_url="https://mainnet.base.org",
signer=EVMAccountSigner.from_private_key("0x..."),
token_address="0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
)
After authorization is completed, each request still needs to sign a new upto envelope, because the nonce, deadline, witness, and amount ceiling are all different.
¶ Zero-amount settlement
upto supports cases where the actual amount is 0. For example, if the target API does not successfully generate billable usage, the Gateway can pass in amount = "0". The Facilitator will return success but will not send an on-chain transaction.
This avoids the issue of “the request was unsuccessful but on-chain fees were still charged.”
¶ Selection recommendations
| Scenario | Recommendation |
|---|---|
| Fixed-price API | Use exact; the logic is simple. |
| Order payment | Use exact. |
| Chat completions, token-based billing | Use Base upto. |
| Permit2 approve has not been completed | First use exact to get it working, then switch to upto. |
If you are not sure which one to choose, first use the SDK default behavior; the SDK will select the matching network payment requirement returned by the server.
¶ Base upto Checklist
When integrating or troubleshooting, please confirm that the following parameters come from the same 402 response and remain consistent when signed by the client:
| Parameter | Checkpoint |
|---|---|
network |
Must be eip155:8453. |
scheme |
Must be upto. |
extra.chainId |
The Base chain id is 8453. |
asset |
Use the Base USDC contract address from the 402 response. |
extra.facilitatorAddress |
Must participate in the witness and match Facilitator /supported. |
| Permit2 allowance | The payer wallet needs to authorize Permit2 for Base USDC first. |
Common errors and handling methods:
| Error | Handling method |
|---|---|
invalid_upto_evm_payload_invalid_signature |
Check whether the chain id, facilitator address, Permit2 domain, spender, signing account, and witness match the 402 response. |
PERMIT2_ALLOWANCE_REQUIRED |
Execute Permit2 approve for USDC on the target chain and initiate the request again. |
amount exceeds permitted amount |
Actual usage exceeds the signed ceiling; sign again with a higher ceiling. |
