Skip to main content

QWED-UCP

Verify AI-driven commerce transactions before they reach payment.

PyPI version Tests License


What is QWED-UCP?​

QWED-UCP provides deterministic verification guards for the Universal Commerce Protocol (UCP) - Google's open standard for AI-driven commerce.

The Problem​

When AI agents shop on behalf of users, they can make calculation errors that result in:

  • πŸ’Έ Wrong totals - Customers overcharged or undercharged
  • πŸ“‰ Bad discounts - Percentage calculations off
  • 🧾 Incorrect tax - Legal compliance issues
  • πŸ’± Currency errors - International payment failures

The Solution​

QWED-UCP intercepts checkout requests and mathematically verifies every calculation before payment:

AI Agent β†’ UCP Checkout β†’ QWED-UCP Guard β†’ Payment Gateway
β”‚
βœ… Pass β†’ Continue
❌ Fail β†’ Block + Error

How It Works​

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ AI Shopping Agent β”‚
β”‚ (Claude, GPT, etc.) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β”‚ UCP Checkout Request
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ QWED-UCP Middleware β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ Money Guard β”‚ β”‚ State Guard β”‚ β”‚ Line Items Guard β”‚ β”‚
β”‚ β”‚ β‚Ή+$+Β₯ β”‚ β”‚ β†’ β†’ β†’ β†’ β”‚ β”‚ qty Γ— price = total β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚ β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚Discount Guardβ”‚ β”‚Currency Guardβ”‚ β”‚ Schema Guard β”‚ β”‚
β”‚ β”‚ 10% = 10 β”‚ β”‚ USD/EUR/JPY β”‚ β”‚ JSON Validation β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚ β”‚
β”‚ All Guards Pass? β”‚
β”‚ YES ─────┬───── NO β”‚
β”‚ β”‚ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β–Ό β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Payment β”‚ β”‚ 422 Error β”‚
β”‚ Gateway β”‚ β”‚ + Details β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

The 6 Guards​

GuardWhat It VerifiesError When Wrong
Money Guardtotal = subtotal - discount + tax"Calculated 98.25, Agent claimed 100.00" (Checked via SymPy)
State GuardValid checkout state transitions"Invalid transition: completed β†’ incomplete" (Checked via Z3 Solver)
Schema GuardUCP JSON schema compliance"Missing required field: currency"
Line Items Guardprice Γ— quantity = line_total"Line item mismatch: 2 Γ— $35 β‰  $65"
Discount GuardPercentage and fixed discount math"10% of $100 should be $10, not $15"
Currency GuardISO 4217 codes, JPY no-decimals"JPY cannot have decimal amounts"

Installation​

Python (PyPI)​

pip install qwed-ucp

Node.js (npm)​

npm install qwed-ucp-middleware

Quick Start​

Basic Verification​

from qwed_ucp import UCPVerifier

verifier = UCPVerifier()

# Verify a checkout
result = verifier.verify_checkout({
"currency": "USD",
"status": "ready_for_complete",
"line_items": [
{"id": "roses", "quantity": 2, "item": {"price": 35.00}},
{"id": "vase", "quantity": 1, "item": {"price": 15.00}}
],
"totals": [
{"type": "subtotal", "amount": 85.00}, # 2Γ—35 + 1Γ—15 = 85 βœ“
{"type": "discount", "amount": 8.50}, # 10% off
{"type": "tax", "amount": 6.29}, # 8.2% tax on $76.50
{"type": "total", "amount": 82.79} # 85 - 8.50 + 6.29 = 82.79 βœ“
]
})

if result.verified:
print("βœ… Checkout verified! Safe to proceed to payment.")
else:
print(f"❌ Verification failed: {result.error}")
print(f" Guard: {result.failed_guard}")

Middleware Integration (FastAPI)​

from fastapi import FastAPI
from qwed_ucp.middleware.fastapi import QWEDUCPMiddleware

app = FastAPI()

# Add QWED-UCP middleware - automatically verifies all /checkout endpoints
app.add_middleware(QWEDUCPMiddleware)

@app.post("/checkout")
async def checkout(request: CheckoutRequest):
# If we get here, QWED-UCP already verified the math!
return {"status": "completed", "order_id": "ORD-123"}

Middleware Integration (Express.js)​

const express = require('express');
const { createQWEDUCPMiddleware } = require('qwed-ucp-middleware');

const app = express();

// Add QWED-UCP middleware
app.use('/checkout', createQWEDUCPMiddleware());

app.post('/checkout', (req, res) => {
// If we get here, QWED-UCP already verified the math!
res.json({ status: 'completed', orderId: 'ORD-123' });
});

GitHub Action (CI/CD)​

Use QWED-UCP as a GitHub Action to audit transaction logs in your CI/CD pipeline.

Installation​

Add to your workflow (.github/workflows/commerce-audit.yml):

name: Commerce Audit

on:
push:
paths:
- 'logs/transactions/**'

jobs:
audit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Audit Commerce Transactions
uses: QWED-AI/qwed-ucp@v0.2.0
with:
log_path: logs/transactions/
strict_mode: true

Parameters​

InputDescriptionDefault
log_pathPath to transaction JSON logs./logs/
strict_modeFail on any violationtrue
toleranceRounding tolerance (cents)0.01

What Gets Audited​

The Action scans all .json files in the specified path and verifies:

  • βœ… Line item math: price Γ— quantity = total
  • βœ… Discount calculations
  • βœ… Tax amounts
  • βœ… Currency format (ISO 4217)
  • βœ… No "Penny Slicing" (rounding theft)

Example Output​

πŸ›‘οΈ QWED-UCP Audit: 3 vulnerabilities blocked

1. ❌ Penny Slicing: tx_001.json - Tax $7.99 should be $8.00
2. ❌ Zombie Return: tx_045.json - Return without original order
3. ❌ Phantom Discount: tx_089.json - 15% discount on non-sale item

Action: BLOCKED (Exit Code 1)

Why QWED-UCP?​

Business Impact​

ScenarioWithout QWED-UCPWith QWED-UCP
AI miscalculates 10% discount as 15%Customer overcharged $5❌ Blocked, 422 returned
Tax calculation rounds wrongLegal audit issuesβœ… Caught before payment
Currency format invalidPayment gateway rejectsβœ… Caught at middleware
State transition invalidOrder stuck in limboβœ… Proper error message

ROI Calculation​

For a platform processing 100M transactions/year:

  • Error rate without verification: ~0.1%
  • Errors per year: 100,000 transactions
  • Average error cost: $6.39 (dispute handling + refunds)
  • Potential loss: $638,700/year

QWED-UCP catches these errors before they become expensive problems.


Configuration​

Environment Variables​

VariableDescriptionDefault
QWED_UCP_STRICTFail on any schema mismatchtrue
QWED_UCP_LOG_LEVELLogging levelINFO
QWED_UCP_TOLERANCETolerance for rounding (cents)0.01

Custom Guard Configuration​

from qwed_ucp import UCPVerifier, MoneyGuard

# Custom tolerance for floating-point errors
verifier = UCPVerifier(
money_guard=MoneyGuard(tolerance=0.02), # $0.02 tolerance
strict_mode=False # Allow minor schema violations
)

Next Steps​