HridaSpro Docs
Cookbook

Cookbook / Agents

Build an order-status agent

An agent that looks up orders and issues refunds by calling your own functions, with every run traced.

In this recipe you build a small agent that answers order questions by calling your own functions. The model decides which function to call, your code runs it, and the loop repeats until the model has an answer. Every run is grouped into one trace, so you can see each step.

What you need

  • A Spro API key. See API keys.
  • Python 3.9 or newer, and the OpenAI SDK: pip install openai.
Bash
export SPRO_API_KEY="YOUR_SPRO_API_KEY"

The code

Save this as order_agent.py. The "database" is a Python dictionary, so you can run it as is. Replace it with real calls in your own app.

Python
import jsonimport osimport uuid
from openai import OpenAI
client = OpenAI(    base_url="https://spro.hridaai.com/v1",    api_key=os.environ["SPRO_API_KEY"],)
# --- Your "backend". Swap these for real database or API calls. ---ORDERS = {    "1001": {"status": "shipped", "item": "Desk lamp", "total": 39.0},    "1002": {"status": "processing", "item": "Notebook set", "total": 18.5},}

def lookup_order(order_id: str) -> str:    order = ORDERS.get(order_id)    return json.dumps(order if order else {"error": "order not found"})

def issue_refund(order_id: str) -> str:    order = ORDERS.get(order_id)    if not order:        return json.dumps({"error": "order not found"})    if order["status"] != "shipped":        return json.dumps({"error": "only shipped orders can be refunded here"})    return json.dumps({"refunded": order["total"], "order_id": order_id})

FUNCTIONS = {"lookup_order": lookup_order, "issue_refund": issue_refund}
# --- Tell the model what it can call. ---TOOLS = [    {        "type": "function",        "function": {            "name": "lookup_order",            "description": "Get the status, item and total of an order by its id.",            "parameters": {                "type": "object",                "properties": {"order_id": {"type": "string"}},                "required": ["order_id"],                "additionalProperties": False,            },        },    },    {        "type": "function",        "function": {            "name": "issue_refund",            "description": "Refund a shipped order in full.",            "parameters": {                "type": "object",                "properties": {"order_id": {"type": "string"}},                "required": ["order_id"],                "additionalProperties": False,            },        },    },]

def run_agent(question: str, max_steps: int = 5) -> str:    run_id = f"order-agent-{uuid.uuid4().hex[:8]}"  # groups every step of this run into one trace    messages = [        {            "role": "system",            "content": "You are an order assistant. Use the tools to look things up. "            "Never refund an order unless the customer clearly asks for a refund.",        },        {"role": "user", "content": question},    ]
    for _ in range(max_steps):        response = client.chat.completions.create(            model="gpt-oss-120b",            messages=messages,            tools=TOOLS,            extra_headers={"x-spro-trace-id": run_id, "x-spro-trace-name": "order-agent"},        )        message = response.choices[0].message
        if not message.tool_calls:  # no more tools needed, so this is the final answer            return message.content
        messages.append(message)        for call in message.tool_calls:            args = json.loads(call.function.arguments)            result = FUNCTIONS[call.function.name](**args)            messages.append({"role": "tool", "tool_call_id": call.id, "content": result})
    return "Sorry, I couldn't finish that. Let me pass you to a human."

print(run_agent("Where is order 1001?"))print(run_agent("Please refund order 1001, it arrived broken."))

Run it

Bash
python order_agent.py
Text
Order 1001, a desk lamp, has shipped.I've refunded $39.00 for order 1001.

Your exact wording will differ, because it comes from a model.

How it works

  • The loop. Each turn the model either asks to call a tool or gives a final answer. If it asks for a tool, your code runs the function, adds the result to messages, and calls the model again. See Function calling for the full flow.
  • A step limit. max_steps stops the agent from looping forever if the model keeps asking for tools.
  • A trace per run. Every step carries the same x-spro-trace-id. In Traces in your workspace, search for order-agent to see each model call and the order they ran in. See Tracing.

Make it safer

  • Check before you act. The model decides when to call issue_refund, so put the real checks in your function, as the example does with the order status. Don't rely on the system prompt alone.
  • Limit spend. Add the usage limit guardrails (token, request and cost) so a bad loop can't run up a bill. See Guardrails.

Ideas to extend it

  • Add a send_email tool, and ask for confirmation before it runs.
  • Put the agent behind the support chatbot from Build a support chatbot.