Use Cases
AI Token Usage
Meter token consumption across models, providers, and operations.
Use AI token metering when your product consumes language model tokens or similar AI units on behalf of customers. This works for chat products, workflow automation, agents, summarization, embeddings, and internal AI tooling.
Model
Create a meter such as ai_tokens with:
| Field | Value |
|---|---|
| Unit | token |
| Aggregation | sum |
| Quantity | Tokens consumed by the completed operation |
Useful dimensions:
modelprovideroperationcached
How To Use It
Record usage after the provider returns a response and you know the final token count. Use dimensions to separate expensive models, cached paths, and product features.
This lets you answer:
- How many tokens did a customer consume?
- Which models cost the most?
- Which operations generate the most AI usage?
- How much traffic is served through cache paths?
Example
Create a token meter:
curl -X POST "$OPEN_SPANNER_BASE_URL/v1/meters" \
-H "Authorization: Bearer $OPEN_SPANNER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"name": "ai_tokens",
"description": "AI provider tokens consumed",
"unit": "token",
"aggregation": "sum",
"event_retention_days": 180,
"dimensions": [
{ "name": "model", "type": "string", "required": true },
{ "name": "provider", "type": "string", "required": true },
{ "name": "operation", "type": "string" },
{ "name": "cached", "type": "boolean" }
]
}'Record the token count after the provider returns final usage:
curl -X POST "$OPEN_SPANNER_BASE_URL/v1/usages" \
-H "Authorization: Bearer $OPEN_SPANNER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"idempotency_key": "chatcmpl_abc123_tokens",
"subject": "org_123",
"meter": "ai_tokens",
"quantity": 1842,
"metadata": {
"model": "gpt-4.1",
"provider": "openai",
"operation": "chat_completion",
"cached": false
}
}'