> ## Documentation Index
> Fetch the complete documentation index at: https://polar.sh/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Track usage

> Send usage events from your application and aggregate them with a meter.

> [!NOTE]
>
> **Why start here?** Events are the raw record of what customers do. Send them early and you'll have real usage data, whether you charge for it or not. See [Usage](https://polar.sh/docs/concepts/usage.md).

In this step you'll send events for an AI feature and create a meter that sums the tokens each customer uses.

The examples use the sandbox client from the [quickstart overview](https://polar.sh/docs/quickstart/overview.md#prerequisites), on API version `2026-10`.

## Send your first event

Send an event whenever a customer does something you want to measure, whether you'll bill for it, limit it or just track it. Identify the customer with your own user ID as `external_customer_id` (see [External IDs](https://polar.sh/docs/integrate/external-ids.md)):

```typescript TypeScript theme={null}
import { polar } from "./polar";

await polar.events.ingest({
  events: [
    {
      name: "ai_usage",
      external_customer_id: "user_123",
      metadata: {
        model: "gpt-4.1-nano",
        total_tokens: 77,
      },
    },
  ],
});
```

```python Python theme={null}
polar.events.ingest(
    events=[
        {
            "name": "ai_usage",
            "external_customer_id": "user_123",
            "metadata": {
                "model": "gpt-4.1-nano",
                "total_tokens": 77,
            },
        }
    ]
)
```

```bash cURL theme={null}
curl -X POST https://sandbox-api.polar.sh/v1/events/ingest \
  -H "Authorization: Bearer $POLAR_ACCESS_TOKEN" \
  -H "Polar-Version: 2026-10" \
  -H "Content-Type: application/json" \
  -d '{
    "events": [
      {
        "name": "ai_usage",
        "external_customer_id": "user_123",
        "metadata": { "model": "gpt-4.1-nano", "total_tokens": 77 }
      }
    ]
  }'
```

You don't need to create the customer first. Polar accepts the event now, and links it to the customer once one exists with `external_id` set to `user_123`. You'll do that in [step 3](https://polar.sh/docs/quickstart/checkout.md).

> [!TIP]
>
> Send raw facts, like the tokens used by one request, not running totals. Polar does the aggregation, so the same events can feed several meters later.

For LLM calls, streams and S3 uploads, an [ingestion strategy](https://polar.sh/docs/features/usage-based-billing/ingestion-strategies/ingestion-strategy.md) can send these events for you.

## Create a meter

A meter selects events with a filter and aggregates them into usage. Create one that sums `total_tokens` across `ai_usage` events:

> [tab] Dashboard
>
> * Open **Products → Meters**
> * Click **Create Meter**
> * Set **Name** to "AI tokens"
> * Under **Filters**, add a condition: **Name** equals `ai_usage`
> * Set **Aggregation** to **Sum** over the property `total_tokens`
> * Click **Create Meter**

> [tab] CLI
>
> ```bash theme={null}
> export METER_ID=$(polar meters create --name "AI tokens" \
>   --filter '{"conjunction":"and","clauses":[{"property":"name","operator":"eq","value":"ai_usage"}]}' \
>   --aggregation '{"func":"sum","property":"total_tokens"}' | jq -r .id)
> ```
>
> See [`polar meters`](https://polar.sh/docs/integrate/cli/reference/meters).

> [tab] Prompt
>
> ```text Prompt theme={null}
> Create a meter called AI tokens in my Polar sandbox that sums total_tokens across ai_usage events.
> ```
>
> Uses the `meters_create` operation of the [Polar MCP server](https://polar.sh/docs/integrate/mcp.md).

From your code:

```typescript TypeScript theme={null}
const meter = await polar.meters.create({
  name: "AI tokens",
  filter: {
    conjunction: "and",
    clauses: [{ property: "name", operator: "eq", value: "ai_usage" }],
  },
  aggregation: { func: "sum", property: "total_tokens" },
});
```

```python Python theme={null}
meter = polar.meters.create(
    name="AI tokens",
    filter={
        "conjunction": "and",
        "clauses": [{"property": "name", "operator": "eq", "value": "ai_usage"}],
    },
    aggregation={"func": "sum", "property": "total_tokens"},
)
```

```bash cURL theme={null}
export METER_ID=$(curl -s -X POST https://sandbox-api.polar.sh/v1/meters/ \
  -H "Authorization: Bearer $POLAR_ACCESS_TOKEN" \
  -H "Polar-Version: 2026-10" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "AI tokens",
    "filter": {
      "conjunction": "and",
      "clauses": [{ "property": "name", "operator": "eq", "value": "ai_usage" }]
    },
    "aggregation": { "func": "sum", "property": "total_tokens" }
  }' | jq -r .id)
```

Metadata keys are used directly as properties, without a `metadata.` prefix.

Polar backfills a new meter from events you've already sent, so your first event counts.

Keep the meter's `id`. You'll use it in the next step. The CLI and cURL examples save it as `METER_ID`.

## Verify it works

Read the meter's usage for `user_123`. It includes the 77 tokens from your event.

> [tab] Dashboard
>
> * Open **Products → Meters**
> * Select **AI tokens**
> * Open the **Customers** tab to see usage per customer, or **Events** to see the event you sent

> [tab] CLI
>
> ```bash theme={null}
> polar meters quantities $METER_ID --external-customer-id user_123 \
>   --start-timestamp 2026-11-01T00:00:00Z --end-timestamp 2026-12-01T00:00:00Z --interval month
> ```

> [tab] Prompt
>
> ```text Prompt theme={null}
> Show this month's usage of the AI tokens meter for customer user_123 in my Polar sandbox.
> ```
>
> Uses the `meters_quantities` operation of the [Polar MCP server](https://polar.sh/docs/integrate/mcp.md).

## Learn more

| Resource | Description |
| - | - |
| [Send events](https://polar.sh/docs/features/usage-based-billing/event-ingestion.md) | Batching, balance checks, limits and backdated events |
| [Meters](https://polar.sh/docs/features/usage-based-billing/meters.md) | Filters, aggregations and units |
| [Cost insights](https://polar.sh/docs/features/cost-insights/introduction.md) | Track what each customer costs you on the same events |

> [card] [Next: Set up your catalog](https://polar.sh/docs/quickstart/catalog.md)
>
> Turn this meter into a price, and include credits in a plan.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.