cd /news/ai-agents/tutorial-connect-an-ai-support-agent… · home › topics › ai-agents › article
[ARTICLE · art-162983] src=dev.to ↗ pub= topic=ai-agents verified=true sentiment=· neutral

Tutorial: Connect an AI Support Agent to Order and Refund APIs With Tool Calling

A developer published a tutorial showing how to connect an AI support agent to order and refund APIs using tool calling, with a FastAPI backend that exposes read-only order-status lookups and approval-gated refund requests. The approach keeps the model away from direct database or payment access, requiring independent backend permission checks and audit logging for every tool invocation.

by read9 min views3 publishedNov 3, 2026

Customer support automation becomes much more useful when an AI agent can do more than generate replies. It can check an order's shipping status, retrieve tracking information, verify refund eligibility, and help customers resolve common account issues.

But connecting an AI agent to business APIs introduces an important engineering challenge: how do you ensure that automated replies contain accurate information and that actions such as refunds or account changes follow business rules?

Imagine a customer asking, "Where is my order?"

A chatbot using only a static knowledge base might explain the company's shipping policy but cannot reliably tell the customer whether their specific order has shipped.

Now imagine connecting that chatbot to an order management API. The agent can retrieve the latest order status and generate a response based on actual business data.

This is the foundation of an AI support agent order API tool calling architecture.

Instead of allowing an AI model to guess operational details, developers give it access to controlled tools that retrieve information and perform approved actions.

This tutorial explains how to build that workflow using Python, API endpoints, permission checks, and audit logging. It also explores how Conversational AI Chatbots can handle common Tier 1 support requests without unnecessary human escalation.

Before implementing the integration, you should have:

For this example, we'll use FastAPI to build a lightweight support API.

The example will support two operations:

The refund operation will not automatically transfer money. It will create a controlled approval request.

Start by deciding which operations the AI agent should perform.

Keep read-only operations separate from actions that modify business data.

For example:

Tool Purpose Permission
get_order_status Retrieve order details Read-only
request_refund Submit a refund request Approval required

The model should not have unrestricted access to your database or payment system.

Instead, it should call predefined backend functions with validated parameters.

A simplified tool definition might look like this:

tools = [
    {
        "name": "get_order_status",
        "description": "Retrieve the current status of an authenticated customer's order.",
        "parameters": {
            "order_id": "string"
        }
    },
    {
        "name": "request_refund",
        "description": "Create a refund request that requires approval.",
        "parameters": {
            "order_id": "string",
            "reason": "string"
        }
    }
]

These definitions tell the AI agent what tools are available. They do not grant permission to execute them.

The backend must independently verify every request.

Install FastAPI and Uvicorn:

pip install fastapi uvicorn

Create a file named main.py:

from fastapi import FastAPI, Header, HTTPException
from pydantic import BaseModel
from datetime import datetime, timezone
from uuid import uuid4

app = FastAPI()

orders = {
    "ORD1001": {
        "customer_id": "CUST01",
        "status": "shipped",
        "tracking": "TRK12345",
        "amount": 1200
    }
}

refund_requests = []
audit_logs = []

class RefundRequest(BaseModel):
    order_id: str
    reason: str

def audit(action, customer, outcome):
    audit_logs.append({
        "id": str(uuid4()),
        "action": action,
        "customer": customer,
        "outcome": outcome,
        "timestamp": datetime.now(
            timezone.utc
        ).isoformat()
    })

@app.get("/orders/{order_id}")
def get_order_status(
    order_id: str,
    x_customer_id: str = Header(...)
):
    order = orders.get(order_id)

    if not order or order["customer_id"] != x_customer_id:
        audit("get_order_status", x_customer_id, "denied")
        raise HTTPException(404, "Order not found")

    audit("get_order_status", x_customer_id, "success")

    return {
        "order_id": order_id,
        "status": order["status"],
        "tracking": order["tracking"]
    }

@app.post("/refunds/request")
def request_refund(
    request: RefundRequest,
    x_customer_id: str = Header(...)
):
    order = orders.get(request.order_id)

    if not order or order["customer_id"] != x_customer_id:
        audit("request_refund", x_customer_id, "denied")
        raise HTTPException(404, "Order not found")

    refund = {
        "request_id": str(uuid4()),
        "order_id": request.order_id,
        "reason": request.reason,
        "status": "pending_approval"
    }

    refund_requests.append(refund)

    audit("request_refund", x_customer_id, "pending_approval")

    return refund

This example provides basic customer ownership checks and records tool execution events.

In production, replace the in-memory dictionaries with a database, use verified authentication rather than trusting a customer ID header, and add idempotency keys to prevent duplicate refund requests.

Start the API:

uvicorn main:app --reload

Your local API will be available at:

http://127.0.0.1:8000

FastAPI also provides interactive API documentation at /docs.

Once your backend is available, connect the tool definitions to an LLM orchestration layer.

The workflow should look like this:

Customer: Where is order ORD1001?

AI agent: Calls get_order_status.

API response:

{
  "order_id": "ORD1001",
  "status": "shipped",
  "tracking": "TRK12345"
}

AI agent: Your order has been shipped. Your tracking number is TRK12345.

Notice that the model does not invent the order status. It uses the information returned by the API.

For a refund request, the workflow changes.

Customer: I want a refund for order ORD1001.

AI agent: Calls request_refund.

API response: Refund request created with pending_approval status.

AI agent: Your refund request has been submitted for review. We will notify you when it has been approved.

This separation prevents the AI agent from claiming that a refund has been completed when it has only been requested.

Two additional controls are essential before deploying the integration.

Permission rules

Audit logging

Record every tool call, including:

Avoid storing passwords, access tokens, or unnecessary sensitive customer information.

For state-changing operations, also implement idempotency keys so retries cannot accidentally create duplicate refunds or account changes.

Before deploying the agent, test normal requests and unexpected scenarios.

You can test the order endpoint with:

curl http://127.0.0.1:8000/orders/ORD1001 \
  -H "X-Customer-ID: CUST01"

Expected result:

{
  "order_id": "ORD1001",
  "status": "shipped",
  "tracking": "TRK12345"
}

Test a refund request:

curl -X POST \
  http://127.0.0.1:8000/refunds/request \
  -H "Content-Type: application/json" \
  -H "X-Customer-ID: CUST01" \
  -d '{
    "order_id": "ORD1001",
    "reason": "Customer requested refund"
  }'

The expected response should contain:

{
  "order_id": "ORD1001",
  "reason": "Customer requested refund",
  "status": "pending_approval"
}

Invalid customer access: A customer attempts to retrieve another customer's order. The API should reject the request.

API timeout: The order service becomes temporarily unavailable. The agent should communicate that it cannot verify the latest status instead of guessing.

Duplicate refund request: A customer repeats the same request or the orchestration layer retries after a timeout. Use an idempotency key and persistent operation state to prevent duplicate requests.

Missing tracking information: The order exists, but tracking information has not been generated. The agent should communicate that the shipment details are not yet available.

Approval rejection: A refund request is rejected. The agent must communicate the actual decision rather than suggesting the refund was completed.

These scenarios should be included in automated integration tests before production deployment.

Developers do not always need to build every component from scratch. Several customer support platforms offer AI agents, API integrations, workflow automation, and configurable actions.

The right platform depends on the existing support stack and the types of requests you want to automate.

Gorgias is particularly relevant to ecommerce businesses using Shopify.

Its AI Agent supports post-purchase conversations, including order tracking, returns, cancellations, and order changes. Configurable actions can update Shopify orders and shipping information under defined conditions.

It also supports customer confirmation and action conditions. However, certain operations involving fulfillment integrations or complex order changes may still require human intervention.

Documentation: Gorgias AI Agent.

Intercom Fin supports customer service automation through knowledge, workflows, procedures, and integrations.

Its developer platform allows Fin to be used as a tool within an existing agent architecture. Developers can also configure escalation workflows and procedures for specific support requests.

This makes it relevant to SaaS teams handling common questions about account access, product usage, and customer onboarding, provided the required authentication and account integrations are configured.

Documentation: Fin AI Agent.

Zendesk provides AI agent capabilities that developers can extend through custom CRMs, business systems, and third-party APIs.

Its developer documentation covers workflow automation, webhooks, conversation management, and custom escalation logic.

This can be useful for SaaS and ecommerce support teams that already use Zendesk and need to connect AI workflows to existing business applications.

Documentation: Zendesk AI Agents Developer Docs.

Platform Relevant Tier 1 use cases Integration approach
Gorgias AI Agent Shipping, returns, order changes Shopify and connected app actions
Intercom Fin Common support questions, workflows, account-related procedures Workflows, procedures, APIs
Zendesk AI Agents Order inquiries, account support, ticket automation Custom APIs, webhooks, integrations

An important distinction: No platform should be assumed to resolve every shipping, login, or account-change request without escalation. Successful automation depends on API access, customer verification, permissions, workflow configuration, and the complexity of the request.

For example, checking a shipment is usually easier to automate than recovering a compromised account. Basic account changes may be automated after verification, while suspicious login activity should trigger additional security checks.

If your AI support agent does not behave as expected, investigate these areas first.

Problem Possible cause Recommended fix
Incorrect order status Stale or incorrect data source Retrieve status from the live order API
Tool call fails Invalid arguments or API configuration Validate schemas and API responses
Unauthorized access Missing identity verification Add authentication and ownership checks
Duplicate refund requests Retry without idempotency Implement idempotency keys
Agent claims refund completed Incorrect response handling Return actual approval and transaction states
Too many escalations Missing tools or incomplete workflows Expand tested Tier 1 automation coverage

A useful debugging strategy is to trace each customer request from the initial message through tool selection, API execution, permission checks, and final response generation.

This makes it easier to identify whether a problem originates in the AI agent, orchestration layer, or connected business application.

A reliable support agent needs more than an LLM and a collection of API endpoints. It needs access to the right business information, well-defined tools, and controlled execution workflows.

For developers planning more advanced integrations, explore custom AI assistants on your data to understand how AI assistants can work with APIs, databases, and external files.

Connecting an AI support agent to order and refund APIs allows ecommerce and SaaS businesses to automate repetitive customer inquiries while maintaining access to accurate operational information.

The key is to separate AI reasoning from business execution.

Use APIs to retrieve live order information, backend permission checks to control sensitive operations, and audit logs to track every tool call. Add approval workflows for refunds and account changes, and test failure scenarios before enabling autonomous execution.

Your next step: Start with one Tier 1 workflow, such as order tracking. Connect it to your live order API, test customer authentication, validate the returned information, and measure how many requests can be resolved accurately without human intervention.

Once that workflow is reliable, expand into other support operations such as delivery updates, basic account changes, and eligible refund requests.

The objective is not to eliminate human support. It is to let AI handle predictable requests while ensuring that complex, sensitive, or uncertain situations reach the right person.

── more in #ai-agents 4 stories · sorted by recency
── more on @fastapi 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/tutorial-connect-an-…] indexed:0 read:9min 2026-11-03 · —