01 / ENGINEERING CASE STUDY · AI systems
Autonomous Billing Proxy
I built a FastAPI gateway that returns API responses without waiting for verification, evaluates their usefulness separately, and records approved charges in an atomic wallet ledger.
AT A GLANCE
- The problem
- An API response can be empty or unusable and still incur a charge. Agents need billing tied to useful results.
- What I built
- I built the FastAPI gateway, Gemini-backed verifier, and Postgres wallet and ledger flow.
- The result
- Responses return before verification finishes. Approved charges update the wallet and ledger in one transaction.
01 / CONTEXT
The problem
An HTTP request can return an empty response, a bot challenge, or irrelevant content and still count as billable traffic. I built this prototype to explore charging an AI agent only after the response is judged useful, while keeping verification off the agent’s response path.
02 / SYSTEM DESIGN
Architecture
Return the response; evaluate the charge separately
- Gemini structured judgment + heuristic fallback
- Failed verification: no charge
- Next.js dashboard: tokens, balance, history
03 / TRADEOFFS
Engineering decisions
Keep verification off the response path
I moved verification into asynchronous tasks so the agent does not wait for model scoring and billing. The current client is fire-and-forget, so an unreachable verifier can mean dropped telemetry. I need a durable queue before relying on this flow for revenue.
Asynchronous verification clientMake a charge explainable
I use Gemini for structured judgments and local heuristics as a fallback when the model is unavailable. Status, body coherence, and failure signals inform the decision. Scores and proof metadata accompany ledger entries so I can trace why a request was charged.
Structured verification judgeCommit the debit and ledger entry together
I put the wallet debit and ledger insertion in one PL/pgSQL transaction, with a rollback if the balance is insufficient. The request-hash precheck helps identify repeated executions, but concurrent duplicates still need stronger guarantees. My next iteration needs concurrency-safe idempotency and tests that exercise retries.
Atomic billing functionTreat the gateway as an enforcement boundary
I store bearer tokens as SHA-256 hashes and use session checks, request fingerprints, per-developer rate limits, and bounded telemetry to constrain what reaches downstream services. Distributed rate limiting is still needed before running the gateway across multiple instances.
Gateway implementationConnect usage to the developer wallet
I built a Next.js and TypeScript dashboard for bearer-token creation, balances, and transaction history. Stripe Checkout handles wallet top-ups; payment webhooks connect completed checkouts to account credits.
Stripe Checkout integration04 / DEBUGGING
A high score could hide a failed request
05 / VALIDATION
Evidence & measurement
The gateway, verifier, and atomic billing flow are implemented. Evaluator tests exercise billing decisions, and a local simulation script sends a request through the gateway and polls for its ledger entry.
End-to-end simulation
The script seeds test fixtures, makes one proxied request, and checks for a transaction. It is a functional walkthrough, not a load test or proof of retry safety.
View supporting referenceAtomic wallet + ledger writes
The database function combines wallet debit and ledger insertion in one transaction and rejects insufficient balances.
View supporting referenceEvaluator coverage
Pytest cases cover HTTP failures, empty bodies, refusal signals, and the composite decision pipeline.
View supporting referenceBenchmark notes
These reported figures do not have published benchmark or load-test results. Validating them requires measurements of the complete path, including successful writes and reconciliation between wallets and the ledger.
| Figure | What I need to document |
|---|---|
| <50 ms proxy overhead | A direct-versus-proxied baseline, payload sizes, concurrency, hardware, and latency percentiles. The gateway’s upstream timer alone does not measure proxy overhead. |
| 100K+ micro-transactions | Run duration, transaction rate, environment, completed ledger writes, failures, and wallet-to-ledger reconciliation results. |
06 / NEXT ITERATION
What I’d improve next
Code links point to revision 43cf21d of my billing gateway.