Skip to main content
Cost tracking lets you record the variable cost of serving each customer — AI inference, third-party API calls, cloud compute, data enrichment, or any other spend that varies with usage. Paygentic aggregates these cost events per customer and period, so you can compare total cost against revenue and understand your gross margin at the customer level. Unlike billable metrics (which measure what your customers consume for billing), cost metrics measure what you pay to serve them. Common use cases:
  • AI-powered products — Track LLM token costs, embedding calls, and agent compute per customer
  • Data enrichment platforms — Record the cost of third-party API calls (geocoding, credit checks, identity verification) attributed to each customer
  • Document processing — Attribute OCR, translation, or extraction pipeline costs to the customers who triggered them
  • Multi-tenant SaaS — Track cloud compute or storage spend that varies by customer workload

How it works

  1. Create a cost metric that defines what you measure and how to aggregate it
  2. After each operation that incurs a cost, send a cost event to POST /v0/events with the dollar amount
  3. Paygentic aggregates cost events by customer and time period
  4. Query the aggregated costs via GET /v0/costs/{id}/summary to build margin dashboards or feed your own analytics

Before you begin

  • An API key for your organization
  • A product created in Paygentic (you need the productId)
  • Your customer IDs available at the time operations are performed

Step 1: Create a cost metric

A cost metric defines which events contribute to cost tracking, how to extract the amount, and how to slice costs by dimension. Every cost must be attached to a product via productId.
Response:

Step 2: Send cost events

After each operation that incurs a cost, send an event with the dollar amount. Use the same POST /v0/events endpoint as meter events — the type field links the event to your cost metric.
Send cost events after the operation completes, once you have the actual cost from your provider. The amount field should be the total cost in the currency configured on the cost metric.

Calculating the amount

The amount you record should reflect what you actually paid — not an estimate. Retrieve this from your provider’s response or invoice data:

Step 3: Query cost summary

Use GET /v0/costs/{id}/summary to retrieve aggregated cost data for a specific cost metric over a time window.
Response:

Group by dimensions

Add groupBy to break down costs by the dimensions you configured on the cost metric:
Response includes a groups array:

Time-series breakdown

Add window to get a time-series breakdown:
Response includes a timeSeries array:

Filter by dimension values

Use filterGroupBy to narrow results to specific dimension values:
The filterGroupBy value is a JSON-encoded object. Keys must match those defined in the cost’s groupBy configuration.

Managing costs

List costs

Retrieve all costs for your organization, optionally filtered by product:
Response:

Get a single cost

Update a cost

All fields are optional — only include the fields you want to change:

Delete a cost

Deleting a cost performs a soft delete. The cost is no longer returned in list queries, but historical data is preserved.
Returns 204 No Content on success.

Tracking multiple cost dimensions

Create separate cost metrics for each distinct type of spend. Each metric has its own eventType and tracks independently. Example: separate metrics for different cost categories
Events are sent with the matching type:
Multiple cost metrics can read from the same eventType. This lets you extract and aggregate different fields from the same event — for example, tracking GPU cost and CPU cost separately from a single compute event.A single event can also be processed by both a billable metric and a cost metric simultaneously. For example, an ai.inference event can drive customer billing via a billable metric while the same event feeds your cost tracking — no need to send separate events.

API reference

Create cost request

Cost response object

Cost summary query parameters

Cost summary response

Cost event data fields

The data payload is arbitrary JSON. Only amount (or whichever path valueProperty points to) is required — all other fields are optional context that improves filtering and dimension slicing. Any other fields you include in data can be referenced in groupBy on your cost metric.

Idempotency

Use idempotencyKey to avoid double-counting costs when retrying failed requests:
  • If two events share the same idempotencyKey, only the first is recorded
  • The deduplication window is 24 hours
  • Omitting idempotencyKey means retries will create duplicate cost entries

Best practices

Send cost events after the operation completes. You need the actual cost from your provider — don’t estimate upfront and correct later. Use idempotencyKey on every event. Derive it from a stable operation identifier (job ID, request ID) to make retries safe. One cost event per billable operation, not per customer request. A single customer request may trigger multiple downstream calls — track each one individually for accurate attribution. Separate metrics for different cost categories. Inference, enrichment, and compute have different unit economics. Tracking them independently makes it easier to understand where margin pressure is coming from.

Next steps