Most support automation fails because it can’t reach real systems. This tutorial builds an autogen support agent order status api integration that lets a conversational agent query a backend orders service and return grounded answers instead of hallucinating tracking numbers.
Prerequisites
- Python 3.10 or newer
pyautogen,flask,requestsinstalled (pip install pyautogen flask requests)- An OpenAI-compatible API key, or a gateway endpoint. If you route through n4n.ai, its single OpenAI-compatible endpoint covers 240+ models and handles provider fallback automatically.
- Basic comfort with REST and function calling
You should be able to run two processes locally: a mock orders API and the AutoGen script.
Stand up a mock order status API
A support bot is only useful if it talks to authoritative data. We’ll spin up a minimal Flask service that mimics an internal orders system.
# mock_orders.py
from flask import Flask, jsonify
app = Flask(__name__)
ORDERS = {
"1001": {"status": "shipped", "eta": "2024-07-20", "items": ["widget"]},
"1002": {"status": "processing", "eta": None, "items": ["gadget"]},
}
@app.route("/orders/<order_id>")
def get_order(order_id):
order = ORDERS.get(order_id)
if not order:
return jsonify({"error": "not found"}), 404
return jsonify(order)
if __name__ == "__main__":
app.run(port=5000)
Run it:
python mock_orders.py
Verify with a quick curl:
curl http://localhost:5000/orders/1001
Expected output:
{"status":"shipped","eta":"2024-07-20","items":["widget"]}
Define the API client function
AutoGen executes local Python functions on behalf of the LLM. The function must accept typed arguments and return JSON-serializable data. We wrap requests so the agent never sees raw HTTP.
# client.py
import requests
def check_order_status(order_id: str) -> dict:
"""Look up order status by ID from the orders API."""
resp = requests.get(f"http://localhost:5000/orders/{order_id}", timeout=5)
if resp.status_code == 404:
return {"error": f"Order {order_id} not found"}
resp.raise_for_status()
return resp.json()
The docstring matters. AutoGen converts it into the OpenAI function schema, so the LLM knows when to call this tool.
Wire up the AutoGen support agent order status api integration
We use two agents: an AssistantAgent that reasons and emits tool calls, and a UserProxyAgent that executes the registered function and returns results.
# agent.py
from autogen import AssistantAgent, UserProxyAgent, register_function
from client import check_order_status
llm_config = {
"config_list": [
{
"model": "gpt-4o",
"api_key": "sk-your-key",
# For n4n.ai, add: "base_url": "https://api.n4n.ai/v1"
}
],
"temperature": 0,
}
assistant = AssistantAgent(
name="support_agent",
llm_config=llm_config,
system_message=(
"You are a customer support agent. "
"Use the check_order_status tool to answer questions about orders. "
"Never invent order details."
),
)
user_proxy = UserProxyAgent(
name="executor",
human_input_mode="NEVER",
max_consecutive_auto_reply=3,
)
register_function(
check_order_status,
caller=assistant,
executor=user_proxy,
name="check_order_status",
)
The autogen support agent order status api link is now live: the assistant can request a lookup, the proxy calls your Flask route, and the JSON flows back into the chat context.
Run a conversation
Initiate a chat from the user proxy side. Because human_input_mode="NEVER", the script runs end-to-end.
user_proxy.initiate_chat(
assistant,
message="Where is my order 1001? What did I buy?",
)
Expected terminal output (abridged):
executor (to support_agent):
Where is my order 1001? What did I buy?
support_agent (to executor):
***** Suggested function call: check_order_status *****
Arguments: {"order_id": "1001"}
executor (to support_agent):
***** Response from calling function "check_order_status" *****
{"status":"shipped","eta":"2024-07-20","items":["widget"]}
support_agent (to executor):
Your order 1001 has shipped and is expected to arrive on 2024-07-20.
It contains: widget.
The LLM grounded its reply in the API response. No fabricated ETAs.
Handle missing orders and errors
Change the user message to a bad ID:
user_proxy.initiate_chat(
assistant,
message="Check order 9999 please.",
)
The Flask route returns 404, our client returns {"error": "Order 9999 not found"}, and the agent should respond honestly:
support_agent (to executor):
I couldn't find an order with ID 9999. Please double-check the number.
If the API throws a 500, resp.raise_for_status() triggers a requests.HTTPError. In production, catch it and return a structured error so the model can apologize or escalate.
def check_order_status(order_id: str) -> dict:
try:
resp = requests.get(f"http://localhost:5000/orders/{order_id}", timeout=5)
resp.raise_for_status()
return resp.json()
except requests.RequestException as e:
return {"error": f"Orders API unavailable: {e}"}
Production considerations
The mock above is single-process and unauthenticated. Real deployments need:
- Auth on the orders API: mTLS or bearer tokens. Pass them via
requestsheaders in the client function. - Timeouts and retries: 5s timeout is fine; backoff on 5xx.
- Caching: order status changes slowly. Cache successful lookups for 30–60s to cut token and latency costs.
- Model routing: if your primary provider is rate-limited, an OpenAI-compatible gateway with automatic fallback keeps the agent responsive. n4n.ai forwards provider cache-control hints and meters per-token usage, which helps track support cost per conversation.
- Function surface: expose only
check_order_status. Don’t give the agent arbitrary SQL or write endpoints unless you have a human approval step.
A hardened client with caching:
import time
import requests
_CACHE = {}
_CACHE_TTL = 45
def check_order_status(order_id: str) -> dict:
if order_id in _CACHE and time.time() - _CACHE[order_id][0] < _CACHE_TTL:
return _CACHE[order_id][1]
try:
resp = requests.get(
f"http://localhost:5000/orders/{order_id}",
timeout=5,
headers={"Authorization": "Bearer service-token"},
)
resp.raise_for_status()
data = resp.json()
_CACHE[order_id] = (time.time(), data)
return data
except requests.RequestException as e:
return {"error": f"Orders API unavailable: {e}"}
Extending the autogen support agent order status api
You can register more tools (cancel_order, list_returns) using the same register_function pattern. Keep the system message strict: “Only call tools you have.” AutoGen’s max_consecutive_auto_reply prevents runaway loops if the API stays broken.
If you need to log every tool call for audit, subclass UserProxyAgent and override execute_function_call — but for most teams, the default executor plus structured error returns is enough to ship a reliable support bot.