Anthropic Integration
Using Invariant Gateway with Anthropic
With just a few changes in your Anthropic
client setup, you can start using the Invariant Gateway to visualize and debug your traces with Invariant.
Getting the Invariant API Key
First, you need to obtain your Invariant API key. This key is essential for authenticating your requests to the Invariant Gateway.
Visit the Explorer Documentation to learn how to obtain your own API key.
Setup Anthropic API Key
In your runtime environment, set the Anthropic API key as an environment variable. This is necessary for the Anthropic client to authenticate requests.
Code
You can now use the Invariant Gateway with your Anthropic client as follows:
from anthropic import Anthropic
from httpx import Client
http_client = Client(
headers={
"Invariant-Authorization": "Bearer {your-invariant-api-key}"
},
)
anthropic_client = Anthropic(
http_client=http_client,
base_url="https://explorer.invariantlabs.ai/api/v1/gateway/{your-dataset-name}/anthropic",
)
result = anthropic_client.messages.create(
model="claude-3-5-sonnet-20241022",
messages=[
{"role": "user", "content": "What is the capital of France?"},
],
)
print("result: ", result)
This code will push your trace to the Invariant Explorer where it will be available under the dataset name used above, i.e. your-dataset-name
.