This is one of several ingestion strategies. For the bigger picture — meters, metered prices, and credits — start with the strategy introduction.
Why meter LLM usage
The token count from a single completion is the raw material for three different billing models. Pick the one that matches how you charge:
The LLM strategy captures the per-call token detail. From there, a meter aggregates the events and a metered price turns them into charges — or feeds your agent billing and value-receipt flows when you’re charging for outcomes rather than raw tokens.
TypeScript SDK
The strategy wraps any model from the@ai-sdk/* family. Once wrapped, prompt and completion tokens fire automatically on every model call — generateText, streamText, and friends all work unchanged.
1
Install the packages
2
Wrap the model and route calls through it
Configure the strategy once at module scope, then derive a per-customer client inside each request so events are attributed correctly.
What gets ingested
Each model call emits an event named after the string you passed to.ingest(). Token metadata is filled in automatically; the _cost block appears only when you provide a .cost() callback.
Amounts are always integer cents, never decimals —
$0.42 is 42, $1.23 is 123.Python SDK
The ingestion helper ships inside the Macropay SDK. It batches events and sends them in the background, so metering never blocks the response you’re returning to the user.Ingestion helper
For full control, call the helper directly. This is the lowest-level entry point — useful when you’re metering something other than a framework’s built-in usage object.PydanticAI strategy
PydanticAI is a typed agent framework for Python. ItsRunResult already carries token usage, so the strategy reads it straight off the result you get back — one ingest() call and the run is metered.
The structured-output pattern above follows the Pydantic model example in the PydanticAI docs.