aforo-agent-metering
Meter AI agent runs from Python: record each capability invocation and reasoning step as an AI_AGENT usage event, buffered and batched to Aforo. For agent runtimes such as LangChain, LlamaIndex, CrewAI, AutoGen and FastAPI-hosted agents. Python sibling of @aforoai/agent-metering (Node).
Version: 1.1.1 · Changelog · User guide
Install
pip install aforo-agent-metering
The package depends on httpx. 1.0.0 changes who is billed when no customer is given; see the changelog before upgrading from 0.x.
From source:
git clone https://github.com/aforoai/SDKs.git
cd SDKs/aforo-metering-sdks/python-agent
pip install -e .
Quickstart
import asyncio
import os
from aforo_agent_metering import AforoAgentClient
async def main():
client = AforoAgentClient(
tenant_id="tenant_xxx",
product_id="prod_ai_001",
api_key=os.environ["AFORO_API_KEY"],
# ingestor_url defaults to https://api.aforo.ai
)
client.record_capability( # synchronous: buffers the event
capability_name="summarize_email",
agent_id="agt_001",
customer_id="cust_42",
session_id="sess_1",
input_tokens=320,
output_tokens=84,
execution_duration_ms=510,
)
await client.shutdown() # sends what is left (also runs at exit)
asyncio.run(main())
record_capability and record_step only queue the event; they need no event loop and never block. A background thread sends batches. Nothing has to be started (start() still exists and does nothing). Code without an event loop can use client.flush_sync() and client.shutdown_sync().
Events POST to https://api.aforo.ai/v1/ingest/batch as {"events": [...]} with X-API-Key: <api_key> and X-Tenant-Id: <tenant_id>. No Authorization header is sent.
Decorator
from aforo_agent_metering import AforoAgentClient, wrap_capability_handler
client = AforoAgentClient(...)
@wrap_capability_handler(
client,
capability_name="summarize_email",
customer_resolver=lambda args, kwargs: current_user().customer_id,
)
async def summarize_email(text: str, *, agent_id: str, session_id: str):
...
The decorator needs to know who to bill. Give it one of:
customer_resolver(args, kwargs)— returns the customer from something your service authenticated.trust_customer_header=True— bills the call'scustomer_idkeyword argument. Only when that value comes from your own trusted code, not from an end user.default_customer_idon the client — bills every call to that customer (single-customer deployments).
With none of these, decorating raises ValueError. When you pass a customer_resolver and it raises or returns nothing, the call is not sent (reason invalid) even if the client has a default_customer_id: the default never stands in for a failed lookup. agent_id and session_id are read from the call's keyword arguments; a call without either is not sent (reason invalid) — the ingestor rejects an AI_AGENT event without a session. Before 1.0.0 a missing customer was billed to the agent_id, and a missing agent to "unknown".
The decorator times the handler, records one invocation when it finishes, and re-raises whatever the handler raised. Status: returned → SUCCESS; TimeoutError → TIMEOUT; any other exception → ERROR; asyncio.CancelledError / KeyboardInterrupt / SystemExit → CANCELLED. The handler must be async def.
Configuration
| Option | Type | Default | What it does |
|---|---|---|---|
tenant_id |
str |
— (required) | Aforo tenant; sent as X-Tenant-Id. |
product_id |
str |
— (required) | AI_AGENT product the events bill against; stamped in metadata.productId. |
api_key |
str |
— (required) | Aforo API key, sent as X-API-Key. |
ingestor_url |
str |
https://api.aforo.ai |
Host; /v1/ingest/batch is appended. |
default_customer_id |
str? |
None |
customerId when a call passes none. The only fallback: without it, an event with no customer is dropped as invalid. |
product_type |
str |
"AI_AGENT" |
Top-level productType on every event (trimmed + upper-cased). Override per event with record_capability(..., product_type=...). |
flush_count |
int |
500 |
Batch size; a queue this long wakes the sender (clamped to 1000, the ingestor's batch limit). |
flush_interval_sec |
float |
5.0 |
Seconds between background sends. |
max_queue_size |
int |
10000 |
Events held in memory. When full, the oldest are dropped with reason overflow. |
max_retries |
int |
3 |
Retries per batch before the circuit breaker pauses sending. The events stay queued. |
timeout_sec |
float |
10.0 |
Per-request timeout. |
shutdown_timeout_sec |
float |
5.0 |
Time limit for shutdown() and the exit hook. |
allow_insecure_http |
bool |
False |
Accept a plain http:// ingestor that isn't localhost. Otherwise ingestor_url must be https://. |
on_error |
Callable[[Exception], None]? |
logs at debug | Receives a TransportError; branch on .kind: outage / auth (events still queued and retried), rejected (the ingestor refused them; message carries its errors[].message), shutdown, closed. .events and .status say which events and which HTTP status. |
on_drop |
Callable[[list[dict], str], None]? |
None |
Opt-in hook for events the SDK loses for good. |
post_fn |
PostFn? |
None (httpx) |
Async (url, headers, body) -> (status, body[, retry_after]) used instead of httpx, for tests. Runs on the sender thread. |
compression |
str |
"gzip" |
"gzip" sends batches of 1 KiB or more with Content-Encoding: gzip (needs usage-ingestor wave 8 or later); "none" always sends plain JSON. Always off with post_fn, which receives the JSON as text. |
Execution status
execution_status is trimmed and upper-cased. Accepted values: SUCCESS, PARTIAL, TIMEOUT, ERROR, VALIDATION_FAILED, FAILED, FAILURE, CANCELLED, PENDING, BLOCKED, HITL_REQUIRED (exported as EXECUTION_STATUSES). A blank or unknown value is logged and left off; the event is still sent. OUTCOME_BASED rate plans bill each event at the weight set for its status — see the repository README.
Retries and dropped events
One background thread sends, one request at a time, over one pooled connection. Network errors, timeouts, 408, 429, 5xx, 401 and 403 are an outage: the batch is retried with jittered backoff (Retry-After read as seconds or an HTTP date, capped at 60 s), then a circuit breaker pauses sending and the events stay queued. Nothing is dropped for being unreachable — only when the queue is full. Any other 4xx drops the batch as rejected; a 413 splits it first. Every 5xx is retried, not only 500/502/503/504. A redirect (3xx) is never followed, so the API key cannot reach another host; it is treated like 401/403 and logged at ERROR with the target host. The circuit-breaker pause is full jitter: at most 5 s at first and at least 250 ms, growing to 5 min. Each request's timeout_sec covers the whole exchange, including reading the response. A flush during an outage makes one attempt; shutdown keeps retrying until its deadline. An event that cannot be turned into JSON is dropped as invalid when it is recorded.
record_capability and record_step never raise for event content. Every event the SDK cannot deliver is counted in client.dropped_count, WARN-logged, and passed to on_drop(events, reason). Dropped events keep their idempotency keys.
reason |
When |
|---|---|
overflow |
The queue was full (max_queue_size); the oldest events were dropped to make room. |
shutdown |
Still queued when shutdown() or the exit hook reached shutdown_timeout_sec. |
closed |
Recorded after shutdown(). |
rejected |
A non-retryable 4xx for the batch (on_error gets the ingestor's errors[].message), a batch that could not be JSON-encoded, or events the ingestor rejected individually in a 202. For a 202, only the events the response names by index are passed to the hook; if it names none, they are counted only. |
invalid |
The SDK refused the event before queueing it: no customer (none passed and no default_customer_id, or the decorator's customer_resolver found none), blank capability_name / agent_id / session_id (the ingestor requires a session on every AI_AGENT event; step_type too for a step), agent_id over 36 characters, session_id, customer_id or capability_name over 64 UTF-16 code units, trace_id over 128, a token count that is not a whole number (None counts as 0; "84" and 84.0 are accepted), metadata that is not a dict. Nothing passed to record_* or to the decorator is truncated. The one exception: a capability name that wrap_capability_handler reads from the wrapped call's capability_name kwarg is truncated to 64 and the event is still sent. The WARN names the field and limit; it is logged on the first occurrence and then every 1000th. |
Wire format
Each event carries top-level customerId, metricName (ai_agent.capability_invocations or ai_agent.steps), quantity 1, occurredAt (UTC, milliseconds, Z; always stamped by the SDK), idempotencyKey (agent:<uuid4>, minted once when the event is created), productType, agentId, sessionId, traceId, executionDurationMs, optional executionStatus, and metadata with capability_name (snake_case — the key the ingestor maps to the per-capability billing dimension), token counts and sdkVersion.
traceId is how the ingestor groups an agent session's steps, tokens and cost. It is 32 lowercase hex characters, like @aforoai/agent-metering (Node): record_*(..., trace_id=...) if you pass one, otherwise session_trace_id(product_id, customer_id, agent_id, session_id), so every process that records the same session sends the same one. Token counts go in metadata.input_tokens / metadata.output_tokens, the keys Node sends and the ingestor reads for COGS and session totals; inputTokens, outputTokens and totalTokens are still sent. An execution_duration_ms a Java int cannot hold (the ingestor stores executionDurationMs as one) is left off, the event is still sent, and one WARNING is logged per client.
The endpoint path and body shape are checked against contract/ingest-contract.json by this package's tests.
Walk me through it
Install → construct → record → confirm the event in Aforo: USER_GUIDE.md. A runnable example is in examples/hello_agent.
What this doesn't cover
This SDK emits usage events. It does not send session heartbeats, check entitlements or quotas, or price anything — rate plans and per-capability pricing are configured in the Aforo console. A process killed with SIGKILL (or out of memory) loses whatever was still queued.
Metadata
Release files for aforo-agent-metering 1.1.1
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 |
|---|---|---|---|---|
| aforo_agent_metering-1.1.1.tar.gz | 59.3 kB | With provenance | Details for aforo_agent_metering-1.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| aforo_agent_metering-1.1.1-py3-none-any.whl | Python 3 | none | any | Details for aforo_agent_metering-1.1.1-py3-none-any.whl |
Total release size: 103.2 kB
Release files / aforo_agent_metering-1.1.1.tar.gz
| Download URL | aforo_agent_metering-1.1.1.tar.gz |
|---|---|
| Size | 59.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1530d77af8e3c9bdf1b9267d35119df160a5abcb9c3967bcb1516b37444bf1e6
|
|
BLAKE2b-256 checksum How to use checksums |
da0f662889ed9b613821c0cbf5b5a8b36492483b4a2a9cd2dfeee9a6c1bddd56
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Nov 10, 2026.
Transparency logRelease files / aforo_agent_metering-1.1.1-py3-none-any.whl
| Download URL | aforo_agent_metering-1.1.1-py3-none-any.whl |
|---|---|
| Size | 43.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
62b5ac86e8f9dbd4579f785cdee797cc69f4012909b49bea5f59e0e1a2981253
|
|
BLAKE2b-256 checksum How to use checksums |
02c496f800d3d06e15907f7928bc4129e7885b67c0fa2130f92529f984a6593e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Nov 10, 2026.
Transparency logSecurity for aforo-agent-metering 1.1.1
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
Both files in this release have been uploaded with provenance metadata.
Attestation for aforo_agent_metering-1.1.1.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 Nov 10, 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.