Skip to main content
Agent Therapy’s OpenAI integration lets you pipe conversation history from any OpenAI Assistant directly into the Agent Therapy analysis engine. Once connected, every thread becomes a diagnostic session: Agent Therapy scans message sequences for behavioral anti-patterns — instruction drift, repetitive failure loops, over-refusal, and more — and returns a structured health report with actionable recommendations.

How It Works

Agent Therapy ingests OpenAI thread messages (the full conversation history stored in the Assistants API) and analyzes them for behavioral patterns. When you export a thread and submit it, Agent Therapy reconstructs the turn-by-turn dialogue, identifies the roles involved, and applies its behavioral model to score the assistant’s performance. Results surface in your Agent Therapy dashboard under the registered agent’s profile, and you can retrieve them programmatically via the REST API. No changes to your OpenAI Assistant configuration are required — the integration works entirely on the output side by reading threads after they occur.

Prerequisites

Before you begin, make sure you have the following:
  • An OpenAI API key with access to the Assistants API (beta.threads endpoints)
  • An Agent Therapy account with an active API key — find yours in the Agent Therapy dashboard
  • The openai Python package installed (pip install openai)

Step-by-Step Setup

1

Register your assistant as an agent in Agent Therapy

Before submitting any sessions, register your OpenAI Assistant as an agent so Agent Therapy knows how to categorize and score it. Send a POST request to /agents with the framework field set to "openai".
Store the returned agent_id. You’ll attach it to every session you submit for this assistant.
2

Export thread messages using the OpenAI SDK

After an assistant run completes, retrieve the full message list from the thread. The OpenAI SDK returns messages in reverse-chronological order, so sort them before processing.
Each item in messages.data is a ThreadMessage object containing the role (user or assistant), content blocks, and metadata timestamps.
3

Transform messages to Agent Therapy format

Map the OpenAI ThreadMessage objects to Agent Therapy’s universal behavior_log format. Each turn needs a role, content string, and an optional timestamp.
4

Submit the session to Agent Therapy

With your formatted log ready, submit it as a session via POST /sessions. Agent Therapy queues the session for analysis and returns a session_id you can use to poll for results.

Automated Monitoring

Rather than submitting threads manually, wrap the full pipeline in a single function that you call after every assistant run. This ensures Agent Therapy receives a session for every interaction without extra effort.
You can integrate monitor_thread() into any post-run hook, webhook handler, or polling loop that watches for completed runs via openai_client.beta.threads.runs.retrieve().
Thread size limits: Agent Therapy sessions support up to 500 turns per submission. If your threads exceed this length, split them into chronological chunks of 500 messages and submit each chunk as a separate session with the same agent_id. Use the context field (e.g., "thread_abc123 — part 2 of 3") to keep chunks identifiable in your dashboard.