AgentLedger Python SDK — track, attribute, and govern AI agent spend across OpenAI, Anthropic, and Gemini.
Project description
AgentLedger Python SDK
Track, attribute, and govern AI agent spend across OpenAI, Anthropic, and Gemini — with per-agent / per-task / per-user / per-customer cost attribution, kill switches, loop detection, and ROI reporting.
Zero dependencies. Python 3.9+.
pip install useagentledger
(The import name is agentledger: from agentledger import AgentLedger.)
Quick start
from agentledger import AgentLedger
al = AgentLedger(api_key="al_live_...") # provider keys read from env by default
response = al.run(
agent="invoice-processor",
task="extraction",
customer="acme-corp",
llm={
"provider": "anthropic",
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "Extract line items from this invoice: ..."}],
},
)
print(response["content"][0]["text"])
Every call is tracked automatically — token counts, latency, and attribution dimensions. Cost is computed server-side from AgentLedger's pricing table (134+ models), so the SDK never goes stale.
Kill switch
run() checks AgentLedger's block list before every call (cached 5 seconds). If an alert rule has blocked this agent, task, user, or customer, the call raises before any provider spend happens:
from agentledger import AgentLedger, AgentLedgerBlockError
try:
al.run(agent="support-bot", customer="acme-corp", llm={...})
except AgentLedgerBlockError as e:
print(f"Blocked: {e} (dimension={e.dimension}, value={e.dimension_value})")
The check fails open — if AgentLedger is unreachable, your LLM call proceeds normally. Governance never becomes a point of failure.
Streaming
Pass stream=True and iterate the returned generator. Usage is captured from the stream's own events and tracked when the stream ends (even if you stop early):
stream = al.run(
agent="chat-assistant",
user="user-42",
llm={
"provider": "openai",
"model": "gpt-4o",
"stream": True,
"messages": [{"role": "user", "content": "Hello!"}],
},
)
for event in stream:
delta = event.get("choices", [{}])[0].get("delta", {}).get("content")
if delta:
print(delta, end="", flush=True)
Works for all three providers (Anthropic message_start/message_delta events, OpenAI usage chunk, Gemini usageMetadata).
Sessions (multi-step agents)
Group a multi-step task into one session so loop detection and per-session cost work across steps:
session_id = al.start_session(agent="research-bot", task="report")
for step in plan:
al.run(agent="research-bot", session_id=session_id, llm={...})
al.end_session(session_id, success=True)
Calls without an explicit session are auto-grouped server-side (same agent + task within a 10-minute window).
Track calls you make yourself
Already using the official openai / anthropic clients? Just report usage:
result = openai_client.chat.completions.create(model="gpt-4o-mini", messages=msgs)
al.track(
agent="summarizer",
customer="acme-corp",
provider="openai",
model="gpt-4o-mini",
input_tokens=result.usage.prompt_tokens,
output_tokens=result.usage.completion_tokens,
)
track() runs on a background thread by default (zero added latency) and swallows all errors. Pass background=False to send synchronously.
ROI units
Record completed business outcomes for cost-per-outcome reporting (custom_event count method):
al.record_unit(agent="invoice-processor", unit_count=1)
Configuration
al = AgentLedger(
api_key="al_live_...",
base_url="https://api.useagentledger.com", # or your self-hosted proxy
provider_keys={ # optional — env vars used otherwise
"anthropic": "...", # ANTHROPIC_API_KEY
"openai": "...", # OPENAI_API_KEY
"gemini": "...", # GOOGLE_API_KEY
},
)
Project details
Release history Release notifications | RSS feed
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 useagentledger-0.1.0.tar.gz.
File metadata
- Download URL: useagentledger-0.1.0.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
17d37863e07b8d52c5ca483534d9dc3c19ae81e438bf1d5c36a09d1dff4d9d23
|
|
| MD5 |
c54607479a048be29d0b64c187ae004d
|
|
| BLAKE2b-256 |
79693bd9d34efc699bec1cd1373ec54cdae1a8ee1055ee75eeede2bcc08eb247
|
File details
Details for the file useagentledger-0.1.0-py3-none-any.whl.
File metadata
- Download URL: useagentledger-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c5f0944c9c0422b5ea167398eb67109c27865e2c6e401949328d96de0e3561bd
|
|
| MD5 |
3219a25b4cb56ef55aae1af07587fcf0
|
|
| BLAKE2b-256 |
1719dc25fb42f66e32766338dc3feafe7160976f8c752e68875556532325e79a
|