Anthropic SDK wrapper with observability and tracing (Python)
Project description
Prefactor Anthropic SDK (Python)
Python wrapper for the Anthropic SDK with built-in Prefactor observability and tracing.
Installation
pip install prefactor-anthropic
Quick Start
from anthropic import Anthropic
from prefactor_anthropic import wrap_anthropic_client, shutdown
# Create your Anthropic client
client = Anthropic(api_key="your-anthropic-api-key")
# Wrap it with Prefactor observability
wrapped_client = wrap_anthropic_client(client, {
"api_token": "your-prefactor-token",
"agent_id": "your-agent-id"
})
# Use it like normal - all calls are automatically tracked
response = wrapped_client.messages.create(
model="claude-sonnet-4-5-20250929",
max_tokens=1024,
messages=[
{"role": "user", "content": "Hello, Claude!"}
]
)
print(response.content[0].text)
# Gracefully shutdown and flush telemetry
shutdown()
Configuration
Simple Configuration
wrapped_client = wrap_anthropic_client(client, {
"api_url": "https://api.prefactor.ai", # Optional, defaults to env var
"api_token": "your-token", # Required
"agent_id": "your-agent-id", # Optional
"max_input_messages": 10, # Limit message history in spans
"max_output_content_length": 1000, # Truncate long outputs
})
Advanced Configuration
wrapped_client = wrap_anthropic_client(client, {
"prefactor_config": {
"transport_type": "http",
"http_config": {
"api_url": "https://api.prefactor.ai",
"api_token": "your-token",
"agent_id": "your-agent-id",
"agent_identifier": "my-agent-v1",
"agent_name": "My Custom Agent",
"agent_description": "Custom agent for X task"
}
}
})
Environment Variables
You can also configure using environment variables:
export PREFACTOR_API_URL=https://api.prefactor.ai
export PREFACTOR_API_TOKEN=your-token
export PREFACTOR_AGENT_ID=your-agent-id
Then:
# Config will be loaded from environment
wrapped_client = wrap_anthropic_client(client)
Features
- ✅ Automatic tracing - All Anthropic API calls are tracked
- ✅ Token usage tracking - Input/output tokens recorded
- ✅ Streaming support - Works with streaming responses
- ✅ Error tracking - Captures and reports errors
- ✅ Configurable limits - Control message history and output length
- ✅ Type hints - Full typing support with TypedDict
Streaming Example
stream = wrapped_client.messages.stream(
model="claude-sonnet-4-5-20250929",
max_tokens=1024,
messages=[{"role": "user", "content": "Tell me a story"}]
)
for event in stream:
if hasattr(event, 'delta') and hasattr(event.delta, 'text'):
print(event.delta.text, end='', flush=True)
shutdown()
API Reference
wrap_anthropic_client(client, config=None)
Wraps an Anthropic client with Prefactor observability.
Parameters:
client(Anthropic): The Anthropic client to wrapconfig(PrefactorAnthropicConfig, optional): Configuration options
Returns:
AnthropicWrapper: Wrapped client with observability
shutdown()
Gracefully shuts down the Prefactor SDK and flushes pending telemetry data.
Development
# Install in development mode
pip install -e ".[dev]"
# Run tests
pytest
# Type checking
mypy src/
License
MIT
Links
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file matts_starter_prefactor-0.1.0.tar.gz.
File metadata
- Download URL: matts_starter_prefactor-0.1.0.tar.gz
- Upload date:
- Size: 9.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e7949b22cd5a0995eed3920f86e10f82726a727c0fb58c47fdc451a24ca3daf7
|
|
| MD5 |
7617cf3596425f413ab745dfcb9305f0
|
|
| BLAKE2b-256 |
7b6b4c6e4ee1c300fcedbcc26495c23dbc5e38f8b149ea8ee15f1b5b311fc108
|
File details
Details for the file matts_starter_prefactor-0.1.0-py3-none-any.whl.
File metadata
- Download URL: matts_starter_prefactor-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2331b3edef44af1e92ae7cdbc7f36c79c9dba203d8eef82efebd404b089b3b00
|
|
| MD5 |
a9c7526c925c8033f52db4335f223062
|
|
| BLAKE2b-256 |
deaee5259e50b1a0029fd2814b3bd77531fb2c08b765695ced0f2fe97152bf5d
|