Skip to main content

Observability SDK for LLM applications

Project description

R4U Python SDK

An observability SDK that automatically traces HTTP requests from your applications, with special support for LLM API calls.

Installation

pip install r4u

Quick Start

Automatic HTTP Tracing

The easiest way to get started is with automatic HTTP tracing. This will trace all HTTP requests made by any supported HTTP library:

from r4u.tracing import trace_all

# Enable automatic tracing for all HTTP libraries with default AI provider patterns
trace_all()

# Or with custom patterns that extend the defaults
trace_all(
    allow_urls=["https://api.custom.com/*"],
    deny_urls=["https://api.openai.com/v1/models"]
)

# Now any HTTP requests will be automatically traced
import httpx
import requests
import aiohttp

# All of these will be automatically traced
httpx_client = httpx.Client()
requests_session = requests.Session()
aiohttp_session = aiohttp.ClientSession()

URL Filtering

By default, the SDK only traces requests to common AI provider APIs. You can configure which URLs to trace using allow and deny patterns:

from r4u.tracing.http.auto import configure_url_filter, trace_all

# Configure URL filtering
configure_url_filter(
    allow_patterns=[
        "https://api.openai.com/*",      # OpenAI API
        "https://api.anthropic.com/*",   # Anthropic API
        "https://api.groq.com/*",        # Groq API
    ],
    deny_patterns=[
        "https://api.openai.com/v1/models",  # Deny specific endpoints
    ]
)

# Enable tracing
trace_all()

Default Allow Patterns:

  • OpenAI: https://api.openai.com/*
  • Anthropic: https://api.anthropic.com/*, https://docs.claude.com/*
  • Groq: https://api.groq.com/*, https://console.groq.com/*
  • xAI (Grok): https://api.x.ai/*, https://latenode.com/*
  • Mistral AI: https://api.mistral.ai/*, https://apidog.com/*
  • Google: https://generativelanguage.googleapis.com/*, https://aiplatform.googleapis.com/*

Pattern Matching

The filter supports wildcard patterns using fnmatch syntax:

  • * matches any characters except /
  • ** matches any characters including /
  • Patterns are case-insensitive
  • Both full URLs and host-only patterns are supported

Examples:

  • https://api.openai.com/* - matches all OpenAI API endpoints
  • *.openai.com/* - matches all OpenAI subdomains
  • api.openai.com - matches only the exact host

OpenAI Integration

Since OpenAI uses httpx internally, you can trace OpenAI API calls by enabling HTTP tracing:

from r4u.tracing import trace_all

# Enable tracing BEFORE importing OpenAI
trace_all()

# Now import and use OpenAI normally
from openai import OpenAI

client = OpenAI(api_key="your-api-key")
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello, world!"}]
)

Features

  • Automatic HTTP Tracing: Automatically trace all HTTP requests from supported libraries (httpx, requests, aiohttp)
  • Multiple HTTP Library Support: Works with httpx, requests, and aiohttp
  • OpenAI Integration: Automatically trace OpenAI API calls since they use httpx internally
  • Streaming Support: Full support for streaming HTTP requests and responses
  • Async Support: Full async/await support for both sync and async HTTP clients
  • Background Processing: Traces are sent asynchronously in batches to minimize performance impact
  • Error Tracking: Automatic error capture and reporting
  • Minimal Overhead: Lightweight wrapper with minimal performance impact
  • Custom Tracers: Support for custom tracer implementations

HTTP Trace Data

Each HTTP request generates a comprehensive trace with:

Request Details:

  • HTTP method (GET, POST, etc.)
  • Full URL
  • Request headers (including Authorization)
  • Request body (raw bytes)

Response Details:

  • HTTP status code
  • Response headers
  • Response body (raw bytes, including streaming responses)

Timing Information:

  • Request start time
  • Request completion time
  • Total duration

Error Information (if applicable):

  • Error messages
  • Exception details

Custom Metadata:

  • Additional context you can provide
  • Extracted fields for convenience

Streaming Example

from r4u.tracing import trace_all

# Enable tracing BEFORE importing OpenAI
trace_all()

from openai import OpenAI

client = OpenAI(api_key="your-api-key")

# Streaming requests are automatically traced
stream = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Tell me a story"}],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content is not None:
        print(chunk.choices[0].delta.content, end="")

Custom Tracer Example

from r4u.client import AbstractTracer, HTTPTrace
from r4u.tracing import trace_all

class ConsoleTracer(AbstractTracer):
    def log(self, trace: HTTPTrace):
        print(f"HTTP {trace.method} {trace.url} -> {trace.status_code}")

# Use custom tracer
tracer = ConsoleTracer()
trace_all(tracer)

# All HTTP requests will now be printed to console

For more comprehensive examples, see the examples/ directory.

Configuration

Environment Variables

  • R4U_API_URL: Base URL for the R4U server (default: http://localhost:8000)
  • R4U_TIMEOUT: HTTP request timeout in seconds (default: 30.0)
  • R4U_TOKEN: R4U Cloud server authorization token (optional, not needed for local Open R4U Server)

Development

See the examples/ directory for comprehensive usage examples and the tests/ directory for test cases.

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

r4u-0.1.4.tar.gz (136.3 kB view details)

Uploaded Source

Built Distribution

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

r4u-0.1.4-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

Details for the file r4u-0.1.4.tar.gz.

File metadata

  • Download URL: r4u-0.1.4.tar.gz
  • Upload date:
  • Size: 136.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.19

File hashes

Hashes for r4u-0.1.4.tar.gz
Algorithm Hash digest
SHA256 3aa04589a763d495475294ec87a5797906980725ee23ff523f710c4f889e9b43
MD5 8a393e62dd3fcd16217ee396424f36cd
BLAKE2b-256 f18a432b0715683bfcab05a81eea0ebf5fed29f3ce8f7a85cb11b8c26553ddd9

See more details on using hashes here.

File details

Details for the file r4u-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: r4u-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 18.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.19

File hashes

Hashes for r4u-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a56acdaed078b2829dbcb194b5d3a57ad5bf322fc6fa292f16ea977b042b776f
MD5 02f9e20a26d5c4f9b7ff2df2ccc7f98d
BLAKE2b-256 67b0bc78e86e72f0685f49228967b7234a9b9d4ac494768d508676ed1e2096ab

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