Avenix is a focused Python tracing library for AI requests with beautiful terminal output
Project description
Avenix v0.1
A Python tracing library for AI/LLM requests with beautiful terminal output.
Avenix provides a decorator-based API for tracing AI model requests, automatically capturing execution metrics like timing, token usage, and costs, then displaying them in richly formatted terminal output.
Overview
Avenix simplifies monitoring AI/LLM requests by:
- Automatic Capture: Uses a simple
@tracedecorator to automatically capture request metrics - Beautiful Display: Renders trace information in a formatted terminal panel with colors and separators
- Multi-Provider Support: Works with OpenAI and Anthropic model responses out of the box
- Cost Tracking: Automatically calculates request costs based on model pricing
- Extensible: Easy to add custom extractors for new AI providers
Installation
pip install avenix
Requirements
- Python 3.11+
- pydantic ^2.0
- rich ^13.0
Quick Start
Using the @trace Decorator
The simplest way to use Avenix is with the @trace decorator:
from avenix import trace
from openai import OpenAI
client = OpenAI()
@trace
def get_gpt_response(prompt: str):
"""Call GPT-4 with the given prompt."""
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": prompt}]
)
return response
# When you call the function, Avenix will automatically:
# 1. Measure execution time
# 2. Extract model, tokens, and response from the result
# 3. Calculate cost based on token usage
# 4. Display formatted trace output to terminal
result = get_gpt_response("What is machine learning?")
Manual Trace Creation
For more control, you can manually create traces using the Tracer API:
from avenix import Tracer
tracer = Tracer()
# Later, manually create a trace with explicit values
tracer.create_trace(
model="gpt-4",
latency=2.5,
input_tokens=150,
output_tokens=300,
cost=0.045,
prompt="What is AI?",
response="AI is artificial intelligence..."
)
Supported Models
Avenix includes built-in support for:
OpenAI
- gpt-4
- gpt-4-turbo
- gpt-3.5-turbo
Anthropic
- claude-3-opus
- claude-3-sonnet
- claude-3-haiku
Features in v0.1
✅ Decorator-based tracing API ✅ Automatic timing measurement with perf_counter ✅ OpenAI and Anthropic response extraction ✅ Model pricing table and cost calculation ✅ Beautiful terminal output with rich formatting ✅ Error handling and graceful fallbacks ✅ Property-based test suite for correctness verification
Out of Scope for v0.1
The following features are planned for future releases:
- Custom formatter plugins
- Database/file persistence of traces
- Trace filtering and search
- Performance statistics aggregation
- Integration with external logging services
- Support for additional AI providers
- Rate limiting and quota management
- Async/await support
Documentation
API Reference
@trace Decorator
@trace
def your_function():
# ... your code that calls an AI model
return response
The @trace decorator:
- Measures execution time with
time.perf_counter() - Captures the function result
- Calls the global
Tracerinstance to extract data and display trace - Propagates exceptions without suppression
- Preserves the original function's return value and metadata
Tracer Class
from avenix import Tracer
tracer = Tracer(logger=None, formatter=None)
Methods:
capture_trace(result, latency, func_name=None): Capture and display a trace from function executioncreate_trace(model, latency, input_tokens, output_tokens, cost, prompt, response): Manually create and display a trace
TraceModel
The TraceModel class represents a single trace with validation:
Fields:
model(str): Name of the AI modellatency(float): Execution time in seconds (rounded to 2 decimals)input_tokens(int): Number of input tokens (must be non-negative)output_tokens(int): Number of output tokens (must be non-negative)cost(float): Request cost in dollars (rounded to 4 decimals, must be non-negative)prompt(str): Input prompt text (defaults to empty string)response(str): Model response text (defaults to empty string)
Examples
See the examples/ directory for complete working examples:
openai_example.py: Using @trace with OpenAI APIanthropic_example.py: Using @trace with Anthropic APImanual_trace.py: Creating traces manually with Tracer API
Testing
Avenix includes a comprehensive test suite with property-based tests:
pytest tests/ -v # Run all tests
pytest tests/ --cov # Run with coverage report
pytest tests/test_models.py -v # Run specific test file
Test coverage targets:
- Core modules (decorator, tracer, models, formatter, extractors): >90%
- Property-based tests for 16 correctness properties
- Unit tests for all feature components
Architecture
Avenix follows a layered architecture:
- Models Layer (
models.py): DefinesTraceModelwith Pydantic validation - Decorator Layer (
decorator.py): Provides@tracedecorator for wrapping functions - Tracer Layer (
tracer.py): Core orchestration with optional custom logger/formatter - Extraction Layer (
extractors.py): Provider-specific response extractors - Formatting Layer (
formatter.py): Beautiful terminal output with rich library - Logging Layer (
logger.py): Terminal display with fallback handling
Error Handling
Avenix is designed to be resilient to errors:
- Extraction Failures: If response format doesn't match known providers, uses sensible defaults
- Validation Failures: If TraceModel validation fails, falls back to defaults
- Rendering Failures: If rich formatting fails, falls back to basic print output
- Exception Propagation: Errors in traced functions propagate normally without suppression
Contributing
This is a v0.1 release. Feedback and contributions are welcome!
API Reference
For detailed API documentation, see API.md.
License
MIT License - See LICENSE file for details
Changelog
See CHANGELOG.md for version history and release notes
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 avenix-0.1.0.tar.gz.
File metadata
- Download URL: avenix-0.1.0.tar.gz
- Upload date:
- Size: 24.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9641021bc8dd6e8e2ae41a2318c03b6c34070479f62d494de31f8a8a7bff41a0
|
|
| MD5 |
4e16e5859f143af8d08a8df032c90296
|
|
| BLAKE2b-256 |
7cf0750f9aefc2e03eae6b133abb8faea1f6c1b08aa9edaf009344c6ef974fea
|
File details
Details for the file avenix-0.1.0-py3-none-any.whl.
File metadata
- Download URL: avenix-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d05318ab9e9fddaa381ca1112c82771ea623ec21c96cbc35b0d828efabff2d7
|
|
| MD5 |
7639a4ffb722a8765636ee7046d477ca
|
|
| BLAKE2b-256 |
0358ed0eed563ae62469bee43f690dac7cdd13147f7ff911867061f4cd97d4e6
|