Autotune Hook
Python instrumentation for reporting OpenRouter calls to AutoTune.
Zero-code: with_session is the only line (recommended)
With AUTOTUNE_ENDPOINT (and usually AUTOTUNE_API_KEY) in the environment,
entering a session auto-enables process-wide httpx interception: every
OpenRouter chat completion made by any httpx-based SDK (openai-python,
pydantic-ai) is preflighted against cloud workflow policy and shadow-reported
or proxied accordingly. No hook construction, no transport wiring, no
decorator — the session context is the only AutoTune line in application
code, and it carries all dynamic attribution:
from autotune_hook import with_session
with with_session(
workflow="slack-agent",
session_id=thread_id,
user_id=user_id,
):
completion = client.chat.completions.create(...) # any httpx-based SDK
Workflow, project, session, and user are code-only: the hook deliberately
never reads them from env vars, because they vary with code and callers.
The only attribution default that rides the environment is
AUTOTUNE_ENVIRONMENT (deployment-static by nature). Requires the httpx
extra (pip install autotune-hook[httpx]).
Controls:
AUTOTUNE_INTERCEPT=0— kill switch: sessions only attribute, never install interception (for apps wired with a decorator or explicit transport instead).AUTOTUNE_HOOK_MODE=observe— report-only tee semantics: no preflight, no proxy, AutoTune being down can never block a model call. The default (full) honors the cloud policy envelope, including proxy mode.enable_interception()/disable_interception()— explicit runtime control over the same machinery (mirrors the TypeScript hook'senableInterception()).
Per-call overrides can also ride request headers wherever the SDK lets you
add them (openai-python extra_headers): x-autotune-workflow,
x-autotune-project, x-autotune-environment, x-autotune-session-id,
x-autotune-user-id. Headers beat the session context and are stripped
before the provider sees the request.
Non-httpx HTTP stacks (requests, aiohttp) are not intercepted — use the wrapper/decorator below or the drop-in proxy base URL for those.
Transport integration (explicit wiring)
Same machinery, scoped to one client instead of the process. Wrap the app's
httpx transport and every OpenRouter chat completion through it is visible to
AutoTune. mode="observe" (default) is a pure tee; mode="full" adds the
preflight/proxy envelope:
import httpx
from autotune_hook import AutotuneAsyncTransport
client = httpx.AsyncClient(transport=AutotuneAsyncTransport(mode="full"))
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, and for non-httpx
stacks. 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)
Integrations stack safely: a call already observed by a decorator or an explicit transport is suppressed at the interception layer, so one logical call is never recorded twice.
Sessions
from autotune_hook import with_session
with with_session(session_id=run_id, user_id=user_id, workflow="ocr"):
... # every hooked/intercepted call inside is attributed
Sessions nest (inner values win), flow through async code and
asyncio.to_thread, and carry workflow, project, environment,
session_id, and user_id.
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/headers so each request is
attributed to the right run.
Metadata
Release files for autotune-hook 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| autotune_hook-0.2.0.tar.gz | 22.7 kB | With provenance | Details for autotune_hook-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| autotune_hook-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for autotune_hook-0.2.0-py3-none-any.whl |
Total release size: 40.1 kB
Release files / autotune_hook-0.2.0.tar.gz
| Download URL | autotune_hook-0.2.0.tar.gz |
|---|---|
| Size | 22.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
07931608ce6287a84373f20b3061cc4c8d29c276e67756ebab77b3ba267732e5
|
|
BLAKE2b-256 checksum How to use checksums |
4b8d7971935b8d3492e38b5ca3cb8b6167c5fca58b1be4c86e205fcf6c885ce4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 9, 2026.
Transparency logRelease files / autotune_hook-0.2.0-py3-none-any.whl
| Download URL | autotune_hook-0.2.0-py3-none-any.whl |
|---|---|
| Size | 17.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a2f687f00d8ebcea41232a37a7722dcae5ec5dd4c70130ffe4b1be7bf6c1a5cb
|
|
BLAKE2b-256 checksum How to use checksums |
42a086e3578e553014b5bae296fe009526c52acb7c5039032adc1cd263d5f57b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 9, 2026.
Transparency logSecurity for autotune-hook 0.2.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.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for autotune_hook-0.2.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 9, 2026.
Transparency logA 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.