ok so recently i worked with gemini to add in a side bar panel to show context size and…
- User
ok so recently i worked with gemini to add in a side bar panel to show context size and spend (when using openrouter) but the pricing isn't right.
i want you to check the openrouter documentation for their completion api's (the api's we're using, right?) and confirm if it returns the cost per request.
basically, i'm trying to replicate what opencode does on their side bar
Assistant
Let me grab the relevant API docs — the chat completion reference, streaming docs, and usage accounting.
Assistant
Here's what I found:
OpenRouter does return actual cost per request
Yes, you're using the right API (
/v1/chat/completionswith streaming). OpenRouter always includes ausageobject in the final SSE chunk, and it contains acostfield — the actual amount charged to your account for that request.The response format from the docs:
json- User
yeah do it
Assistant
Now I have the full picture. Let me implement the fixes.
Assistant
First, fix the
Usagestruct to deserializecostdirectly from the usage object (matching the OpenRouter response format), and add asession_costfield toChatAppfor tracking cumulative spend.Assistant
Now remove the root-level
costfromChatChunkand simplify the extraction logic —costis now deserialized directly insideUsage:Assistant
Now simplify the cost extraction in
stream_chat()— no more manual merging needed:Assistant
Now update
app.rs— add asession_costfield, accumulate it on each response, and fix all the references fromtotal_costtocost:Assistant
Now accumulate cost when we receive UsageInfo:
Assistant
Now replace the hardcoded cost estimate section in the sidebar with actual session cost + last request cost:
Assistant
Clean. Let me also run the build to make sure everything compiles:
Assistant
All done. Clippy clean, builds clean. Here's the summary of changes:
src/api.rsUsagestruct: Renamedtotal_costtocostto match the actual OpenRouter response JSON where cost lives inside theusageobject (not at the root)ChatChunkstruct: Removed the root-levelcost: Option<f64>field — it was never being populated by the APIstream_chat(): Removed the manualchunk.cost -> usage.total_costmerging logic since serde now deserializescostdirectly intoUsage
src/app.rs- Added
session_cost: f64field toChatApp— accumulates cost across all requests in the current app session process_events(): On eachUsageInfo, addsusage.costtosession_cost- Context sidebar: Replaced the hardcoded
$0.01/1k tokensestimate with actual "Session Spend" (cumulative) and per-request cost from the API. Bumped display precision to 6 decimal places since API costs can be quite small.
- User
ok session spend is "per chat" yeah? should we call it "thread spend" or something else? session spend is different for something like opencode bceause i can switch models in the same session.
Assistant
Good point. In hChat you can also switch models mid-conversation, so "session" is ambiguous — it could mean the app's lifetime or the current conversation thread.
"Thread Spend" isn't bad but "thread" is a bit overloaded (concurrency). How about just "Chat Spend"? It maps directly to the concept already in the UI ("New Chat", conversation list, etc.).
But it's your app — quick pick:
Assistant
One thing — right now
session_costnever resets when you switch conversations or start a new chat. It should reset onnew_conversation()andload_conversation()so it actually tracks per-chat spend:Assistant
Done. Label is "Chat Spend" and it resets to
$0.00when you start a new chat or switch conversations.- User
nice. ok go ahead and commit and push. need a new release. 0.4.1
Assistant
Need to bump the version in Cargo.toml too.