Skip to main content

Avenix is a focused Python tracing library for AI requests with beautiful terminal output

Project description

๐Ÿš€ Avenix

A lightweight, type-safe Python tracing library for AI & LLM requests with breathtaking terminal output.

Python Version Pydantic v2 Rich Formatting License: MIT Version Tests


Avenix Banner

Say goodbye to messy print statements and clunky cloud observability dashboards when debugging LLM apps.
Avenix gives you instant, structured terminal traces with zero setup.

Key Features โ€ข Terminal Preview โ€ข Architecture & Workflows โ€ข Quick Start โ€ข Supported Models & Pricing โ€ข API Reference


๐ŸŒŸ Overview

As LLM applications grow in complexity, developers need immediate visibility into model performance, token consumption, latency, and costs without configuring heavyweight observability platforms or leaking API keys to third-party dashboards.

Avenix solves this by providing a hyper-focused, non-intrusive tracing solution that lives right in your local development environment:

  1. Zero-Configuration Tracing: Simply wrap any LLM function with the @trace decorator.
  2. Automated Intelligence: Automatically captures timing metrics via high-resolution perf_counter, extracts token usage, and computes real-time request costs.
  3. Stunning Visual Display: Renders trace data in a rich, color-coded terminal panel using Rich.
  4. Multi-Provider Support: Seamlessly parses responses from OpenAI and Anthropic out of the box, with an extensible architecture for adding custom providers.

๐ŸŽจ Terminal Preview

Here is what you see in your terminal when an Avenix-traced function executes:

โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ ๐Ÿš€ Avenix Trace โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚                                                                              โ”‚
โ”‚  Model:    gpt-4                                                             โ”‚
โ”‚  Latency:  1.23s                                                             โ”‚
โ”‚  Input:    150 tokens                                                        โ”‚
โ”‚  Output:   300 tokens                                                        โ”‚
โ”‚  Cost:     $0.0225                                                           โ”‚
โ”‚                                                                              โ”‚
โ”‚  โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”  โ”‚
โ”‚  Prompt                                                                      โ”‚
โ”‚  โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”  โ”‚
โ”‚  Explain quantum computing and its primary advantages over classical         โ”‚
โ”‚  supercomputers in two concise paragraphs.                                   โ”‚
โ”‚                                                                              โ”‚
โ”‚  โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”  โ”‚
โ”‚  Response                                                                    โ”‚
โ”‚  โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”  โ”‚
โ”‚  Quantum computing leverages the fundamental principles of quantum           โ”‚
โ”‚  mechanicsโ€”specifically superposition and entanglementโ€”to process data in    โ”‚
โ”‚  ways classical computers cannot. While classical bits represent either a 0  โ”‚
โ”‚  or a 1, quantum bits (qubits) can exist in multiple states simultaneously... โ”‚
โ”‚                                                                              โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

โœจ Key Features

Feature Description
โšก Decorator-Based API Simply add @trace above your functions. Zero modification to your existing model invocation code or return values.
๐Ÿ’ฐ Automated Cost Calculation Built-in pricing engine accurately calculates request costs in USD down to 4 decimal places based on model pricing tables.
๐ŸŽฏ Precision Latency Tracking Utilizes Python's time.perf_counter() to capture microsecond-accurate execution latency.
๐Ÿ›ก๏ธ Type-Safe & Validated All trace records are strictly typed and validated using Pydantic v2, ensuring rock-solid data integrity.
๐Ÿ”Œ Extensible Extractor Chain Includes native parsers for OpenAI and Anthropic Claude models, with abstract base classes for custom integrations.
๐Ÿ—๏ธ Resilient Architecture Built with graceful fallbacks. If extraction or rendering fails, your application code executes uninterrupted without exception suppression.
๐Ÿงช Property-Based Tested Verified using comprehensive property-based testing via Hypothesis across 16 correctness invariants.

๐Ÿ—๏ธ Architecture & Workflows

Avenix is built from the ground up using modern software engineering best practices, emphasizing clean separation of concerns, strict typing, and fault tolerance.

1. Tracing Data Flow Lifecycle

The following diagram illustrates the lifecycle of a request passing through an Avenix-traced function:

flowchart TD
    %% Styling Definitions
    classDef userCode fill:#2563eb,stroke:#1e3a8a,stroke-width:2px,color:#ffffff
    classDef avenixCore fill:#7c3aed,stroke:#4c1d95,stroke-width:2px,color:#ffffff
    classDef provider fill:#059669,stroke:#064e3b,stroke-width:2px,color:#ffffff
    classDef terminal fill:#1f2937,stroke:#374151,stroke-width:2px,color:#f9fafb

    A["User Function Call"] --->|"Wrapped with @trace"| B["Avenix Decorator"]
    
    subgraph Execution["1. Execution Phase"]
        B --->|"Start time.perf_counter()"| C["Call AI Model API"]
        C --->|"Return Response & Stop Timer"| D["Capture Latency & Result"]
    end
    
    subgraph Extraction["2. Extraction & Analysis Phase"]
        D ---> E["Response Extractor Engine"]
        E --->|"OpenAI / Anthropic Format"| F["Extract Tokens & Content"]
        F ---> G["Calculate Request Cost (USD)"]
        G ---> H["Pydantic TraceModel Validation"]
    end
    
    subgraph Presentation["3. Presentation Phase"]
        H --->|"Validated Trace Object"| I["Rich Renderer Formatter"]
        I --->|"Stylized Panel Box"| J["Terminal Console Display"]
    end
    
    class A userCode;
    class B,D,E,F,G,H,I avenixCore;
    class C provider;
    class J terminal;

2. Runtime Interaction Sequence

When an application invokes a traced function, Avenix orchestrates timing, extraction, and rendering asynchronously while preserving the original return payload:

sequenceDiagram
    autonumber
    actor User as User Application
    participant Dec as @trace Decorator
    participant API as AI Provider (OpenAI / Anthropic)
    participant Core as Avenix Tracer Engine
    participant Term as Terminal Console

    User->>Dec: Invoke Traced Function(prompt)
    activate Dec
    Dec->>Dec: Start Timer (perf_counter)
    Dec->>API: Send API Request (Chat / Messages)
    activate API
    API-->>Dec: Return API Response (Tokens, Content, Usage)
    deactivate API
    Dec->>Dec: Stop Timer & Compute Latency
    
    Dec->>Core: capture_trace(result, latency, func_name)
    activate Core
    Core->>Core: Match Extractor Chain (OpenAI / Anthropic)
    Core->>Core: Compute USD Cost via Pricing Matrix
    Core->>Core: Validate Data via Pydantic v2 TraceModel
    Core->>Term: Render Formatted Rich Panel
    deactivate Core
    
    Dec-->>User: Return Original API Response Unchanged
    deactivate Dec

3. Layered Component Architecture

Avenix enforces a strict 6-layer modular architecture to guarantee extensibility and maintainability:

flowchart TB
    subgraph UserLayer["1. User Application Layer"]
        U1["@trace Decorator"] 
        U2["Manual Tracer API"]
    end

    subgraph OrchestrationLayer["2. Tracer Orchestration Layer"]
        T1["Tracer Class (Core Engine)"]
    end

    subgraph ExtractionLayer["3. Extraction Layer"]
        E1["ResponseExtractor (ABC)"]
        E2["OpenAIExtractor"]
        E3["AnthropicExtractor"]
        E1 --> E2
        E1 --> E3
    end

    subgraph DataLayer["4. Data & Validation Layer"]
        M1["TraceModel (Pydantic v2)"]
        M2["Model Pricing Matrix"]
    end

    subgraph FormattingLayer["5. Formatting Layer"]
        F1["RichFormatter (Panel & Text Builder)"]
    end

    subgraph LoggingLayer["6. Output & Logging Layer"]
        L1["RichLogger / Terminal Console"]
    end

    U1 --> T1
    U2 --> T1
    T1 --> E1
    T1 --> M1
    T1 --> M2
    T1 --> F1
    F1 --> L1

    classDef layerStyle fill:#0f172a,stroke:#334155,stroke-width:1px,color:#e2e8f0;
    classDef nodeStyle fill:#1e293b,stroke:#475569,stroke-width:1.5px,color:#38bdf8;
    class U1,U2,T1,E1,E2,E3,M1,M2,F1,L1 nodeStyle;

๐Ÿ“ฆ Installation

Install Avenix directly from PyPI using pip or your preferred package manager:

pip install avenix

Or using Poetry / UV:

poetry add avenix
# or
uv pip install avenix

Requirements

  • Python: >= 3.11
  • Pydantic: >= 2.0, < 3.0
  • Rich: >= 13.0, < 14.0

๐Ÿš€ Quick Start

1. Using the @trace Decorator (Recommended)

The simplest and most powerful way to use Avenix is by attaching @trace to your function:

from avenix import trace
from openai import OpenAI

client = OpenAI()

@trace
def ask_gpt(prompt: str):
    """Call OpenAI GPT-4 with automated terminal tracing."""
    response = client.chat.completions.create(
        model="gpt-4",
        messages=[{"role": "user", "content": prompt}]
    )
    return response

# When called, Avenix automatically captures latency, token usage,
# calculates pricing, and prints the visual trace panel to your terminal!
result = ask_gpt("Explain the difference between synchronous and asynchronous programming.")

2. Using with Anthropic Claude

Avenix automatically detects whether the return payload belongs to OpenAI or Anthropic:

from avenix import trace
from anthropic import Anthropic

client = Anthropic()

@trace
def ask_claude(prompt: str):
    """Call Anthropic Claude 3 Opus with automated terminal tracing."""
    response = client.messages.create(
        model="claude-3-opus-20240229",
        max_tokens=1024,
        messages=[{"role": "user", "content": prompt}]
    )
    return response

result = ask_claude("What are the core design patterns of microservices?")

3. Manual Tracing API

When working with custom AI pipelines, background jobs, or proprietary APIs, you can instantiate the Tracer directly to create explicit traces:

from avenix import Tracer
import time

tracer = Tracer()

# Measure custom execution
start_time = time.perf_counter()
# ... custom pipeline logic ...
latency = time.perf_counter() - start_time

# Manually generate and render the trace panel
tracer.create_trace(
    model="custom-meta-llama-3-70b",
    latency=latency,
    input_tokens=450,
    output_tokens=820,
    cost=0.0038,
    prompt="Summarize the latest advancements in solid-state battery technology.",
    response="Solid-state batteries replace liquid electrolytes with solid counterparts..."
)

๐Ÿ’ต Supported Models & Pricing

Avenix includes a built-in cost calculation engine updated with standard provider pricing rates. Costs are calculated automatically per 1,000 tokens:

$$\text{Total Cost} = \left(\frac{\text{Input Tokens}}{1000} \times \text{Input Price}\right) + \left(\frac{\text{Output Tokens}}{1000} \times \text{Output Price}\right)$$

Provider Model Identifier Input Price ($ / 1K Tokens) Output Price ($ / 1K Tokens) Ideal Use Case
OpenAI gpt-4 $0.03000 $0.06000 Complex reasoning, code generation, advanced analysis
OpenAI gpt-4-turbo $0.01000 $0.03000 High-performance multimodal reasoning at lower latency
OpenAI gpt-3.5-turbo $0.00050 $0.00150 Fast, lightweight tasks, formatting, and classification
Anthropic claude-3-opus $0.01500 $0.07500 Top-tier intelligence and nuanced creative writing
Anthropic claude-3-sonnet $0.00300 $0.01500 Balanced capability and speed for enterprise workloads
Anthropic claude-3-haiku $0.00025 $0.00125 Near-instantaneous response times and high-volume parsing

Note: If an unknown or custom model string is encountered, Avenix gracefully defaults the calculated cost to $0.0000 without throwing an error.


๐Ÿ› ๏ธ API Reference

@trace Decorator

Wraps any synchronous function that returns an AI model response object.

  • Behavior:
    • Captures execution duration via time.perf_counter().
    • Invokes the global Tracer instance to inspect the return value.
    • Renders the terminal panel without altering the function's return payload.
    • Fully transparent to exceptions (errors in the underlying function propagate normally).

Tracer Class

from avenix import Tracer

tracer = Tracer(logger=None, formatter=None)
  • Parameters:
    • logger: Optional custom logging class (defaults to built-in RichLogger).
    • formatter: Optional custom formatting class (defaults to built-in RichFormatter).
  • Methods:
    • capture_trace(result: Any, latency: float, func_name: Optional[str] = None) -> None: Inspects an arbitrary result object using the extractor chain and outputs the trace.
    • create_trace(model: str, latency: float, input_tokens: int, output_tokens: int, cost: float, prompt: str, response: str) -> None: Validates and displays an explicitly defined trace record.

TraceModel (Pydantic v2 Schema)

Represents the validated data structure of an execution trace:

class TraceModel(BaseModel):
    model: str          # Name of the AI model
    latency: float      # Execution time in seconds (ge=0.0, rounded to 2 decimals)
    input_tokens: int   # Number of prompt tokens (ge=0)
    output_tokens: int  # Number of completion tokens (ge=0)
    cost: float         # Computed request cost in USD (ge=0.0, rounded to 4 decimals)
    prompt: str = ""    # Input prompt text
    response: str = ""  # Output completion text

๐Ÿงช Testing & Quality Assurance

Avenix is built with uncompromising quality standards, featuring both unit tests and property-based testing with Hypothesis.

To run the local test suite:

# Clone the repository
git clone https://github.com/avenix/avenix.git
cd Avenix

# Install with development dependencies
pip install -e ".[dev]"

# Execute unit and property tests
pytest tests/ -v

# Run with HTML and terminal coverage reporting
pytest tests/ --cov=avenix

Test Suite Highlights:

  • Core Coverage: >90% code coverage across decorators, extractors, formatters, and models.
  • Property-Based Invariants: 16 dedicated property tests ensuring precision rounding, non-negative token counts, and cost calculation correctness across arbitrary data distributions.

๐Ÿ—บ๏ธ Roadmap & Out of Scope (v0.1)

Avenix v0.1 is intentionally designed as a lightweight, zero-dependency (beyond Pydantic/Rich) local developer tool. The following features are planned for future major releases:

  • Async/Await Support: Native @trace_async decorator for asyncio pipelines and streaming responses.
  • Custom Formatter Plugins: Support for JSON, compact ASCII, and Markdown file exporters.
  • Local Persistence: Optional SQLite or JSONL local trace logging for historical session review.
  • Additional Providers: Built-in extractors for Google Gemini, Mistral, Cohere, and Ollama/LocalLLMs.
  • Usage Aggregation: Session-wide token and budget counters with quota warnings.

๐Ÿค Contributing

We welcome contributions from the developer community! Whether it is adding a new model extractor, enhancing terminal aesthetics, or improving documentation:

  1. Fork the Project.
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature).
  3. Run the test suite (pytest tests/) to verify all property tests pass.
  4. Commit your Changes (git commit -m 'Add some AmazingFeature').
  5. Push to the Branch (git push origin feature/AmazingFeature).
  6. Open a Pull Request.

๐Ÿ“„ License

Distributed under the MIT License. See LICENSE for more information.


Built with โค๏ธ by the Avenix Contributors.
Elevate your terminal observability today!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

avenix-0.1.1.tar.gz (33.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

avenix-0.1.1-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file avenix-0.1.1.tar.gz.

File metadata

  • Download URL: avenix-0.1.1.tar.gz
  • Upload date:
  • Size: 33.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for avenix-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9d811b0c6cbd6aa9a443663660b629b753480c107f891f9f53eacb667920fb9d
MD5 67435e49a8618f7c08b420a9e7c68a8d
BLAKE2b-256 e6b8fc570cb60619bdedfd2b0223189f4df4e54a436908349ef2752ee721fa7d

See more details on using hashes here.

File details

Details for the file avenix-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: avenix-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 15.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for avenix-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 10a5c94d82e507c8c754b6aeec0047227a35b7c3ca6cd8a25ba1b4cfdb50469f
MD5 3f7fe96017bb0e10c88eeb1ead6f42c5
BLAKE2b-256 876bce43932a77f8555bdda1e55cd529fa6d65a9710f6d7bd620e4a5abe9dd3a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page