DEV Community

Damien
Damien

Posted on

I built a 250-tool AI API on a Raspberry Pi 5 — architecture, economics, and what I learned

Six months ago I started building an AI tool API in my apartment. The idea: I was paying $60/month across OpenAI, Anthropic, and various scraping tools — using maybe 2% of what I paid for. Why isn't there a pay-per-call option?

So I built one. AiPayGen is a single API with 250+ pre-built tools and 15 AI models from 7 providers. You call /research, /scrape_website, /sentiment, /translate — not raw model completions. Pay per call, starting at $0.004.

The Architecture

Client -> Cloudflare Tunnel -> Gunicorn (2 workers, 4 threads)
  -> Flask app -> Model Router -> [Anthropic|OpenAI|Google|DeepSeek|xAI|Together]
  -> SQLite (WAL mode) for everything: auth, billing, memory, job queue
Enter fullscreen mode Exit fullscreen mode

Why SQLite? One file, zero config, WAL mode handles concurrent reads. At my scale (< 1000 req/day), it outperforms Postgres by eliminating network round trips. The entire billing system is atomic deductions in SQLite.

Why Flask? I know it. Shipping speed > perfect architecture for a solo project.

The Model Router

Each tool maps to a model tier. Summarization uses Haiku (cheap, fast). Research synthesis uses Sonnet or GPT-4o. The router:

  1. Checks if user specified a model
  2. If not, picks best model for the task
  3. Checks circuit breaker (3 failures in 5 min = skip provider)
  4. Makes the call with timeout
  5. On failure, retries on fallback chain

Fallback chains:

  • Anthropic -> OpenAI
  • OpenAI -> Anthropic
  • DeepSeek -> Together AI
  • Google -> Anthropic

Response caching: 5-minute TTL on deterministic requests. Saves ~15-20% of API costs.

x402 Payments — AI Agents Paying Per-Call

This is the most interesting part. x402 is a protocol where AI agents pay per HTTP request using USDC stablecoins on Base (Ethereum L2).

When an agent hits a 402 endpoint without an API key, the server responds with payment requirements. The agent's wallet signs a USDC transfer, includes the receipt in the next request, and the server verifies and processes it. No accounts, no API keys — just money in the HTTP header.

# Simplified x402 middleware
@app.before_request
def check_x402():
    if has_api_key(request):
        return  # Traditional auth
    if request.headers.get("X-PAYMENT"):
        receipt = verify_usdc_payment(request.headers["X-PAYMENT"])
        if receipt.amount >= tool_price:
            return  # Payment verified, proceed
    return jsonify({"price": "0.01", "currency": "USDC", "network": "base"}), 402
Enter fullscreen mode Exit fullscreen mode

This matters because autonomous AI agents can't fill out Stripe forms. x402 lets them pay programmatically.

MCP Integration

MCP (Model Context Protocol) is how AI coding tools discover external tools. AiPayGen is an MCP server — install it and Claude Code/Cursor/Windsurf automatically see all 250 tools:

pip install aipaygen-mcp
claude mcp add aipaygen -- aipaygen-mcp
Enter fullscreen mode Exit fullscreen mode

Now Claude can call research, scrape_website, translate, etc. without you configuring anything.

The Economics

Tool My cost I charge Margin
/summarize (Haiku) $0.001 $0.006 83%
/research (Sonnet) $0.01 $0.02 50%
/code (GPT-4o) $0.015 $0.025 40%
/translate (Flash) $0.0005 $0.004 87%

Server costs: ~$0.50/day electricity. Break-even: ~100 paid calls/day.

Current numbers after 2 weeks:

  • 415 MCP connections/week
  • 1,977 PyPI downloads/month
  • $0 revenue — most users on free tier

What I Learned

1. Tool naming matters more than docs. Claude picks tools by name and description. Verb-first names (research, summarize, extract) get used naturally. I renamed get_summary to summarize and usage doubled.

2. The free tier drives adoption. Nobody sets up an API key to try something. 3 free calls/day with zero signup was essential for MCP adoption.

3. Structured JSON > raw text. Claude chains tools better with structured responses. Every tool returns {"result": ..., "model_used": ..., "tokens": ...}.

4. Circuit breakers are not optional. Provider outages happen weekly. Without automatic fallback, your entire API goes down when one provider hiccups.

Try It

# No signup, no API key
curl -X POST https://api.aipaygen.com/research \
  -H "Content-Type: application/json" \
  -d '{"topic": "best practices for building MCP servers"}'

# Or install the MCP server
pip install aipaygen-mcp
Enter fullscreen mode Exit fullscreen mode

Website: aipaygen.com
PyPI: pypi.org/project/aipaygen-mcp
Try page: aipaygen.com/try

What tools would you want to see added? What would make you convert from free to paid?

Top comments (0)