DEV Community

Cover image for Tutorial: Connect an AI Support Agent to Order and Refund APIs With Tool Calling
Eric Weston
Eric Weston

Posted on Edited on

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

What Is Tool Calling in an AI Support Agent?

Tool calling allows an AI model to use external functions or APIs to complete a task.

Instead of guessing an answer, the AI agent can call a tool to retrieve information from an order management system, CRM, or ERP.

For example:

  1. A customer asks, "Where is my order?"
  2. The AI agent identifies that it needs order information.
  3. It calls the order lookup API.
  4. The backend checks the customer's identity and permissions.
  5. The API returns the latest order status.
  6. The AI agent explains the result.

This approach keeps important business information in your actual systems instead of relying on the model's memory.

1. Requirements and Setup

For this tutorial, you will need:

  • Python 3.10 or later
  • FastAPI
  • Uvicorn
  • Basic knowledge of REST APIs
  • A test order record

Install the required packages:

pip install fastapi uvicorn
Enter fullscreen mode Exit fullscreen mode

We will create two API endpoints:

  • GET /orders/{order_id} to retrieve order information.
  • POST /refunds/request to submit a refund request.

This example uses sample data. In a real application, connect these endpoints to your ecommerce platform, CRM, or ERP.

2. Create the Order and Refund APIs

Create a file called main.py.

Add the following code:

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

app = FastAPI()

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("support-agent")

# Sample order data
orders = {
    "ORD-1001": {
        "customer_id": "CUST-101",
        "status": "Shipped",
        "tracking_number": "TRK12345",
        "total": 120.00
    }
}

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

def log_tool_call(tool_name, customer_id, order_id, result):
    logger.info({
        "event_id": str(uuid.uuid4()),
        "tool": tool_name,
        "customer_id": customer_id,
        "order_id": order_id,
        "result": result,
        "timestamp": datetime.now(timezone.utc).isoformat()
    })

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

    if not order:
        log_tool_call(
            "get_order", x_customer_id, order_id, "not_found"
        )
        raise HTTPException(
            status_code=404,
            detail="Order not found"
        )

    if order["customer_id"] != x_customer_id:
        log_tool_call(
            "get_order", x_customer_id, order_id, "denied"
        )
        raise HTTPException(
            status_code=403,
            detail="You cannot access this order"
        )

    log_tool_call(
        "get_order", x_customer_id, order_id, "success"
    )

    return {
        "order_id": order_id,
        "status": order["status"],
        "tracking_number": order["tracking_number"],
        "total": order["total"]
    }

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

    if not order:
        raise HTTPException(
            status_code=404,
            detail="Order not found"
        )

    if order["customer_id"] != x_customer_id:
        log_tool_call(
            "request_refund",
            x_customer_id,
            request.order_id,
            "denied"
        )
        raise HTTPException(
            status_code=403,
            detail="You cannot request a refund for this order"
        )

    # Demo only: submit a request for approval.
    # This does not issue an actual refund.
    refund_id = str(uuid.uuid4())

    log_tool_call(
        "request_refund",
        x_customer_id,
        request.order_id,
        "pending_approval"
    )

    return {
        "refund_id": refund_id,
        "status": "pending_approval",
        "message": "Your refund request has been submitted for review."
    }
Enter fullscreen mode Exit fullscreen mode

The first endpoint checks the latest order details. The second submits a refund request.

Notice that the refund endpoint does not immediately send money. It creates a request that can be reviewed and approved through a separate workflow.

Security note: The customer ID header is only for this demonstration. In production, use a trusted authentication system and verify the customer's identity on the server. Never trust an identity supplied directly by an unverified chatbot or browser request.

3. Run and Test the APIs

Start the application:

uvicorn main:app --reload
Enter fullscreen mode Exit fullscreen mode

Open the interactive API documentation:

http://127.0.0.1:8000/docs
Enter fullscreen mode Exit fullscreen mode

Test the order endpoint:

curl -H "X-Customer-ID: CUST-101" \
http://127.0.0.1:8000/orders/ORD-1001
Enter fullscreen mode Exit fullscreen mode

Example response:

{
  "order_id": "ORD-1001",
  "status": "Shipped",
  "tracking_number": "TRK12345",
  "total": 120.0
}
Enter fullscreen mode Exit fullscreen mode

Now test the refund request:

curl -X POST \
http://127.0.0.1:8000/refunds/request \
-H "Content-Type: application/json" \
-H "X-Customer-ID: CUST-101" \
-d '{
  "order_id": "ORD-1001",
  "reason": "Product arrived damaged"
}'
Enter fullscreen mode Exit fullscreen mode

The response will show that the refund request is waiting for approval.

You now have two working API endpoints that an AI support agent can use.

4. Connect the APIs to an AI Support Agent

The next step is to make the APIs available as tools to your AI model.

For example, you can describe the tools like this:

[
  {
    "name": "get_order",
    "description": "Check the latest order status for the authenticated customer",
    "parameters": {
      "order_id": "string"
    }
  },
  {
    "name": "request_refund",
    "description": "Submit a refund request for approval",
    "parameters": {
      "order_id": "string",
      "reason": "string"
    }
  }
]
Enter fullscreen mode Exit fullscreen mode

The exact tool definition depends on the AI model or framework you use.

You should also give the agent clear instructions:

You are a customer support assistant.

1. Use get_order when a customer asks about an order.
2. Never guess order status or tracking information.
3. Use request_refund only when the customer asks to start a refund.
4. Never say a refund is complete unless the refund system confirms it.
5. If an API returns an error or unclear information, explain the issue.
6. Never access another customer's order.
Enter fullscreen mode Exit fullscreen mode

The AI model decides which tool is needed, but your backend must enforce authentication, permissions, and business rules.

This is how Conversational AI Chatbots can do more than answer general questions. They can interact with business systems while keeping important operations under backend control.

5. Set Permissions for Sensitive Actions

Not every customer support action should be available to an AI agent.

Start by dividing actions into three categories:

Action Suggested permission
Check order status Allow after customer authentication
Check tracking details Allow after customer authentication
Submit a refund request Allow after eligibility checks
Approve or issue a refund Require an authorized workflow
Change account email Require identity verification
Delete an account Require additional approval

The main rule is simple: give the AI agent only the permissions it needs.

For example, an agent can collect a customer's refund reason and submit a request. However, an authorized system or employee should approve the actual refund.

This reduces the risk of accidental payments, unauthorized changes, and incorrect account actions.

6. Log Every Tool Call

Every action performed by the AI agent should be traceable.

For each tool call, record:

  • Tool name
  • Customer or session identifier
  • Order identifier
  • Request time
  • Result or error
  • Approval status
  • Unique event or request ID

Avoid storing passwords, access tokens, payment details, or unnecessary personal information in logs.

For production systems, save logs in a persistent database or monitoring platform instead of relying only on console output.

These logs help when a customer reports that:

  • The chatbot displayed the wrong order status.
  • A refund request was submitted twice.
  • An account was changed without permission.
  • The chatbot provided an incorrect answer.

Developers can review the recorded events and identify where the issue occurred.

7. Test Common Failure Cases

A successful API response is not enough. You should also test what happens when something goes wrong.

Test case Expected result
Valid order and customer Return current order details
Unknown order ID Return a clear not-found message
Wrong customer ID Deny access
Missing authentication Reject the request
Refund not eligible Explain the reason or escalate
API timeout Retry safely or report unavailable information
Duplicate refund request Avoid creating duplicate actions

For actions that may be repeated, use an idempotency key. This helps prevent duplicate refund requests when a network timeout causes the AI agent to retry the same operation.

The AI agent should never invent a successful result when an API fails. It should explain that it could not retrieve the information and provide a safe next step.

8. Which AI Support Platforms Can Handle Tier 1 Customer Service?

If you do not want to build the entire system from scratch, several platforms offer AI-powered customer support automation.

Platform Useful capabilities What to check
Gorgias AI Agent Ecommerce support, order updates, and supported Shopify actions Check your Shopify setup and action limitations
Intercom Fin Customer support automation, workflows, and integrations Configure permissions and escalation rules
Zendesk AI Agents Support automation and connections to business systems Custom API integration may be needed for specific actions

For ecommerce businesses using Shopify, Gorgias is worth evaluating for order-related support. Intercom Fin is worth considering for SaaS and customer-service workflows. Zendesk AI Agents may suit teams that need deeper integration with existing business systems.

No platform should be expected to resolve every Tier 1 issue without escalation.

Shipping updates can often be automated when order and tracking APIs return valid information. Login problems may require identity verification. Account changes may need additional security checks.

The best choice depends on your existing tools, integrations, permissions, and support workflows. Test each platform with real support scenarios before making a decision.

9. Troubleshoot Common Problems

The chatbot gives an old order status

Check whether the agent is calling the live order API. Avoid using old order details stored in prompts or model memory.

Customers can access the wrong order

Verify authentication and order ownership on the backend. Do not rely only on the AI model to enforce access permissions.

Refund requests are submitted twice

Use idempotency keys and check whether a request has already been processed before creating another one.

The chatbot says a refund is complete when it is pending

Return clear status values from the refund API. Instruct the agent to report only the status confirmed by the system.

The API is unavailable

Use controlled retries for temporary errors. If the system still cannot retrieve the information, tell the customer and offer escalation.

Conclusion

Connecting an AI support agent to order and refund APIs can automate common customer questions without sacrificing accuracy.

The responsibilities should remain clear:

  • The AI agent understands the customer's request.
  • APIs retrieve current order and account information.
  • Backend services enforce permissions.
  • Approval workflows protect sensitive actions.
  • Audit logs make every action traceable.

Start with read-only actions such as checking order status. Once those work reliably, introduce controlled actions such as submitting refund requests.

If you want to build more advanced assistants that work with company documents, databases, and APIs, explore custom AI assistants on your data.

The goal is not to automate every support conversation. It is to automate the

Top comments (0)