Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

fix(opencode): count reasoning tokens inside output_tokens

Claude79be28abe1ba
diff --git a/cli/beacon-hooks/cmd/opencode_event.go b/cli/beacon-hooks/cmd/opencode_event.go
index d550f732..7c23e411 100644
--- a/cli/beacon-hooks/cmd/opencode_event.go
+++ b/cli/beacon-hooks/cmd/opencode_event.go
@@ -465,13 +465,15 @@ func opencodeUsage(input map[string]interface{}) map[string]interface{} {
if value, ok := jsonInt(tokens["input"]); ok {
usage["input_tokens"] = value
}
- if value, ok := jsonInt(tokens["output"]); ok {
- usage["output_tokens"] = value
+ cache := firstMap(tokens, "cache")
+ output, hasOutput := jsonInt(tokens["output"])
+ reasoning, hasReasoning := jsonInt(tokens["reasoning"])
+ if hasOutput || hasReasoning {
+ usage["output_tokens"] = opencodeOutputTokens(tokens, cache, output, reasoning)
}
- if value, ok := jsonInt(tokens["reasoning"]); ok {
- usage["reasoning"] = map[string]interface{}{"output_tokens": value}
+ if hasReasoning {
+ usage["reasoning"] = map[string]interface{}{"output_tokens": reasoning}
}
- cache := firstMap(tokens, "cache")
if value, ok := jsonInt(cache["read"]); ok {
usage["cache_read"] = map[string]interface{}{"input_tokens": value}
}
@@ -484,6 +486,46 @@ func opencodeUsage(input map[string]interface{}) map[string]interface{} {
return usage
}
+// opencodeOutputTokens returns gen_ai output_tokens for an OpenCode tokens
+// object. gen_ai.usage counts reasoning inside output_tokens, with
+// reasoning.output_tokens as the subset, and totals leave the subset out so
+// it is counted once. OpenCode has stored output both ways. Since v1.3.16 its
+// getUsage (packages/opencode/src/session/session.ts) sets output to the
+// provider's outputTokens minus reasoningTokens, and the 2.x runtime stores
+// visibleOutputTokens, so reasoning has to be added back or it drops out of
+// every total. Earlier releases stored outputTokens whole, which already
+// holds reasoning, so adding it again would count it twice.
+//
+// tokens.total settles which shape a message has: OpenCode copies the
+// provider's total, which is the full input side plus every output token, so
+// it reaches input + cache + output + reasoning only when output left
+// reasoning out. A reasoning count above output can only mean the same.
+// 2.x step events carry no total at all. Releases before v1.1.57 (February
+// 2026) did not either, but a live event is far more likely to come from the
+// current runtime, so a missing total is taken as the current shape. The poll
+// backfill (cli/beacon/internal/opencodesession) makes the same call, except
+// that a stored message without a total is from those early releases, because
+// 2.x keeps its messages in a table the backfill does not read.
+func opencodeOutputTokens(tokens, cache map[string]interface{}, output, reasoning int) int {
+ if reasoning <= 0 {
+ return output
+ }
+ if reasoning > output {
+ return output + reasoning
+ }
+ total, ok := jsonInt(tokens["total"])
+ if !ok || total <= 0 {
+ return output + reasoning
+ }
+ input, _ := jsonInt(tokens["input"])
+ cacheRead, _ := jsonInt(cache["read"])
+ cacheWrite, _ := jsonInt(cache["write"])
+ if total >= input+cacheRead+cacheWrite+output+reasoning {
+ return output + reasoning
+ }
+ return output
+}
+
func opencodePartEvents(base map[string]interface{}, part map[string]interface{}) []normalizedEvent {
if len(part) == 0 {
return nil
diff --git a/cli/beacon-hooks/cmd/opencode_event_test.go b/cli/beacon-hooks/cmd/opencode_event_test.go
index 6085c8a9..a4445a4f 100644
--- a/cli/beacon-hooks/cmd/opencode_event_test.go
+++ b/cli/beacon-hooks/cmd/opencode_event_test.go
@@ -288,7 +288,8 @@ func TestOpenCodeCompletedAssistantNormalizesUsageOnce(t *testing.T) {
t.Fatalf("action = %q", action)
}
usage := fields["gen_ai"].(map[string]interface{})["usage"].(map[string]interface{})
- if usage["input_tokens"] != 10 || usage["output_tokens"] != 5 || usage["cost_usd"] != 0.42 {
+ // OpenCode stores output without its reasoning (5 + 2).
+ if usage["input_tokens"] != 10 || usage["output_tokens"] != 7 || usage["cost_usd"] != 0.42 {
t.Fatalf("usage = %#v", usage)
}
}
@@ -603,3 +604,81 @@ func readOpenCodeFixture(t *testing.T, name string) map[string]interface{} {
}
return payload
}
+
+// OpenCode stores tokens.output with reasoning already subtracted (getUsage in
+// packages/opencode/src/session/session.ts sets output to outputTokens minus
+// reasoningTokens). Beacon's gen_ai.usage counts reasoning inside
+// output_tokens, so the mapper adds it back and keeps reasoning as the subset.
+func TestOpenCodeUsageCountsReasoningInsideOutput(t *testing.T) {
+ for _, tt := range []struct {
+ name string
+ tokens map[string]interface{}
+ wantOutput interface{}
+ wantReasoning interface{}
+ }{
+ {
+ name: "reasoning model",
+ tokens: map[string]interface{}{"total": 2462, "input": 1200, "output": 412, "reasoning": 850, "cache": map[string]interface{}{"read": 0, "write": 0}},
+ wantOutput: 1262,
+ wantReasoning: 850,
+ },
+ {
+ // OpenCode 2.x step events (session.step.ended, relayed by the
+ // plugin as message.updated) store visibleOutputTokens and no total.
+ name: "2.x step without total",
+ tokens: map[string]interface{}{"input": 1200, "output": 412, "reasoning": 850, "cache": map[string]interface{}{"read": 300, "write": 0}},
+ wantOutput: 1262,
+ wantReasoning: 850,
+ },
+ {
+ // Before v1.3.16 output was the provider's outputTokens whole, and
+ // total matches input + cache + output with reasoning already in it.
+ name: "pre-1.3.16 output already holds reasoning",
+ tokens: map[string]interface{}{"total": 2462, "input": 900, "output": 1262, "reasoning": 850, "cache": map[string]interface{}{"read": 300, "write": 0}},
+ wantOutput: 1262,
+ wantReasoning: 850,
+ },
+ {
+ // A provider reporting more reasoning than output can only be the
+ // subtracted shape (OpenCode clamps output at zero).
+ name: "reasoning above output",
+ tokens: map[string]interface{}{"total": 500, "input": 400, "output": 0, "reasoning": 120, "cache": map[string]interface{}{"read": 0, "write": 0}},
+ wantOutput: 120,
+ wantReasoning: 120,
+ },
+ {
+ name: "no reasoning",
+ tokens: map[string]interface{}{"input": 1200, "output": 412, "reasoning": 0, "cache": map[string]interface{}{"read": 64, "write": 0}},
+ wantOutput: 412,
+ wantReasoning: 0,
+ },
+ {
+ name: "reasoning without output field",
+ tokens: map[string]interface{}{"input": 10, "reasoning": 6},
+ wantOutput: 6,
+ wantReasoning: 6,
+ },
+ {
+ name: "no reasoning field",
+ tokens: map[string]interface{}{"input": 10, "output": 4},
+ wantOutput: 4,
+ },
+ } {
+ t.Run(tt.name, func(t *testing.T) {
+ usage := opencodeUsage(map[string]interface{}{"tokens": tt.tokens})
+ if usage["output_tokens"] != tt.wantOutput {
+ t.Fatalf("output_tokens = %#v, want %#v (usage %#v)", usage["output_tokens"], tt.wantOutput, usage)
+ }
+ reasoning, hasReasoning := usage["reasoning"].(map[string]interface{})
+ if tt.wantReasoning == nil {
+ if hasReasoning {
+ t.Fatalf("reasoning = %#v, want none", reasoning)
+ }
+ return
+ }
+ if !hasReasoning || reasoning["output_tokens"] != tt.wantReasoning {
+ t.Fatalf("reasoning = %#v, want %#v", usage["reasoning"], tt.wantReasoning)
+ }
+ })
+ }
+}
diff --git a/cli/beacon/internal/opencodesession/helpers.go b/cli/beacon/internal/opencodesession/helpers.go
index a3fe7d02..26fa8fa6 100644
--- a/cli/beacon/internal/opencodesession/helpers.go
+++ b/cli/beacon/internal/opencodesession/helpers.go
@@ -147,6 +147,7 @@ func tokenUsageFromAny(value interface{}) TokenUsage {
Reasoning: intFromAny(m["reasoning"]),
CacheRead: intFromAny(cache["read"]),
CacheWrite: intFromAny(cache["write"]),
+ Total: intFromAny(m["total"]),
}
}
diff --git a/cli/beacon/internal/opencodesession/mapper.go b/cli/beacon/internal/opencodesession/mapper.go
index ec877a8c..8c8f68e7 100644
--- a/cli/beacon/internal/opencodesession/mapper.go
+++ b/cli/beacon/internal/opencodesession/mapper.go
@@ -184,8 +184,7 @@ func (m *mapper) emitUsage(record Record) {
value := record.Tokens.Input
usage.InputTokens = &value
}
- if record.Tokens.Output > 0 {
- value := record.Tokens.Output
+ if value := record.Tokens.outputTokens(); value > 0 {
usage.OutputTokens = &value
}
if record.Tokens.Reasoning > 0 {
@@ -212,6 +211,37 @@ func (m *mapper) emitUsage(record Record) {
m.append(record, "usage", ev)
}
+// outputTokens returns gen_ai output_tokens for a stored OpenCode message.
+// gen_ai.usage counts reasoning inside output_tokens, with
+// reasoning.output_tokens as the subset, and totals leave the subset out so
+// it is counted once. OpenCode has stored output both ways. Since v1.3.16 its
+// getUsage (packages/opencode/src/session/session.ts) sets output to the
+// provider's outputTokens minus reasoningTokens, so reasoning has to be added
+// back or it drops out of every total. Earlier releases, including every
+// message in the legacy file store, stored outputTokens whole, which already
+// holds reasoning, so adding it again would count it twice.
+//
+// Total settles which shape a message has: OpenCode copies the provider's
+// total, which is the full input side plus every output token, so it reaches
+// input + cache + output + reasoning only when output left reasoning out. A
+// reasoning count above output can only mean the same. A message with no
+// total predates v1.1.57 (the 2.x runtime keeps its messages in
+// session_message, which this store does not read), so it keeps its output.
+// The live hook mapper (cli/beacon-hooks/cmd/opencode_event.go) makes the
+// same call, except that it takes a missing total as a 2.x step event.
+func (u TokenUsage) outputTokens() int64 {
+ if u.Reasoning <= 0 {
+ return u.Output
+ }
+ if u.Reasoning > u.Output {
+ return u.Output + u.Reasoning
+ }
+ if u.Total > 0 && u.Total >= u.Input+u.CacheRead+u.CacheWrite+u.Output+u.Reasoning {
+ return u.Output + u.Reasoning
+ }
+ return u.Output
+}
+
func classifyTool(name string, record Record) (action, category string, severity schema.Severity, message string) {
lower := strings.ToLower(strings.TrimSpace(name))
switch {
diff --git a/cli/beacon/internal/opencodesession/types.go b/cli/beacon/internal/opencodesession/types.go
index 9e6733f2..9176a192 100644
--- a/cli/beacon/internal/opencodesession/types.go
+++ b/cli/beacon/internal/opencodesession/types.go
@@ -47,5 +47,9 @@ type TokenUsage struct {
Reasoning int64
CacheRead int64
CacheWrite int64
- CostUSD float64
+ // Total is OpenCode's tokens.total, the provider's own count of every
+ // input and output token. It is kept only to tell which way Output was
+ // stored (see outputTokens); zero when the message carried none.
+ Total int64
+ CostUSD float64
}
diff --git a/cli/beacon/internal/opencodesession/usage_test.go b/cli/beacon/internal/opencodesession/usage_test.go
new file mode 100644
index 00000000..b8c743b3
--- /dev/null
+++ b/cli/beacon/internal/opencodesession/usage_test.go
@@ -0,0 +1,184 @@
+package opencodesession
+
+import (
+ "database/sql"
+ "encoding/json"
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/tokens"
+)
+
+// The assistant messages below are shaped like OpenCode's stored
+// message.data / storage/message JSON. Each pair of numbers is what the
+// provider reported (outputTokens including reasoning, and reasoningTokens);
+// the comments give what OpenCode wrote for it.
+var opencodeUsageMessages = []struct {
+ id string
+ tokens map[string]interface{}
+ cost float64
+ wantOutput int64
+ wantReasoning int64
+}{
+ {
+ // v1.3.16 and later: output = outputTokens (1262) - reasoningTokens (850),
+ // and total = inputTokens (1500, cache included) + outputTokens.
+ id: "msg_a_current",
+ tokens: map[string]interface{}{"total": 2762, "input": 1100, "output": 412, "reasoning": 850, "cache": map[string]interface{}{"read": 400, "write": 0}},
+ cost: 0.031,
+ wantOutput: 1262,
+ wantReasoning: 850,
+ },
+ {
+ // Before v1.3.16: output = outputTokens whole, reasoning already in it.
+ id: "msg_b_pre_1_3_16",
+ tokens: map[string]interface{}{"total": 2762, "input": 1100, "output": 1262, "reasoning": 850, "cache": map[string]interface{}{"read": 400, "write": 0}},
+ cost: 0.052,
+ wantOutput: 1262,
+ wantReasoning: 850,
+ },
+ {
+ // A model without reasoning is the same in every release.
+ id: "msg_c_no_reasoning",
+ tokens: map[string]interface{}{"total": 900, "input": 700, "output": 200, "reasoning": 0, "cache": map[string]interface{}{"read": 0, "write": 0}},
+ cost: 0.004,
+ wantOutput: 200,
+ },
+ {
+ // Before v1.1.57 there was no total, and output was outputTokens whole.
+ id: "msg_d_no_total",
+ tokens: map[string]interface{}{"input": 300, "output": 90, "reasoning": 40, "cache": map[string]interface{}{"read": 0, "write": 10}},
+ wantOutput: 90,
+ wantReasoning: 40,
+ },
+}
+
+// The sum of every message above, counting each reasoning token once.
+const (
+ wantInputTotal = 1100 + 1100 + 700 + 300
+ wantOutputTotal = 1262 + 1262 + 200 + 90
+ wantCacheTotal = 400 + 400 + 0 + 10
+ wantReasoningTotal = 850 + 850 + 40
+ wantTotalTokenCount = wantInputTotal + wantOutputTotal + wantCacheTotal
+)
+
+func TestSQLiteUsageCountsReasoningInsideOutputOnce(t *testing.T) {
+ dir := t.TempDir()
+ db, err := sql.Open("sqlite", filepath.Join(dir, "opencode.db"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer db.Close()
+ mustExec(t, db, `CREATE TABLE session (id TEXT PRIMARY KEY, title TEXT, directory TEXT, time_created INTEGER, time_updated INTEGER)`)
+ mustExec(t, db, `CREATE TABLE message (id TEXT PRIMARY KEY, session_id TEXT, time_created INTEGER, data TEXT)`)
+ mustExec(t, db, `CREATE TABLE part (id TEXT PRIMARY KEY, message_id TEXT, session_id TEXT, time_created INTEGER, data TEXT)`)
+ mustExec(t, db, `INSERT INTO session (id, title, directory, time_created, time_updated) VALUES ('ses_1', 'Reasoning', '/repo', 1770000000000, 1770000009000)`)
+ for i, msg := range opencodeUsageMessages {
+ created := int64(1770000000100 + i*1000)
+ insertJSON(t, db, `INSERT INTO message (id, session_id, time_created, data) VALUES (?, ?, ?, ?)`, msg.id, "ses_1", created, assistantMessage(msg.id, "ses_1", created, msg.tokens, msg.cost))
+ }
+ store, err := NewStore(dir)
+ if err != nil {
+ t.Fatal(err)
+ }
+ assertUsageCountsReasoningOnce(t, store)
+}
+
+func TestLegacyStoreUsageCountsReasoningInsideOutputOnce(t *testing.T) {
+ dir := t.TempDir()
+ storage := filepath.Join(dir, "storage")
+ writeJSONFile(t, filepath.Join(storage, "session", "proj_1", "ses_1.json"), map[string]interface{}{
+ "id": "ses_1", "title": "Reasoning", "directory": "/repo", "version": "0.15.0",
+ "time": map[string]interface{}{"created": 1770000000000, "updated": 1770000009000},
+ })
+ for i, msg := range opencodeUsageMessages {
+ created := int64(1770000000100 + i*1000)
+ writeJSONFile(t, filepath.Join(storage, "message", "ses_1", msg.id+".json"), assistantMessage(msg.id, "ses_1", created, msg.tokens, msg.cost))
+ }
+ store, err := NewStore(dir)
+ if err != nil {
+ t.Fatal(err)
+ }
+ assertUsageCountsReasoningOnce(t, store)
+}
+
+func assistantMessage(id, sessionID string, created int64, tokens map[string]interface{}, cost float64) map[string]interface{} {
+ return map[string]interface{}{
+ "id": id, "sessionID": sessionID, "role": "assistant",
+ "providerID": "openai", "modelID": "gpt-5", "finish": "stop",
+ "time": map[string]interface{}{"created": created, "completed": created + 500},
+ "tokens": tokens,
+ "cost": cost,
+ }
+}
+
+func writeJSONFile(t *testing.T, path string, value interface{}) {
+ t.Helper()
+ data, err := json.Marshal(value)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(path, data, 0o600); err != nil {
+ t.Fatal(err)
+ }
+}
+
+func assertUsageCountsReasoningOnce(t *testing.T, store *Store) {
+ t.Helper()
+ refs, err := store.List()
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(refs) != 1 {
+ t.Fatalf("refs = %d, want 1", len(refs))
+ }
+ records, err := store.Read(refs[0])
+ if err != nil {
+ t.Fatal(err)
+ }
+ mapped := MapTrace(refs[0], records, MapOptions{})
+ var events []schema.Event
+ for _, item := range mapped {
+ if item.Event.Event.Action == "token.usage" {
+ events = append(events, item.Event)
+ }
+ }
+ if len(events) != len(opencodeUsageMessages) {
+ t.Fatalf("usage events = %d, want %d", len(events), len(opencodeUsageMessages))
+ }
+ for i, want := range opencodeUsageMessages {
+ usage := events[i].GenAI.Usage
+ if usage.OutputTokens == nil || *usage.OutputTokens != want.wantOutput {
+ t.Fatalf("%s output_tokens = %v, want %d", want.id, deref(usage.OutputTokens), want.wantOutput)
+ }
+ var reasoning int64
+ if usage.Reasoning != nil && usage.Reasoning.OutputTokens != nil {
+ reasoning = *usage.Reasoning.OutputTokens
+ }
+ if reasoning != want.wantReasoning {
+ t.Fatalf("%s reasoning.output_tokens = %d, want %d", want.id, reasoning, want.wantReasoning)
+ }
+ }
+ report := tokens.Aggregate(events, tokens.Options{})
+ totals := report.Totals
+ if totals.InputTokens != wantInputTotal || totals.OutputTokens != wantOutputTotal ||
+ totals.CacheReadInputTokens+totals.CacheCreationInputTokens != wantCacheTotal ||
+ totals.ReasoningOutputTokens != wantReasoningTotal {
+ t.Fatalf("totals = %+v", totals)
+ }
+ if got := totals.TotalTokens(); got != wantTotalTokenCount {
+ t.Fatalf("TotalTokens = %d, want %d (reasoning counted once, inside output)", got, wantTotalTokenCount)
+ }
+}
+
+func deref(value *int64) interface{} {
+ if value == nil {
+ return nil
+ }
+ return *value
+}
diff --git a/docs/runtimes/opencode.mdx b/docs/runtimes/opencode.mdx
index ce9cff4c..11707372 100644
--- a/docs/runtimes/opencode.mdx
+++ b/docs/runtimes/opencode.mdx
@@ -60,7 +60,7 @@ beacon endpoint opencode sync
| File telemetry | Read/search/glob and write/edit/patch activity with agent-derived paths; empty session diffs are ignored |
| Web and MCP telemetry | Web tool arguments/results and normalized MCP server/tool/resource context |
| Approval telemetry | Requests plus `once`, `always`, and `reject` replies from the live plugin, normalized as requested, allowed, and denied. Approval telemetry is not recovered from the session-store poll path |
-| Model and usage | Provider/model, finish reason, token usage, cache usage, reasoning tokens, and runtime-reported cost where OpenCode reports them |
+| Model and usage | Provider/model, finish reason, token usage, cache usage, reasoning tokens, and runtime-reported cost where OpenCode reports them. Since v1.3.16 OpenCode stores output tokens with reasoning taken out; Beacon adds reasoning back into `output_tokens` so reasoning stays a subset of output, as for every other runtime, and counts once in totals. Messages from earlier releases already include it, and their `tokens.total` tells the two apart |
| Session telemetry | Live create, busy/idle/end, retry, and error boundaries through the plugin; persisted sessions can be backfilled through `beacon endpoint opencode sync` |
| Local JSONL and dashboard | Supported |
| MDM deployment | Supported for the endpoint agent. OpenCode plugins are installed separately in the logged-in user's context |