Skip to main content
The OpenAI integration provides automatic monitoring for GPT API calls with minimal code changes. Simply wrap your existing OpenAI client to start tracking all chat completions, token usage, costs, and compliance violations.

Prerequisites

  • openai package installed
  • AI Agents House SDK installed
  • Valid OpenAI and AI Agents House API keys

Basic Setup

Installation

If you haven’t already, install both the OpenAI SDK and AI Agents House SDK:

Initialize the Monitor

Use the OpenAIAgentMonitor class for automatic GPT monitoring:

Register Your Agent

Register your GPT-powered agent with the monitoring system:

Wrap the OpenAI Client

Wrap your existing OpenAI client to enable automatic monitoring:

Usage Examples

Basic Chat Completion

Use the wrapped client exactly as you would the original openai client. All calls to chat.completions.create will be monitored automatically.

With Function Calling

The integration automatically tracks when GPT decides to call functions (also known as tools).

Automatic Tracking Features

The OpenAI integration automatically captures:
  • Conversation Events: User messages, agent responses, and function calls.
  • Performance Metrics: API call latency, token usage (prompt and completion), and calculated costs.
  • Compliance Monitoring: Real-time scanning of agent responses for PII, Fair Lending violations, and BSA/AML keywords.

Cost Tracking

The integration automatically calculates costs based on OpenAI’s pricing for various models.
  • GPT-4o: 5input/5 input / 15 output per 1M tokens
  • GPT-4o Mini: 0.15input/0.15 input / 0.60 output per 1M tokens
Costs are calculated based on the usage data returned by the API and are included in the tracked agent_response event.

Best Practices

Troubleshooting

Ensure your apiKey and organizationId are correct. For scripts that exit quickly, you may need to call await monitor.shutdown() to ensure the final batch of events is sent.
Session context must be configured when you call wrapOpenAI. The OpenAI SDK does not have a built-in concept of a session, so you must provide it to the wrapper.
The SDK tracks function calls when the API response includes a tool_calls object. Verify that your prompt is successfully inducing the model to use your defined tools.
The wrapper identifies the user message by looking for the last message in the messages array with role: 'user'. Ensure your conversation history is structured correctly.

Comparison with Manual Tracking

The OpenAI wrapper significantly simplifies the monitoring process.

Next Steps

Anthropic Integration

Learn about monitoring Claude models

Manual Tracking

Explore advanced event tracking capabilities for custom integrations.

Compliance Engine

Deep dive into real-time compliance monitoring.

Configuration

Review advanced SDK configuration options.