Inspiration

I am a CS Professor of AI Software Development. At the end of every semester, I see the same tragedy. Students build brilliant AI agents that are economically doomed. They hardcode expensive models like Opus 4.5 or GPT-5.2 for simple tasks. They burn budgets on redundant queries. They struggle with payment infrastructure that was not built for machines.

I realized that for the Agentic Economy to exist, agents need more than just intelligence. They need fiscal responsibility. But agents do not know how to save money. They need a manager.

That is why we built P402.

What it does

P402 is an intelligent middleware that sits between your application and AI providers via OpenRouter. It acts as a Protocol Economist, autonomously managing your AI budget so you do not have to.

It utilizes the Gemini 3 Intelligence Quadplex:

1. The Brain (Gemini 3 Pro)

A Marathon Agent that uses 1M token context to analyze 7-day routing histories. It performs forensic audits to find inefficiencies.

2. The Sentinel (Gemini 3 Flash)

A real-time monitor running at a low "Thinking Level" that watches for anomalies in under 500ms.

3. The Memory (text-embedding-004)

A Semantic Cache that intercepts redundant requests before they hit the provider. This reduces costs to $0.

4. The Hands (Tool Use)

7 custom tools that Gemini executes autonomously. These tools adjust routing weights, set rate limits, and substitute models.

🛡️ The Billing Guard (6-Layer Defense)

Beyond optimization, P402 prevents financial disaster with a military-grade Billing Guard. It enforces a 6-layer defense strategy including rate limiting, circuit breakers, concurrency control, and anomaly detection. This ensures no rogue agent or infinite loop can ever drain your wallet.

How we built it

P402 is a Production-Grade Monolith built with Google Antigravity.

  • Core Stack: Next.js 15, TypeScript, PostgreSQL (Prisma), Redis (BullMQ for queues).
  • Intelligence Layer: We integrated Gemini 3 Pro via the direct Google API rather than OpenRouter. This ensures the "Manager" is separate from the "Workers."
  • Tooling: We built a custom ToolExecutor class. This allows Gemini to call functions like configure_model_substitution and adjust_routing_weights.
  • Vector Search: We use text-embedding-004 to hash incoming prompts. If a prompt creates a vector similarity >0.95 with a stored query, P402 returns the cached response instantly.

Challenges we ran into

The "Watcher" Paradox

We did not want the cost of monitoring the AI to exceed the savings from optimizing the AI. We solved this by using Gemini 3 Flash for the high-frequency "Sentinel" loop. We only call Gemini 3 Pro for the deep-dive "Economist" audits.

Tool Hallucination

Initially, the model would try to optimize routes that did not exist. We fixed this by providing strict JSON schemas for the 7 custom tools. We also used Gemini's high-reasoning mode to validate actions before execution.

Accomplishments that we're proud of

We are most proud of the "Two-Brain Architecture."

Most apps send a prompt to an LLM and get a response. P402 sends the prompt to OpenRouter, but simultaneously sends the metadata of that transaction to Gemini 3.

This means P402 is an application that thinks about itself. It is self-healing, self-optimizing, and fully autonomous.

What's next for P402.io

  • Agent-to-Agent Negotiation: Allowing Gemini to negotiate prices with other agents in real-time.
  • Predictive Scaling: Using the 1M context window to predict traffic spikes before they happen.
  • Public Launch: Moving from a hackathon prototype to a live SaaS for the millions of developers using OpenRouter.

Built With

  • base
  • ethers
  • google-antigravity
  • google-gemini-3-flash
  • google-gemini-3-pro
  • google-text-embedding-004
  • next.js
  • openrouter
  • postgresql
  • rainbowkit
  • redis
  • typescript
  • view/wagmi
Share this project:

Updates

posted an update

Update: Phase 2 — Production Infrastructure & Settlement Layer

This week marks the transition of P402 from an AI routing concept to a live financial protocol. We have successfully deployed the "Financial Rails" required to make Gemini-powered agents economically autonomous.

Technical Highlights:

  1. Backend Overhaul (The Router) We completed a ground-up rewrite of p402.io to support high-frequency Agent-to-Agent (A2A) settlement.

EIP-3009 Implementation: Built a complete exact payment scheme for gasless USDC transfers on Base L2.

Smart Contracts: Deployed P402Settlement.sol and integrated a new Blockchain Service layer (lib/blockchain/client.ts) to replace all simulation stubs with on-chain verification.

Testing: Added a comprehensive integration test suite covering route integrity, API exports, and settlement logic.

  1. Architecture Scale: The Edge Network

To support global agent traffic, we secured Cloudflare credits to deploy the P402 Facilitator network.

Topology: 15 global edge regions running Cloudflare Workers.

Performance: Facilitators now handle EIP-3009 signature verification and rate limiting at the edge, ensuring <50ms latency for agent inference requests.

  1. The Client: Farcaster Mini-App We updated mini.p402.io to serve as the user-facing control center.

Tech Stack: Next.js + Viem + Farcaster Auth.

Feature: Implemented a custom usePayment hook that handles EIP-712 typed data signing, allowing users to authorize USDC spend directly from their Farcaster identity.

Engineering Metrics:

Volume: 7 major commits across router and mini-app.

Diff: +20,556 lines added / -5,269 removed (Net +15k).

Security: Added Replay Protection, 3-of-5 Multisig Treasury, and Traffic Event logging for audit trails.

Current Status: The system is live on Base Mainnet (Chain ID: 8453). Agents can now query Gemini models via OpenRouter and settle usage in real USDC immediately.

Log in or sign up for Devpost to join the conversation.

posted an update

We're live at p402.io with the full Intelligence Quadplex operational: Gemini 3 Pro as the Protocol Economist (forensic cost audits with 1M token context), Gemini 3 Flash as the real-time Sentinel (anomaly detection in <500ms), text-embedding-004 powering semantic cache (cache hits cost $0), and 7 autonomous tools executing optimizations without human supervision. The test suite covers routing, intelligence, A2A protocol compliance, x402 settlement, and API integrity. Ready for judging!

Log in or sign up for Devpost to join the conversation.