Skip to Content
TutorialsTrack prompt costs

Track prompt costs

In this tutorial you tag a few requests with metadata, then read back how much they cost. By the end, you will know how to attribute spend to a specific prompt template, session, or end user.

Prerequisites

  • A PlainField API key (pt_live_...) — see the Quickstart
  • curl

1. Send a tagged request

PlainField reads three metadata headers, strips them before forwarding to the provider, and stores them with the log entry.

curl -X POST https://api.plainfield-ai.com/ai/chat/completions \ -H 'Authorization: Bearer pt_live_...' \ -H 'Content-Type: application/json' \ -H 'plainfield-prompt-id: greeting-v1' \ -H 'plainfield-session-id: session-abc' \ -H 'plainfield-user-id: user-42' \ -d '{ "model": "openai/gpt-4o-mini", "messages": [{"role": "user", "content": "Write a one-line greeting."}] }'
HeaderGroups requests by
plainfield-prompt-idPrompt template
plainfield-session-idUser session
plainfield-user-idEnd user

2. Send a few more

Run the request two or three more times so the cost summary has data to aggregate. Vary the prompt content if you like; keep the same plainfield-prompt-id.

3. Read the cost summary

You can read costs two ways.

In the dashboard

Open Analytics. The overview shows spend, request counts, latency, and token usage, with charts over time and a per-prompt breakdown. Filter by model, provider, and date range, and open the request log to inspect individual requests — including the metadata you attached. This is the easiest way to explore your usage.

From the API

Query the cost summary endpoint with your API key:

curl 'https://api.plainfield-ai.com/api/costs' \ -H 'Authorization: Bearer pt_live_...'

A summary broken down by model:

{ "total_requests": 3, "total_cost_usd": 0.0009, "total_input_tokens": 36, "total_output_tokens": 27, "by_model": { "gpt-4o-mini": { "requests": 3, "cost_usd": 0.0009, "input_tokens": 36, "output_tokens": 27 } } }

4. Inspect individual logs

curl 'https://api.plainfield-ai.com/api/logs?limit=5&provider=openai' \ -H 'Authorization: Bearer pt_live_...'

Each entry includes the model, provider, token counts, estimated cost, and the metadata you attached.

Where cost comes from

PlainField reads token counts from the provider’s response and multiplies them by a built-in pricing table. There is no extra call to the provider for pricing. Read more in Cost estimation.

Common errors

SymptomCauseFix
total_cost_usd is 0The model is not in the pricing tableUse a listed model. See Providers and models.
Metadata not visible in logsHeader names misspelledUse exactly plainfield-prompt-id, plainfield-session-id, plainfield-user-id.
Empty /api/costsNo requests in the time windowSend a request first, then query.

Next steps

Last updated on