Systems Library / Customer Service / How to Create an AI Chatbot with Order Lookup
Customer Service chatbots

How to Create an AI Chatbot with Order Lookup

Build a chatbot that looks up orders and provides real-time status updates.

Jay Banlasan

Jay Banlasan

The AI Systems Guy

An ai chatbot with order tracking lookup is the most requested feature for ecommerce support bots. "Where is my order?" makes up 30-40% of all support volume for most online stores. This system connects your chatbot to your order database so customers get instant answers.

I build these with tool use, which lets the AI model call functions like querying your order system mid-conversation. The customer talks naturally, the bot extracts the order number, looks it up, and responds with real status.

What You Need Before Starting

Step 1: Define the Order Lookup Tool

Claude's tool use feature lets you define functions the AI can call. Define your order lookup:

tools = [
    {
        "name": "lookup_order",
        "description": "Look up an order by order number or customer email to get status, tracking, and delivery estimate.",
        "input_schema": {
            "type": "object",
            "properties": {
                "order_number": {
                    "type": "string",
                    "description": "The order number (e.g., ORD-12345)"
                },
                "email": {
                    "type": "string",
                    "description": "Customer email address"
                }
            }
        }
    }
]

Step 2: Build the Lookup Function

Connect to your order database and return structured data:

import sqlite3

def lookup_order(order_number=None, email=None):
    conn = sqlite3.connect("orders.db")
    if order_number:
        row = conn.execute(
            "SELECT order_id, status, tracking_number, estimated_delivery, items FROM orders WHERE order_id = ?",
            (order_number,)
        ).fetchone()
    elif email:
        row = conn.execute(
            "SELECT order_id, status, tracking_number, estimated_delivery, items FROM orders WHERE customer_email = ? ORDER BY created_at DESC LIMIT 1",
            (email,)
        ).fetchone()
    else:
        return {"error": "Need an order number or email address"}

    if not row:
        return {"error": "Order not found"}

    return {
        "order_id": row[0],
        "status": row[1],
        "tracking_number": row[2],
        "estimated_delivery": row[3],
        "items": row[4]
    }

Step 3: Handle the Tool Use Flow

When Claude decides it needs order info, it returns a tool use request instead of a text response:

import anthropic
import json

client = anthropic.Anthropic()

def chat_with_tools(messages):
    response = client.messages.create(
        model="claude-sonnet-4-20250514",
        max_tokens=500,
        system="You are a helpful order support agent. Use the lookup_order tool when customers ask about their orders.",
        tools=tools,
        messages=messages
    )

    if response.stop_reason == "tool_use":
        tool_block = next(b for b in response.content if b.type == "tool_use")
        result = lookup_order(**tool_block.input)

        messages.append({"role": "assistant", "content": response.content})
        messages.append({
            "role": "user",
            "content": [{
                "type": "tool_result",
                "tool_use_id": tool_block.id,
                "content": json.dumps(result)
            }]
        })

        final = client.messages.create(
            model="claude-sonnet-4-20250514",
            max_tokens=500,
            system="You are a helpful order support agent.",
            tools=tools,
            messages=messages
        )
        return final.content[0].text

    return response.content[0].text

Step 4: Add Security Verification

Never show order details without verifying the customer. Require email match:

def verify_customer(order_number, provided_email):
    conn = sqlite3.connect("orders.db")
    row = conn.execute(
        "SELECT customer_email FROM orders WHERE order_id = ?",
        (order_number,)
    ).fetchone()
    if row and row[0].lower() == provided_email.lower():
        return True
    return False

Step 5: Handle Common Edge Cases

Build responses for orders that are delayed, lost, or returned:

STATUS_RESPONSES = {
    "processing": "Your order is being prepared. It should ship within 1-2 business days.",
    "shipped": "Your order is on the way! Tracking number: {tracking}. Estimated delivery: {delivery}.",
    "delivered": "Your order was delivered on {delivery}.",
    "returned": "This order has been returned. Your refund should appear within 5-7 business days."
}

What to Build Next

Add proactive notifications. When an order status changes, send the customer an update before they even ask. That reduces inbound volume further and makes customers feel informed.

Related Reading

Want this system built for your business?

Get a free assessment. We will map every system your business needs and show you the ROI.

Get Your Free Assessment

Related Systems