LLM API 代理与日志追踪系统 - 透明观察智能体行为
Project description
Agent-Trace-Log - LLM API Logging & Tracing System
An OpenAI-compatible LLM API proxy service focused on logging and agent behavior observation.
Why Agent-Trace-Log?
When learning agent frameworks, you might wonder:
- What prompts does the agent send?
- How does it think and make decisions?
- How are messages passed in multi-turn conversations?
Most proxy APIs or official APIs don't provide log viewing functionality. Agent-Trace-Log makes everything transparent.
Features
- ✅ OpenAI/Anthropic Compatible - Supports both OpenAI and Anthropic API formats
- ✅ Streaming Support - Full SSE streaming response support
- ✅ Complete Logging - Request/response automatically recorded to SQLite database
- ✅ Web Dashboard - Visual log viewing, prompts, and model calls
- ✅ Multi-Provider Support - Configure multiple upstream APIs with flexible switching
- ✅ API Authentication - Supports multiple API key authentication
- ✅ Rate Limiting - Configurable requests per minute limit
- ✅ Model Mapping - Custom model name mapping
- ✅ Docker Deployment - Containerized deployment support
- ✅ pip Installation - Install directly from PyPI
Quick Start
Option 1: Install from PyPI
# Install from PyPI
pip install agent-trace-log
# Start the service
agent-trace-log web
# Specify port
agent-trace-log web --port 9000
Option 2: Install from Source
# Clone the project
git clone https://github.com/xiaozhiagi/agent-trace-log.git
cd agent-trace-log
# Install in development mode
pip install -e .
# Start the service
agent-trace-log web
# Development mode (hot reload)
agent-trace-log web --reload
# Or run directly
python agenttrace/main.py
Option 3: Docker Deployment
# Start with docker-compose
docker-compose up -d
# View logs
docker-compose logs -f
# Stop the service
docker-compose down
CLI Commands
agenttrace # Show help
agent-trace-log web # Start service (default 0.0.0.0:8000)
agent-trace-log web --reload # Development mode, auto-reload on code changes
agent-trace-log web --port 9000 # Specify port
agent-trace-log web --host 127.0.0.1 # Specify host
agent-trace-log version # Show version
Configuration
Environment Variables
| Variable | Description | Default |
|---|---|---|
BAILOU_API_KEY |
Default upstream API key | - |
BAILOU_BASE_URL |
Default upstream API URL | https://dashscope.aliyuncs.com/compatible-mode/v1 |
LISTEN_HOST |
Listen address | 0.0.0.0 |
LISTEN_PORT |
Listen port | 8000 |
API_KEYS |
Service API keys (comma-separated) | Empty |
RATE_LIMIT_ENABLED |
Enable rate limiting | false |
RATE_LIMIT |
Max requests per minute | 60 |
MODEL_MAPPING |
Model mapping (JSON format) | {} |
LOG_LEVEL |
Log level | INFO |
DATABASE_PATH |
Database file path | ./logs.db |
REQUEST_TIMEOUT |
Request timeout (seconds) | 120 |
Model Mapping Example
# Map gpt-3.5 to qwen-turbo, gpt-4 to qwen-max
MODEL_MAPPING='{"gpt-3.5": "qwen-turbo", "gpt-4": "qwen-max"}'
API Usage
OpenAI Format
# Chat completion
curl -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-api-key" \
-d '{
"model": "qwen-turbo",
"messages": [{"role": "user", "content": "Hello"}]
}'
# Streaming request
curl -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-api-key" \
-d '{
"model": "qwen-turbo",
"messages": [{"role": "user", "content": "Hello"}],
"stream": true
}'
Anthropic Format
curl -X POST http://localhost:8000/v1/messages \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-api-key" \
-d '{
"model": "claude-3-5-sonnet-20241022",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "Hello"}]
}'
Other Endpoints
# List models
curl http://localhost:8000/v1/models
# Health check
curl http://localhost:8000/health
Log Query
Web Interface
Visit http://localhost:8000/ to open the dashboard. You can view:
- All request logs
- Complete request/response content
- Token consumption statistics
- Model call distribution
API Endpoints
# Get log list
curl "http://localhost:8000/api/logs?limit=50&offset=0"
# Filter by model
curl "http://localhost:8000/api/logs?model=qwen-turbo"
# Filter by status
curl "http://localhost:8000/api/logs?status=success"
# Get statistics
curl "http://localhost:8000/api/logs/stats"
# Get single log detail
curl "http://localhost:8000/api/logs/req_xxxxxxxxxxxx"
Project Structure
agent-trace-log/
├── agenttrace/ # Python package
│ ├── __init__.py # Package entry point
│ ├── main.py # FastAPI application
│ ├── cli.py # CLI entry (agent-trace-log web)
│ ├── config.py # Configuration management
│ ├── database.py # Database operations
│ ├── middleware.py # Middleware (auth, rate limiting)
│ ├── services/
│ │ ├── __init__.py
│ │ ├── proxy.py # Proxy service
│ │ └── logger.py # Logging service
│ ├── templates/ # HTML templates
│ │ ├── index.html # Home dashboard
│ │ ├── logs.html # Log list
│ │ ├── log_detail.html # Log detail
│ │ ├── docs.html # API docs
│ │ ├── usage.html # Usage guide
│ │ └── health.html # Health check
│ └── static/ # Static files
│ └── i18n.js # Internationalization
├── pyproject.toml # Package configuration
├── requirements.txt # Python dependencies
├── Dockerfile # Docker image
├── docker-compose.yml # Docker Compose config
├── README.md # English documentation
└── README_zh.md # Chinese documentation
Security Recommendations
- Configure API_KEYS in production - Prevent unauthorized access
- Enable rate limiting - Prevent API abuse
- Use HTTPS - Recommended with Nginx reverse proxy
- Backup database regularly -
logs.dbcontains all request records
Nginx Reverse Proxy Example
server {
listen 443 ssl;
server_name your-domain.com;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
License
MIT License
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 agent_trace_log-0.0.1.tar.gz.
File metadata
- Download URL: agent_trace_log-0.0.1.tar.gz
- Upload date:
- Size: 60.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
81f9c4046061a66ab7f766105fd0c1a06d35d1906b9a75060cd2dcfdf07eed75
|
|
| MD5 |
ea45ca58259b5f31691e3102a716ad41
|
|
| BLAKE2b-256 |
678cbde8f42fad0d1f99d8d6385c75e981412762ab1a9fdbece55b74041ecf80
|
File details
Details for the file agent_trace_log-0.0.1-py3-none-any.whl.
File metadata
- Download URL: agent_trace_log-0.0.1-py3-none-any.whl
- Upload date:
- Size: 72.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
640bba32cb0a6647843f2fde88c54ffc16b68cafdb0ec1e19196dc092c0df795
|
|
| MD5 |
9256f9e6ad2e7fce11fbadac009847c6
|
|
| BLAKE2b-256 |
2c2a9aed7f0b5af21c11a90595c13f7eb4fc2abc78a82c4d56aa0640d2316419
|