Skip to main content

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.

Metadata

Release files for r4u 0.1.12

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for r4u 0.1.12
File Size Uploaded
r4u-0.1.12.tar.gz 139.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for r4u 0.1.12
File Interpreter ABI Platform
r4u-0.1.12-py3-none-any.whl Python 3 none any Details

Total release size: 159.3 kB

Release files / r4u-0.1.12.tar.gz

Download URL r4u-0.1.12.tar.gz
Size 139.4 kB
Tags Source
SHA-256 checksum
How to use checksums
da18f6ce398b95d606a63cfd4d8e00e3c228636d09efdc64e62a508ed15688eb
BLAKE2b-256 checksum
How to use checksums
5a27c5659c3fb73c9d803e692622c58e883a0575f6380c675880c8494f249baf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.18

Release files / r4u-0.1.12-py3-none-any.whl

Download URL r4u-0.1.12-py3-none-any.whl
Size 19.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f7079a13c00a1b4805837033fa589d9f9ffd7cc8ba3d702e84104b21c909deda
BLAKE2b-256 checksum
How to use checksums
dc542f140fcf6a0bd3dc1530a2470dc714349d8c7385d5424e0159a1e62e72d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.18

Release history Release notifications | RSS feed

This release

0.1.12 This release

2 release files

0.1.11

2 release files

0.1.10

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page