Answers · agent controls
How do I meter AI agent API usage with signed per-call usage records for invoicing, separate from human traffic?
Last updated
TL;DR: Bill agents from an append-only usage log, not from CDN page-view counters. On every successful agent API call (after auth), write a usage record that names the agent credential id, route or meter name, units (calls, tokens, rows, bytes), idempotency key, and timestamp, then seal a hash of that record (or a batch) in a signed receipt the customer can verify later. Human HTML and logged-in browser sessions stay on a separate, non-billable path so a shopper refreshing a page never appears on an agent invoice. At period end, aggregate only agent records into line items — the same pattern Stripe Billing Meters use with meter events (event_name, customer mapping, value, unique identifier) and that OpenAI's usage APIs expose per key or project for costs. This page is about metered invoicing evidence, not about rate limits (how fast an agent may call) or about x402 pay-per-call settlement at request time.
What should I check, step by step?
1. Create a meter per billable action (for example agent.api_call, agent.rows_exported) with a clear unit and aggregation (sum of calls, sum of rows). Do not reuse human analytics events.
- Identify the payer before you count: map each agent credential id to a customer (tenant or contract id). Reject or park calls that authenticate but have no billing mapping.
- On each billable agent response (2xx you intend to charge), emit one usage event with: customer_id, credential_id, meter/event_name, value, timestamp, and a unique idempotency key (request id). Stripe meter events recommend a UUID-like identifier so retries do not double-bill.
- Seal evidence: hash the canonical event (or a daily batch) and record it with record_ai_run or your own signed usage receipt so neither side can silently rewrite the period's totals later. Keep the full event store yourself; the seal holds hashes and counts.
- Keep humans out: middleware that meters must require an agent credential (or explicit agent API key). Session-cookie browser traffic must not increment agent meters.
- At invoice time, aggregate by customer and meter for the closed period, attach the sealed batch id or receipt ids as supporting evidence, and give the customer verify_ai_run / ledger_verify links for the seals.
- Handle disputes with the raw events: show the idempotency keys and credential ids for contested lines. If you refund, append a negative adjustment event; do not edit history.
- Separate hard rate limits from billing meters. A 429 means try later; a usage record means this successful call is on the next invoice.
Why must AI agent API usage meters exclude human page views?
CDN and product analytics count humans and bots together unless you filter. Invoices for agent API access need a counter that only advances when a verified agent credential (or your agent API key) passed auth on a billable route.
- OpenAI's organization usage and costs Admin APIs group spend by api_key_id, project_id and related dimensions — the same idea: attribute usage to the key or agent that caused it, not to a shared IP.
- If an agent and a human share a NAT, only the credentialed /api/* calls should land on the agent invoice. Browser document requests stay unmetered for this product (or on a different human plan).
What fields belong in a signed per-call AI agent usage record for invoicing?
Minimum fields: event_name (meter), customer_id, agent credential id, value (integer units), timestamp, idempotency key, route or operation id. Optional: model or tool name, bytes, row count, response class/tier.
- Stripe Billing Meters take high-throughput meter_events with event_name, payload mapped to a customer, a numeric value, and an identifier for deduplication — use the same shape even if you settle outside Stripe.
- Sign or hash-seal at the end of the call (or in a flush every N seconds). A CSV exported a week later without a seal is weaker evidence in a dispute than a receipt whose hash was fixed when the call finished.
How do I turn sealed AI agent usage records into an invoice a customer can dispute?
Close the period, sum events by meter and customer, and generate line items (your invoicing system, Stripe subscription invoices fed by meters, or Rightshold's usage-based invoice helper for agent services). Attach the sealed batch receipt ids.
- Let the customer recompute: give them the event export (or hashes) plus verify_ai_run on the seal. ledger_verify checks the chain the seal sits in. Agreement on the hash ends most "we never called that" arguments.
- Do not treat x402 payment at request time as a substitute for period invoices when your contract is monthly prepaid or postpaid usage — pay-per-call and monthly true-up are different products; pick one primary path per route.
How do I ask Rightshold about this (free, no key)?
Add the MCP server https://www.rightshold.com/mcp to Claude, Cursor, ChatGPT, Grok or VS Code, then ask: “Record a signed receipt that seals this billing period's agent API usage batch: SHA-256 of the usage export, call count, customer id and meter name, so my customer can verify the invoice support later.”
{
"tool": "record_ai_run",
"arguments": {
"model": "usage-meter/v1",
"provider": "origin",
"prompt_sha256": "<sha256 of the export query or period key>",
"output_sha256": "<sha256 of the usage export file>",
"params": {
"customer_id": "cus_<id>",
"meter": "agent.api_call",
"call_count": 12840,
"period_start": "2026-11-01T00:00:00Z",
"period_end": "2026-12-01T00:00:00Z",
"export_format": "jsonl, one event per line, sorted by idempotency_key"
},
"ran_at": "2026-12-01T01:00:00Z"
}
}- Needs an API key. Only hashes and the fields you send are stored. Give the customer the export file plus the receipt id so they can recompute output_sha256 and params_sha256.
- verify_ai_run is free and keyless: fetch the sealed receipt or check a detached signature.
- ledger_verify is free and keyless: recomputes hashes, chain links and signatures on the ledger the receipt is written to.
- issue_agent_credential / verify_agent_credential identify which agent produced billable calls; revoke_agent_credential stops further metered calls from that id without erasing past usage records.
Rightshold seals the hashes and fields you send and lets anyone verify them later. It does not count your HTTP traffic, charge cards or generate Stripe invoices unless you wire those systems yourself. Not legal advice.
What are the sources?
- Stripe: Billing meter events
- Stripe: Migrate to billing meters
- Stripe: Subscription invoices
- OpenAI: Organization costs Admin API
- OpenAI Help: Reviewing API usage and costs
- Verified AI Runs on Rightshold
- Usage-based invoices for agent services on Rightshold
Related
- How to rate limit an AI agent's API calls per key
- How to set per-agent rate limits and quotas that do not throttle human browsers
- How to see how much each AI agent spends on LLM API calls
- How to accept x402 payments on an API
Not legal advice. JSON · Markdown · All answers
Get a free API key in one call
No card and no email needed. 100 free credits; the license and policy checks stay free without a key.
curl -s -X POST https://www.rightshold.com/v1/accounts/signup -H "Content-Type: application/json" -d '{"accept_terms":true}'
export RIGHTSHOLD_API_KEY=rs_... # the api_key from the reply (shown once)
curl -s https://www.rightshold.com/v1/licenses/check -H "Authorization: Bearer $RIGHTSHOLD_API_KEY" \
-H "Content-Type: application/json" -d '{"license":"MIT","intended_use":{"commercial_use":true}}'
