Skip to main content

Autotune Hook

Project description

Autotune Hook

Python instrumentation for reporting OpenRouter calls to AutoTune.

Transport integration (recommended)

When requests are built inside an SDK (openai-python, pydantic-ai), wrap the app's httpx transport once and every OpenRouter chat completion that flows through it is shadow-reported in the background — no call-site changes, observation only, and AutoTune being down can never block or fail a model call. Requires the httpx extra (pip install autotune-hook[httpx]).

import httpx
from autotune_hook import AutotuneAsyncTransport

client = httpx.AsyncClient(transport=AutotuneAsyncTransport())
openrouter = AsyncOpenAI(base_url="https://openrouter.ai/api/v1", http_client=client)

Already wrapping transports (retries, sanitizers)? Put the AutoTune transport outermost so one logical call maps to one recorded run — the final response the app saw.

Wrapper / decorator integration

For call sites that hold the request payload directly. These also honor cloud workflow policy: the request is preflighted, and AutoTune answers with an explicit envelope — {"mode": "shadow", "ttl_seconds": N} (execute the wrapped call, report its output in the background, and skip further preflights for that workflow until the TTL expires) or {"mode": "proxy", "response": {...}} (return the AutoTune-executed response). Any response without the envelope falls back to the app's own call, so an intermediary's bare 200 can never be mistaken for an LLM response. Streaming requests pass through unobserved.

from autotune_hook import AutotuneHook

hook = AutotuneHook(workflow="email-triage")

def call_openrouter(payload):
    return hook.openrouter_call(
        payload,
        lambda: openrouter_client.chat.completions.create(**payload),
    )

Decorator form:

@hook.openrouter()
def call_openrouter(**payload):
    return openrouter_client.chat.completions.create(**payload)

Sessions

from autotune_hook import with_session

with with_session(session_id=run_id, user_id=user_id, workflow="ocr"):
    ...  # every hooked/teed call inside is attributed

Reporting behavior

Reports ride a single background thread with a bounded queue: they never block the calling thread or event loop, overflow drops reports (with a warning) rather than growing memory, and flush_reports() drains pending reports at worker shutdown (also registered via atexit). Inline data: URLs larger than 256 bytes (base64 images) are replaced with a size-and-hash placeholder before reporting; disable with AUTOTUNE_HOOK_REDACT_DATA_URLS=0. Reports whose serialized size exceeds AUTOTUNE_HOOK_MAX_REPORT_BYTES (default 4 MB) are dropped with a warning.

Configuration

Set AUTOTUNE_ENDPOINT and, when required, AUTOTUNE_API_KEY in the environment. If AUTOTUNE_ENDPOINT is unset, the hook stays fully out of the way.

Dynamic attribution belongs in code, not static process env. Pass workflow, session, user, project, and environment values through with_session(...), AutotuneHook(...), or per-call metadata so each request is attributed to the right run.

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

autotune_hook-0.1.2.tar.gz (15.1 kB view details)

Uploaded Source

Built Distribution

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

autotune_hook-0.1.2-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file autotune_hook-0.1.2.tar.gz.

File metadata

  • Download URL: autotune_hook-0.1.2.tar.gz
  • Upload date:
  • Size: 15.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for autotune_hook-0.1.2.tar.gz
Algorithm Hash digest
SHA256 7d9fe655e1a08ff20c37978a1a59e8fdb2cd0c34ec11020b65ce3497c9233b72
MD5 8e8c13651af29ef200f7032227224b28
BLAKE2b-256 d57bc0c0a8163a68789809777e0fdbe0ec84519514a6fdc077e6e39461e98c8d

See more details on using hashes here.

Provenance

The following attestation bundles were made for autotune_hook-0.1.2.tar.gz:

Publisher: publish-python-hook.yml on shellzbt/sota-autotune

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

File details

Details for the file autotune_hook-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: autotune_hook-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for autotune_hook-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4873c78b2ed540ba1e74b000b30c0673bc547aa66ac7e56e2a393c6aeae7ebb6
MD5 646b543884f8206002cdd6fbd5a84ff1
BLAKE2b-256 1c338e5eb3247ad5758b25c6609ed4089ec391076e9fef0907a2a15f6d137f2e

See more details on using hashes here.

Provenance

The following attestation bundles were made for autotune_hook-0.1.2-py3-none-any.whl:

Publisher: publish-python-hook.yml on shellzbt/sota-autotune

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