OpenCode

OpenCode cost per session: how to calculate it

Calculate OpenCode cost per session from message-level token and cost fields, including cache writes and multi-model sessions.

Quick answer

Sum the cost field on every assistant message in the session. If the field is missing, apply current rates to input, cache read, cache write, and output separately. A session that switches models needs the sum per model, not one blended rate.

The formula

session_cost = sum(message.cost)

fallback:
  sum(input * in_rate)
+ sum(cache_read * read_rate)
+ sum(cache_write * write_rate)
+ sum(output * out_rate)
FieldCommon mistakeCorrect treatment
InputCounting cache read twiceInput usually excludes cache in OpenCode
Cache readIgnoring itApply the discounted rate
Cache writeTreating as freeApply the write rate
OutputUsing input rateApply the output rate

Compare sessions fairly

  • Divide by completed tasks, not by messages.
  • Note the model or models used.
  • Separate exploration sessions from implementation.
  • Exclude abandoned sessions or label them.
  • Compare the median, not the single most expensive run.

Multi-model sessions

Switching models mid-session is supported and common. Each message carries its own model, so group by model inside the session before applying rates. A blended average will misprice both halves.

UsageAtlas keeps the per-message model, so the session total can be broken down without manual work.

Cost per session, not per prompt

OpenCode sessions are the natural unit: they start when you start a task and end when you finish. Counting prompts flatters you, because one prompt can trigger dozens of model calls while a session with ten prompts can be one small fix.

cost_per_session = total_cost / sessions_completed

Track completed sessions, and keep abandoned ones in a separate bucket. An abandoned session that burned tokens is a process problem, not a pricing problem, and mixing it into the average hides that.

The cost tracking guide covers where the cost field comes from.

Questions, answered

Does OpenCode store cost per session or per message?

Per message in recent versions. A session total is the sum of its messages.

Why is my session cost zero?

The provider did not report pricing, or the model is free. Apply rates manually if you need a number.

Should cache writes be included?

Yes. Writing context to the cache is billed, usually at a higher rate than reading it.

Browse by topic