How to Self-Host AgentGPT and Cut API Costs Per Run
Self-hosting AgentGPT means you run the frontend, the API server, and a Postgres or MySQL database yourself, then point it at your own OpenAI key. The payoff is no per-run subscription cap. The cost is you now own the token bill, and a badly tuned loop can spend a few dollars in ten minutes.
This guide covers the Docker deploy, the settings that actually move cost, and how to measure spend per finished task instead of per run.
What do you need before deploying AgentGPT locally?
Four things: Docker with Compose, a Postgres 13+ instance (or MySQL 8), a Node toolchain of at least version 18 if you build from source, and an OpenAI key with billing enabled.
The official image name is reworkd/agentgpt. The project ships a docker-compose.yml you can pull straight from the repo. Expect the whole stack to take about 700 MB of image space.
Resist the urge to point a brand-new key at it without a budget cap. Go into your OpenAI billing settings first and set a hard monthly limit, then set a second lower limit on a separate project key used only by the agent. When a loop goes wrong, you want it to die at 20 dollars, not 200.
How do you deploy the stack?
- Clone the repo and copy
.env.exampleto.env. - Set
DATABASE_URLto your Postgres connection string, for examplepostgresql://agent:pw@db:5432/agentgpt. - Set
OPENAI_API_KEYand, if you want a non-OpenAI provider,OPEN_AI_API_KEYplus a base URL override. - Run
docker compose up -dand watchdocker compose logs -f nextjsuntil the app reports ready. - Open
http://localhost:3000, create an account, and run a throwaway goal with one or two tasks to confirm the key works.
If the log loops on database migrations, the cause is almost always a Postgres user without create-table permission. Grant it rather than running as superuser long-term.
Which settings control AgentGPT’s cost per run?
Cost is tasks multiplied by tokens per task, and both are adjustable.
- Model choice. The default in most builds is a GPT-3.5-class model for task planning and a stronger one for final summaries. Keeping planning on the cheap model is the single biggest saving, because planning runs on every iteration.
- Max loops. Set the iteration cap explicitly. Twenty loops is generous for a writing task; ten is often enough.
- Fast mode. The hosted version exposes a fast/normal toggle. In self-hosted builds this maps to fewer tokens per task and shorter prompts. It is fine for drafts.
- Context truncation. Long runs resend earlier results every iteration, so the token cost of round nine is far higher than round one. Capping the number of past results passed into the prompt flattens that curve.
A concrete example: a 12-task run using a mid-tier model for both planning and results typically lands around 60,000 to 90,000 total tokens. Moving planning to the cheap tier commonly cuts that by a third or more without visible quality loss in the task list.
How do you measure cost per finished task?
Use the OpenAI usage dashboard filtered by the agent’s project key, not the raw run count. Divide total spend by the number of runs where you actually used the output.
Run the same goal three times on two different model configurations. Keep a small table:
| Config | Runs | Spend | Usable outputs | Cost per usable |
|---|---|---|---|---|
| Cheap planner + cheap writer | 3 | $0.42 | 1 | $0.42 |
| Cheap planner + strong writer | 3 | $0.61 | 3 | $0.20 |
The cheaper configuration often loses on cost per usable output, because you throw most of the results away. This is the number that matters, and it is easy to get wrong by staring at the per-run price.
What breaks in a self-hosted AgentGPT install?
Three recurring problems. Database connections leak if you run more than one API worker against a low connection limit — Postgres defaults to 100 and each worker holds a pool. Second, the frontend calls the API server across origins, so a reverse proxy needs the Access-Control-Allow-Origin header set to your actual domain. Third, results stream over server-sent events; any proxy that buffers responses will make the log appear frozen until the run ends.
If you deploy behind Nginx, disable proxy_buffering on the events route specifically.
Who should self-host and who should not?
Self-host if you run agents weekly, if you need the data to stay on your own hardware, or if you want to add tools the hosted build does not expose. Do not self-host if you only want to try the concept once, or if you have no appetite for keeping a Node app and database patched. The hosted demo plus a small credit top-up is cheaper than an evening of debugging Docker networking.
Quick Summary
- Self-hosting AgentGPT removes per-run caps but moves the token bill to your own OpenAI key.
- Set a hard billing limit and a separate project key before the first run, not after.
- Planning runs every iteration, so putting the planner on a cheap model saves more than any other single change.
- Expect roughly 60,000 to 90,000 tokens for a 12-task run at mid-tier settings.
- Judge configurations by cost per usable output, not cost per run.
