Skip to main content
Every model call your app makes has a cost and, increasingly, a price. The LLM strategy closes the gap between the two: wrap a model once, and Macropay records the input, output, and cached tokens of every call as a billable event — tagged to the customer who triggered it. No manual counting, no glue code in your request path. Because Macropay is your Merchant of Record, the events you ingest here roll straight into invoices where sales tax and VAT are calculated, collected, and remitted for you. You meter the tokens; we handle the seller-of-record obligations.
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.
The optional .cost() callback records what each call cost you. Pair it with billed revenue and Macropay can show your agentic margin — revenue earned versus AI cost — per customer or per agent, instead of leaving you to reconcile two spreadsheets.

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. Its RunResult 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.

What gets ingested

The strategy reads the run’s usage and emits a single event under the name you registered:

Where the events go

Once events land, a meter filters and aggregates them and a metered price converts the aggregate into charges on the customer’s next invoice. If you’re charging for results rather than raw tokens, route the same usage into outcome-based agent billing and issue value receipts that certify the ROI you delivered.

Need a different model framework?

Don’t see your stack? Any token count you can read in code can be sent through the ingestion helper directly — see the strategy introduction for the other built-in strategies, or email support@macropay.ai and we’ll help you wire it up.