How to beginner · 3 min read

AI for order tracking queries

Quick answer
Use a conversational AI model like gpt-4o to handle order tracking queries by integrating it with your order database or API. The AI can parse user input, fetch order status, and respond naturally using chat.completions.create calls.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the openai Python package and set your API key as an environment variable for secure access.

bash
pip install openai>=1.0
output
Collecting openai
  Downloading openai-1.x.x-py3-none-any.whl (xx kB)
Installing collected packages: openai
Successfully installed openai-1.x.x

Step by step

This example shows how to build a simple order tracking chatbot that accepts an order ID and returns a mock status using gpt-4o. Replace the get_order_status function with your real order API.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

def get_order_status(order_id: str) -> str:
    # Mock order status lookup
    mock_db = {
        "12345": "Shipped and expected delivery in 3 days.",
        "67890": "Processing and will ship tomorrow.",
        "54321": "Delivered on 2026-04-10."
    }
    return mock_db.get(order_id, "Order ID not found.")


def create_prompt(order_id: str) -> str:
    status = get_order_status(order_id)
    return f"User asked about order {order_id}. The status is: {status}"


def main():
    order_id = input("Enter your order ID: ")
    prompt = create_prompt(order_id)
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": prompt}]
    )
    print("AI response:", response.choices[0].message.content)


if __name__ == "__main__":
    main()
output
Enter your order ID: 12345
AI response: Your order 12345 has been shipped and is expected to be delivered in 3 days.

Common variations

  • Use async calls with await client.chat.completions.create(...) for non-blocking apps.
  • Switch to claude-3-5-sonnet-20241022 for Anthropic's Claude model with similar usage.
  • Implement streaming responses by setting stream=True and processing chunks for real-time UI updates.
python
import asyncio
import os
from openai import OpenAI

async def async_main():
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    order_id = "67890"
    prompt = f"Check status for order {order_id}."
    stream = await client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": prompt}],
        stream=True
    )
    async for chunk in stream:
        delta = chunk.choices[0].delta.content or ""
        print(delta, end="", flush=True)

if __name__ == "__main__":
    asyncio.run(async_main())
output
Processing and will ship tomorrow.

Troubleshooting

  • If you get authentication errors, verify your OPENAI_API_KEY environment variable is set correctly.
  • For unexpected empty responses, check your prompt formatting and model name.
  • Rate limit errors require retry logic or upgrading your API plan.

Key Takeaways

  • Use gpt-4o with chat.completions.create to handle natural language order tracking queries.
  • Integrate your order database or API to provide real-time status inside AI prompts.
  • Leverage async and streaming for responsive user experiences in chatbots.
  • Validate environment variables and model names to avoid common API errors.
Verified 2026-04 · gpt-4o, claude-3-5-sonnet-20241022
Verify ↗