The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
Add token usage view and /api/tokens endpoint to the dashboard
Claude5aa4ec3f75ef
diff --git a/cli/beacon/internal/endpoint/dashboard/events.go b/cli/beacon/internal/endpoint/dashboard/events.goindex 5fed6fc2..51c8a3a4 100644--- a/cli/beacon/internal/endpoint/dashboard/events.go+++ b/cli/beacon/internal/endpoint/dashboard/events.go@@ -42,6 +42,7 @@ type EventQuery struct {Category stringRepository stringSession string+ Trace stringFile stringCommand stringMCP string@@ -317,6 +318,11 @@ func matchesQuery(record EventRecord, query EventQuery) bool {return false}}+ if query.Trace != "" {+ if event.Trace == nil || (!containsFold(event.Trace.ID, query.Trace) && !containsFold(event.Trace.SpanID, query.Trace) && !containsFold(event.Trace.ParentSpanID, query.Trace)) {+ return false+ }+ }if query.File != "" {if event.File == nil || !containsFold(event.File.Path, query.File) {return false@@ -440,6 +446,9 @@ func searchFields(record EventRecord) []string {if event.Session != nil {fields = append(fields, event.Session.ID, event.Session.WorkingDirectory)}+ if event.Trace != nil {+ fields = append(fields, event.Trace.ID, event.Trace.SpanID, event.Trace.ParentSpanID)+ }if event.Tool != nil {fields = append(fields, event.Tool.Name, event.Tool.Command, event.Tool.Path)}@@ -503,6 +512,7 @@ func activeFilters(query EventQuery) map[string]string {add("category", query.Category)add("repository", query.Repository)add("session", query.Session)+ add("trace", query.Trace)add("file", query.File)add("command", query.Command)add("mcp", query.MCP)diff --git a/cli/beacon/internal/endpoint/dashboard/server.go b/cli/beacon/internal/endpoint/dashboard/server.goindex 2402acbd..56fab34a 100644--- a/cli/beacon/internal/endpoint/dashboard/server.go+++ b/cli/beacon/internal/endpoint/dashboard/server.go@@ -14,6 +14,8 @@ import ("time""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/lifecycle"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/tokens")const DefaultAddr = "127.0.0.1:8765"@@ -88,6 +90,25 @@ func Handler(opts Options) (http.Handler, error) {}writeJSON(w, events)})+ mux.HandleFunc("/api/tokens", func(w http.ResponseWriter, r *http.Request) {+ if r.Method != http.MethodGet {+ methodNotAllowed(w)+ return+ }+ // Token rollups need the full matching log, not the latest page.+ query := parseQuery(r, maxEventLimit)+ query.NoLimit = true+ result, err := ReadEvents(opts.LogPath, query)+ if err != nil {+ writeError(w, http.StatusInternalServerError, err)+ return+ }+ events := make([]schema.Event, 0, len(result.Events))+ for _, record := range result.Events {+ events = append(events, record.Event)+ }+ writeJSON(w, tokens.Aggregate(events, tokenOptions(r)))+ })mux.HandleFunc("/api/event", func(w http.ResponseWriter, r *http.Request) {if r.Method != http.MethodGet {methodNotAllowed(w)@@ -176,6 +197,7 @@ func parseQuery(r *http.Request, fallbackLimit int) EventQuery {Category: q.Get("category"),Repository: q.Get("repository"),Session: q.Get("session"),+ Trace: q.Get("trace"),File: q.Get("file"),Command: q.Get("command"),MCP: q.Get("mcp"),@@ -198,6 +220,20 @@ func parseQuery(r *http.Request, fallbackLimit int) EventQuery {return query}+func tokenOptions(r *http.Request) tokens.Options {+ q := r.URL.Query()+ opts := tokens.Options{SessionID: q.Get("session")}+ if bucket := q.Get("bucket"); bucket != "" {+ if parsed, err := time.ParseDuration(bucket); err == nil && parsed > 0 {+ opts.BucketSize = parsed+ }+ }+ if top, err := strconv.Atoi(q.Get("top")); err == nil && top > 0 {+ opts.TopLimit = top+ }+ return opts+}+func writeJSON(w http.ResponseWriter, value interface{}) {w.Header().Set("Content-Type", "application/json")_ = json.NewEncoder(w).Encode(value)diff --git a/cli/beacon/internal/endpoint/dashboard/server_test.go b/cli/beacon/internal/endpoint/dashboard/server_test.goindex 89c274fa..e3b06134 100644--- a/cli/beacon/internal/endpoint/dashboard/server_test.go+++ b/cli/beacon/internal/endpoint/dashboard/server_test.go@@ -4,9 +4,12 @@ import ("encoding/json""net/http""net/http/httptest"+ "os""path/filepath""strings""testing"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/tokens")func TestStatusUsesExplicitRuntimeLogPath(t *testing.T) {@@ -35,6 +38,69 @@ func TestStatusUsesExplicitRuntimeLogPath(t *testing.T) {}}+func TestTokensEndpointAggregatesUsage(t *testing.T) {+ logPath := filepath.Join(t.TempDir(), "runtime.jsonl")+ lines := []string{+ // Claude Code metric datapoint events (delta temporality).+ `{"timestamp":"2026-06-11T10:00:00Z","vendor":"beacon","product":"endpoint-agent","schema_version":"1.0","event":{"kind":"agent_runtime","action":"token.usage","category":"metric"},"severity":"info","endpoint":{"os":"darwin"},"harness":{"name":"claude_code"},"session":{"id":"local-session"},"model":"claude-sonnet-4-5","gen_ai":{"usage":{"input_tokens":100}},"message":"claude_code.token.usage","raw":{"metric_name":"claude_code.token.usage","metric_temporality":"Delta"}}`,+ `{"timestamp":"2026-06-11T10:00:00Z","vendor":"beacon","product":"endpoint-agent","schema_version":"1.0","event":{"kind":"agent_runtime","action":"token.usage","category":"metric"},"severity":"info","endpoint":{"os":"darwin"},"harness":{"name":"claude_code"},"session":{"id":"local-session"},"model":"claude-sonnet-4-5","gen_ai":{"usage":{"output_tokens":40}},"message":"claude_code.token.usage","raw":{"metric_name":"claude_code.token.usage","metric_temporality":"Delta"}}`,+ `{"timestamp":"2026-06-11T10:05:00Z","vendor":"beacon","product":"endpoint-agent","schema_version":"1.0","event":{"kind":"agent_runtime","action":"cost.usage","category":"metric"},"severity":"info","endpoint":{"os":"darwin"},"harness":{"name":"claude_code"},"session":{"id":"local-session"},"model":"claude-sonnet-4-5","gen_ai":{"usage":{"cost_usd":0.5}},"message":"claude_code.cost.usage","raw":{"metric_name":"claude_code.cost.usage","metric_temporality":"Delta"}}`,+ // Cloud SDK span usage with trace identity.+ `{"timestamp":"2026-06-11T11:00:00Z","vendor":"beacon","product":"endpoint-agent","schema_version":"1.0","event":{"kind":"agent_runtime","action":"tool.invoked","category":"tool"},"severity":"info","endpoint":{"os":"linux"},"harness":{"name":"asymptote_observe"},"origin":"cloud","session":{"id":"cloud-session"},"trace":{"id":"trace-1","span_id":"span-1"},"model":"gpt-4o-mini","gen_ai":{"usage":{"input_tokens":60,"output_tokens":20}},"message":"agent.step"}`,+ }+ if err := os.WriteFile(logPath, []byte(strings.Join(lines, "\n")+"\n"), 0644); err != nil {+ t.Fatalf("write fixture log: %v", err)+ }+ handler, err := Handler(Options{UserMode: true, LogPath: logPath})+ if err != nil {+ t.Fatalf("Handler returned error: %v", err)+ }++ req := httptest.NewRequest(http.MethodGet, "/api/tokens?bucket=1h", nil)+ rec := httptest.NewRecorder()+ handler.ServeHTTP(rec, req)+ if rec.Code != http.StatusOK {+ t.Fatalf("status code = %d, body = %s", rec.Code, rec.Body.String())+ }+ var report tokens.Report+ if err := json.Unmarshal(rec.Body.Bytes(), &report); err != nil {+ t.Fatalf("unmarshal tokens report: %v", err)+ }+ if report.Totals.InputTokens != 160 || report.Totals.OutputTokens != 60 || report.Totals.CostUSD != 0.5 {+ t.Fatalf("totals = %#v", report.Totals)+ }+ if len(report.ByModel) != 2 || report.ByModel[0].Key != "claude-sonnet-4-5" {+ t.Fatalf("by_model = %#v", report.ByModel)+ }+ if len(report.BySession) != 2 {+ t.Fatalf("by_session = %#v", report.BySession)+ }+ if len(report.Series) != 2 {+ t.Fatalf("series = %#v", report.Series)+ }++ // Session filter plus per-step detail for the cloud session.+ req = httptest.NewRequest(http.MethodGet, "/api/tokens?session=cloud-session", nil)+ rec = httptest.NewRecorder()+ handler.ServeHTTP(rec, req)+ if rec.Code != http.StatusOK {+ t.Fatalf("status code = %d, body = %s", rec.Code, rec.Body.String())+ }+ report = tokens.Report{}+ if err := json.Unmarshal(rec.Body.Bytes(), &report); err != nil {+ t.Fatalf("unmarshal session tokens report: %v", err)+ }+ if report.Totals.InputTokens != 60 {+ t.Fatalf("session totals = %#v", report.Totals)+ }+ if report.SessionDetail == nil || report.SessionDetail.SessionID != "cloud-session" || len(report.SessionDetail.Steps) != 1 {+ t.Fatalf("session detail = %#v", report.SessionDetail)+ }+ if report.SessionDetail.Steps[0].SpanID != "span-1" || report.SessionDetail.Steps[0].Usage.InputTokens != 60 {+ t.Fatalf("session step = %#v", report.SessionDetail.Steps[0])+ }+}+func TestStaticDashboardPagesServe(t *testing.T) {handler, err := Handler(Options{UserMode: true, LogPath: filepath.Join(t.TempDir(), "runtime.jsonl")})if err != nil {@@ -47,6 +113,7 @@ func TestStaticDashboardPagesServe(t *testing.T) {}{{path: "/", want: "Beacon Endpoint Log Search"},{path: "/overview.html", want: "Beacon Endpoint Security Overview"},+ {path: "/tokens.html", want: "Beacon Endpoint Token Usage"},}for _, tc := range cases {t.Run(tc.path, func(t *testing.T) {diff --git a/cli/beacon/internal/endpoint/dashboard/static/app.js b/cli/beacon/internal/endpoint/dashboard/static/app.jsindex 2e25cf18..38c3aa66 100644--- a/cli/beacon/internal/endpoint/dashboard/static/app.js+++ b/cli/beacon/internal/endpoint/dashboard/static/app.js@@ -12,6 +12,7 @@ const state = {const $ = (selector) => document.querySelector(selector);const $$ = (selector) => Array.from(document.querySelectorAll(selector));const isOverviewPage = document.body.dataset.page === "overview";+const isTokensPage = document.body.dataset.page === "tokens";const formFields = ["q","harness",@@ -714,7 +715,157 @@ function clearSearch() {clearFields();}-$("#refresh")?.addEventListener("click", () => load().catch(console.error));+async function loadTokens() {+ const params = new URLSearchParams(window.location.search);+ const tokenParams = new URLSearchParams({ bucket: "1h" });+ const session = (params.get("session") || "").trim();+ if (session) tokenParams.set("session", session);+ try {+ const [status, report] = await Promise.all([+ getJSON("/api/status"),+ getJSON(`/api/tokens?${tokenParams.toString()}`),+ ]);+ state.status = status;+ state.error = null;+ renderTokensPage(report, session);+ } catch (err) {+ state.error = err;+ setText("#token-meta", `Failed to load token usage: ${err.message}`);+ }+}++function renderTokensPage(report, session) {+ setText("#log-path", state.status?.log_path || "Runtime log unavailable");+ setText("#token-meta", `${report.events_with_usage} of ${report.total_events} events carry usage`);+ renderTokenCards(report.totals || {});+ renderUtilizationRows(report.utilization || []);+ renderUsageGroupRows("#token-models", report.by_model || [], "model");+ renderUsageGroupRows("#token-sessions", report.by_session || [], "session");+ renderUsageGroupRows("#token-runs", report.by_run || []);+ renderUsageGroupRows("#token-harnesses", report.by_harness || []);+ renderSessionDetail(report.session_detail, session);+}++function renderTokenCards(totals) {+ if (!$("#token-cards")) return;+ const cards = [+ { label: "Input Tokens", value: formatTokens(totals.input_tokens) },+ { label: "Output Tokens", value: formatTokens(totals.output_tokens) },+ { label: "Cache Read", value: formatTokens(totals.cache_read_input_tokens) },+ { label: "Cache Creation", value: formatTokens(totals.cache_creation_input_tokens) },+ { label: "Reasoning", value: formatTokens(totals.reasoning_output_tokens) },+ { label: "Cost (USD)", value: totals.cost_usd ? totals.cost_usd.toFixed(4) : "-", hint: "runtime-reported only" },+ ];+ $("#token-cards").innerHTML = cards+ .map((card) => `+ <div class="card">+ <span class="muted">${escapeHTML(card.label)}</span>+ <span class="value">${escapeHTML(card.value)}</span>+ ${card.hint ? `<span class="muted">${escapeHTML(card.hint)}</span>` : ""}+ </div>+ `)+ .join("");+}++function renderUtilizationRows(utilization) {+ const tbody = $("#token-utilization");+ if (!tbody) return;+ if (!utilization.length) {+ tbody.innerHTML = `<tr><td colspan="7" class="muted">No usage with input tokens captured yet.</td></tr>`;+ return;+ }+ tbody.innerHTML = utilization+ .map((item) => `+ <tr>+ <td class="mono">${escapeHTML(item.model)}</td>+ <td>${item.context_window ? formatTokens(item.context_window) : `<span class="muted">unknown</span>`}</td>+ <td>${escapeHTML(item.calls)}</td>+ <td>${formatTokens(item.max_input_tokens)}</td>+ <td>${formatRatio(item.max_ratio, item.context_window)}</td>+ <td>${formatRatio(item.p95_ratio, item.context_window)}</td>+ <td>${item.near_limit_calls ? badge(`${item.near_limit_calls} near limit`, "badge-warn") : ""}</td>+ </tr>+ `)+ .join("");+}++function renderUsageGroupRows(selector, groups, filterKey) {+ const tbody = $(selector);+ if (!tbody) return;+ if (!groups.length) {+ tbody.innerHTML = `<tr><td colspan="8" class="muted">No usage captured yet.</td></tr>`;+ return;+ }+ tbody.innerHTML = groups+ .map((group) => `+ <tr ${filterKey === "session" ? `class="row-link" data-token-session="${escapeHTML(group.key)}"` : ""}>+ <td class="mono">${escapeHTML(group.key)}</td>+ ${usageCells(group.usage)}+ </tr>+ `)+ .join("");+ $$(`${selector} [data-token-session]`).forEach((row) => {+ row.addEventListener("click", () => {+ const params = new URLSearchParams(window.location.search);+ params.set("session", row.dataset.tokenSession);+ window.location.search = params.toString();+ });+ });+}++function renderSessionDetail(detail, session) {+ const panel = $("#token-session-panel");+ if (!panel) return;+ if (!detail || !session) {+ panel.hidden = true;+ return;+ }+ panel.hidden = false;+ setText("#token-session-title", `Session ${detail.session_id}`);+ const rows = [];+ const walk = (steps, depth) => {+ for (const step of steps || []) {+ const label = step.name || step.action || step.span_id || "step";+ rows.push(`+ <tr>+ <td><span class="mono">${" ".repeat(depth * 4)}${escapeHTML(label)}</span></td>+ <td class="mono">${escapeHTML(step.model || "")}</td>+ ${usageCells(step.usage, { events: false })}+ </tr>+ `);+ walk(step.children, depth + 1);+ }+ };+ walk(detail.steps, 0);+ $("#token-session-detail").innerHTML = rows.length+ ? rows.join("")+ : `<tr><td colspan="8" class="muted">No usage-bearing steps in this session.</td></tr>`;+}++function usageCells(usage = {}, { events = true } = {}) {+ const cells = [+ formatTokens(usage.input_tokens),+ formatTokens(usage.output_tokens),+ formatTokens(usage.cache_read_input_tokens),+ formatTokens(usage.cache_creation_input_tokens),+ formatTokens(usage.reasoning_output_tokens),+ usage.cost_usd ? usage.cost_usd.toFixed(4) : "-",+ ];+ if (events) cells.push(String(usage.events ?? 0));+ return cells.map((value) => `<td>${escapeHTML(value)}</td>`).join("");+}++function formatTokens(value) {+ if (!value) return "0";+ return Number(value).toLocaleString();+}++function formatRatio(ratio, contextWindow) {+ if (!contextWindow) return "-";+ return `${(ratio * 100).toFixed(1)}%`;+}++$("#refresh")?.addEventListener("click", () => (isTokensPage ? loadTokens() : load()).catch(console.error));$("#filters")?.addEventListener("submit", (event) => {event.preventDefault();load({ updateLocation: true }).catch(console.error);@@ -726,9 +877,14 @@ $$("[data-preset]").forEach((button) => {button.addEventListener("click", () => applyPreset(button.dataset.preset));});-hydrateFiltersFromURL();-load().catch(console.error);-setInterval(() => load({ mode: "poll" }).catch(console.error), 10000);+if (isTokensPage) {+ loadTokens().catch(console.error);+ setInterval(() => loadTokens().catch(console.error), 15000);+} else {+ hydrateFiltersFromURL();+ load().catch(console.error);+ setInterval(() => load({ mode: "poll" }).catch(console.error), 10000);+}function setText(selector, value) {const element = $(selector);diff --git a/cli/beacon/internal/endpoint/dashboard/static/index.html b/cli/beacon/internal/endpoint/dashboard/static/index.htmlindex 157da075..5e72e947 100644--- a/cli/beacon/internal/endpoint/dashboard/static/index.html+++ b/cli/beacon/internal/endpoint/dashboard/static/index.html@@ -15,6 +15,7 @@<nav class="nav-toggle" aria-label="Dashboard navigation"><a class="active" href="/" aria-current="page">Log Search</a><a href="/overview.html">Security Overview</a>+ <a href="/tokens.html">Token Usage</a></nav><button id="refresh">Refresh</button></header>diff --git a/cli/beacon/internal/endpoint/dashboard/static/overview.html b/cli/beacon/internal/endpoint/dashboard/static/overview.htmlindex 3b0f0ae1..2e1cd161 100644--- a/cli/beacon/internal/endpoint/dashboard/static/overview.html+++ b/cli/beacon/internal/endpoint/dashboard/static/overview.html@@ -15,6 +15,7 @@<nav class="nav-toggle" aria-label="Dashboard navigation"><a href="/">Log Search</a><a class="active" href="/overview.html" aria-current="page">Security Overview</a>+ <a href="/tokens.html">Token Usage</a></nav><button id="refresh">Refresh</button></header>diff --git a/cli/beacon/internal/endpoint/dashboard/static/styles.css b/cli/beacon/internal/endpoint/dashboard/static/styles.cssindex 0d52e1a9..4e42b130 100644--- a/cli/beacon/internal/endpoint/dashboard/static/styles.css+++ b/cli/beacon/internal/endpoint/dashboard/static/styles.css@@ -616,3 +616,11 @@ pre {padding: 1rem;}}++.row-link {+ cursor: pointer;+}++.row-link:hover {+ background: rgba(125, 211, 252, 0.08);+}diff --git a/cli/beacon/internal/endpoint/dashboard/static/tokens.html b/cli/beacon/internal/endpoint/dashboard/static/tokens.htmlnew file mode 100644index 00000000..53a1c041--- /dev/null+++ b/cli/beacon/internal/endpoint/dashboard/static/tokens.html@@ -0,0 +1,107 @@+<!doctype html>+<html lang="en">+ <head>+ <meta charset="utf-8" />+ <meta name="viewport" content="width=device-width, initial-scale=1" />+ <title>Beacon Endpoint Token Usage</title>+ <link rel="stylesheet" href="/styles.css" />+ </head>+ <body data-page="tokens">+ <header class="topbar">+ <div>+ <p class="eyebrow">Local-only</p>+ <h1>Beacon Endpoint Token Usage</h1>+ </div>+ <nav class="nav-toggle" aria-label="Dashboard navigation">+ <a href="/">Log Search</a>+ <a href="/overview.html">Security Overview</a>+ <a class="active" href="/tokens.html" aria-current="page">Token Usage</a>+ </nav>+ <button id="refresh">Refresh</button>+ </header>++ <main>+ <section class="panel status-panel">+ <div>+ <h2>Endpoint</h2>+ <p id="log-path" class="muted">Loading runtime log...</p>+ </div>+ </section>++ <section>+ <div class="section-head overview-head">+ <div>+ <h2>Token Totals</h2>+ <p class="muted">Prompt, completion, cache, reasoning, and runtime-reported cost across all captured telemetry.</p>+ </div>+ <div id="token-meta" class="muted"></div>+ </div>+ <div class="cards" id="token-cards"></div>+ </section>++ <section class="panel">+ <h2>Context Window Utilization</h2>+ <p class="muted">Input plus cache tokens per call against each model's context window. Unknown models show raw input tokens.</p>+ <table>+ <thead>+ <tr><th>Model</th><th>Window</th><th>Calls</th><th>Max Input</th><th>Max</th><th>P95</th><th>Near Limit</th></tr>+ </thead>+ <tbody id="token-utilization"></tbody>+ </table>+ </section>++ <section class="panel">+ <h2>By Model</h2>+ <table>+ <thead>+ <tr><th>Model</th><th>Input</th><th>Output</th><th>Cache Read</th><th>Cache Create</th><th>Reasoning</th><th>Cost USD</th><th>Events</th></tr>+ </thead>+ <tbody id="token-models"></tbody>+ </table>+ </section>++ <section class="panel">+ <h2>By Session</h2>+ <p class="muted">Select a session to inspect per-step usage.</p>+ <table>+ <thead>+ <tr><th>Session</th><th>Input</th><th>Output</th><th>Cache Read</th><th>Cache Create</th><th>Reasoning</th><th>Cost USD</th><th>Events</th></tr>+ </thead>+ <tbody id="token-sessions"></tbody>+ </table>+ </section>++ <section class="panel" id="token-session-panel" hidden>+ <h2 id="token-session-title">Session Detail</h2>+ <table>+ <thead>+ <tr><th>Step</th><th>Model</th><th>Input</th><th>Output</th><th>Cache Read</th><th>Cache Create</th><th>Reasoning</th><th>Cost USD</th></tr>+ </thead>+ <tbody id="token-session-detail"></tbody>+ </table>+ </section>++ <section class="panel">+ <h2>By CI Run</h2>+ <table>+ <thead>+ <tr><th>Run</th><th>Input</th><th>Output</th><th>Cache Read</th><th>Cache Create</th><th>Reasoning</th><th>Cost USD</th><th>Events</th></tr>+ </thead>+ <tbody id="token-runs"></tbody>+ </table>+ </section>++ <section class="panel">+ <h2>By Harness</h2>+ <table>+ <thead>+ <tr><th>Harness</th><th>Input</th><th>Output</th><th>Cache Read</th><th>Cache Create</th><th>Reasoning</th><th>Cost USD</th><th>Events</th></tr>+ </thead>+ <tbody id="token-harnesses"></tbody>+ </table>+ </section>+ </main>++ <script src="/app.js"></script>+ </body>+</html>