← DevBytes

The Real Math of Self-Hosting AI Agents: API vs Your Own VPS

Everyone defaults to API calls for AI agents. It feels cheaper — no servers, no ops, just a key and a bill. But once your agents run on a schedule instead of on demand, the math flips hard. This is the actual arithmetic, from a stack that has been running production workloads daily for four months.

The moment the API bill stops making sense

A single agent doing useful daily work — reading inputs, drafting deliverables, reviewing its own output with a second model — easily pushes 3 to 5 million tokens a month. Priced at typical mid-tier rates, that is $80 to $400 a month. Per agent. Add a second worker agent and a QC reviewer, which any honest setup needs, and you are at $250 to $1,200 monthly before you have a single paying customer.

>>> NEW: The Self-Hosted Agent Factory — Production Kit

Run a 3-agent production line (orchestrator + workers + adversarial QC) on a $6/mo VPS. 120-page book + the complete working Docker stack: task ledger, model router, 15-min watchdog, QC rubric.

Launch price $29 (first 100 copies, then $59) →

The self-hosted equivalent: one 2 vCPU / 2 GB VPS at $6 a month runs the orchestration layer, the task queue, the watchdog, and the webhooks for all three agents. Your model spend drops to only the tasks that genuinely need a frontier model — because routing matters more than raw model choice.

Routing is the biggest bill-cutter nobody talks about

When we auditated a month of our own task logs, roughly 70% of everything the agents did was classification, formatting, summarization, template work. A cheap model tier handled all of it with no measurable quality drop. The strong tier only earned its price on genuine judgment calls.

The result: about 74% off the bill compared to sending everything to the strong model "to be safe." That single discipline saved more money than any model switch we ever tried. It is also the easiest thing to get wrong silently — most stacks route by habit, not by task kind.

What the $6 buys you

Three Docker containers: an orchestrator that only delegates, worker agents that each own one task at a time, and a QC agent that reviews everything in a fresh session — independence is a context property, not a model property. Under them, a 120-line file-based task ledger with atomic writes and 45-minute claim expiry, so a crashed worker cannot deadlock the queue. Above them, a 15-minute watchdog that restarts dead containers and escalates stale state to a human.

None of this needs a database, Redis, or a framework. The whole point is boring parts arranged so every failure mode — crash, hang, loop, bad output — has a mechanical recovery path.

When the API is still the right answer

Honesty section: if your agents run occasionally, or your volume is under a few hundred thousand tokens a month, or you need frontier reasoning on every call — stay on the API. Self-hosting pays off at the intersection of schedule-driven workloads and volume. Below that threshold it is a hobby, not a business decision.

The full playbook

We wrote everything up — the worked cost math, Docker isolation patterns, HTTPS with Caddy, state management, the QC rubric, security red lines — into a 120-page book, Self-Hosting AI Agents with Docker, packaged with the complete production stack (task ledger, model router, watchdog, role prompts) as The Self-Hosted Agent Factory — Production Kit. The free core (task queue, router, watchdog) is also open-sourced on GitHub if you just want the parts.