OpenAI Assistants Integration Guide
Track OpenAI Assistants API run costs and events in OpsViz.
How it works in 30 seconds
- 1.Create an agent in OpsViz → get your webhook URL (10 seconds)
- 2.Add one step to your OpenAI Assistants workflow → paste the URL (2 minutes)
- 3.See costs, tokens, and alerts in real time (instantly)
Privacy note: OpsViz receives only operational metadata — event type, token counts, model name, cost, and duration. Your prompts, responses, user data, and AI provider credentials are never sent to OpsViz.
Set up with AI — copy and paste
The fastest way to add OpsViz monitoring is to paste this prompt into your AI coding assistant. It sets everything up automatically.
Using ChatGPT code assistant
Paste this prompt into ChatGPT or Claude Code with your code attached:
I use the OpenAI Assistants API and want to track costs with OpsViz. After each run completes, read the usage data from the run object (run.usage.prompt_tokens, run.usage.completion_tokens) and POST it to [WEBHOOK_URL] with event_type, model, tokens_in, tokens_out, cost_usd, agent_name, and duration_ms. Make the request non-blocking. Show me where to add this in my polling loop or webhook handler.
Prerequisites
- OpenAI Python or Node.js SDK
- An OpsViz account with an agent created
- Your OpsViz webhook URL
1
Report after each run completes
import openai, requests, time
OPSVIZ_URL = "[WEBHOOK_URL]"
def run_assistant_with_tracking(thread_id, assistant_id, agent_name="My Assistant"):
start = time.time()
run = openai.beta.threads.runs.create_and_poll(
thread_id=thread_id,
assistant_id=assistant_id
)
duration_ms = int((time.time() - start) * 1000)
try:
requests.post(OPSVIZ_URL, json={
"event_type": "task.completed" if run.status == "completed" else "task.failed",
"agent_name": agent_name,
"model": run.model,
"tokens_in": run.usage.prompt_tokens if run.usage else None,
"tokens_out": run.usage.completion_tokens if run.usage else None,
"duration_ms": duration_ms,
"status": run.status,
"metadata": {"run_id": run.id, "thread_id": thread_id}
}, timeout=5)
except Exception:
pass # Never let monitoring block your agent
return run2
Handle streaming runs (webhook-based)
# In your webhook handler for run.completed events:
def handle_run_completed(run_data):
try:
requests.post(OPSVIZ_URL, json={
"event_type": "task.completed",
"agent_name": "My Assistant",
"model": run_data["model"],
"tokens_in": run_data.get("usage", {}).get("prompt_tokens"),
"tokens_out": run_data.get("usage", {}).get("completion_tokens"),
}, timeout=5)
except Exception:
passThat's it!
Events will appear on your OpsViz dashboard as they arrive. Check the webhook reference for full payload options.