DEV Community

shashank ms
shashank ms

Posted on

Mastering OpenAI SDK: A Step-by-Step Tutorial

In this tutorial we will build a working support ticket triage agent that reads raw customer emails, classifies the issue, assigns priority, and drafts a first response. It is aimed at engineering teams who want to automate level-zero support without managing complex infrastructure. We will run everything through Oxlo.ai using the standard OpenAI SDK, so the code you write here drops into any existing Python project.

What you'll need

  • Python 3.10 or newer.
  • The OpenAI SDK: pip install openai.
  • An Oxlo.ai API key from https://portal.oxlo.ai. The free tier includes 60 requests per day, which is enough to follow along.

Step 1: Initialize the client

Import the SDK and point the client at Oxlo.ai. The only difference from a standard OpenAI setup is the base_url.

from openai import OpenAI

client = OpenAI(
    base_url="https://api.oxlo.ai/v1",
    api_key="YOUR_OXLO_API_KEY",
)

response = client.chat.completions.create(
    model="llama-3.3-70b",
    messages=[
        {"role": "user", "content": "Confirm the client is working with one word."},
    ],
)

print(response.choices[0].message.content)

Step 2: Write the triage system prompt

The agent behavior lives in the system prompt. I force strict JSON output so downstream code never has to guess what the model will say.

SYSTEM_PROMPT = """You are a support triage agent. Read the customer ticket below and produce a structured assessment.

Output strict JSON with exactly these keys:
- issue_type: one of [billing, technical, account_access, feature_request]
- priority: one of [low, medium, high, critical]
- summary: a one-sentence summary
- draft_reply: a brief, polite first response

Return only raw JSON. No markdown fences, no explanation."""

Step 3: Format an incoming ticket

I wrap incoming tickets in a consistent format so the model sees the same structure every time. Here is a realistic technical ticket.

def format_ticket(subject, body):
    return f"Subject: {subject}\n\nBody:\n{body}"

ticket = format_ticket(
    subject="Export button throws 500 after last night's update",
    body=(
        "Hi team,\n\n"
        "Since the update around midnight, clicking Export on the dashboard "
        "returns a 500 error. This is blocking our morning standup.\n\n"
        "Thanks,\nAlex"
    )
)

Step 4: Call the model and parse structured output

Now we send the ticket to Oxlo.ai. I use Llama 3.3 70B because it is reliable for structured instruction following, then parse the JSON response.

import json

response = client.chat.completions.create(
    model="llama-3.3-70b",
    messages=[
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": ticket},
    ],
)

raw = response.choices[0].message.content
result = json.loads(raw)
print(json.dumps(result, indent=2))

Step 5: Add multi-turn memory for follow-ups

Real conversations do not end after one turn. We can keep the thread alive by appending the assistant reply and any new user context, then calling the model again. Swapping to Qwen 3 32B here is trivial because Oxlo.ai uses the same SDK for every model.

messages = [
    {"role": "system", "content": SYSTEM_PROMPT},
    {"role": "user", "content": ticket},
    {"role": "assistant", "content": raw},
    {
        "role": "user",
        "content": (
            "Update from customer: the error only happens in Chrome. "
            "Safari works fine."
        ),
    },
]

response = client.chat.completions.create(
    model="qwen-3-32b",
    messages=messages,
)

updated = json.loads(response.choices[0].message.content)
print(json.dumps(updated, indent=2))

Run it

Save the complete script as triage.py, set your key, and run it. You should see structured output immediately because Oxlo.ai serves popular models with no cold starts.

$ export OXLO_API_KEY="oxlo_..."
$ python triage.py

Expected output:

{
  "issue_type": "technical",
  "priority": "high",
  "summary": "Dashboard export returns 500 error after update.",
  "draft_reply": "Thanks for the details, Alex. We are investigating the 500 error on exports and will share an update within 30 minutes."
}

Wrap-up and next steps

This agent is already useful as a CLI tool, but the obvious next step is to wire it into a webhook so it processes tickets as they arrive. If you need to pass in long conversation histories or full log dumps, Oxlo.ai's request-based pricing keeps costs flat per call regardless of input length. You can compare plans at https://oxlo.ai/pricing. Another solid upgrade is adding function calling so the agent can create issues in Jira or Linear automatically, which Oxlo.ai supports on the same chat completions endpoint.

Top comments (0)