← Software Services

Deep Dive · Nov 17, 2026 · 7 min read

Integration Patterns for GenAI: APIs, Webhooks, and Queues

How to connect GenAI to business systems reliably: event-driven design, idempotency, rate limits, and failure handling.

Swimlane of an event-driven GenAI integration with retries and dead-letteringShort version← Connecting GenAI to the Tools You Already Use

Most integration pain comes from the connections, not the AI. Treat GenAI as a slow, occasionally failing service and design the integration accordingly.

Staircase of increasing retry delays ending in a dead-letter queue
Alert on dead-letter volume; it often signals an upstream API change.

Prefer events and queues

Model calls take seconds and can fail or be rate limited. Receive a webhook, enqueue the work, answer quickly, and process asynchronously. This protects the source system and smooths bursts.

webhook.py
from fastapi import FastAPI, Request, HTTPException
import hmac, hashlib

app = FastAPI()

@app.post("/webhooks/helpdesk")
async def helpdesk_webhook(request: Request):
    body = await request.body()
    expected = hmac.new(SECRET, body, hashlib.sha256).hexdigest()
    if not hmac.compare_digest(expected, request.headers.get("x-signature", "")):
        raise HTTPException(status_code=401)
    event = json.loads(body)
    queue.enqueue("draft_reply", ticket_id=event["ticket_id"], idempotency_key=event["event_id"])
    return {"status": "queued"}

Idempotency

Webhooks and queues deliver at least once, so the same event can arrive twice. Use a unique event id to ensure the work and the write-back happen once.

worker.py
def process(job, store, helpdesk, copilot):
    if store.already_done(job.idempotency_key):
        return
    draft = copilot(job.ticket_id)
    helpdesk.add_internal_note(job.ticket_id, draft, external_ref=job.idempotency_key)
    store.mark_done(job.idempotency_key)

Rate limits and backpressure

  • Limit concurrency per provider and per downstream API.
  • Use retries with exponential backoff and a dead-letter queue.
  • Shed or defer low-priority work during spikes.

Write back safely

  • Write drafts as internal notes or suggestions, not final actions, at first.
  • Use service accounts with least privilege.
  • Record the system, field, old value, and new value for each change.

Authentication and secrets

Keep keys in a secrets manager, rotate them, and scope tokens narrowly. Verify webhook signatures as above.

Observability

Propagate a correlation id from the event through the queue, the model call, and the write-back so a single trace explains what happened.

Synchronous versus asynchronous

Use synchronous calls when a person is waiting and the response is short, such as a suggestion inside a form, and stream the answer. Use asynchronous processing for anything triggered by an event, such as a new ticket or a document upload, so spikes do not overload downstream systems.

Retries, dead letters, and poison messages

jobs.py
def run_job(job, handler, queue, max_attempts=5):
    try:
        handler(job)
    except RetryableError:
        if job.attempts + 1 >= max_attempts:
            queue.dead_letter(job, reason="max_attempts")
        else:
            queue.requeue(job, delay=min(300, 2 ** job.attempts * 5))
    except Exception as e:
        queue.dead_letter(job, reason=f"non_retryable: {e}")    # needs human attention

Alert on dead-letter volume. A growing dead-letter queue is often the first sign that a downstream API changed.

Contract testing against vendor APIs

APIs change. Keep a small suite of tests that call a sandbox of each system you integrate, and run it daily. Pin API versions where the vendor allows, and read their change logs.

Mapping data between systems

  • Define a canonical internal model for the objects you handle, such as customer, ticket, and document.
  • Write adapters per system rather than letting vendor formats leak through the code.
  • Handle missing and malformed fields explicitly.
  • Log mapping failures with enough context to fix them.

Security for integrations

  • Least-privilege service accounts, scoped per system.
  • Rotated credentials in a secrets manager.
  • Network allow lists and signed webhooks.
  • Audit logs of every read and write the integration performs.

Testing end to end

Build a staging environment with sandbox accounts, replay recorded events through the full path, and assert the final state in each system. These tests catch the integration problems that unit tests never will.

How we can help

We build integrations for helpdesks, CRMs, and ERPs, with the queueing, idempotency, and monitoring that make them dependable. Contact us to scope an integration.

Related reading

Need help implementing this?

Our consultants run architecture reviews and build production pilots. Book a free scoping call to talk through your design.

Book a Free Scoping Call

or email us at hello@deepvero.com