Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust
9.3 KB
Raw Blame
<section id="token-usage" class="usage-panel section-block" aria-labelledby="token-heading">
  <div class="usage-heading"><div><p class="eyebrow">FOLLOW THE CONSUMPTION</p><h2 id="token-heading">Token usage</h2><p>Measured model responses · usage updates every 10 seconds</p></div><span class="usage-status">During sessions</span></div>
  <div class="usage-metrics">
    <div><span>Captured calls</span><strong>{{ token_usage.count }}</strong></div><div><span>Fresh input</span><strong>{{ token_usage.input }}</strong></div><div><span>Output</span><strong>{{ token_usage.output }}</strong></div><div><span>Cache read</span><strong>{{ token_usage.cache_read }}</strong></div><div><span>Cache write</span><strong>{{ token_usage.cache_write }}</strong></div><div><span>Input from cache</span><strong>{{ token_usage.hit_rate }}</strong></div>
  </div>
  <form class="usage-controls" id="call-query" method="get">
    <input type="hidden" name="tab" value="tokens"><input type="hidden" name="offset" value="{{ token_usage.observation_offset }}"><input type="hidden" name="limit" value="{{ token_usage.observation_limit }}">
    <label for="call-filter" class="sr-only">Filter calls by model, agent or response</label><input id="call-filter" name="calls_q" maxlength="256" type="search" placeholder="Find a model, agent, or response…" value="{{ token_usage.search }}" data-filter-calls>
    <label for="call-signal" class="sr-only">Filter investigation signals</label><select id="call-signal" name="calls_signal" data-filter-calls><option value="all">All responses</option><option value="signals" {% if token_usage.signals_only %}selected{% endif %}>Investigation signals</option></select>
    <label for="call-sort" class="sr-only">Sort calls</label><select id="call-sort" name="calls_sort" data-filter-calls><option value="tokens">Largest usage first</option><option value="recent" {% if token_usage.recent %}selected{% endif %}>Recent response time first</option></select>
    <label>From (UTC)<input type="datetime-local" step="1" name="calls_from" value="{{ token_usage.from }}" data-filter-calls></label>
    <label>Until (UTC, exclusive)<input type="datetime-local" step="1" name="calls_until" value="{{ token_usage.until }}" data-filter-calls></label>
    <label>Model (exact)<input type="text" name="calls_model" maxlength="128" value="{{ token_usage.model }}" placeholder="All models" data-filter-calls></label>
    <label>Harness<select name="calls_agent" data-filter-calls><option value="">All harnesses</option>{% for agent in token_usage.agents %}<option value="{{ agent.value }}" {% if agent.selected %}selected{% endif %}>{{ agent.label }}</option>{% endfor %}</select></label>
    <label>Session ID<input type="text" name="calls_session" value="{{ token_usage.session }}" placeholder="All sessions in this scope" data-filter-calls></label>
    <label>Turn ID<input type="text" name="calls_turn" maxlength="128" value="{{ token_usage.turn }}" placeholder="All reported turns" data-filter-calls></label>
    <label>Tool (exact)<input type="text" name="calls_tool" maxlength="128" value="{{ token_usage.tool }}" placeholder="All reported tools" data-filter-calls></label>
    <button type="submit" class="button-secondary">Search</button>
  </form>
  <div class="usage-results">
  {% if token_usage.rows.is_empty() %}{% if token_usage.count > 0 %}<p class="usage-filter-empty">No captured responses match this page and these filters. Change the search or return to the previous page.</p>{% else %}<div class="empty-state" style="border:0"><span aria-hidden="true">⌁</span><h3>No response usage reported yet</h3><p>Updated Claude Code and Codex clients report usage during tool calls and completed turns. A session does not need to end.</p><p>Existing session totals remain visible in the session table.</p></div>{% endif %}{% else %}
  <div class="usage-scroll" tabindex="0" role="region" aria-label="Captured response usage"><table class="usage-table"><thead><tr><th>Response / session</th><th>Model</th><th>Fresh input</th><th>Output</th><th>Cache</th><th>Total</th><th>Signal</th></tr></thead><tbody>
  {% for row in token_usage.rows %}<tr data-call-row data-total="{{ row.total_value }}" data-observed="{{ row.observed_at }}" data-signal="{% if row.signal != "—" %}yes{% else %}no{% endif %}"><td><a href="{{ row.href }}" title="{{ row.id }}">{{ row.agent }} ↗</a><small class="usage-response-id" title="{{ row.id }}">{{ row.id }}</small><small title="{{ row.time_basis }} · {{ row.recorded_at }}">{{ row.when }}</small><details class="response-context"><summary>Turn &amp; tools</summary><p>Session: <code>{{ row.session }}</code></p>{% if row.turn != "" %}<p>Turn: <a href="{{ row.turn_href }}">{{ row.turn }}</a></p>{% else %}<p>Turn unknown — not reported by this client.</p>{% endif %}{% if row.response_native != "" %}<p>Native response: <code>{{ row.response_native }}</code></p>{% endif %}<p>{{ row.time_basis }}: <time>{{ row.recorded_at }}</time></p>{% if row.tools.is_empty() %}<p>No tool association was reported.</p>{% else %}<ul>{% for tool in row.tools %}<li><a href="{{ tool.href }}">{{ tool.name }}</a> <code>{{ tool.id }}</code><small>{{ tool.relation }}{% if let Some(failed) = tool.is_error %} · {% if failed %}error reported{% else %}no error reported{% endif %}{% endif %}</small></li>{% endfor %}</ul>{% endif %}</details></td><td>{{ row.model }}</td><td>{{ row.input }}</td><td>{{ row.output }}</td><td>{{ row.cache }}</td><td title="{{ row.total_value }} tokens"><strong>{{ row.total }}</strong></td><td><span class="{% if row.signal != "—" %}usage-alert{% endif %}">{{ row.signal }}</span></td></tr>{% endfor %}
  </tbody></table></div><p class="usage-filter-empty" hidden>No captured responses match these filters.</p>{% endif %}
    <nav class="usage-pagination" aria-label="Response pages"><span>{{ token_usage.shown_start }}–{{ token_usage.shown_end }} of {{ token_usage.matching }} matching responses · {{ token_usage.count }} captured · {{ token_usage.matching_tokens }} tokens in matches</span>{% if token_usage.previous_href != "" %} <a href="{{ token_usage.previous_href }}" data-call-page>← Previous responses</a>{% endif %}{% if token_usage.next_href != "" %} <a href="{{ token_usage.next_href }}" data-call-page>Next responses →</a>{% endif %}</nav>
  </div>
  <p class="usage-note">Dates use the reported response time when available, otherwise capture time. A shared turn is an association, not proof that a tool caused the token usage. Historical context remains unknown. Search, filters, signals and ordering cover all captured responses; pages show up to 50. Totals cover the whole scope, including cache. Signals flag context ≥100k, output ≥20k, or usage above 3× the captured average after five calls. They are investigation hints, not confirmed errors or monetary costs. Missing usage is unknown, never zero.</p>
  <p class="usage-note" id="token-refresh-status" role="status" aria-live="polite"></p>
</section>
<script>
(function(){
  var form=document.getElementById("call-query"),timer,controller,stopped=false;
  if(!form)return;
  async function load(url){
    clearTimeout(timer);if(controller)controller.abort();controller=new AbortController();
    form.setAttribute("aria-busy","true");
    var request=controller,timeout=setTimeout(function(){request.abort();},5000);
    history.replaceState(history.state,"",url);
    document.dispatchEvent(new Event("usage:query"));
    try{
      var response=await fetch(url,{credentials:"same-origin",cache:"no-store",signal:request.signal,headers:{"Accept":"text/html"}});
      if(!response.ok||response.redirected)throw new Error("Query unavailable");
      var page=new DOMParser().parseFromString(await response.text(),"text/html");
      if(stopped||controller!==request||location.href!==url.href)return;
      if(!page.querySelector("#token-usage .usage-results"))throw new Error("Missing usage");
      var resultFocused=document.querySelector(".usage-results").contains(document.activeElement);
      document.dispatchEvent(new CustomEvent("usage:response",{detail:page}));
      if(resultFocused){var target=document.querySelector(".usage-scroll");if(target)target.focus({preventScroll:true});}
    }catch(_){if(!stopped&&controller===request)document.getElementById("token-refresh-status").textContent="Search unavailable. Showing the last received values. Try Search again.";}
    finally{clearTimeout(timeout);if(controller===request)form.removeAttribute("aria-busy");}
  }
  function search(){var url=new URL(location.href);new FormData(form).forEach(function(value,key){url.searchParams.set(key,value);});url.searchParams.delete("calls_offset");load(url);}
  form.addEventListener("submit",function(event){event.preventDefault();search();});
  form.addEventListener("input",function(event){if(event.target.matches("[data-filter-calls]")){clearTimeout(timer);timer=setTimeout(search,300);}});
  form.addEventListener("change",function(event){if(event.target.matches("[data-filter-calls]"))search();});
  document.addEventListener("click",function(event){var link=event.target.closest("[data-call-page]");if(!link||event.button!==0||event.altKey||event.ctrlKey||event.metaKey||event.shiftKey)return;event.preventDefault();load(new URL(link.href));});
  window.addEventListener("pagehide",function(){stopped=true;clearTimeout(timer);if(controller)controller.abort();});
  window.addEventListener("pageshow",function(event){if(event.persisted)stopped=false;});
})();
</script>