The Anthropic integration provides automatic monitoring for Claude API calls with minimal code changes. Simply wrap your existing Anthropic client to start tracking all interactions, token usage, costs, and compliance violations.

Prerequisites

  • @anthropic-ai/sdk package installed
  • Agent Governance SDK installed
  • Valid Anthropic and Agent Governance API keys

Basic Setup

Installation

If you haven’t already, install both the Anthropic SDK and Agent Governance SDK:
npm install @anthropic-ai/sdk @agent-governance/node

Initialize the Monitor

Use the AnthropicAgentMonitor class for automatic Claude monitoring:
import { AnthropicAgentMonitor } from '@agent-governance/node';
import Anthropic from '@anthropic-ai/sdk';

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

Register Your Agent

Register your Claude-powered agent with the monitoring system:
await monitor.registerAgent({
  id: 'banking-claude',
  name: 'Banking Assistant Claude',
  category: 'tool_calling',
  specialty: 'personal_banking',
  version: '1.0.0',
  llmProvider: 'anthropic',
  model: 'claude-3-5-sonnet-20241022',
  description: 'Claude-powered banking assistant',
  complianceSettings: {
    sr11_7_enabled: true,
    fair_lending_monitoring: true,
    bsa_aml_checks: true
  }
});

Wrap the Anthropic Client

Wrap your existing Anthropic client to enable automatic monitoring:
const anthropic = new Anthropic({
  apiKey: process.env.ANTHROPIC_API_KEY
});

// Wrap the client - all API calls will now be monitored
const monitoredAnthropic = monitor.wrapAnthropic(anthropic, 'banking-claude');

Usage Examples

Basic Conversation

// Use the wrapped client exactly like the normal Anthropic client
const response = await monitoredAnthropic.messages.create({
  model: 'claude-3-5-sonnet-20241022',
  max_tokens: 1000,
  messages: [
    {
      role: 'user',
      content: 'Can you help me understand my account balance?'
    }
  ],
  sessionId: 'customer-session-456' // Optional: links events together
});

console.log(response.content[0].text);
// Response: "I'd be happy to help you understand your account balance..."

With Tool Calls

The integration automatically tracks when Claude decides to call tools:
const response = await monitoredAnthropic.messages.create({
  model: 'claude-3-5-sonnet-20241022',
  max_tokens: 1000,
  messages: [
    {
      role: 'user',
      content: 'What is my current checking account balance?'
    }
  ],
  tools: [
    {
      name: 'get_account_balance',
      description: 'Get the current balance for a bank account',
      input_schema: {
        type: 'object',
        properties: {
          account_type: {
            type: 'string',
            description: 'Type of account (checking, savings, etc.)'
          }
        },
        required: ['account_type']
      }
    }
  ],
  sessionId: 'banking-session-789'
});

// If Claude decides to call a tool, it's automatically tracked
// Tool calls are logged with parameters and execution details

Multi-turn Conversations

For conversations with multiple exchanges, including tool results:
// Initial user message
let messages = [
  { role: 'user', content: 'I want to check my account and transfer money' }
];

let response = await monitoredAnthropic.messages.create({
  model: 'claude-3-5-sonnet-20241022',
  messages,
  tools: bankingTools,
  sessionId: 'banking-session-123'
});

// Add Claude's response to conversation
messages.push({ role: 'assistant', content: response.content });

// If Claude used a tool, add the tool result
if (response.content.some(content => content.type === 'tool_use')) {
  messages.push({
    role: 'user',
    content: [
      {
        type: 'tool_result',
        tool_use_id: 'tool_abc123',
        content: JSON.stringify({ balance: 2547.83, currency: 'USD' })
      }
    ]
  });

  // Continue the conversation
  response = await monitoredAnthropic.messages.create({
    model: 'claude-3-5-sonnet-20241022',
    messages,
    sessionId: 'banking-session-123' // Same session ID
  });
}

Automatic Tracking Features

The Anthropic integration automatically captures:

Conversation Events

  • Conversation Start: When a new session begins
  • User Messages: All user inputs including text and tool results
  • Agent Responses: Claude’s text responses
  • Tool Calls: When Claude decides to use a tool

Performance Metrics

  • Latency: Time taken for each API call
  • Token Usage: Input, output, and total tokens
  • Cost Calculation: Automatic cost tracking based on model pricing
  • Response Quality: Optional quality scoring

Compliance Monitoring

  • PII Detection: Scans for personal information in responses
  • Fair Lending: Checks for discriminatory language
  • BSA/AML: Monitors for suspicious activity indicators

Configuration Options

Session Management

Link related interactions by providing a sessionId:
const response = await monitoredAnthropic.messages.create({
  model: 'claude-3-5-sonnet-20241022',
  messages: [{ role: 'user', content: 'Hello' }],
  sessionId: 'unique-session-id' // Groups related events
});

Monitoring Options

Pass additional monitoring configuration:
const monitoredAnthropic = monitor.wrapAnthropic(anthropic, 'agent-id', {
  userId: 'customer-123',
  trackConversationStart: true, // Default: true
  agentCategory: 'tool_calling',
  agentSpecialty: 'personal_banking'
});

Custom User Identification

Track which customer is interacting with the agent:
const response = await monitoredAnthropic.messages.create({
  model: 'claude-3-5-sonnet-20241022',
  messages: [{ role: 'user', content: 'What services do you offer?' }],
  sessionId: 'session-456',
  userId: 'customer-789' // Links events to specific user
});

Cost Tracking

The integration automatically calculates costs based on Claude’s pricing:
  • Input: $3 per million tokens
  • Output: $15 per million tokens
Costs are automatically calculated and included in the tracked events:
// Cost calculation happens automatically
const response = await monitoredAnthropic.messages.create({
  model: 'claude-3-haiku-20240307', // Lower cost model
  messages: [{ role: 'user', content: 'Quick question' }]
});

// Check your governance dashboard for cost analytics

Error Handling

The wrapper preserves normal error handling while tracking errors:
try {
  const response = await monitoredAnthropic.messages.create({
    model: 'claude-3-5-sonnet-20241022',
    messages: [{ role: 'user', content: 'Hello' }],
    sessionId: 'session-123'
  });
} catch (error) {
  // Error is automatically tracked and then re-thrown
  console.error('API call failed:', error.message);
  // Handle error normally
}

Banking Example

Here’s a complete example for a banking use case:
import { AnthropicAgentMonitor } from '@agent-governance/node';
import Anthropic from '@anthropic-ai/sdk';

async function bankingAssistantExample() {
  // Initialize monitor
  const monitor = new AnthropicAgentMonitor({
    apiKey: process.env.AGENT_GOVERNANCE_API_KEY,
    organizationId: 'first-national-bank',
    environment: 'production',
    enableComplianceChecks: true
  });

  // Register banking agent
  await monitor.registerAgent({
    id: 'personal-banking-claude',
    name: 'Personal Banking Assistant',
    category: 'tool_calling',
    specialty: 'personal_banking',
    version: '2.1.0',
    llmProvider: 'anthropic',
    model: 'claude-3-5-sonnet-20241022',
    description: 'AI assistant for personal banking services',
    availableTools: [
      {
        name: 'get_account_summary',
        description: 'Retrieves account balance and recent transactions',
        category: 'banking',
        riskLevel: 'medium'
      },
      {
        name: 'transfer_funds',
        description: 'Transfers money between accounts',
        category: 'banking',
        riskLevel: 'high'
      }
    ],
    complianceSettings: {
      sr11_7_enabled: true,
      fair_lending_monitoring: true,
      bsa_aml_checks: true
    }
  });

  // Initialize Claude client
  const anthropic = new Anthropic({
    apiKey: process.env.ANTHROPIC_API_KEY
  });

  // Wrap with monitoring
  const monitoredClaude = monitor.wrapAnthropic(
    anthropic,
    'personal-banking-claude',
    {
      agentCategory: 'tool_calling',
      agentSpecialty: 'personal_banking'
    }
  );

  // Handle customer interaction
  const sessionId = `banking-${Date.now()}`;
  const response = await monitoredClaude.messages.create({
    model: 'claude-3-5-sonnet-20241022',
    max_tokens: 1000,
    messages: [
      {
        role: 'user',
        content: 'I need to check my account balance and possibly transfer some money to my savings account.'
      }
    ],
    tools: bankingTools, // Your banking tool definitions
    sessionId: sessionId
  });

  console.log('Claude Response:', response.content[0].text);

  // Cleanup
  await monitor.shutdown();
}

bankingAssistantExample().catch(console.error);

Best Practices

Troubleshooting

Next Steps