Skip to main content

Aurex Python SDK (aurex-sdk)

Open-source, in-process LLM FinOps shield, runtime circuit breaker, and CI regression gate for Python (<0.1 ms overhead, zero proxy hops, zero-PII local ledger).

1. What Problems Are People Facing?

  • Silent cache & cliff traps: Dynamic timestamps or unsorted JSON tool schemas in system/developer prompts break 50%–90% prompt-cache discounts, and crossing 200,000 input tokens doubles whole-request pricing on Claude 4+ and Gemini 2.5+.
  • Unbounded runtime spend: Looping tool calls, hidden reasoning tokens (o3/o4-mini, Claude Extended Thinking, Gemini thoughts_token_count), aborted SSE streams, and multiplied SDK retries drain budgets mid-flight.
  • Billed HTTP-200 soft failures: Thinking-exhausted empty outputs and \n\n\n JSON grammar loops return HTTP 200 OK and bill full output rates while producing nothing usable.

2. What Does Aurex Solve?

aurex-sdk instruments openai (.create, .parse, .stream, responses.create, plus Groq/Together/Fireworks/OpenRouter/Ollama base_url clients), anthropic (messages.create, .parse, .stream, 1h/5m prompt caching, ThinkingBlock), google-genai (sync & .aio async), litellm, ollama, and huggingface_hub across three stages:

  1. Before the call (Stage 1): Deterministically sorts tool/schema JSON keys, pinpoints prefix-cache mutations by character offset, flags >5m Anthropic idle TTL gaps, and warns before the 200k 2x cliff.
  2. During the call (Stage 2): Isolates workflows via workflow_id / contextvars / OpenTelemetry trace_id, shares spend across workers via optional Redis, clamps thinking budgets, injects stream usage & bills aborted streams, disables duplicate SDK retries, and raises AurexBlocked.
  3. After the call + CI (Stage 3): Tags HTTP-200 soft failures as unfulfilled_spend, pairs spend with mark_success() to measure true cost_per_success, and diffs PR ledgers in CI.

3. How to Use It in Any Python Codebase (Step-by-Step)

Step 1: Install

pip install aurex-sdk
# Optional Redis multi-worker state + OpenTelemetry trace correlation:
pip install "aurex-sdk[redis,otel]"

Step 2: Initialize Once at Startup (FastAPI, Django, Celery, LangGraph, Scripts)

from aurex_sdk import AurexAuditor, AurexBlocked, AurexConfig, patch_all
import openai

auditor = AurexAuditor(
    AurexConfig(
        redis_url="redis://localhost:6379/0",  # Optional: omit for in-memory single-process mode
        enable_loop_guard=True,
        budget_guard={
            "max_cost_per_session": 1.50,
            "max_thinking_tokens": 4096,
            "on_budget_exceeded": "throw",
        },
    )
)
patch_all(auditor)

client = openai.OpenAI()
try:
    # Works identically for sync, AsyncOpenAI, .parse(response_format=Model), .stream(), and client.responses.create()
    resp = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {"role": "developer", "content": "You are a concise support engineer."},
            {"role": "user", "content": "Draft release notes for v0.5.0."},
        ],
        workflow_id="release-job-1",  # Stripped cleanly before hitting provider HTTP wire
        tags={"tenant_id": "acme"},
    )
    auditor.mark_success("release-job-1")
except AurexBlocked as err:
    print(f"Blocked runaway workflow {err.workflow_id}: {err.reason}")
finally:
    auditor.flush()  # Call on app shutdown or AWS Lambda / Cloud Run request exit

Step 3: Run Local Audit & CI PR Gate

aurex audit
aurex ci --ledger .aurex/ledger.jsonl --baseline main_ledger.jsonl --github-comment --fail-on-regression

(Zero-code startup hook: run aurex install-hook once in your virtualenv/image, then run with AUREX_ENABLED=1.)

4. How Can You Improve Your Setup?

  • Move dynamic values to the tail: Keep messages[0] / system static and append timestamps or request IDs at the end of the user turn.
  • Guard the 200k cliff: Set "max_context_tokens": 195000 in budget_guard on Claude 4+ and Gemini 2.5+ workflows.
  • Enable Redis across workers: Set AUREX_REDIS_URL=redis://localhost:6379/0 so Gunicorn/Uvicorn workers share live session spend.
  • Fail CI on upstream SDK drift: Set AUREX_STRICT_DRIFT=1 in CI to raise AurexProviderDriftError if a future openai / anthropic / google-genai major bump renames internal classes (production always fails open and logs auditor.get_health()["drift_warnings"]).

Quick Reference for AI Coding Agents

  • Singleton lifecycle: Initialize AurexAuditor(AurexConfig(...)) + patch_all(auditor) once at process startup (e.g., FastAPI lifespan). Call AurexAuditor.reset() between unit tests.
  • Auto-stripped call kwargs: workflow_id, tags, tenant_id, and quality_score can be passed directly to any patched provider .create(), .parse(), or .stream() call—Aurex removes them before HTTP serialization.
  • Read-only containers / Lambda: Use storage_mode="memory" (or let Aurex auto-fallback when the filesystem is read-only) and call auditor.flush() (or auditor.flush_async()) before exit.
  • Typed exceptions: Catch AurexBlocked / AurexBudgetExceeded (from aurex_sdk). Real provider exceptions (openai.APIError, anthropic.APIError) pass through unchanged.

Metadata

Release files for aurex-sdk 0.5.0

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

Source distribution (sdist)

Source distribution for aurex-sdk 0.5.0
File Size Uploaded Provenance Actions
aurex_sdk-0.5.0.tar.gz 87.3 kB Without provenance Details for aurex_sdk-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for aurex-sdk 0.5.0
File Interpreter ABI Platform Actions
aurex_sdk-0.5.0-py3-none-any.whl Python 3 none any Details for aurex_sdk-0.5.0-py3-none-any.whl

Total release size: 162.1 kB

Release files / aurex_sdk-0.5.0.tar.gz

Download URL aurex_sdk-0.5.0.tar.gz
Size 87.3 kB
Tags Source
SHA-256 checksum
How to use checksums
84175a07989723d51656d90ae87bb606b69e0489f75a3af4cf4f348971baaf69
BLAKE2b-256 checksum
How to use checksums
3c3e178893f5f07efc717dcf8183cf925df908dc3bb65f21508b1b5e46013548
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Release files / aurex_sdk-0.5.0-py3-none-any.whl

Download URL aurex_sdk-0.5.0-py3-none-any.whl
Size 74.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
824d73ccb89a6fe06057ab97d0f55d2470ae4a12af02af1997479f819ce8c3a4
BLAKE2b-256 checksum
How to use checksums
2250f5372566e768d47caed1758bad35f7160facedfa30912dd22d3d52987a87
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Security for aurex-sdk 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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