Skip to main content
Back to Community & Earn Hub
Q&A Questionai-agents +150 pt Bounty

Handling webhook retries when your AI agent takes 18 seconds to reply

@liam_oconnor2 days ago 1683 views

We connected our support agent to Slack and WhatsApp webhooks using FastAPI.

Because a multi-step tool turn takes anywhere from 10 to 20 seconds, the webhook caller hits its 3-second timeout, assumes our server dropped the event, and retries the exact same payload 3 times. Our users end up getting the same answer three times in a row.

We don't want to stand up Kafka or Celery just for a handful of webhooks per minute. What's the cleanest lightweight pattern in FastAPI + Redis to return 200 OK immediately and make sure retries are ignored?

1 Answers

Community Answers

@noah_goldstein Accepted Answer2 days ago

You don't need Kafka or Celery for this—Redis SET NX EX + FastAPI BackgroundTasks (or a small asyncio worker) handles it in ~15 lines.

  1. Deduplicate by the provider's event/message ID (event_id in Slack, entry[0].changes[0].value.messages[0].id in WhatsApp) using SET key 1 NX EX 300.
  2. If SET NX returns False, it's a retry—return 200 OK immediately and do nothing.
  3. If True, hand the actual LLM + tool run to BackgroundTasks and return 200 OK in < 20ms, then post the reply back via the Slack/WhatsApp outbound API when finished.
python
@router.post("/webhooks/slack")
async def slack_webhook(req: Request, bg: BackgroundTasks):
    payload = await req.json()
    event_id = payload.get("event_id")
    # Atomic dedup for 5 minutes so retries never re-run the agent
    is_new = await redis.set(f"wh:seen:{event_id}", "1", nx=True, ex=300)
    if not is_new:
        return {"ok": True, "deduped": True}

    bg.add_task(run_agent_and_reply, payload)
    return {"ok": True}

Have a solution or working configuration?

Sign in or create a free account to answer this question (+10 pts, +25 pts if accepted) and tag helpful AIStoreHub repositories.