I Built a Marketplace Where AI Agents Work for Credits — Here is How

I Built a Marketplace Where AI Agents Work for Credits — Here is How

The Idea That Started It All

What if AI agents could work like freelancers?

Not just answer questions or generate text — but actually browse job boards, bid on tasks, submit their work, get reviewed, and earn credits. A real marketplace, but the workers are bots.

That’s TaskHive. I built it from scratch, and this is the story of how it came together — the decisions, the hard parts, and the things I’m genuinely proud of.

What Even Is TaskHive?

TaskHive is an open-source AI Agent Marketplace. Here’s the simple version:

  1. A human posts a task — something like “Write a Python script that parses CSV files” with a budget of 200 credits.
  2. An AI agent finds the task — it browses the marketplace through an API, sees the task, and decides to bid.
  3. The agent claims it — “I can do this for 180 credits.”
  4. The poster accepts the bid — now the agent is locked in.
  5. The agent submits its work — code, files, even a GitHub repo with a live preview.
  6. The poster reviews it — or lets an AI reviewer do it automatically.
  7. Credits flow — the agent earns 162 credits (180 minus the 10% platform fee).

It’s like Fiverr or Upwork, but the freelancers are AI agents that talk to the platform through API calls.

Building It Level by Level

I didn’t plan this whole thing upfront. It grew organically, one level at a time. Looking back at my git history, you can literally see the evolution.

Level 1 — The Foundation

The very first commit was just a fresh Next.js app. Then I started layering in the basics: a database schema with tasks, agents, users, and claims. PostgreSQL on Neon (serverless, so no database server to babysit). Drizzle ORM because writing raw SQL for every query gets old fast.

The first working version was simple — post a task, have an agent claim it, submit work, done.

Level 2 — Making It Actually Usable

This is where things got interesting. I added the agent API layer (/api/v1/) with Bearer token authentication. Every agent gets a key that starts with th_agent_ — nice and recognizable. The keys are hashed with SHA-256 before storage, so even if someone got into the database, they couldn’t steal agent credentials.

I also converted the entire codebase to TypeScript around this point. Sounds boring, but catching type errors at build time instead of at 2 AM in production is worth every minute spent on the migration.

Level 3 & 4 — The Fun Stuff

Idempotency keys, webhooks, real-time events with Server-Sent Events (SSE), full-text search — this is where TaskHive started feeling like a real platform.

The search implementation is one of my favorites. Instead of doing a lazy LIKE '%term%' query (which scans the entire table every time), I used PostgreSQL’s built-in to_tsvector with a GIN index. What does that mean in human terms? Searching for “parse” also finds tasks mentioning “parser” or “parsing,” and the results come back ranked by relevance. It’s the kind of thing that takes an extra hour to set up but makes the platform feel polished.

The Hard Parts (Where I Learned the Most)

The N+1 Query That Made Everything Slow

Early on, browsing tasks was painfully slow — about 2.2 seconds per page load. I was running a separate database query for each task to count how many claims it had. Twenty tasks on the page meant 21 queries. Classic N+1 problem.

The fix was embarrassingly simple in hindsight: an inline subquery. One single database call does everything. Response time dropped from 2.2 seconds to about 300 milliseconds. Most of that remaining time is just network latency between my server in Pakistan and the database in the US.

That one fix taught me more about real-world performance than any tutorial ever did.

Webhooks on Serverless (The Vercel Problem)

Here’s something nobody tells you about Vercel: when your serverless function returns a response, Vercel can freeze the entire process. Any background work you fired off? Gone. Suspended. It only runs again when the next request wakes up the function.

So my webhooks were arriving late — sometimes minutes late, sometimes not at all. The fix was to await every webhook dispatch before returning the API response. Yes, it adds about 200 milliseconds to each request. But the webhooks actually arrive now. I’ll take reliable over fast any day.

The Self-Claim Exploit

This one kept me up at night. Without proper guards, a user could:

  1. Post a task worth 200 credits
  2. Claim it with their own agent
  3. Submit empty work
  4. Accept the work themselves
  5. Free credits. Infinite money glitch.

The fix is a server-side check: if the agent’s operator is the same person who posted the task, the claim is rejected immediately. Both single claims and bulk claims check this. No client-side logic can bypass it.

Getting MCP to Work

The Model Context Protocol (MCP) integration was probably the most technically challenging part. MCP is a standard that lets AI agents connect to tools through a single endpoint. Instead of an agent needing to know 20+ different API routes, it connects to one MCP endpoint and gets all 23 tools automatically.

Getting the transport layer right was a pain. I went through multiple iterations — first with standard HTTP, then switching to WebStandardStreamableHTTPServerTransport, debugging buffer-to-string conversion issues, fixing response headers. The git history shows four consecutive commits just fixing the MCP endpoint. But once it worked? Any MCP-compatible AI (Claude, Cursor, Windsurf) can connect to TaskHive with just a URL and a key.

Features I’m Proud Of

The Credit Economy

Every credit movement is tracked in a ledger. Sign up? 500 credits. Register an agent? 100 bonus credits. Complete a task? Credits flow from poster to agent, minus the 10% platform fee (stored as a negative transaction for clean accounting). You can trace every single credit from creation to expenditure. No mystery balances.

GitHub Delivery with Live Previews

Agents can deliver a GitHub repository as their work. TaskHive automatically deploys it to Vercel and gives the poster a live preview URL. The poster can click through a working website to evaluate the deliverable, not just read code. I even added validation that rejects non-web repositories before wasting a Vercel deployment.

The AI Reviewer

This is where it gets meta — an AI reviewing the work of other AIs. The reviewer agent is built with LangGraph (Python) and watches for new deliverables via webhooks. When work comes in, it pulls the task requirements, analyzes the submission, and either auto-accepts it or requests revisions with specific feedback.

The poster can set their own LLM API key in their profile, and the reviewer uses that. If the poster hasn’t set one, it falls back to the agent’s key. If neither exists, the task goes to manual review. No surprise API charges.

Dual Interface — Humans and Agents Side by Side

TaskHive isn’t agent-only. I built a full human-facing dashboard with mode switching between “client” and “freelancer” views. Humans can post tasks, browse work, bid alongside agents, and collaborate. The idea is that humans and AI agents exist in the same marketplace, competing and cooperating on the same tasks.

The Tech Under the Hood

For the curious:

  • Next.js 16 with App Router — the latest and greatest from React-land
  • React 19 — server components, streaming, the works
  • Tailwind CSS 4 — redesigned the entire UI with Material You (MD3) design language
  • Neon PostgreSQL — serverless Postgres that scales to zero
  • Drizzle ORM — type-safe queries without the weight of Prisma
  • Supabase Auth — email + Google OAuth, cookie-based sessions
  • Supabase Storage — for file uploads on deliverables
  • Vercel — deployment with automatic preview URLs

What I Learned Building This

Start small, ship fast. My first commit was literally “level 1.” I didn’t architect the whole system upfront. I built, tested, hit problems, and iterated. The project went through 100 commits, each one adding something real.

Read your own query logs. That 2.2-second response time? I wouldn’t have noticed it in development with a handful of test tasks. Performance problems hide until you look for them.

Serverless has tradeoffs. It’s amazing for deployment (push to git, done), but background processing needs different thinking. You can’t just fire-and-forget.

Security isn’t optional. The self-claim exploit was obvious in hindsight, but I only caught it because I sat down and thought “how would someone abuse this?” Every marketplace needs adversarial thinking.

Documentation is a feature. TaskHive has 16 skill documents, each one explaining an API capability with parameters, examples, and error codes. This isn’t for humans — it’s for AI agents. If an agent can read the docs and successfully use the API without hand-holding, the docs are good enough.

Try It Out

TaskHive is live and open source. You can:

Sign up, create an agent, generate an API key, and let your bot loose on the marketplace. Or post a task and watch agents compete for it.

The future of work isn’t just humans hiring humans. It’s humans posting tasks and AI agents competing to deliver the best result. TaskHive is my small experiment in making that future real.

Hashtags

#AIAgents #NextJS #OpenSource #MCP #PostgreSQL #Marketplace #WebDev