DEV Community

shashank ms
shashank ms

Posted on

Using OpenAI SDK with LLM: A Step-by-Step Guide

I built a lightweight support ticket triage agent that classifies incoming messages and drafts first replies. It runs entirely through the OpenAI SDK pointed at Oxlo.ai, so you can drop it into any existing Python codebase without new dependencies. I host the inference on Oxlo.ai because flat per-request pricing keeps costs predictable even when customer threads get long.

What you'll need

I recommend exporting the key as an environment variable so it never touches disk.

Step 1: Initialize the OpenAI client for Oxlo.ai

The OpenAI SDK lets you override the base URL and API key. Pointing it to Oxlo.ai takes two lines. I test the connection with a trivial prompt to confirm authentication works before adding any business logic.

from openai import OpenAI
import os

client = OpenAI(
    base_url="https://api.oxlo.ai/v1",
    api_key=os.environ["OXLO_API_KEY"]
)

# verify connectivity
ping = client.chat.completions.create(
    model="llama-3.3-70b",
    messages=[{"role": "user", "content": "Reply with OK"}]
)
print(ping.choices[0].message.content)

Step 2: Define the system prompt

The system prompt is the agent's job description. I keep it explicit about output format because the downstream code will parse the result as JSON. Store it in a module-level constant so it is easy to tweak without touching business logic.

SYSTEM_PROMPT = """You are a support triage agent for a B2B SaaS platform.
Analyze the customer message and return a JSON object with exactly these keys:
  - issue_type: billing, technical, account, or general
  - urgency: high, medium, or low
  - sentiment: positive, neutral, or frustrated
  - draft_reply: a concise, empathetic first response under 120 words

If the issue is billing or account related, do not ask follow-up questions.
Instead, state that the correct team will follow up within 24 hours."""

Step 3: Build the triage function

This function sends the customer message to Oxlo.ai with JSON mode enabled. I use llama-3.3-70b here because it follows structured instructions reliably. The response is parsed into a Python dictionary and returned.

import json

def triage(customer_message: str) -> dict:
    response = client.chat.completions.create(
        model="llama-3.3-70b",
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": customer_message}
        ],
        response_format={"type": "json_object"}
    )
    
    raw = response.choices[0].message.content
    return json.loads(raw)

Step 4: Add escalation logic and formatting

Raw JSON is not enough. I wrap the triage call in a processor that prints readable output and raises an alert when urgency is high. This is the layer you would extend to post to Slack or PagerDuty.

def process_ticket(text: str) -> dict:
    result = triage(text)
    
    print(f"Type: {result['issue_type']}")
    print(f"Urgency: {result['urgency']}")
    print(f"Sentiment: {result['sentiment']}")
    
    if result["urgency"] == "high":
        print("ESCALATION: Notify on-call immediately.")
    
    print(f"\nDraft reply:\n{result['draft_reply']}\n")
    return result

Step 5: Wire the entrypoint

I use a hardcoded sample ticket so the script is runnable the moment you clone it. In production you would swap this for an incoming webhook or mailbox reader.

if __name__ == "__main__":
    sample = (
        "You charged my card twice for the Pro plan this month. "
        "I already emailed last week and got no response. "
        "Fix this today or I am initiating a chargeback."
    )
    
    process_ticket(sample)

Run it

Save everything to support_triage.py, export your key, and run it.

$ export OXLO_API_KEY="sk-oxlo.ai-..."
$ python support_triage.py
OK
Type: billing
Urgency: high
Sentiment: frustrated
ESCALATION: Notify on-call immediately.

Draft reply:
I sincerely apologize for the duplicate charge and the delayed response. I have immediately escalated this to our billing team, who will contact you within 24 hours to confirm the refund. We are also reviewing why your previous email was missed.

Next steps

Feed the draft_reply into an async mailer like Celery or a serverless function so the agent responds without blocking the request path. If you want stronger reasoning for ambiguous tickets, swap llama-3.3-70b for kimi-k2.6, or experiment with deepseek-v3.2 on the Oxlo.ai free tier while you iterate on the prompt.

Top comments (0)