DEV Community

carsonroell-debug
carsonroell-debug

Posted on

I replaced $149/mo API error monitoring with $0.001 per fix — and agents pay only when it works

How I built an outcome-based API proxy using x402 micropayments where AI agents only pay when errors are successfully diagnosed.

tags: ai, webdev, opensource, blockchain
cover_image:

canonical_url: https://selfheal.dev

The problem with API error handling for AI agents

Every AI agent builder hits the same wall: your agent calls an API, gets a 422, and crashes. The error message says "Unprocessable Entity" — not helpful for an autonomous system running at 3 AM.

The existing solutions are all subscription-based: $149-$349/month for error monitoring that tells you something went wrong. Your agent still crashes. You still get paged.

I wanted something different: an API proxy that fixes the error and only charges when the fix actually works.

What I built

SelfHeal is an API proxy that sits between your agent and any third-party API. Here's what happens:

When the API succeeds (2xx): Free pass-through. Zero cost. Under 200ms overhead.

When the API fails (4xx/5xx):

  1. SelfHeal returns HTTP 402 with an x402 payment spec
  2. Your agent pays $0.001-$0.005 USDC (gasless, on Base)
  3. An LLM analyzes the exact error + your request payload
  4. Returns structured JSON: what went wrong, how to fix it, whether to retry
  5. Payment only settles if the analysis succeeds

That last point is key. Failed analyses = no charge. You literally only pay for results.

The x402 protocol

x402 is a payment protocol built by Coinbase for machine-to-machine micropayments. It works like this:

  • Server returns HTTP 402 (Payment Required) with a payment spec
  • Client signs a USDC transfer authorization (gasless via EIP-3009)
  • Client retries with payment proof in the X-PAYMENT header
  • Server verifies, delivers the resource, settles payment

For SelfHeal, "the resource" is an LLM-powered error diagnosis. The settlement only happens if the diagnosis is useful.

Show me the code

Without SelfHeal:

const res = await fetch("https://api.crm.com/contacts", {
  method: "POST",
  body: JSON.stringify({ name: "John Doe" })
});
// 422 Unprocessable Entity
// Agent crashes. You get paged.
Enter fullscreen mode Exit fullscreen mode

With SelfHeal:

const res = await fetch("https://selfheal.dev/api/x402/proxy", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    url: "https://api.crm.com/contacts",
    method: "POST",
    body: JSON.stringify({ name: "John Doe" })
  })
});

if (res.status === 402) {
  // Pay $0.001 USDC, get fix instructions
} else {
  // Success — free pass-through
}
Enter fullscreen mode Exit fullscreen mode

No API keys. No signup. No monthly bill.

What the heal response looks like

{
  "healed": true,
  "settled": true,
  "transaction": "0xbf023f9c...",
  "error_analysis": {
    "error_category": "validation",
    "human_readable_explanation": "The request body is missing the required 'email' field.",
    "actionable_fix_for_agent": "Add the 'email' field to the request body.",
    "is_retriable": true,
    "suggested_payload_diff": {
      "add": { "email": "string (valid email address)" }
    }
  },
  "meta": {
    "cost_usdc": 0.001,
    "latency_ms": 3038
  }
}
Enter fullscreen mode Exit fullscreen mode

Your agent gets structured instructions it can act on programmatically. Not a generic error page.

Pricing comparison

SelfHeal Sentry TraceRoot.AI Struct AI
Model Pay per fix $26-80/mo $0-200/mo $20-200/mo
Auto-fix Yes No Human review Human review
AI agents Native Bolted on Partial Partial
Signup None Required Required Required
Cost when it works $0 Same price Same price Same price

SDKs

Both SDKs handle the x402 payment flow automatically:

npm install graceful-fail    # Node.js/TypeScript
pip install graceful-fail    # Python
Enter fullscreen mode Exit fullscreen mode

LangChain and CrewAI integrations included.

What's next

  • Mainnet launch (currently on Base Sepolia testnet)
  • More LLM providers for error analysis
  • Agent framework integrations beyond LangChain/CrewAI

The code is open source: github.com/carsonlabs/graceful-fail

Try it: selfheal.dev


Built by Freedom Engineers. We build tools for AI agents.

Top comments (0)