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:Initialize the Monitor
Use theOpenAIAgentMonitor
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 tochat.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 Models
- GPT-4 Turbo
- GPT-3.5 Turbo
- GPT-4o: 15 output per 1M tokens
- GPT-4o Mini: 0.60 output per 1M tokens
agent_response
event.
Best Practices
Troubleshooting
Events not appearing in dashboard
Events not appearing in dashboard
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 tracking is not working
Session tracking is not working
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.Function calls are not being tracked
Function calls are not being tracked
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.User messages are not detected correctly
User messages are not detected correctly
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.- Automatic (Recommended)
- Manual Tracking