An open API service indexing awesome lists of open source software.

https://github.com/jamesdconley/anthropic-proxy-logger


https://github.com/jamesdconley/anthropic-proxy-logger

Last synced: 3 months ago
JSON representation

Awesome Lists containing this project

README

          

# Anthropic API Logger

A proxy server that logs all requests to and responses from the Anthropic API, allowing for detailed analysis of all your AI tools.

Fully compatible with the Anthropic API, just change your base URL and go.

## Overview

This project provides a proxy server that sits between your application and the Anthropic API. It forwards all requests to Anthropic, logs the complete request and response data to disk, and returns the responses to your application unchanged. This enables:

- Detailed logging of all API interactions
- Analysis of AI model responses
- Debugging and monitoring of API usage

## Features

- **Complete API Proxy**: Transparently proxies all requests to Anthropic's API
- **Request/Response Logging**: Saves all API interactions to disk in JSON format
- **Streaming Support**: Handles both standard and streaming API requests
- **Docker Support**: Easy deployment using Docker
- **Testing**: Includes tests to verify proxy functionality

## Installation

### Prerequisites

Python or Docker

### Local Installation

1. Clone the repository:
```bash
git clone
cd anthropic-logger
```

2. Install dependencies:
```bash
pip install -r requirements.txt
```

3. If you intend to run the tests, run the server and set your API key as an environment variable.
```
export ANTHROPIC_API_KEY=your-api-key
```

### Docker Installation

1. Clone the repository:
```bash
git clone
cd anthropic-logger
```

2. Run the provided script to build and start the Docker container:
```bash
./run.sh
```

## Usage

### Starting the Proxy Server

I recommend running with Docker which uses gunicorn for real world usage. Running directly will use the flask development server, which is more useful for testing.

#### Local Execution

```bash
python src/server.py
```

By default, the server runs on `http://0.0.0.0:5001`.

#### Docker Execution

```bash
./run.sh
```

### Configuring Your Application

Update your application to use the proxy server instead of the direct Anthropic API:

1. Change the API base URL from `https://api.anthropic.com` to `http://localhost:5001`
2. Keep all other API parameters the same (API key, headers, etc.)

Example with the Anthropic Python client:

```python
import anthropic

# Instead of using the default endpoint
client = anthropic.Anthropic(
api_key="your-api-key",
base_url="http://localhost:5001" # Point to the proxy
)

# Use the client as normal
response = client.messages.create(
model="claude-3-5-haiku-20241022",
max_tokens=100,
messages=[
{"role": "user", "content": "Hello, Claude!"}
]
)
```

### Log Files

Logs are stored in the `logs` directory in JSON format. Each log file contains:

- Timestamp and request ID
- API endpoint called
- Complete request data
- Complete response data
- For streaming responses, reconstructed content and events sequence

## Configuration

The proxy server can be configured using environment variables:

| Variable | Description | Default |
|----------|-------------|---------|
| `PORT` | Port to run the server on | `5001` |
| `HOST` | Host to bind the server to | `0.0.0.0` |
| `LOGS_DIR` | Directory to store log files | `./logs` |

## Testing

To test the proxy server:

1. Ensure your Anthropic API key is set in the environment:
```bash
export ANTHROPIC_API_KEY=your-api-key
```

2. Run the test script:
```bash
./test.sh
```

The tests verify that the proxy correctly handles both standard and streaming requests.

## Project Structure

```
anthropic-logger/
├── src/
│ ├── server.py # Main proxy server implementation
│ └── test_proxy.py # Tests for the proxy server
├── logs/ # Directory for storing log files
├── Dockerfile # Docker configuration
├── requirements.txt # Python dependencies
├── run.sh # Script to build and run the Docker container
└── test.sh # Script to run tests
```