anomalyco / anomalyco/opencode
Incorrect session cost estimate for models with context tiered pricing
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 209k
- Forks
- 27.5k
- PR merge metrics
- PR metrics pending
Description
Description
Opencode's pricing estimate is incorrect when a model with with tiered context pricing and your session goes into the higher tier. The local cost estimates use the price of smaller context for the entire session, resulting in a cost estimate that is lower than what it should be.
For example, the session that I discovered this showed $3.37 in opencode's UI, but the real price was $3.71. The longer the session, the wider the discrepancy.
Testing with grok 4.6, which has different pricing for > 200k, the model calls reported in Zen differed from what is stored in the local sqlite database.
Plugins
No response
OpenCode version
1.18.18
Steps to reproduce
For testing with Zen:
Use a model that has tiered context pricing, such as grok 4.6 or one of the gpt 5.6 models. Have a session go into the higher tiered pricing. Compare local session pricing vs Zen's UI.
WITH ranked_calls AS (
SELECT
ROW_NUMBER() OVER (ORDER BY time_created, id) AS call_number,
time_created,
data
FROM message
WHERE session_id = 'ses_ff50d39fbffe2mOJcly5gtxQyW'
AND json_extract(data, '$.role') = 'assistant'
AND (
COALESCE(json_extract(data, '$.tokens.input'), 0) +
COALESCE(json_extract(data, '$.tokens.cache.read'), 0) +
COALESCE(json_extract(data, '$.tokens.cache.write'), 0) +
COALESCE(json_extract(data, '$.tokens.output'), 0) +
COALESCE(json_extract(data, '$.tokens.reasoning'), 0)
) > 0
)
SELECT
call_number,
strftime('%b %d, %H:%M', time_created / 1000, 'unixepoch', 'localtime') AS date,
json_extract(data, '$.modelID') AS model,
COALESCE(json_extract(data, '$.tokens.input'), 0) +
COALESCE(json_extract(data, '$.tokens.cache.read'), 0) +
COALESCE(json_extract(data, '$.tokens.cache.write'), 0) AS input,
printf('$%.4f', COALESCE(json_extract(data, '$.cost'), 0)) AS cost,
substr('ses_ff50d39fbffe2mOJcly5gtxQyW', -8) AS session
FROM ranked_calls
WHERE call_number >= (
SELECT MAX(call_number) - 9 FROM ranked_calls
)
ORDER BY call_number DESC;
Screenshot and/or share link
Zen pricing:
Local sqlite pricing:
Operating System
No response
Terminal
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No source file or test is named. Start by reproducing the issue with grok 4.6 or a GPT 5.6 model, then use the supplied SQLite query to compare stored call costs with Zen's pricing. Done means sessions crossing a context-pricing tier produce local estimates that match the applicable tiered costs.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- sqlite, typescript
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100