AgentSpend — runtime cost optimizer for AI agents. Route LLM calls to the cheapest capable model with fallbacks, loop guards, and telemetry.
Project description
token-aud
AI cost optimization toolkit for LLM workloads.
token-aud now includes two complementary workflows:
- Audit mode (CLI/API): Analyze historical usage logs and estimate savings opportunities with Student-Teacher-Judge sampling.
- AgentSpend SDK: Route live agent steps (
plan,reason,tool,verify,draft,summarize) to cost/quality-appropriate models with fallbacks, loop guards, and telemetry.
Installation
uv sync --no-editable
For local development tooling (tests):
uv sync --no-editable --extra dev
Quick Start (AgentSpend)
Run these three commands from repo root:
uv sync --no-editable --extra dev
uv run --no-sync python -m pytest tests/agent -q
uv run --no-sync python examples/agent_routing_demo.py
Expected results:
- Agent tests pass.
- Demo prints routed step decisions, per-step telemetry, and total run cost.
agent_telemetry.jsonlis generated locally.
AgentSpend Usage
1) Default policy
from token_aud.agent import AgentSpend
agent = AgentSpend.default()
result = agent.route_call(
step="plan",
messages=[{"role": "user", "content": "Break this task into a plan"}],
)
print(result.model_used, result.cost_usd, result.content)
2) Custom policy YAML
from token_aud.agent import AgentSpend
agent = AgentSpend.from_yaml("routing_policy.yaml")
result = agent.route_call(
step="reason",
messages=[{"role": "user", "content": "Compare two architectures"}],
)
print(result.model_used, result.fallbacks_tried)
Built-in default policy path:
src/token_aud/data/default_routing_policy.yaml
AgentSpend Core Components
src/token_aud/agent/policy.py: Pydantic policy schema + YAML loadingsrc/token_aud/agent/router.py: deterministic model selectionsrc/token_aud/agent/runtime.py:route_call()execution + fallbackssrc/token_aud/agent/loop_guard.py: repeated-turn loop detectionsrc/token_aud/agent/telemetry.py: JSONL/HTTP telemetry sinkssrc/token_aud/agent/adaptive.py: optional adaptive routing layer
AgentSpend Examples
examples/agent_routing_demo.py: end-to-end routed run with telemetryexamples/custom_policy_demo.py: loop escalation and hard-stop behaviorexamples/framework_agnostic_integration.py: generic agent-loop integration with explicit success feedbackscripts/summarize_telemetry.py: convertagent_telemetry.jsonlinto cost/fallback/latency summary
uv run --no-sync python scripts/summarize_telemetry.py agent_telemetry.jsonl
Audit CLI (legacy + still supported)
uv run --no-sync token-aud --help
uv run --no-sync token-aud analyze sample_data.csv --dry-run
Environment Variables
Common provider credentials:
OPENAI_API_KEYANTHROPIC_API_KEYGEMINI_API_KEYorGOOGLE_API_KEY(depending on provider path)
For Google Vertex flows, ensure ADC is configured (gcloud auth application-default login).
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 agentspend_sdk-0.1.0.tar.gz.
File metadata
- Download URL: agentspend_sdk-0.1.0.tar.gz
- Upload date:
- Size: 224.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0ba818d150f69d87cf4436219abdfd5b4ef69d6a6ecfb962fd03bc8142383493
|
|
| MD5 |
020b3d66a84dfa932d536e452fe69211
|
|
| BLAKE2b-256 |
68f5d523c18953e01d198ea19954fb996b7401aef277ff1bdf71dc1e778c908c
|
File details
Details for the file agentspend_sdk-0.1.0-py3-none-any.whl.
File metadata
- Download URL: agentspend_sdk-0.1.0-py3-none-any.whl
- Upload date:
- Size: 54.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cd153ec9d7ea677eac8765489c2cee4c330491b9c772cfc396794ee0816e200d
|
|
| MD5 |
1323f35113f4bb784974f6ba25fbd7ee
|
|
| BLAKE2b-256 |
0d4e77897825a9c7e13116449cb4ff531884eddf4f0148839c548d0d8998d69f
|