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
  • Agent Governance SDK installed
  • Valid OpenAI and Agent Governance API keys

Basic Setup

Installation

If you haven’t already, install both the OpenAI SDK and Agent Governance SDK:
npm install openai @agent-governance/node

Initialize the Monitor

Use the OpenAIAgentMonitor class for automatic GPT monitoring:
import { OpenAIAgentMonitor } from '@agent-governance/node';
import OpenAI from 'openai';

const monitor = new OpenAIAgentMonitor({
  apiKey: 'your-governance-api-key',
  organizationId: 'your-org-id',
  environment: 'development',
  enableComplianceChecks: true
});

Register Your Agent

Register your GPT-powered agent with the monitoring system:
await monitor.registerAgent({
  id: 'gpt-banking-assistant',
  name: 'GPT Banking Assistant',
  category: 'tool_calling',
  specialty: 'customer_service',
  version: '1.0.0',
  llmProvider: 'openai',
  model: 'gpt-4o',
  description: 'GPT-4o powered customer service assistant',
  complianceSettings: {
    sr11_7_enabled: true,
    fair_lending_monitoring: true,
    bsa_aml_checks: true
  }
});

Wrap the OpenAI Client

Wrap your existing OpenAI client to enable automatic monitoring:
const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY
});

// Wrap the client - all chat completion calls will now be monitored
const monitoredOpenAI = monitor.wrapOpenAI(openai, 'gpt-banking-assistant');

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.
const response = await monitoredOpenAI.chat.completions.create({
  model: 'gpt-4o-mini',
  messages: [
    {
      role: 'system',
      content: 'You are a helpful banking assistant.'
    },
    {
      role: 'user',
      content: 'Can you help me understand my account fees?'
    }
  ],
  max_tokens: 500
});

console.log(response.choices[0].message.content);
// Response: "I'd be happy to help explain your account fees..."

With Function Calling

The integration automatically tracks when GPT decides to call functions (also known as tools).
const response = await monitoredOpenAI.chat.completions.create({
  model: 'gpt-4o',
  messages: [
    {
      role: 'user',
      content: 'What is my current checking account balance?'
    }
  ],
  tools: [
    {
      type: 'function',
      function: {
        name: 'get_account_balance',
        description: 'Get the current balance for a specific account',
        parameters: {
          type: 'object',
          properties: {
            account_type: {
              type: 'string',
              description: 'Type of account (e.g., checking, savings)'
            }
          },
          required: ['account_type']
        }
      }
    }
  ],
  tool_choice: 'auto'
});

// If GPT decides to call a function, it's automatically tracked.
// The 'tool_call' event is logged with the function name and arguments.

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

Comparison with Manual Tracking

The OpenAI wrapper significantly simplifies the monitoring process.

Next Steps