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
Defaults come from environment variables:
AUTOTUNE_ENDPOINT(unset = the hook stays fully out of the way)AUTOTUNE_API_KEYAUTOTUNE_WORKFLOWAUTOTUNE_PROJECTAUTOTUNE_ENVIRONMENTAUTOTUNE_SESSION_IDAUTOTUNE_USER_IDAUTOTUNE_HOOK_TIMEOUT(seconds per hook HTTP request, default 2.0)AUTOTUNE_HOOK_MODE_TTL(seconds to cache a shadow decision, default 60)AUTOTUNE_HOOK_REDACT_DATA_URLS(default on)AUTOTUNE_HOOK_MAX_REPORT_BYTES(default 4000000)
Publishing
This package publishes from GitHub Actions using PyPI Trusted Publishing. No PyPI token is stored in GitHub.
One-time PyPI setup for autotune-hook:
- Go to the
autotune-hookproject on PyPI. - Add a trusted publisher for GitHub Actions.
- Use owner
shellzbt, repositorysota-autotune. - Use workflow filename
publish-python-hook.yml. - Use environment name
pypi.
To publish a new release:
- Bump the version in
pyproject.tomlandsrc/autotune_hook/__init__.py. - Merge the version bump to
main. - Create and publish a GitHub release whose tag exactly matches that version,
prefixed with
autotune-hook-v, for exampleautotune-hook-v0.1.1.
The workflow fails before publishing if the version already exists on PyPI, if
the release tag does not match the package version, or if pyproject.toml and
src/autotune_hook/__init__.py disagree. Manual dispatch from main is
available for retries before a version has been accepted by PyPI.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file autotune_hook-0.1.1.tar.gz.
File metadata
- Download URL: autotune_hook-0.1.1.tar.gz
- Upload date:
- Size: 15.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0a4d57a6f8c8bdf60f4ec2564b0e9e45b7ebf6dcba66b6419107d16d2b4bc645
|
|
| MD5 |
793caeb7f11ea44d4760199f081de9b9
|
|
| BLAKE2b-256 |
68911e87ccb5fc0fd78352ec72c866f564ae2605c493a64be2ae905ead579232
|
Provenance
The following attestation bundles were made for autotune_hook-0.1.1.tar.gz:
Publisher:
publish-python-hook.yml on shellzbt/sota-autotune
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
autotune_hook-0.1.1.tar.gz -
Subject digest:
0a4d57a6f8c8bdf60f4ec2564b0e9e45b7ebf6dcba66b6419107d16d2b4bc645 - Sigstore transparency entry: 2112016427
- Sigstore integration time:
-
Permalink:
shellzbt/sota-autotune@4f4eb7b87f794c9d8544ef0f32da79d776336167 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/shellzbt
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-python-hook.yml@4f4eb7b87f794c9d8544ef0f32da79d776336167 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file autotune_hook-0.1.1-py3-none-any.whl.
File metadata
- Download URL: autotune_hook-0.1.1-py3-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5dff050d56dbbb86d29072c62274450b04fc893e6bd5431a2595e7f4390b1703
|
|
| MD5 |
7b409229fc386ead5475308091cabef0
|
|
| BLAKE2b-256 |
2123ec46d501b89ec187bebba2e35f28d51dd0ccf85f9affabb67a1a9880d841
|
Provenance
The following attestation bundles were made for autotune_hook-0.1.1-py3-none-any.whl:
Publisher:
publish-python-hook.yml on shellzbt/sota-autotune
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
autotune_hook-0.1.1-py3-none-any.whl -
Subject digest:
5dff050d56dbbb86d29072c62274450b04fc893e6bd5431a2595e7f4390b1703 - Sigstore transparency entry: 2112016787
- Sigstore integration time:
-
Permalink:
shellzbt/sota-autotune@4f4eb7b87f794c9d8544ef0f32da79d776336167 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/shellzbt
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-python-hook.yml@4f4eb7b87f794c9d8544ef0f32da79d776336167 -
Trigger Event:
workflow_dispatch
-
Statement type: