agentaudit — Python SDK
Tamper-evident audit logging for AI agents. See docs/03_SDK_QUICKSTART.md.
import agentaudit
agentaudit.init(api_key="aa_live_sk_...", agent_id="my-support-agent")
agentaudit.set_session("sess_abc123")
agentaudit.log_event(
event_type="decision",
action="route_to_human",
metadata={"reason": "low_confidence"},
severity="warning",
)
Inputs and outputs are SHA-256 hashed, never sent raw (ADR-002). Pass
hash_inputs=False to init() to opt into raw logging; raw values then travel
under the reserved keys metadata.raw_input and metadata.raw_output, since
the event schema has no top-level field for them. init(environment=...) is
likewise recorded as metadata.environment.
Delivery
Capture never blocks: log_event appends to an in-memory queue and returns
(~0.05 ms). A daemon thread owning its own asyncio loop and httpx.AsyncClient
drains that queue every flush_interval seconds, or as soon as batch_size
events are waiting — whichever comes first (ADR-005). One implementation serves
both sync and async callers; nothing touches your event loop if you have one.
flush(timeout=10.0)forces a send and blocks until it completes.- Failed batches are retried
max_retriestimes with exponential backoff and jitter, then spilled to a JSONL file in the temp directory. The buffer is keyed to the API key andagent_id, so a restarted process finds its backlog. - While the backend is down, new events queue behind the buffered ones so delivery order matches production order. Buffered events replay first.
- A repeated failure starts a cooldown between cycles, so a long outage does not
cost a full retry budget every
flush_interval. An explicitflush()ignores it. - Permanent failures (
400,401) are discarded rather than buffered — they would fail identically forever. - An
atexithook flushes whatever is queued at process exit.
Decorator and LangChain integration (Week 2 Day 5-6)
@agentaudit.trace
def search_documents(query: str, top_k: int = 5) -> list:
return vector_db.similarity_search(query, k=top_k)
Logs a tool_call event per invocation: action is the function's qualified
name, input_hash/output_hash are SHA-256 digests of the arguments and
return value (never raw), metadata.duration_ms is execution time. A raised
exception is logged as severity="critical" with metadata.error and always
still propagates — @trace never changes what the wrapped function does.
Works on sync and async functions.
from agentaudit.integrations import LangChainHandler # pip install agentaudit[langchain]
chain.invoke(inputs, config={"callbacks": [LangChainHandler()]})
Handles on_llm_start/on_llm_end (and on_chat_model_start, which chat
models call instead), on_llm_error, on_tool_start/on_tool_end,
on_tool_error, and on_chain_error. LLM calls log model name and token
usage only — no prompt or response content, hashed or otherwise, since a
callback wired into every LLM call in a chain has no per-call decision behind
it. Tool calls are hashed, matching the SDK's default elsewhere.
All three error paths log the identical shape: event_type="error",
severity="critical", metadata={"error": {"type": ..., "message": ...}}.
A chain error's action is the chain's name when LangChain provides one
(config={"run_name": "..."}"), else generic. A tool or LLM error's action
is the tool/model name. on_tool_error fires even when the surrounding chain
catches the exception and completes normally — a tool failure the chain
recovers from is still worth an audit trail, and on_chain_error alone would
miss it entirely.
Chain start/end events are not implemented — that's session/trace-grouping territory, not an audit-trail gap, since every LLM call, tool call, and error within a chain is already captured on its own.
Both reuse Day 1-2's hashing and Day 3-4's sender through agentaudit.log_event
— no new transport code.
Implemented (Week 2 Day 1-6): init, log_event, set_session, status,
flush, shutdown, @trace, LangChainHandler, input/output hashing, event
builder, bounded queue, batch sender, offline buffer, retries, atexit hook.
Not yet: PyPI packaging (Day 7).
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 agentaudit_python-0.1.0.tar.gz.
File metadata
- Download URL: agentaudit_python-0.1.0.tar.gz
- Upload date:
- Size: 35.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7f03b1ef26bc7f7b51b50b304841964f54b1543ec6945903246c2074c4d00490
|
|
| MD5 |
729c4c2c9c0a5b7aea1d5c8c4d32ed0a
|
|
| BLAKE2b-256 |
f9e25f9c43975308656526c760c7c639d4c14541947e6909445d5de3a7f4b252
|
File details
Details for the file agentaudit_python-0.1.0-py3-none-any.whl.
File metadata
- Download URL: agentaudit_python-0.1.0-py3-none-any.whl
- Upload date:
- Size: 28.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1912606a3c7454e4c2192fd23d9e0dcd739bfd12c8775a17fc1d09dde728ac1e
|
|
| MD5 |
9769c01cb45919345c2a5524ad3e5b19
|
|
| BLAKE2b-256 |
9ff08fbd8818f6cbecb3ab6d0b1ee823aca7ffcfa276f999262f1f90c26a85a7
|