Guides

Anthropic Provider Configuration

Overview

This guide explains how to configure Anthropic to work with Pay-i Instrumentation. Anthropic is a supported Provider with several Resources that Pay-i can track.

Prerequisites

To use Pay-i with Anthropic, you'll need:

  • Anthropic Service:

  • Pay-i:

  • Client Library:

    • Anthropic Python SDK (pip install anthropic)
    • Optional: httpx for advanced features (pip install httpx)

SDK Support

The examples in this guide use the Pay-i Python SDK, which provides comprehensive support for Anthropic integration. If you're using a different programming language, you can utilize the Pay-i OpenAPI specification to generate a client SDK for your language of choice. The core concepts remain the same, though the exact implementation details may vary depending on the language and client library used.

Basic Setup

Let's start with the common configuration variables needed for both standard and proxy approaches:

import os
import anthropic
from payi.lib.instrument import payi_instrument
from payi.lib.helpers import create_headers

# API keys and configuration
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY", "YOUR_ANTHROPIC_API_KEY")
PAYI_API_KEY = os.getenv("PAYI_API_KEY", "YOUR_PAYI_API_KEY")

Direct Provider Call with Telemetry (Default)

The standard approach makes API calls directly to Anthropic while Pay-i tracks usage:

# Initialize Pay-i instrumentation
payi_instrument()

# Configure direct Anthropic client
client = anthropic.Anthropic(api_key=ANTHROPIC_API_KEY)

# Make a request with annotations
message = client.messages.create(
    model="claude-3-sonnet-20240229",
    max_tokens=1000,
    messages=[{"role": "user", "content": "Hello, how are you?"}],
    headers=create_headers(
        use_case_name="anthropic_example",
        user_id="example_user",
        limit_ids=["anthropic_limit"],
        request_tags=["anthropic-example"]
    )
)

print(message.content[0].text)

With this configuration, Pay-i automatically tracks API calls and calculates costs without adding any latency to your requests. You can also use decorators for tracking multiple related API calls - see Custom Instrumentation for more details.

Optional Proxy Routing (For Block Limits)

If you need to implement Block limits that prevent requests from being sent to the provider when a budget is exceeded, use Pay-i's proxy configuration. Here's what changes compared to the standard approach:

from payi.lib.helpers import payi_anthropic_url
import json

# 1. Initialize with proxy mode enabled
payi_instrument(config={"proxy": True})

# 2. Configure Anthropic client to use Pay-i as a proxy
client = anthropic.Anthropic(
    api_key=ANTHROPIC_API_KEY,
    base_url=payi_anthropic_url(),  # Use Pay-i's URL instead of direct endpoint
    default_headers={"xProxy-api-key": PAYI_API_KEY}  # Include Pay-i proxy headers
)

# Use the client normally
message = client.messages.create(
    model="claude-3-sonnet-20240229",
    max_tokens=1000,
    messages=[{"role": "user", "content": "Hello, how are you?"}]
)

print(message.content[0].text)

# With proxy mode, you get access to real-time cost information
xproxy_result = message.xproxy_result
print(json.dumps(xproxy_result, indent=4))

For detailed information about proxy configuration, including when to use it and how it works, see the Pay-i Proxy Configuration guide.

Advanced Configuration

Note: The following advanced configurations can be applied to both standard and proxy configurations. If you're using proxy configuration, remember to include the base_url=payi_anthropic_url() and default_headers parameters as shown in the proxy configuration example above.

Connection Pooling

For high-throughput applications, you may want to configure connection pooling:

import httpx

# Configure with httpx transport for connection pooling
transport = httpx.HTTPTransport(limits=httpx.Limits(max_connections=100))

# For standard instrumentation
client = anthropic.Anthropic(
    api_key=ANTHROPIC_API_KEY,
    http_client=httpx.Client(transport=transport)
)

# For proxy configuration, you would add:
# base_url=payi_anthropic_url(),
# default_headers={"xProxy-api-key": PAYI_API_KEY}

Timeouts

For operations that might take longer, consider configuring appropriate timeouts:

import httpx

# Configure with longer timeouts for large requests
timeout = httpx.Timeout(timeout=60.0)  # 60 seconds

# For standard instrumentation
client = anthropic.Anthropic(
    api_key=ANTHROPIC_API_KEY,
    http_client=httpx.Client(timeout=timeout)
)

# For proxy configuration, you would add:
# base_url=payi_anthropic_url(),
# default_headers={"xProxy-api-key": PAYI_API_KEY}

Related Resources

Quickstart Guides

Conceptual Guides

Example Repositories