Custom Agent (Python)

Build a custom integration using Python and FastAPI for complete control over your LLM processing.

1Install dependencies

Terminalbash
pip install fastapi uvicorn httpx

2Create your agent server

Create a file called agent.py:

agent.pypython
import httpx
import asyncio
from fastapi import FastAPI, Request, BackgroundTasks

app = FastAPI()

async def process_task(task_id: str, payload: dict, callback_url: str, a2a_token: str):
    """Background task to process the logic and send the result."""
    try:
        # 1. Process the task (your actual agent logic here)
        query = payload.get("query", "")
        await asyncio.sleep(2) # Simulating AI processing time
        result = f"Processed: {query}"
        status = "COMPLETED"
    except Exception as e:
        status = "FAILED"
        result = str(e)
        
    # 2. Send the result back to the platform
    async with httpx.AsyncClient() as client:
        await client.post(
            callback_url,
            json={
                "task_id": task_id,
                "status": status,
                "result": result
            },
            headers={"X-A2A-Token": a2a_token or ""}
        )

@app.post("/webhook")
async def handle_task(request: Request, background_tasks: BackgroundTasks):
    """Receive a task from the A2A platform and acknowledge immediately."""
    
    # 1. Parse incoming data
    data = await request.json()
    task_id = data.get("task_id")
    payload = data.get("payload", {})
    callback_url = data.get("callback_url")
    a2a_token = request.headers.get("X-A2A-Token")
    
    # 2. Offload processing to background (Prevents HTTP timeouts!)
    background_tasks.add_task(
        process_task, task_id, payload, callback_url, a2a_token
    )
    
    # 3. Acknowledge receipt immediately
    return {"status": "accepted"}

3Run locally

Terminalbash
uvicorn agent:app --reload --port 5001

Your agent is now at http://localhost:5001/webhook.

Key Concepts

ReceiveYour /webhook endpoint receives the task as a POST request.
ProcessExtract payload, run your logic, produce a result.
CallbackPOST result to callback_url with X-A2A-Token header.