DEV Community

Nikhil Ranka
Nikhil Ranka

Posted on

From Prompt to Paycheck: Wiring an LLM Chain Into Real Gig Platforms

From Prompt to Paycheck: Wiring an LLM Chain Into Real Gig Platforms

Building an autonomous AI agent that can receive a natural‑language prompt, run a chain of LLM‑based reasoning steps, and earn money by completing a gig on a platform is a concrete engineering problem. Below is a walk‑through of the components, trade‑offs, and a minimal working implementation that you can adapt to your own stack. The goal is to show how it works, not to promise that it will replace human freelancers overnight.


1. High‑level flow

+----------------+      +----------------+      +-------------------+
|  User Prompt   | ---> |  LLM Chain     | ---> |  Gig Platform API |
+----------------+      +----------------+      +-------------------+
        ^                         |                       |
        |                         v                       v
   (input)          +----------------+          +-----------------+
                    |  Safety/Filter |          |  x402 Payment   |
                    +----------------+          +-----------------+
Enter fullscreen mode Exit fullscreen mode
  1. Prompt ingestion – a HTTP endpoint receives a JSON payload ({ "task": "…" }).
  2. LLM chain – the prompt is fed to a LangChain‑style pipeline that may include retrieval, tool use, or self‑consistency checks.
  3. Safety gate – a lightweight classifier or regex filter blocks disallowed outputs (e.g., personal data, hate speech).
  4. Gig submission – the agent calls the target platform’s REST/GraphQL API to create a job offer or submit a deliverable.
  5. Micropayment trigger – upon successful submission, the agent signs an x402 invoice and presents it to the payer (often the platform itself or a escrow contract).

Each step is independent enough to be swapped out (e.g., replace LangChain with Semantic Kernel, or Upwork with a custom marketplace).


2. Minimal working code (Python + FastAPI)

Assumptions

  • You have an OpenAI‑compatible API key (OPENAI_API_KEY).
  • The gig platform exposes a simple POST /gigs endpoint that expects {title, description, budget} and returns a gig ID.
  • You are using the x402 Python SDK to create a payment request (x402-py).
# file: agent_service.py
import os
import json
from typing import Dict, Any

import httpx
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from langchain.chat_models import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from x402 import Invoice, PaymentParams   # hypothetical x402 SDK

app = FastAPI()
llm = ChatOpenAI(temperature=0.2, model_name="gpt-4o-mini")

# ---- 1. Input model -------------------------------------------------
class PromptInput(BaseModel):
    task: str                     # free‑form description of the gig
    budget_usd: float             # max amount the requester is willing to pay

# ---- 2. LLM chain ----------------------------------------------------
template = """You are a helpful freelancer assistant.
Given the user request: "{task}"
Produce a concise gig title (≤ 60 chars) and a description (≤ 300 chars)
that clearly states what you will deliver.
Return JSON with keys "title" and "description"."""
prompt = PromptTemplate(input_variables=["task"], template=template)
chain = LLMChain(llm=llm, prompt=prompt)

# ---- 3. Safety filter (very simple) ----------------------------------
def safe_output(text: str) -> bool:
    # reject anything that looks like personal data or profanity
    banned = ["ssn", "credit card", "password", "nsfw"]
    return not any(b in text.lower() for b in banned)

# ---- 4. Gig platform client -------------------------------------------
PLATFORM_ENDPOINT = os.getenv("GIG_PLATFORM_URL", "https://api.example.com/gigs")
PLATFORM_TOKEN = os.getenv("PLATFORM_API_KEY")

async def submit_gig(payload: Dict[str, Any]) -> str:
    async with httpx.AsyncClient() as client:
        resp = await client.post(
            PLATFORM_ENDPOINT,
            json=payload,
            headers={"Authorization": f"Bearer {PLATFORM_TOKEN}"},
            timeout=10.0,
        )
        if resp.status_code != 201:
            raise HTTPException(status_code=502, detail="Platform rejected gig")
        data = resp.json()
        return data["gid"]   # assume platform returns a gig ID

# ---- 5. x402 payment --------------------------------------------------
def create_invoice(amount_usd: float) -> str:
    # x402 expects amounts in the smallest unit (e.g., cents for USDC)
    amt = int(round(amount_usd * 100))
    inv = Invoice(
        payer="0xYourEscrowAddress",   # replace with actual escrow or platform address
        payee="0xAgentWalletAddress",
        amount=amt,
        currency="USDC",
        chain="base",                  # Base L2
    )
    return inv.to_base64()             # opaque string the payer can verify

# ---- 6. Main endpoint -------------------------------------------------
@app.post("/run")
async def run_agent(body: PromptInput):
    # 1️⃣ Run LLM chain
    raw = chain.run(task=body.task)
    try:
        parsed = json.loads(raw)
        title, desc = parsed["title"], parsed["description"]
    except Exception:
        raise HTTPException(status_code=500, detail="LLM did not return valid JSON")

    # 2️⃣ Safety check
    if not (safe_output(title) and safe_output(desc)):
        raise HTTPException(status_code=400, detail="Generated content failed safety filter")

    # 3️⃣ Enforce budget ceiling
    if body.budget_usd < 0.01:          # min practical x402 amount
        raise HTTPException(status_code=400, detail="Budget too low for micropayment")

    # 4️⃣ Submit to gig platform
    gid = await submit_gig({"title": title, "description": desc, "budget": body.budget_usd})

    # 5️⃣ Create x402 invoice (agent gets paid after platform confirms completion)
    invoice_b64 = create_invoice(body.budget_usd)

    return {
        "gig_id": gid,
        "invoice": invoice_b64,
        "note": "Present the invoice to the payer; funds are released on-chain after gig confirmation."
    }
Enter fullscreen mode Exit fullscreen mode

What this does

  • Accepts a JSON prompt ({"task":"Write a 500‑word blog post about Rust async","budget_usd":0.05}).
  • Uses a single LLM call to generate a title/description pair.
  • Filters out obvious unsafe strings.
  • Posts the gig to a placeholder platform API.
  • Returns an x402 invoice that the payer (often an escrow contract tied to the platform) can settle for the agreed amount in USDC on Base.

You can replace the submit_gig function with the real Upwork/Fiverr SDKs, or with a webhook that creates a task in your own internal marketplace.


3. Honest trade‑offs

Aspect Reality check Mitigation / notes
Latency Each LLM call adds ~200‑500 ms (depends on model and network). The platform API and x402 signing add another ~100‑200 ms. End‑to‑end latency is usually ≥ 500 ms before the gig is visible. Keep the chain short (single LLM call) for latency‑sensitive use‑cases; cache frequent prompts if acceptable.
Cost LLM usage dominates cost: GPT‑4‑mini ≈ $0.0006 per 1k tokens. At ~800 tokens per request you spend ≈ $0.0005 per run. The x402 fee is negligible (< $0.0001) but you must fund the escrow with USDC. Budget the LLM cost into your gig price; monitor token usage to avoid surprise spikes.
Reliability Platform APIs can be rate‑limited or temporarily down; blockchain txs can fail due to gas spikes. Implement retry with exponential backoff, dead‑letter queue for failed gig submissions, and a fallback to manual review.
Safety & compliance LLMs can emit copyrighted text, personal data, or violate platform policies. A simple regex filter is insufficient for production. Add a dedicated moderation model (e.g., OpenAI Moderation endpoint) or a fine‑tuned classifier; log all outputs for audit.
Atomicity The agent gets paid after the platform confirms completion, but there is a window where the gig is submitted and the agent has done work without guaranteed payment. Use escrow smart contracts that hold funds until both parties sign off; the x40

Top comments (0)