# Usage Lab

Usage Lab is a usage-metering API for AI and SaaS products, operated by Canada Site Ops. Define meters, send usage events with an idempotency key, and read monthly totals per customer (called `subscription`) for usage-based billing.

- Operator: Canada Site Ops
- Contact: canada.site.ops@gmail.com
- Site: https://usage-lab.pages.dev/

## How to start

1. Read https://usage-lab.pages.dev/llms.txt (and https://usage-lab.pages.dev/llms-full.txt if you need the full docs text).
2. Or connect to the remote MCP server at https://usage-lab.pages.dev/mcp (Streamable HTTP). Docs: https://usage-lab.pages.dev/docs/mcp/index.md
   The server is also listed in the official MCP Registry as dev.pages.usage-lab/usage-lab: https://registry.modelcontextprotocol.io/v0.1/servers?search=dev.pages.usage-lab/usage-lab
3. Or follow the Agent Skill at https://usage-lab.pages.dev/.well-known/agent-skills/usage-lab-metering/SKILL.md
4. OpenAPI 3.1: https://usage-lab.pages.dev/openapi.json
5. A free key (100k events / 3 meters) comes from https://usage-lab.pages.dev/signup/. The user must supply the key; never invent one. Keys start with `ul_`.

## API basics

- Base URL: `https://usage-lab.pages.dev/api/v1`
- Auth: `Authorization: Bearer <api key>` or `X-Api-Key`
- Record usage: `POST /usage` with header `Idempotency-Key`. Build the key from something stable (response id, run id + tool call id, request id), never a random value per retry. Same key and same payload returns the original event (`duplicate: true`); same key with a different payload returns 409 `idempotency_conflict` (the original is kept). Do not retry a 409 with a new key.
- Create or list meters: `POST /meters` and `GET /meters`. Aggregations: `sum`, `count`, `unique_count`. 409 `meter_exists` means the meter is already there; treat that as success.
- Monthly rollups: `GET /meters/{meter}/rollups?month=YYYY-MM&subscription=...` (months are UTC).
- Late events are accepted up to 30 days back. Batches hold up to 100 events and are all-or-nothing on validation errors.
- Errors are `{"error":{"code","message"}}`.

## Agent and bot traffic

Agent, bot and MCP tool calls are real billable usage. Never filter them out. Count them in their own meter (a separate traffic class). Guide: https://usage-lab.pages.dev/meter-ai-agent-tool-calls-per-customer/index.md

## Packages

- `pip install https://usage-lab.pages.dev/sdk/pkg/usage_lab_tokens-1.0.0-py3-none-any.whl`
- `npm install https://usage-lab.pages.dev/sdk/pkg/usage-lab-tokens-1.0.0.tgz`

More: https://usage-lab.pages.dev/sdk/index.md

## Check events without a key

Paste event JSON into https://usage-lab.pages.dev/validator/ to check it against the real API rules. Docs Markdown: https://usage-lab.pages.dev/docs/index.md
