Skip to main content

OpenTelemetry SDK for Coalex.ai with OTLP integration and comprehensive OpenInference auto-instrumentation

Project description

Coalex SDK

A Python package for OpenTelemetry integration with Coalex.ai observability platform. Automatically capture token usage, latency, and traces from all your LLM applications.

Installation

Basic Installation

pip install coalex

With Auto-Instrumentation (Recommended)

Install with support for common LLM frameworks:

pip install "coalex[auto-instrument]"

This includes instrumentation for: OpenAI, LlamaIndex, LangChain, VertexAI, Anthropic, and AWS Bedrock.

Framework-Specific Installation

# OpenAI only
pip install "coalex[openai]"

# LangChain only
pip install "coalex[langchain]"

# LlamaIndex only
pip install "coalex[llamaindex]"

# VertexAI only
pip install "coalex[vertexai]"

# All supported frameworks
pip install "coalex[all]"

Quick Start (Recommended)

The easiest way to get started is with auto-instrumentation, which automatically captures tokens and traces from all installed LLM libraries:

import coalex
from openai import OpenAI

# One-time setup - register and auto-instrument
tracer_provider = coalex.register(agent_id="YOUR_AGENT_ID")
results = coalex.auto_instrument(tracer_provider=tracer_provider)

# See what was instrumented
instrumented = [k for k, v in results.items() if v == "success"]
print(f"Instrumented: {', '.join(instrumented)}")

# Use your LLM library normally - tokens are automatically captured!
client = OpenAI()

with coalex.coalex_context(
    request_id="req_001",
    prompt_version="v1.0.0"
):
    response = client.chat.completions.create(
        model="gpt-4",
        messages=[{"role": "user", "content": "Hello!"}]
    )
    print(response.choices[0].message.content)

Supported Frameworks

Coalex automatically instruments these frameworks via OpenInference:

LLM Frameworks & Agents:

  • OpenAI SDK
  • LangChain
  • LlamaIndex
  • DSPy
  • CrewAI
  • Haystack
  • Instructor
  • Guardrails

LLM Providers:

  • Google VertexAI
  • AWS Bedrock
  • Anthropic Claude
  • MistralAI
  • Groq
  • liteLLM

Features

  • Auto-Instrumentation: One call instruments all installed LLM libraries
  • Token Tracking: Automatically captures input/output tokens for all LLM calls
  • Zero Code Changes: Use your LLM libraries normally - tracing happens automatically
  • Resilient: Never crashes your app - errors are logged but don't interrupt execution
  • Context Management: Proper span hierarchy and attribute propagation with coalex_context()
  • OpenInference Standard: Uses industry-standard semantic conventions
  • Authentication: Automatic authentication using agent_id

Usage

Auto-Instrumentation (Recommended)

Auto-instrument all installed LLM libraries:

import coalex

# Register and auto-instrument everything
tracer_provider = coalex.register(agent_id="YOUR_AGENT_ID")
results = coalex.auto_instrument(tracer_provider=tracer_provider)

Selective Instrumentation

Only instrument specific libraries:

# Only OpenAI and LangChain
results = coalex.auto_instrument(
    tracer_provider=tracer_provider,
    include_libraries=["openai", "langchain"]
)

Exclude certain libraries:

# Everything except Bedrock and Groq
results = coalex.auto_instrument(
    tracer_provider=tracer_provider,
    exclude_libraries=["bedrock", "groq"]
)

Individual Instrumentors

For fine-grained control:

# Instrument specific libraries individually
coalex.instrument_openai(tracer_provider)
coalex.instrument_langchain(tracer_provider)
coalex.instrument_vertexai(tracer_provider)

Context Management

Use coalex_context() to group related LLM calls and add metadata:

with coalex.coalex_context(
    request_id="user_session_123",
    prompt_version="v2.1.0"
):
    # All LLM calls here are grouped under this context
    response1 = client.chat.completions.create(...)
    response2 = client.chat.completions.create(...)

Configuration

register() Parameters

tracer_provider = coalex.register(
    agent_id="YOUR_AGENT_ID",              # Required: Your agent ID
    endpoint=None,                          # Optional: Custom OTLP endpoint
    additional_attributes={},               # Optional: Extra attributes for all spans
    suppress_export_errors=True,            # Optional: Suppress export errors
    suppress_registration_errors=True,      # Optional: Never crash on registration errors
)

auto_instrument() Parameters

results = coalex.auto_instrument(
    tracer_provider=tracer_provider,        # Optional: TracerProvider from register()
    suppress_errors=True,                   # Optional: Suppress instrumentation errors
    include_libraries=None,                 # Optional: Only instrument these libraries
    exclude_libraries=None,                 # Optional: Skip these libraries
)

Examples

Complete working examples are available in the examples/ directory:

  • openai_example.py: OpenAI SDK with streaming and non-streaming
  • langchain_example.py: LangChain with LCEL chains and batch processing
  • langgraph_async_example.py: Async LangGraph with tool calling
  • vertexai_example.py: Google VertexAI with streaming

To run an example:

# Set your API keys
export OPENAI_API_KEY="your-key"
export COALEX_AGENT_ID="your-agent-id"

# Install dependencies
pip install "coalex[openai]" openai

# Run the example
python examples/openai_example.py

Troubleshooting

Token Counts Showing as 0?

If you see token counts as 0 in your Coalex dashboard:

  1. Use auto-instrumentation: The most common issue is forgetting to instrument your LLM libraries

    coalex.auto_instrument(tracer_provider=tracer_provider)
    
  2. For streaming requests: Some providers require special configuration:

    # OpenAI streaming - include usage in stream
    stream = client.chat.completions.create(
        model="gpt-4",
        messages=[...],
        stream=True,
        stream_options={"include_usage": True}  # Important!
    )
    
  3. Check instrumentation results: Verify your library was actually instrumented:

    results = coalex.auto_instrument(tracer_provider=tracer_provider)
    print(results)  # Should show "success" for your libraries
    

Registration Errors

Coalex SDK is designed to never crash your application. If registration fails:

  • Errors are logged but don't raise exceptions (by default)
  • A no-op tracer is used (your app continues without tracing)
  • Check logs for details: logger.warning("Failed to register Coalex tracing...")

To raise exceptions on registration errors:

tracer_provider = coalex.register(
    agent_id="YOUR_AGENT_ID",
    suppress_registration_errors=False  # Will raise on errors
)

Migration Guide

Upgrading from v0.4.x to v0.5.0

Before (v0.4.x) - Manual instrumentation:

from coalex.otel import register, coalex_context
from openinference.instrumentation.openai import OpenAIInstrumentor
from openinference.instrumentation.langchain import LangChainInstrumentor

tracer_provider = register(agent_id="YOUR_AGENT_ID")

# Had to manually instrument each library
OpenAIInstrumentor().instrument(tracer_provider=tracer_provider)
LangChainInstrumentor().instrument(tracer_provider=tracer_provider)
# ... and so on for each library

After (v0.5.0) - Auto-instrumentation:

import coalex

tracer_provider = coalex.register(agent_id="YOUR_AGENT_ID")

# One line instruments everything!
coalex.auto_instrument(tracer_provider=tracer_provider)

Benefits:

  • No need to import individual instrumentors
  • Automatically instruments all installed libraries
  • Easier to maintain - works with new libraries without code changes
  • More reliable - gracefully handles missing libraries

Backward Compatibility: Manual instrumentation still works! You can continue using the old approach if needed.

Advanced Usage

Custom Endpoint

Use a custom OTLP endpoint:

tracer_provider = coalex.register(
    agent_id="YOUR_AGENT_ID",
    endpoint="https://your-custom-endpoint.com/v1/traces"
)

Additional Resource Attributes

Add custom attributes to all spans:

tracer_provider = coalex.register(
    agent_id="YOUR_AGENT_ID",
    service_name="my-chatbot",
    additional_attributes={
        "environment": "production",
        "version": "2.1.0",
        "team": "ai-platform"
    }
)

Manual Span Creation

For advanced use cases, create custom spans:

from opentelemetry import trace

tracer = trace.get_tracer(__name__)

with tracer.start_as_current_span("custom_operation") as span:
    span.set_attribute("custom.attribute", "value")
    # Your code here

Contributing

Contributions are welcome! Please open an issue or pull request on GitHub.

License

See LICENSE file for details.

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

coalex-0.6.0.tar.gz (26.7 kB view details)

Uploaded Source

Built Distribution

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

coalex-0.6.0-py3-none-any.whl (20.8 kB view details)

Uploaded Python 3

File details

Details for the file coalex-0.6.0.tar.gz.

File metadata

  • Download URL: coalex-0.6.0.tar.gz
  • Upload date:
  • Size: 26.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for coalex-0.6.0.tar.gz
Algorithm Hash digest
SHA256 b3f299f7febdbd05a489182b092afdd671ab2ae2504c690dde91ed64baebe7d5
MD5 782f1c43651a97a0102eabb19e512b9d
BLAKE2b-256 60278368fd613ced904ece0a7fa6234c8a1c97f412cbd8438bbd77eb206560f6

See more details on using hashes here.

Provenance

The following attestation bundles were made for coalex-0.6.0.tar.gz:

Publisher: publish.yml on carlosmlribeiro/coalex.ai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file coalex-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: coalex-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 20.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for coalex-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 951b8174b5caa864b71671f8e913960f5c53a287151d1039f683b7f901376021
MD5 4723f6b6c14246afd6f6fa80064c2d37
BLAKE2b-256 0dc60b86c8067b183dd82b0cbbe3949e3cacca92d361658288ed37bb79195061

See more details on using hashes here.

Provenance

The following attestation bundles were made for coalex-0.6.0-py3-none-any.whl:

Publisher: publish.yml on carlosmlribeiro/coalex.ai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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