> ## Documentation Index
> Fetch the complete documentation index at: https://docs.aiagentshouse.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Anthropic Integration

> Automatically monitor Claude API calls with seamless integration

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
* AI Agents House SDK installed
* Valid Anthropic and AI Agents House API keys

## Basic Setup

### Installation

If you haven't already, install both the Anthropic SDK and AI Agents House SDK:

```bash theme={null}
npm install @anthropic-ai/sdk @agent-governance/node
```

### Initialize the Monitor

Use the `AnthropicAgentMonitor` class for automatic Claude monitoring:

```javascript theme={null}
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:

```javascript theme={null}
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:

```javascript theme={null}
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

```javascript theme={null}
// 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:

```javascript theme={null}
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:

```javascript theme={null}
// 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`:

```javascript theme={null}
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:

```javascript theme={null}
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:

```javascript theme={null}
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:

<Tabs>
  <Tab title="Claude 3.5 Sonnet">
    * Input: \$3 per million tokens
    * Output: \$15 per million tokens
  </Tab>

  <Tab title="Claude 3 Haiku">
    * Input: \$0.25 per million tokens
    * Output: \$1.25 per million tokens
  </Tab>

  <Tab title="Claude 3 Opus">
    * Input: \$15 per million tokens
    * Output: \$75 per million tokens
  </Tab>
</Tabs>

Costs are automatically calculated and included in the tracked events:

```javascript theme={null}
// 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:

```javascript theme={null}
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:

```javascript theme={null}
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

<Tips>
  ### Session Management

  * Use consistent session IDs for related interactions
  * Include user IDs when possible for better analytics
  * Start new sessions for different customer interactions

  ### Tool Integration

  * Register all available tools in your agent configuration
  * Tool calls are automatically tracked when Claude decides to use them
  * Tool results passed back to Claude are also monitored

  ### Error Handling

  * The wrapper preserves normal Anthropic error handling
  * All errors are automatically logged for debugging
  * Use try/catch blocks as you normally would

  ### Performance

  * The monitoring wrapper adds minimal latency
  * Events are batched and sent asynchronously
  * No impact on your normal Claude API experience
</Tips>

## Troubleshooting

<AccordionGroup>
  <Accordion title="Events not appearing in dashboard">
    Ensure you've properly configured your API key and organization ID. Check that `monitor.flush()` is called if you're not seeing events immediately.
  </Accordion>

  <Accordion title="Cost calculations seem incorrect">
    Verify you're using the correct model name. Cost calculations are based on the model specified in your API call.
  </Accordion>

  <Accordion title="Compliance violations not detected">
    Make sure `enableComplianceChecks: true` is set in your monitor configuration. Compliance checks only run on agent responses.
  </Accordion>

  <Accordion title="Tool calls not being tracked">
    Tool calls are automatically detected when Claude returns `tool_use` content. Ensure your tools are properly configured in your Claude API call.
  </Accordion>
</AccordionGroup>

## Next Steps

<CardGroup cols={2}>
  <Card title="OpenAI Integration" icon="brain" href="/integrations/openai">
    Learn about monitoring GPT models
  </Card>

  <Card title="Manual Tracking" icon="hand" href="/features/manual-tracking">
    Advanced event tracking capabilities
  </Card>

  <Card title="Compliance Engine" icon="shield-check" href="/features/compliance-engine">
    Deep dive into compliance monitoring
  </Card>

  <Card title="Configuration" icon="gear" href="/features/configuration">
    Advanced configuration options
  </Card>
</CardGroup>
