ATPSettlementMiddleware is a FastAPI middleware that automatically deducts payment from Solana wallets based on token usage for configured endpoints. It delegates all settlement logic to the ATP Settlement Service, ensuring immutable and centralized settlement operations.
Overview
The middleware intercepts responses from specified endpoints, extracts usage information (input/output tokens), calculates payment amounts, and executes Solana blockchain transactions to deduct payment before returning the response to the client.Key Features
- Automatic payment deduction based on token usage
- Response encryption until payment confirmation
- Support for multiple usage data formats (OpenAI, Anthropic, Google, etc.)
- Automatic payment splitting (treasury fee + recipient payment)
- Centralized settlement logic via settlement service
- Configurable error handling modes
Installation
Basic Usage
Configuration Parameters
Required Parameters
allowed_endpoints
- Type:
List[str] - Description: List of endpoint paths to apply settlement to (e.g.,
["/v1/chat"]). Supports exact path matches only. - Example:
["/v1/chat", "/v1/completions"]
input_cost_per_million_usd
- Type:
float - Description: Cost per million input tokens in USD.
- Example:
10.0(means $10 per million input tokens)
output_cost_per_million_usd
- Type:
float - Description: Cost per million output tokens in USD.
- Example:
30.0(means $30 per million output tokens)
recipient_pubkey
- Type:
str - Description: Solana public key of the recipient wallet (the endpoint host). This wallet receives the main payment (after processing fee). Required.
- Example:
"YourSolanaWalletPublicKeyHere"
Optional Parameters
wallet_private_key_header
- Type:
str - Default:
"x-wallet-private-key" - Description: HTTP header name containing the wallet private key. The private key should be in JSON array format (e.g.,
"[1,2,3,...]") or base58 string format.
payment_token
- Type:
PaymentToken - Default:
PaymentToken.SOL - Description: Token to use for payment (SOL or USDC).
- Options:
PaymentToken.SOL,PaymentToken.USDC
skip_preflight
- Type:
bool - Default:
False - Description: Whether to skip preflight simulation for Solana transactions. Setting to
Truecan speed up transactions but may result in failed transactions.
commitment
- Type:
str - Default:
"confirmed" - Description: Solana commitment level for transaction confirmation.
- Options:
"processed": Fastest, but may be rolled back"confirmed": Recommended default, confirmed by cluster"finalized": Slowest, but cannot be rolled back
require_wallet
- Type:
bool - Default:
True - Description: Whether to require wallet private key. If
False, skips settlement when missing.
settlement_service_url
- Type:
Optional[str] - Default:
None(usesATP_SETTLEMENT_URLenvironment variable orhttps://facilitator.swarms.world/) - Description: Base URL of the settlement service. The middleware always uses the settlement service for all settlement operations.
fail_on_settlement_error
- Type:
bool - Default:
False - Description: If
True, raisesHTTPExceptionwhen settlement fails. IfFalse, returns the response with settlement error info instead of failing the request.
settlement_timeout
- Type:
Optional[float] - Default:
None(usesATP_SETTLEMENT_TIMEOUTenvironment variable or300.0) - Description: Timeout in seconds for settlement service requests. Settlement operations may take longer due to blockchain confirmation times. Increase this value if you experience timeout errors even when payments are successfully sent.
Request Flow
- Request Arrives: Request arrives at a configured endpoint
- Wallet Extraction: Middleware extracts wallet private key from request headers
- Endpoint Execution: Request is forwarded to the endpoint handler
- Response Interception: Response is intercepted and parsed for usage data
- Response Encryption: Response is encrypted to prevent unauthorized access
- Usage Parsing: Usage data is sent to settlement service for parsing and payment calculation
- Payment Execution: Payment transaction is executed on Solana blockchain
- Response Decryption: Response is decrypted only after payment confirmation
- Response Return: Response is returned to client with settlement details
Response Modifications
The middleware automatically adds the following fields to responses:atp_usage
Normalized usage data with standard keys:
atp_settlement
Settlement details including transaction signature and payment breakdown:
atp_settlement_status
Status of settlement:
"paid": Payment succeeded and confirmed"failed": Payment failed"skipped": Payment was skipped (zero cost)
atp_message
Informational message about response encryption status:
Usage Data Parsing
The middleware sends the entire response body to the settlement service’s/v1/settlement/parse-usage endpoint, which automatically handles:
- Multiple API formats (OpenAI, Anthropic, Google/Gemini, Cohere, etc.)
- Nested structures (usage.usage, meta.usage, statistics, etc.)
- Recursive parsing for deeply nested usage objects
- Normalization to standard format (input_tokens, output_tokens, total_tokens)
Supported Formats
OpenAI Format
Anthropic Format
Google/Gemini Format
Nested Formats
Security Features
Response Encryption
Agent responses are encrypted before payment verification, ensuring users cannot see output until payment is confirmed. The middleware encrypts common output fields:outputresponseresultmessage
ATP_ENCRYPTION_KEY environment variable.
Payment Verification
Responses are only decrypted after successful blockchain transaction confirmation:- Status must be
"paid" - Transaction signature must be present
- Transaction must be confirmed on-chain
Error Handling
Failed payments result in encrypted responses with error details, preventing unauthorized access to agent output.Error Handling Modes
fail_on_settlement_error=False (Default)
Returns encrypted response with settlement error details. Useful for debugging and graceful degradation.
Example response on payment failure:
fail_on_settlement_error=True
Raises HTTPException when settlement fails. Useful for strict payment requirements.
Example exception:
Payment Splitting
Payments are automatically split between:- Treasury: Receives the processing fee (configured via
SWARMS_TREASURY_PUBKEYon settlement service). Default fee percentage is 5%. - Recipient: Receives the remainder (95% by default). This is the endpoint host’s wallet specified via
recipient_pubkey.
Error Scenarios
Missing Wallet Key
If wallet private key is missing andrequire_wallet=True:
Status: 401 Unauthorized
Response:
No Usage Data
If usage data cannot be parsed from the response: Status: 200 OK (original response returned) Behavior: Original response is returned without settlement. A warning is logged.Encryption Failure
If response encryption fails: Status: 500 Internal Server Error Response:Settlement Failure
If settlement fails andfail_on_settlement_error=False:
Status: 200 OK (with error details)
Response: Encrypted response with settlement error details (see Error Handling Modes above)
If settlement fails and fail_on_settlement_error=True:
Status: HTTP status code from settlement service (typically 400 or 500)
Response: HTTPException with error details
Timeout Errors
Settlement operations may take time due to blockchain confirmation. If a timeout occurs: Status: Depends onfail_on_settlement_error setting
Note: The payment may have been sent successfully. Check the blockchain for transaction confirmation.
Best Practices
- Endpoint Selection: Only add endpoints that return usage data. Health checks and status endpoints should not use the middleware.
-
Error Handling: Use
fail_on_settlement_error=Falsefor graceful degradation. Checkatp_settlement_statusin responses to handle payment failures. -
Timeout Configuration: Increase
settlement_timeoutif you experience timeout errors even when payments succeed. Blockchain confirmation can take 30-60 seconds or more. - Usage Data Accuracy: Always include accurate token counts in your responses. The middleware will skip settlement if usage data cannot be parsed.
- Wallet Security: Never log or persist wallet private keys. They are only used in-memory for transaction signing.
- Testing: Use testnet wallets and small amounts for testing. Verify payment transactions on Solana explorer.
-
Monitoring: Monitor
atp_settlement_statusin responses to track payment success rates and identify issues.
Advanced Usage
Custom Error Handling
You can implement custom error handling by checkingatp_settlement_status in your client code:
Conditional Settlement
Userequire_wallet=False to make settlement optional:
Multiple Endpoints with Different Pricing
You can add multiple middleware instances with different configurations, but it’s simpler to use a single middleware with consistent pricing:Troubleshooting
Payment Timeouts
Problem: Requests timeout even when payments succeed. Solution: Increasesettlement_timeout to allow more time for blockchain confirmation:
Missing Usage Data
Problem: Settlement is skipped even when response contains usage data. Solution: Ensure usage data is in a supported format. Check logs for parsing errors. The middleware logs warnings when usage cannot be parsed.Encrypted Responses
Problem: Responses remain encrypted even after payment. Solution: Checkatp_settlement_status. If status is not "paid", payment may have failed. Check atp_settlement for error details.
Transaction Failures
Problem: Payments fail with transaction errors. Solution: Check settlement service logs. Common issues:- Insufficient funds in wallet
- Invalid recipient pubkey
- Network congestion
- Invalid private key format