Skip to main content

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.0.0 · 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's customer_id keyword argument. Only when that value comes from your own trusted code, not from an end user.
  • default_customer_id on the client — bills every call to that customer (single-customer deployments).

With none of these, decorating raises ValueError. agent_id is read from the call's keyword arguments; a call without one is not sent (reason invalid). 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.

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), blank capability_name / agent_id (or session_id / step_type for a step), agent_id over 36 characters, session_id, customer_id or capability_name over 64 UTF-16 code units. 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, idempotencyKey (agent:<uuid4>, minted once when the event is created), productType, agentId, sessionId, 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.

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.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aforo-agent-metering 1.0.0
File Size Uploaded Provenance Actions
aforo_agent_metering-1.0.0.tar.gz 48.0 kB With provenance Details for aforo_agent_metering-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for aforo-agent-metering 1.0.0
File Interpreter ABI Platform Actions
aforo_agent_metering-1.0.0-py3-none-any.whl Python 3 none any Details for aforo_agent_metering-1.0.0-py3-none-any.whl

Total release size: 82.8 kB

Release files / aforo_agent_metering-1.0.0.tar.gz

Download URL aforo_agent_metering-1.0.0.tar.gz
Size 48.0 kB
Tags Source
SHA-256 checksum
How to use checksums
123f858e1dde2d386b4d1835d227f290d5564c9b37a0ff195939a7f728070b69
BLAKE2b-256 checksum
How to use checksums
e701aa66ce284d2767eff4ca488ef0f742841de022f4c4cd614f597afb430332
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 Oct 27, 2026.

Transparency log

Release files / aforo_agent_metering-1.0.0-py3-none-any.whl

Download URL aforo_agent_metering-1.0.0-py3-none-any.whl
Size 34.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4f35df331a81425bf7245df90120001986181f8e4aaf8d46a1f979a1cb39933e
BLAKE2b-256 checksum
How to use checksums
d9dcdaaf9ddc5b78d9fc5e4adb5e6e54896b7bfc0245cd8ddb64df5189eb113e
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 Oct 27, 2026.

Transparency log

Security for aforo-agent-metering 1.0.0

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.

Select from 2 files

Attestation for aforo_agent_metering-1.0.0.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 Oct 27, 2026.

Transparency log

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page