Skip to content

[FEATURE]:Token Usage & Cache Hit Dashboard per Session #42295

Closed as duplicate

Description

@GuixJoy

Describe the enhancement you want to request

Add a live token usage and cache analytics panel visible during an active OpenCode session. This would display:

  • Total tokens consumed in the current session (input + output separately)
  • Cache hit count and cache hit ratio as a percentage
  • Estimated cost for the current session based on the model being used
  • A per-message breakdown showing tokens used per exchange

Right now there is no visibility into how much context is being consumed during a session. This creates two problems. First, developers have no idea when they are approaching context limits until the model starts degrading in quality. Second, there is no way to evaluate whether caching is actually working effectively across a session.

A token usage panel solves both. It helps developers make informed decisions about when to start a new session, which parts of their workflow are token-heavy, and whether their cache configuration is performing as expected.

This is especially useful for long agentic sessions where token consumption compounds quickly and cost visibility matters.

Activity

  1. github-actions commented on Aug 13, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

  2. VladSmr commented on Oct 2, 2026

    @VladSmr

    A per-session version of this exists as a plugin: /context-breakdown renders a
    per-category dashboard (system prompt, tool schemas, user/assistant, reasoning,
    tool args, residual) for the session and all its subagent sessions. The new /cx
    panel in the terminal TUI adds a largest-contributors view — which message,
    tool call or tool result eats the most, measured from the live message list.

    Cache read/write participates in the native overflow metric, though the category
    breakdown doesn't split it out separately yet.

    opencode-context-indicator — still in a testing stage; with your help and
    feedback we can make it better together. Dual V1 + V2, tested against 2.0.22,
    everything local (no uploads, no extra LLM calls).

    https://github.com/VladSmr/opencode-context-indicator
    npm: opencode-context-indicator

  3. Songwanci commented on Oct 28, 2026

    @Songwanci

    Per-message token breakdown in the desktop app

    The desktop app shows session-level totals today (e.g. 101k · $4.55 next to the context ring, added by #53785), and the Review → Context panel aggregates the session. But there is still no way to see per-message token usage anywhere in the app.

    This is exactly the "per-message breakdown showing tokens used per exchange" that this issue asks for. I'd like to request it for the desktop app specifically:

    For each assistant message, show its own:

    • input (uncached)
    • output
    • reasoning
    • cache read / write
    • cost

    My use case: I'm on a free/metered token quota and need to know which individual messages are consuming the most tokens. Session totals don't tell me that — I need the per-message figure to find the outliers.

    Context / related:

    The data already exists per message (session_message.data.tokens in the V2 database, and message.data.tokens in V1), so this is purely a presentation gap.

    A per-message row in the session view, or a click-to-expand on each message, would be ideal. Thanks!

  4. opencodejr commented on Oct 28, 2026

    @opencodejr
    Contributor

    Thanks for the request, @GuixJoy! To restate it: you'd like a live usage panel for the current session that shows input and output tokens separately, how often the cache was hit and the hit ratio, the estimated cost, and the tokens used by each exchange. Maintainers will consider it.

    Some of this already exists in v2:

    • The TUI sidebar's Context block shows the current context size, the % of the model's context window used, and the session cost.
    • Setting "debug": { "turn_tokens": true } in ~/.config/opencode/cli.json adds a per-turn table of new and cached tokens under each response, and flags likely cache busts.
    • The desktop app's Context panel shows input, output, reasoning and cache read/write tokens and the cost.

    Until then, you can get the rest in the TUI with a small plugin. It adds a Usage block to the session sidebar with input and output totals, cache read/write, cache hits per turn, the share of prompt tokens served from cache, the cost, and the last few turns:

    // ~/.config/opencode/plugins/session-usage/tui.tsx
    import { Plugin } from "@opencode/plugin/tui"
    import { createMemo, For, Show } from "solid-js"
    
    const money = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" })
    const n = (value: number) => value.toLocaleString()
    
    export default Plugin.define({
      id: "local.session-usage",
      setup(context) {
        const theme = context.theme
        const perTurn = context.options.turns === undefined ? 5 : Number(context.options.turns)
        return context.ui.slot({
          append: "sidebar.content",
          render: (props) => {
            const turns = createMemo(() =>
              context.data.session.message
                .list(props.sessionID)
                .flatMap((message) => (message.type === "assistant" && message.tokens ? [message.tokens] : [])),
            )
            const totals = createMemo(() =>
              turns().reduce(
                (sum, t) => ({
                  input: sum.input + t.input,
                  output: sum.output + t.output + t.reasoning,
                  read: sum.read + t.cache.read,
                  write: sum.write + t.cache.write,
                  hits: sum.hits + (t.cache.read > 0 ? 1 : 0),
                }),
                { input: 0, output: 0, read: 0, write: 0, hits: 0 },
              ),
            )
            const ratio = () => {
              const t = totals()
              const prompt = t.input + t.read + t.write
              return prompt > 0 ? Math.round((t.read / prompt) * 100) : 0
            }
            return (
              <Show when={turns().length > 0}>
                <box>
                  <text fg={theme.text.base}>
                    <b>Usage</b>
                  </text>
                  <text fg={theme.text.muted}>In {n(totals().input)} · Out {n(totals().output)}</text>
                  <text fg={theme.text.muted}>
                    Cache {n(totals().read)} read · {n(totals().write)} write
                  </text>
                  <text fg={theme.text.muted}>
                    Cache hits {totals().hits}/{turns().length} turns · {ratio()}% of prompt
                  </text>
                  <text fg={theme.text.muted}>{money.format(context.data.session.cost(props.sessionID))} spent</text>
                  <Show when={perTurn > 0}>
                    <For each={turns().slice(-perTurn)}>
                      {(t) => (
                        <text fg={theme.text.muted}>
                          · {n(t.input + t.cache.read + t.cache.write)} in ({n(t.cache.read)} cached) / {n(t.output + t.reasoning)} out
                        </text>
                      )}
                    </For>
                  </Show>
                </box>
              </Show>
            )
          },
        })
      },
    })

    To install it, save the file as tui.tsx in a folder such as ~/.config/opencode/plugins/session-usage/. Then add that folder's absolute path to plugins in ~/.config/opencode/cli.json and restart the TUI. The turns option sets how many recent turns are listed, and 0 hides the list:

    {
      "plugins": [
        { "package": "/home/you/.config/opencode/plugins/session-usage", "options": { "turns": 5 } }
      ]
    }

    The input and output totals add up every assistant step in the session. "% of prompt" is cache reads divided by all prompt tokens (fresh input plus cache read and write).

    Related issues:

  5. neriousy commented on Nov 13, 2026

    @neriousy
    Member

    Thanks! This asks for the same per-session, per-message token and cost breakdown as #41915, so I'm closing it as a duplicate. Some of it already exists: the desktop header shows session tokens and cost (#53785), and the TUI's debug.turn_tokens setting shows new vs cached tokens per step. A cache hit rate in the desktop Context panel is tracked in #58024.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions