Skip to content

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

Read Cursor's context occupancy from preCompact

Claude546ebc38c6d9
diff --git a/cli/beacon-hooks/cmd/pre_tool_test.go b/cli/beacon-hooks/cmd/pre_tool_test.go
index b5752cd1..cf43728e 100644
--- a/cli/beacon-hooks/cmd/pre_tool_test.go
+++ b/cli/beacon-hooks/cmd/pre_tool_test.go
@@ -3,6 +3,7 @@ package cmd
import (
"bytes"
"encoding/json"
+ "fmt"
"io"
"os"
"path/filepath"
@@ -269,6 +270,53 @@ func TestRunCursorEventEmitsPreCompact(t *testing.T) {
}
}
+// Cursor is the second runtime to report context occupancy, and the first to prove the alias-list
+// design pays off: its preCompact payload names the same measurement Qwen does under different
+// keys, and reading it took two list entries and no change to this mapper.
+//
+// The payload is Cursor's documented preCompact shape. context_tokens over context_window_size is
+// the context_usage_percent beside them, which stays in raw rather than being stored twice in
+// different units -- the same call made for Qwen's context_usage.
+func TestRunCursorEventNormalizesPreCompactContextSize(t *testing.T) {
+ setupHookConfigDirs(t)
+ platformFlag = "cursor"
+ logPath := filepath.Join(t.TempDir(), "runtime.jsonl")
+ t.Setenv("BEACON_ENDPOINT_LOG", logPath)
+
+ runHookWithInput(t, runCursorEvent, map[string]interface{}{
+ "conversation_id": "conv-compact",
+ "hook_event_name": "preCompact",
+ "cwd": "/repo",
+ "model": "claude-opus-5",
+ "trigger": "auto",
+ "context_usage_percent": 92.5,
+ "context_tokens": 185000,
+ "context_window_size": 200000,
+ "message_count": 64,
+ })
+
+ event := lastEndpointEvent(t, logPath)
+ genAI, ok := event["gen_ai"].(map[string]interface{})
+ if !ok {
+ t.Fatalf("event has no gen_ai block: %#v", event)
+ }
+ context, ok := genAI["context"].(map[string]interface{})
+ if !ok {
+ t.Fatalf("gen_ai has no context block: %#v", genAI)
+ }
+ if got := fmt.Sprint(context["used_tokens"]); got != "185000" {
+ t.Errorf("gen_ai.context.used_tokens = %s, want 185000", got)
+ }
+ if got := fmt.Sprint(context["limit_tokens"]); got != "200000" {
+ t.Errorf("gen_ai.context.limit_tokens = %s, want 200000", got)
+ }
+ // Occupancy is not spend. Cursor reports no per-call usage at all, and a compaction notice is
+ // the least likely place for one to appear.
+ if usage, ok := genAI["usage"]; ok {
+ t.Errorf("gen_ai.usage = %#v, want absent -- Cursor reports no token usage", usage)
+ }
+}
+
func TestRunPromptSubmitEmitsFactoryPromptEvent(t *testing.T) {
setupHookConfigDirs(t)
platformFlag = "factory"
diff --git a/cli/beacon/internal/tokens/coverage.go b/cli/beacon/internal/tokens/coverage.go
index 8b73f705..4a20830d 100644
--- a/cli/beacon/internal/tokens/coverage.go
+++ b/cli/beacon/internal/tokens/coverage.go
@@ -88,7 +88,7 @@ var usageExpectation = map[string]struct {
// carries the semconv names -- the generic-OTLP case exactly, not a runtime Beacon reads.
"grok_bot": {ExpectGenericOTLP, "only if Cursor's server-side OTel export carries GenAI semconv usage"},
- "cursor": {ExpectNone, "Cursor hook payloads carry no token counts"},
+ "cursor": {ExpectNone, "no usage on any hook payload; preCompact reports context only"},
"antigravity_cli": {ExpectNone, "hook payloads carry no token counts"},
"grok": {ExpectNone, "hook payloads carry no token counts"},
"hermes": {ExpectNone, "hook payloads carry no token counts"},
@@ -101,7 +101,7 @@ var usageExpectation = map[string]struct {
"devin-cli": {ExpectNone, "hook payloads carry no token counts"},
"devin-desktop": {ExpectNone, "Cascade/Windsurf hook payloads carry no token counts"},
"muse_code": {ExpectNone, "usage arrives on PostLLMCall, which Beacon does not subscribe to"},
- "qwen_code": {ExpectNone, "Stop carries session context counters, not per-call usage"},
+ "qwen_code": {ExpectNone, "Stop reports context occupancy, not per-call usage"},
"prime_agent": {ExpectNone, "no managed extension ships yet"},
"claude_web": {ExpectNone, "no recorded claude.ai stream has carried a usage object"},
"chatgpt_web": {ExpectNone, "the chat stream reports no token counts"},
diff --git a/docs/cli/token-usage.mdx b/docs/cli/token-usage.mdx
index 5559102f..c21db62e 100644
--- a/docs/cli/token-usage.mdx
+++ b/docs/cli/token-usage.mdx
@@ -60,14 +60,30 @@ Cline reports usage once per task rather than per model call, because Beacon's r
This table records what Beacon is built to read, not what your machine actually produced. A runtime can be listed here and still contribute nothing, because it was never run, or because its build reports no usage.
+## Context occupancy
+
+Some runtimes report how full the model's context window was without reporting what a turn cost. That is a different measurement from spend, and it is recorded in a different place: `gen_ai.context`, holding `used_tokens` and `limit_tokens`.
+
+The distinction is not cosmetic. Everything in `gen_ai.usage` is **additive** -- every total on this page sums it. Occupancy is a level at one moment: a session whose window was 92% full ten times did not spend ten windows. So context is never summed, never added to a total, and never counted as a runtime reporting its spend. It feeds the utilization view, where a reported window also replaces the model table's default, because a reported limit reflects the tier a call actually ran under.
+
+| Runtime | Reported on | Fields read |
+|---------|-------------|-------------|
+| Cursor | `preCompact` | `context_tokens`, `context_window_size` |
+| Qwen Code | `Stop` | `input_tokens`, `context_limit` |
+
+Both runtimes also report the ratio of the two (`context_usage_percent`, `context_usage`). It is derivable from the pair, so it stays in `raw` rather than being stored a second time in different units.
+
+A runtime that reports occupancy and no spend still shows as `not_instrumented` in the coverage report below. That is deliberate: it has not told Beacon what it cost.
+
+
**Not collected:**
| Runtime | Why |
|---------|-----|
-| Cursor | Cursor's hook payloads carry no token counts |
+| Cursor | No hook payload carries a token count. Cursor does report context occupancy on `preCompact`, which is recorded separately -- see [Context occupancy](#context-occupancy) below |
| Antigravity CLI, Grok Build, Hermes Agent, Devin CLI, Devin Desktop | No token counts on the hook payloads Beacon receives |
| Muse Code | Usage arrives on `PostLLMCall` and in Muse's own session log, neither of which Beacon reads. See [Muse Code](/runtimes/muse-code) |
-| Qwen Code | The `Stop` payload carries session-level context counters, not per-call usage. Preserved under `raw.qwen` rather than normalized. See [Qwen Code](/runtimes/qwen-code) |
+| Qwen Code | The `Stop` payload carries session-level context counters, not per-call usage. Those counters are recorded as context occupancy rather than spend -- see [Context occupancy](#context-occupancy) below. See [Qwen Code](/runtimes/qwen-code) |
| OpenHands | None of the six hook payloads carries token counts, and OpenHands exposes no other hook that would. See [OpenHands](/runtimes/openhands) |
| Prime Agent | No managed extension ships yet, so nothing is collected |
| Claude.ai | The adapter parses counts, but no recorded claude.ai stream has carried a usage object. See [Claude.ai](/runtimes/claude-web#limits) |
diff --git a/docs/runtimes/cursor.mdx b/docs/runtimes/cursor.mdx
index 426a0c29..0221e239 100644
--- a/docs/runtimes/cursor.mdx
+++ b/docs/runtimes/cursor.mdx
@@ -51,6 +51,8 @@ beacon endpoint hooks install --harness cursor --level project
| Command, tool, and file telemetry | Supported for tool, shell command, MCP-like, approval, and file edit payloads where Cursor exposes them; Cursor Cloud support is limited to Cursor's [cloud-supported command hooks](https://cursor.com/docs/hooks#cloud-agent-support). |
| Agent reasoning telemetry | Supported for local Cursor through `afterAgentThought` hooks. Each completed thinking block is recorded as an `agent.reasoning` event carrying the reasoning text in the OpenTelemetry GenAI output-messages shape (`gen_ai.output.messages` with a `reasoning` part), plus a `content` marker with the original text's hash and byte count. Reasoning is only available for models/modes where Cursor exposes visible thinking text. |
| Repository and branch context | Events include the workspace path as `repository`, and `branch` is resolved from the workspace's local git checkout (`.git/HEAD`) when Cursor does not supply one; disable local git resolution with `BEACON_DISABLE_GIT_METADATA=1`. |
+| Token usage and cost | Not collected. No Cursor hook payload carries a token count or a cost. |
+| Context occupancy | Supported through `preCompact`, which reports `context_tokens` and `context_window_size`. Recorded as `gen_ai.context` (`used_tokens`, `limit_tokens`) and used by the token-usage utilization view. This is a level, not spend: it is never summed into a total, and Cursor still reports as `not_instrumented` in `beacon token-usage --coverage` because it has not said what a turn cost. The sibling `context_usage_percent` is derivable from the pair and stays in the retained payload. |
| Local JSONL and dashboard | Supported |
| MDM deployment | Supported for the endpoint agent. Cursor hooks are installed separately in the logged-in user's context |
diff --git a/pkg/asymptoteobserve/identity.go b/pkg/asymptoteobserve/identity.go
index ffd774f9..7732845c 100644
--- a/pkg/asymptoteobserve/identity.go
+++ b/pkg/asymptoteobserve/identity.go
@@ -151,6 +151,10 @@ var ContextUsedKeys = []string{
// Qwen Code's Stop payload. Its sibling context_usage is this value divided by the limit, so
// it is derivable and stays in raw rather than being stored twice in different units.
"input_tokens",
+ // Cursor's preCompact payload. Unambiguous on its own -- unlike input_tokens, nothing in the
+ // ecosystem uses this name for an additive count -- but it still only promotes alongside a
+ // limit, because the pairing rule is a property of the list rather than of any one key.
+ "context_tokens",
}
// ContextLimitKeys names the context window a runtime reported for a call. A reported window beats
@@ -162,4 +166,7 @@ var ContextLimitKeys = []string{
"beacon.gen_ai.context.limit_tokens",
// Qwen Code's Stop payload.
"context_limit",
+ // Cursor's preCompact payload. Its context_usage_percent is this over the used count, so it
+ // stays in raw for the reason Qwen's context_usage does.
+ "context_window_size",
}