Skip to main content
AUTOGEN INTEGRATION

AutoGen payment integration.

Add USDC payments to any AutoGen group chat. One tool, one billing agent in the team, one termination condition.

SHORT ANSWER

Blockchain0x provides a drop-in payment_request_tool for AutoGen 0.4+ teams. Install blockchain0x-autogen, add it to a dedicated biller AssistantAgent in your RoundRobinGroupChat or SelectorGroupChat, and the team can request USDC payments before doing paid work. Payments land in your wallet on Base. Webhooks confirm.

WHY GROUP-CHAT BILLING FITS AUTOGEN

AutoGen's conversational design fits the request-pay-deliver pattern.

AutoGen agents communicate by sending messages to each other and to a shared chat. This makes the payment-before-work pattern especially natural: a biller agent sends the payment request as a chat message containing the hosted URL, the chat terminates on a known tag, and the researcher agent picks up later (in a new run, after the webhook confirms payment).

The pattern works equally well in research-team setups (multi-agent conversations producing a single deliverable) and in classic conversational shapes (a single AssistantAgent gated by a billing UserProxyAgent in front). Both styles are demonstrated in the starter repo below.

INSTALLATION

One pip install. Three environment variables. AutoGen 0.4+ ready.

Targets Python 3.10+ (AutoGen 0.4's minimum). Provides both AutoGen 0.4+ APIs (default) and legacy 0.2.x compatibility (in the .legacy submodule). No additional dependencies beyond AutoGen's own.

INSTALL
pip install blockchain0x-autogen
ENVIRONMENT VARIABLES
export BLOCKCHAIN0X_API_KEY=sk_live_...
export BLOCKCHAIN0X_AGENT_ID=agt_abc123
export BLOCKCHAIN0X_SIGNING_SECRET=whsec_...

BLOCKCHAIN0X_API_KEY and BLOCKCHAIN0X_AGENT_ID come from the agent's settings page after creation in the Blockchain0x dashboard. BLOCKCHAIN0X_SIGNING_SECRET is needed only in the process that handles webhooks (typically your FastAPI app, not the AutoGen team itself).

FULL TEAM EXAMPLE

A two-agent team with billing + research roles.

Below is a complete AutoGen 0.4+ RoundRobinGroupChat with a biller AssistantAgent (carrying the payment_request_tool) and a researcher AssistantAgent (doing the paid work). The chat terminates when the biller outputs a known tag containing the hosted URL.

TEAM.PY
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.conditions import TextMentionTermination
from autogen_ext.models.openai import OpenAIChatCompletionClient
from blockchain0x.autogen import payment_request_tool

model_client = OpenAIChatCompletionClient(model="gpt-4o")

# Researcher agent (does the work after payment)
researcher = AssistantAgent(
    name="researcher",
    model_client=model_client,
    description={t("Senior market analyst")},
    system_message=(
        "You produce thorough Q4 LLM market analyses. "
        "Only run after the biller confirms payment is received."
    ),
)

# Biller agent (handles payment requests)
biller = AssistantAgent(
    name="biller",
    model_client=model_client,
    description={t("Client billing agent")},
    system_message=(
        "You request USDC payment from the client before research begins. "
        "Use payment_request_tool to create a hosted URL. "
        "Output 'PAYMENT_REQUESTED <hosted_url>' and end your turn."
    ),
    tools=[payment_request_tool],
)

team = RoundRobinGroupChat(
    [biller, researcher],
    termination_condition=TextMentionTermination("PAYMENT_REQUESTED"),
)

result = await team.run(task="The client wants a $5 USDC Q4 LLM market analysis.")

When you await team.run(), the biller is selected first (round-robin), calls the payment_request_tool, outputs PAYMENT_REQUESTED + URL, and the TextMentionTermination fires. The researcher never runs in this turn - it waits for the payment.confirmed webhook to trigger a fresh team.run() that begins with the researcher this time.

WEBHOOK HANDLING

FastAPI handler that resumes the team after payment.

The webhook handler verifies the signature and triggers a new team.run() (or enqueues one) when payment.confirmed fires. FastAPI example below; the same pattern works in any async Python framework.

WEBHOOK.PY
from fastapi import FastAPI, Request, HTTPException
from blockchain0x.autogen import verify_webhook
import os

app = FastAPI()
SIGNING_SECRET = os.environ["BLOCKCHAIN0X_SIGNING_SECRET"]

@app.post("/webhooks/payment")
async def receive(request: Request):
    signature = request.headers.get("X-Blockchain0x-Signature", "")
    body = await request.body()
    if not verify_webhook(body, signature, SIGNING_SECRET):
        raise HTTPException(status_code=401)

    event = await request.json()
    if event["type"] == "payment.confirmed":
        # Resume the researcher with the confirmed payment context
        await trigger_research_run(event["data"]["payment_request_id"])
    return {"ok": True}

verify_webhook does HMAC-SHA256 in constant time. Read the raw body via await request.body(); do not use request.json() then re-serialize, because parsing destroys the signature. For production, do not run the new team.run() inline in the webhook handler (it can take minutes) - enqueue a job via Celery, RQ, or arq, and respond 200 immediately.

STARTER REPOSITORY

Working example with 0.4+ and 0.2.x variants.

A complete AutoGen repository at the GitHub link below. Includes the two-agent team example for AutoGen 0.4+, a legacy/ subdirectory with the equivalent 0.2.x setup, a FastAPI webhook handler, and a Docker Compose orchestrating the team, the webhook server, and a Redis queue.

github.com/blockchain0x/agent-wallet-autogen

Repository layout: team.py (0.4+ team), legacy/team.py (0.2.x ConversableAgent pattern), webhook.py (FastAPI handler), docker-compose.yml, README with deployment notes for Fly.io, Modal, and AWS Lambda.

COMMON PITFALLS

Five group-chat and async traps to avoid.

AutoGen's multi-agent + async design unlocks powerful patterns but introduces its own footguns. These come from our support inbox.

PITFALL 1

Group-chat termination conditions are easy to misconfigure

AutoGen's RoundRobinGroupChat or SelectorGroupChat run until a termination condition fires. If your biller agent issues the payment request but the termination string never appears in the chat (typo, model paraphrasing), the group keeps looping. Use TextMentionTermination with a specific tag like PAYMENT_REQUESTED that you instruct the biller to output verbatim. Validate this works with a non-paying test before deploying.

PITFALL 2

Tool registration scope

AutoGen tools registered to one agent are not automatically available to other agents in the team. If the researcher tries to call payment_request_tool, the call fails because the tool is scoped to the biller. This is usually correct, but first integrators sometimes assume tools are team-wide. They are not; assign tools per agent.

PITFALL 3

Async vs sync APIs

AutoGen 0.4+ is async-first. payment_request_tool is awaitable; calling it as a sync function from a non-async context returns a coroutine, not a result. Use within asyncio.run() at the top level, or ensure your enclosing function is async. The error 'coroutine was never awaited' usually means you called the tool from a sync function.

PITFALL 4

Model client config across agents

Each agent in AutoGen takes a model_client. If you instantiate one OpenAIChatCompletionClient per agent (rather than sharing one), you pay 2x or more on token usage because each client maintains its own context. Share a single model_client instance across the team unless you have a specific reason for separate config (different temperature, different model per role).

PITFALL 5

Webhook handler waking dormant runs

AutoGen group-chat runs are typically short-lived (they end at the termination condition). When the payment.confirmed webhook fires minutes later, your original team.run() call has already returned. You need an async job queue or persistent task store (Redis, Postgres) to record the payment-pending state and trigger a new team.run() that picks up where the first one left off. Do not try to keep the original team alive waiting; AutoGen is not designed for long-lived runs.

FREQUENTLY ASKED

Three AutoGen-specific questions.

Does this work with classic AutoGen (0.2.x) or only the new 0.4+ API?

Both, but the import paths differ. For AutoGen 0.4+ (autogen_agentchat, autogen_ext), use the imports shown above. For AutoGen 0.2.x (autogen.ConversableAgent style), import from blockchain0x.autogen.legacy and use the older register_function pattern shown in the starter repo's legacy/ subdirectory. New projects should start on 0.4+; the 0.2.x path is maintained for existing codebases.

Can I use this with AutoGen Studio?

Yes. AutoGen Studio is a no-code UI on top of AutoGen 0.4+. Add payment_request_tool as a custom tool in the Studio UI by referencing the blockchain0x.autogen.payment_request_tool path. The tool's parameters (amount, reason) become Studio fields the user can fill in or that the LLM can populate. Studio support is currently in beta; let us know if the UI integration is missing anything.

How does this interact with AutoGen's human-in-the-loop UserProxyAgent?

The biller agent should not be a UserProxyAgent - keep payment request creation in an AssistantAgent so the LLM controls it. The UserProxyAgent typically receives the hosted_url output and either pays manually or hands it to the human user. The pattern: AssistantAgent biller -> creates payment request -> UserProxyAgent receives the URL -> human (or simulated human in test) pays -> webhook fires -> next AssistantAgent run begins. This separation keeps the autonomous logic in the LLM agents and the human interaction in the UserProxyAgent.

Add billing to your AutoGen team.

Five minutes from pip install to your first paid group chat. Pro at $9/agent/month.