OpenCode

OpenCode info.cost: reading the message field

What the info.cost field on an OpenCode message means, when it is populated, and how to compute a cost when it is missing.

Quick answer

`info.cost` is the cost value attached to an OpenCode assistant message. It is populated when the provider reports pricing or when OpenCode can compute it from a known model. It can be zero for free models, null for unknown models, and absent in older records. Treat it as a per-message estimate.

The three states

ValueMeaningWhat to do
Positive numberReported or computed costSum it
ZeroFree model or unpriced providerCheck the model before assuming free
MissingOlder record or unknown modelApply rates manually

Read it safely

Do not assume every message has the field. A query that sums info.cost across a mixed data set will silently ignore missing rows, which understates the total.

select count(*),
       sum(case when cost is null then 1 else 0 end) as missing,
       sum(cost) as total
from message;

Compute the missing rows

  1. Group missing rows by model.
  2. Look up the current rate for each model.
  3. Apply input, cache read, cache write, and output separately.
  4. Add the computed value to the reported total.

If you would rather not maintain a rate table, UsageAtlas shows both reported and computed cost and marks which is which.

What the cost field really contains

When OpenCode records a cost value, it comes from what the provider reported for that message. That makes it the most accurate per-message number available locally, but it is not an invoice: discounts, credits, and plan allowances are applied later.

ValueSourceUse it for
Reported costProvider responseRelative comparison
Calculated costTokens times ratesEstimation and planning
Plan usageProvider dashboardWhether you can continue
InvoiceBillingWhat you actually pay

Read the field as a signal, not a receipt. The cost tracking guide explains how to combine it with token-based estimates.

Questions, answered

Is info.cost a string or a number?

It is a numeric value in the message metadata. Formats vary by version, so parse defensively.

Does info.cost include cache writes?

When the provider prices them. If the field is computed locally, it depends on the pricing table in use.

Why do two messages with the same tokens have different costs?

Different models, different cache shares, or a pricing change between the two.

Browse by topic