hermes_state_usage.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. """Token/usage accounting mixin for SessionDB: the coalescing background token writer,
  2. per-model usage rows, and billing-route columns. Writer thread state lives on the instance."""
  3. from __future__ import annotations
  4. import asyncio
  5. import atexit
  6. import contextlib
  7. import logging
  8. import threading
  9. import time
  10. import weakref
  11. from typing import Any, Dict, List, Optional, Tuple
  12. # caplog tests pin the "hermes_state" logger name.
  13. logger = logging.getLogger("hermes_state")
  14. _TOKEN_COUNTERS = ("input_tokens", "output_tokens", "cache_read_tokens", "cache_write_tokens", "reasoning_tokens")
  15. def _token_update_sql(delta: bool) -> str:
  16. """``UPDATE sessions`` for one usage report: *delta* adds to the stored counters (CLI
  17. per-call path), otherwise sets them (gateway cumulative path). Cost/route columns
  18. COALESCE-fill either way (statement text is pinned by the SQL trace harness)."""
  19. def add(col: str) -> str: # "col + ?" / "COALESCE(col, 0) + ?" in delta mode, bare "?" otherwise
  20. return f"{col} + ?" if delta else "?"
  21. def add0(col: str) -> str:
  22. return f"COALESCE({col}, 0) + ?" if delta else "?"
  23. counters = "".join(f" {c} = {add(c)},\n" for c in _TOKEN_COUNTERS)
  24. estimated = "COALESCE(estimated_cost_usd, 0) + COALESCE(?, 0)" if delta else "COALESCE(?, 0)"
  25. return (
  26. "UPDATE sessions SET\n" + counters
  27. + f""" estimated_cost_usd = {estimated},
  28. actual_cost_usd = CASE
  29. WHEN ? IS NULL THEN actual_cost_usd
  30. ELSE {add0("actual_cost_usd")}
  31. END,
  32. cost_status = COALESCE(?, cost_status),
  33. cost_source = COALESCE(?, cost_source),
  34. pricing_version = COALESCE(?, pricing_version),
  35. billing_provider = COALESCE(billing_provider, ?),
  36. billing_base_url = COALESCE(billing_base_url, ?),
  37. billing_mode = COALESCE(billing_mode, ?),
  38. model = COALESCE(model, ?),
  39. api_call_count = {add0("api_call_count")}
  40. WHERE id = ?"""
  41. )
  42. _TOKEN_UPDATE_ABSOLUTE_SQL = _token_update_sql(delta=False)
  43. _TOKEN_UPDATE_DELTA_SQL = _token_update_sql(delta=True)
  44. _MODEL_USAGE_UPSERT_SQL = """INSERT INTO session_model_usage (
  45. session_id, model, billing_provider, billing_base_url, billing_mode,
  46. task, api_call_count, input_tokens, output_tokens,
  47. cache_read_tokens, cache_write_tokens, reasoning_tokens,
  48. estimated_cost_usd, actual_cost_usd, cost_status, cost_source,
  49. first_seen, last_seen
  50. ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  51. ON CONFLICT(session_id, model, billing_provider, billing_base_url, billing_mode, task)
  52. DO UPDATE SET
  53. api_call_count = api_call_count + excluded.api_call_count,
  54. input_tokens = input_tokens + excluded.input_tokens,
  55. output_tokens = output_tokens + excluded.output_tokens,
  56. cache_read_tokens = cache_read_tokens + excluded.cache_read_tokens,
  57. cache_write_tokens = cache_write_tokens + excluded.cache_write_tokens,
  58. reasoning_tokens = reasoning_tokens + excluded.reasoning_tokens,
  59. estimated_cost_usd = estimated_cost_usd + excluded.estimated_cost_usd,
  60. actual_cost_usd = actual_cost_usd + excluded.actual_cost_usd,
  61. cost_status = COALESCE(excluded.cost_status, cost_status),
  62. cost_source = COALESCE(excluded.cost_source, cost_source),
  63. last_seen = excluded.last_seen"""
  64. # Kwargs forwarded verbatim from update_token_counts / record_auxiliary_usage into
  65. # _record_model_usage (the per-route attribution row).
  66. _MODEL_USAGE_FIELDS = frozenset((
  67. "model", "billing_provider", "billing_base_url", "billing_mode", "input_tokens", "output_tokens",
  68. "cache_read_tokens", "cache_write_tokens", "reasoning_tokens", "estimated_cost_usd",
  69. "actual_cost_usd", "cost_status", "cost_source", "api_call_count"))
  70. class SessionUsageMixin:
  71. """Coalesced token writer, per-model usage rows, billing route."""
  72. def update_session_billing_route(
  73. self, session_id: str, *, provider: str, base_url: str, billing_mode: Optional[str] = None,
  74. ) -> None:
  75. """Unconditionally set the billing route (``update_token_counts`` only COALESCE-fills
  76. NULLs) so the dashboard reflects the latest /model switch.
  77. Route writers never touch the stored prompt; ``_stored_prompt_matches_runtime`` decides staleness.
  78. See #48173, #48248.
  79. """
  80. # Barrier against queued token deltas — see update_session_model.
  81. self.flush_token_counts()
  82. def _do(conn):
  83. conn.execute("""UPDATE sessions SET
  84. billing_provider = ?,
  85. billing_base_url = ?,
  86. billing_mode = COALESCE(?, billing_mode)
  87. WHERE id = ?""", (provider, base_url, billing_mode, session_id))
  88. self._execute_write(_do)
  89. def queue_token_counts(self, session_id: str, **kwargs) -> None:
  90. """Enqueue a token/cost delta for the background writer (same kwargs as
  91. :meth:`update_token_counts`). After close() stopped the writer, falls back to the
  92. synchronous path and may raise."""
  93. with self._token_queue_cond:
  94. thread = self._token_writer_thread
  95. writer_alive = thread is not None and thread.is_alive()
  96. writer_stopped = self._token_writer_stop and not writer_alive
  97. if not writer_stopped:
  98. self._token_queue.append((session_id, kwargs))
  99. if not writer_alive:
  100. # Daemon so exit never hangs on accounting; the atexit hook drains
  101. # leftovers. ``not is_alive()`` respawns a writer that died unexpectedly.
  102. thread = threading.Thread(
  103. target=self._token_writer_loop, name="session-db-token-writer", daemon=True)
  104. self._token_writer_thread = thread
  105. thread.start()
  106. if self._token_atexit_hook is None:
  107. self_ref = weakref.ref(self)
  108. def _drain_at_exit() -> None:
  109. db = self_ref()
  110. if db is not None:
  111. db._drain_token_queue_at_exit()
  112. self._token_atexit_hook = _drain_at_exit
  113. atexit.register(_drain_at_exit)
  114. self._token_queue_cond.notify_all()
  115. if writer_stopped:
  116. # close() ran: enqueueing would drop the delta silently, so apply inline.
  117. self.update_token_counts(session_id, **kwargs)
  118. def _apply_claimed_batch(self, batch) -> None:
  119. """Apply a batch whose ``busy`` flag the caller already claimed, then release."""
  120. try:
  121. self._apply_token_batch(batch)
  122. finally:
  123. with self._token_queue_cond:
  124. self._token_writer_busy = False
  125. self._token_queue_cond.notify_all()
  126. def flush_for_read(self) -> bool:
  127. """Read-side token barrier. On a thread running an asyncio loop it never waits: the writer can sit
  128. behind another connection's write lock, and a gateway handler reading a session there froze every
  129. socket for up to the 5 s flush budget (#141966). That read sees totals stale by the queued deltas,
  130. which the next read picks up; write barriers keep calling ``flush_token_counts`` and still wait."""
  131. try:
  132. asyncio.get_running_loop()
  133. except RuntimeError:
  134. return self.flush_token_counts()
  135. return self.flush_token_counts(timeout=0.0)
  136. def flush_token_counts(self, timeout: float = 5.0) -> bool:
  137. """Block until every queued token delta has been applied. False on timeout (callers
  138. then read totals stale by the queued deltas). Never raises."""
  139. # Lock-free fast path: reads queue-then-busy (see ordering notes below).
  140. if not self._token_queue and not self._token_writer_busy:
  141. return True
  142. batch = None
  143. with self._token_queue_cond:
  144. deadline = time.monotonic() + timeout
  145. while self._token_queue or self._token_writer_busy:
  146. # A live writer is authoritative even when stop-flagged: draining here would
  147. # race its in-flight batch and reorder deltas (breaking last-non-None-wins /
  148. # first-accounted-route / COALESCE-backfill fields). Only a dead writer lets
  149. # the caller take leftovers; a claimed busy means "wait".
  150. thread = self._token_writer_thread
  151. if (thread is None or not thread.is_alive()) and not self._token_writer_busy:
  152. self._token_writer_busy = True
  153. batch = list(self._token_queue)
  154. self._token_queue.clear()
  155. break
  156. remaining = deadline - time.monotonic()
  157. if remaining <= 0:
  158. return False
  159. self._token_queue_cond.wait(remaining)
  160. if batch:
  161. self._apply_claimed_batch(batch)
  162. return True
  163. def _token_writer_loop(self) -> None:
  164. while True:
  165. with self._token_queue_cond:
  166. idle_deadline = time.monotonic() + self._TOKEN_WRITER_IDLE_SECONDS
  167. while not self._token_queue and not self._token_writer_stop:
  168. remaining = idle_deadline - time.monotonic()
  169. if remaining <= 0:
  170. # Retire under the lock queue_token_counts() spawns under, so no
  171. # delta strands behind an exiting worker.
  172. self._token_writer_thread = None
  173. return
  174. self._token_queue_cond.wait(remaining)
  175. if not self._token_queue:
  176. self._token_writer_thread = None
  177. return # stop requested and fully drained
  178. # busy BEFORE clearing the queue: flush's lock-free fast path must never see
  179. # "empty and idle" while a popped batch is unapplied.
  180. self._token_writer_busy = True
  181. batch = list(self._token_queue)
  182. self._token_queue.clear()
  183. self._apply_claimed_batch(batch)
  184. def _apply_token_batch(self, batch: list[tuple[str, dict[str, Any]]]) -> None:
  185. """Apply queued deltas in order, coalescing where safe. Never raises."""
  186. try:
  187. coalesced = self._coalesce_token_deltas(batch)
  188. except Exception as exc:
  189. # Coalescing must never kill the writer; the merge is only an optimization.
  190. logger.warning("async token accounting: coalesce failed, applying raw batch: %s", exc)
  191. coalesced = batch
  192. for session_id, kwargs in coalesced:
  193. try:
  194. self.update_token_counts(session_id, **kwargs)
  195. except Exception as exc:
  196. # Accounting loss is logged, never raised into a turn.
  197. logger.warning("async token accounting: apply failed (session=%s): %s", session_id, exc)
  198. def _coalesce_token_deltas(self, batch: list[tuple[str, dict[str, Any]]]) -> list[tuple[str, dict[str, Any]]]:
  199. """Merge adjacent incremental deltas with an identical route, so ordering across
  200. sessions and /model switches is preserved exactly. absolute=True never merges."""
  201. groups: list[tuple[Optional[tuple], str, dict[str, Any]]] = []
  202. for session_id, kwargs in batch:
  203. key = None
  204. if not kwargs.get("absolute"):
  205. key = (session_id, *(kwargs.get(f) for f in self._TOKEN_DELTA_ROUTE_FIELDS))
  206. if groups and key is not None and groups[-1][0] == key:
  207. merged = groups[-1][2]
  208. for f in self._TOKEN_DELTA_SUM_FIELDS:
  209. merged[f] = merged.get(f, 0) + kwargs.get(f, 0)
  210. for f in self._TOKEN_DELTA_COST_FIELDS:
  211. value = kwargs.get(f)
  212. if value is not None:
  213. # All-None runs stay None so COALESCE keeps the stored value.
  214. merged[f] = (merged.get(f) or 0.0) + value
  215. else:
  216. groups.append((key, session_id, dict(kwargs)))
  217. return [(sid, kw) for _, sid, kw in groups]
  218. def _stop_token_writer(self, join_timeout: float = 10.0) -> None:
  219. """Stop the writer thread and drain remaining deltas. Never raises."""
  220. with self._token_queue_cond:
  221. self._token_writer_stop = True
  222. self._token_queue_cond.notify_all()
  223. thread = self._token_writer_thread
  224. if thread is not None and thread.is_alive():
  225. thread.join(timeout=join_timeout)
  226. if thread.is_alive():
  227. # Writer stuck mid-apply: leave deltas unapplied rather than race it.
  228. logger.warning(
  229. "async token accounting: writer did not stop within %.0fs; "
  230. "%d queued delta(s) not persisted", join_timeout, len(self._token_queue))
  231. return
  232. # Writer gone: apply leftovers synchronously under the same busy protocol. Wait out
  233. # a flush caller-drain that already claimed busy — close() nulls the connection
  234. # right after this returns and must not yank it mid-batch.
  235. with self._token_queue_cond:
  236. deadline = time.monotonic() + join_timeout
  237. while self._token_writer_busy:
  238. remaining = deadline - time.monotonic()
  239. if remaining <= 0:
  240. logger.warning(
  241. "async token accounting: concurrent drain did not "
  242. "finish within %.0fs; %d queued delta(s) not persisted",
  243. join_timeout, len(self._token_queue))
  244. return
  245. self._token_queue_cond.wait(remaining)
  246. # busy BEFORE clearing the queue (same ordering as the writer loop).
  247. batch = list(self._token_queue)
  248. if batch:
  249. self._token_writer_busy = True
  250. self._token_queue.clear()
  251. if batch:
  252. self._apply_claimed_batch(batch)
  253. def _drain_token_queue_at_exit(self) -> None:
  254. with contextlib.suppress(Exception): # never fatal at interpreter shutdown
  255. self._stop_token_writer()
  256. def update_token_counts(
  257. self, session_id: str, input_tokens: int=0, output_tokens: int=0, model: str | None=None, cache_read_tokens: int=0,
  258. cache_write_tokens: int=0, reasoning_tokens: int=0, estimated_cost_usd: Optional[float]=None,
  259. actual_cost_usd: Optional[float]=None, cost_status: Optional[str]=None, cost_source: Optional[str]=None,
  260. pricing_version: Optional[str]=None, billing_provider: Optional[str]=None, billing_base_url: Optional[str]=None,
  261. billing_mode: Optional[str]=None, api_call_count: int=0, absolute: bool=False,
  262. source: Optional[str]=None, task: str = "",
  263. ) -> None:
  264. """Update totals and route attribution, ensuring the legacy missing-row backfill.
  265. *absolute*=False increments (per-API-call deltas, CLI path); *absolute*=True sets directly
  266. (gateway path, where the cached agent holds cumulative totals). ``source`` is the session's
  267. real surface for the row-existence guard; callers that don't know it leave the placeholder.
  268. ``task`` names a per-turn route (``voice_chat``): its calls count in the session totals and
  269. land in ``session_model_usage`` under that task, but never become the session's recorded route."""
  270. values = dict(locals())
  271. values.pop('self')
  272. values.pop('session_id')
  273. values.pop('source')
  274. # Ensure the row exists: under concurrent load create_session() may have failed on
  275. # locking, and the UPDATE would silently affect 0 rows. When this guard is the first
  276. # writer it must carry the agent's real source: the turn lease treats an existing row as
  277. # proof the create already happened, so the creator never returns to repair an anonymous
  278. # ``unknown`` placeholder and the session stays a phantom for life (#111999).
  279. self._insert_session_row(session_id, source or "unknown", model=model)
  280. self._execute_write(lambda conn: self._update_token_counts_in_transaction(conn, session_id, **values))
  281. def _update_token_counts_in_transaction(
  282. self, conn, session_id: str, input_tokens: int=0, output_tokens: int=0, model: str | None=None, cache_read_tokens: int=0,
  283. cache_write_tokens: int=0, reasoning_tokens: int=0, estimated_cost_usd: Optional[float]=None,
  284. actual_cost_usd: Optional[float]=None, cost_status: Optional[str]=None, cost_source: Optional[str]=None,
  285. pricing_version: Optional[str]=None, billing_provider: Optional[str]=None, billing_base_url: Optional[str]=None,
  286. billing_mode: Optional[str]=None, api_call_count: int=0, absolute: bool=False, task: str="",
  287. ) -> None:
  288. usage = {k: v for k, v in locals().items() if k in _MODEL_USAGE_FIELDS}
  289. sql = _TOKEN_UPDATE_ABSOLUTE_SQL if absolute else _TOKEN_UPDATE_DELTA_SQL
  290. has_usage = bool(input_tokens or output_tokens or cache_read_tokens or cache_write_tokens or reasoning_tokens
  291. or api_call_count or estimated_cost_usd)
  292. has_accounted_usage = bool(has_usage or actual_cost_usd)
  293. params = (
  294. input_tokens, output_tokens, cache_read_tokens, cache_write_tokens, reasoning_tokens,
  295. estimated_cost_usd, actual_cost_usd, actual_cost_usd, cost_status, cost_source, pricing_version,
  296. billing_provider if has_accounted_usage else None,
  297. billing_base_url if has_accounted_usage else None,
  298. billing_mode if has_accounted_usage else None, model if has_accounted_usage else None,
  299. api_call_count, session_id)
  300. # Per-model attribution: the sessions row keeps one (model, provider) pair, so a
  301. # mid-session /model switch would attribute every token to the initial model. Only
  302. # the incremental path records here — absolute cumulative updates cannot be split
  303. # back into routes; Insights reconciles the residual instead.
  304. # ``update_token_counts`` is the single chokepoint every per-API-call delta flows through (CLI,
  305. # gateway, cron, delegated runs — see conversation_loop / codex_runtime), and each call carries the
  306. # model/provider *active at the time of that call*. Recording the per-call delta into
  307. # session_model_usage keyed by the live model preserves an accurate per-model breakdown regardless
  308. # of how many times the user switches. See #51607.
  309. record_model_usage = (not absolute) and has_usage
  310. row = conn.execute(
  311. "SELECT model, billing_provider, api_call_count FROM sessions WHERE id = ?", (session_id,),
  312. ).fetchone()
  313. existing = dict(row) if row is not None else {}
  314. # create_session records the requested route before any API call. If that fails
  315. # and fallback succeeds, the first accounted usage is the authoritative route;
  316. # after that keep the row as is (one row cannot represent mixed usage).
  317. first_accounted_route = (
  318. not task and int(existing.get("api_call_count") or 0) == 0 and has_accounted_usage and bool(model)
  319. and bool(billing_provider)
  320. and (existing.get("model") != model or existing.get("billing_provider") != billing_provider)
  321. )
  322. if first_accounted_route:
  323. conn.execute("""UPDATE sessions
  324. SET model = ?, billing_provider = ?,
  325. billing_base_url = ?, billing_mode = ?
  326. WHERE id = ?""", (model, billing_provider, billing_base_url, billing_mode, session_id))
  327. conn.execute(sql, params)
  328. if record_model_usage:
  329. self._record_model_usage(conn, session_id, task=task, **usage)
  330. def _record_model_usage(
  331. self, conn, session_id: str, *, model: Optional[str]=None, billing_provider: Optional[str]=None,
  332. billing_base_url: Optional[str]=None, billing_mode: Optional[str]=None, input_tokens: int=0,
  333. output_tokens: int=0, cache_read_tokens: int=0, cache_write_tokens: int=0, reasoning_tokens: int=0,
  334. estimated_cost_usd: Optional[float]=None, actual_cost_usd: Optional[float]=None,
  335. cost_status: Optional[str]=None, cost_source: Optional[str]=None, api_call_count: int=0, task: str="",
  336. ) -> None:
  337. """Accumulate a per-API-call usage delta into session_model_usage, inside the caller's
  338. write txn after the ``sessions`` UPDATE. A missing model/provider falls back to
  339. the session row — except for aux rows (``task`` set), which must NOT inherit the
  340. main-loop route (vision on gemini while the main loop runs anthropic): missing
  341. info stays 'unknown'/empty.
  342. ``task`` distinguishes what kind of work consumed the tokens: ``''`` (empty) is the main agent loop;
  343. auxiliary calls record their task name (``vision``, ``compression``, ``title_generation``, ...) via
  344. :meth:`record_auxiliary_usage` (issue #23270).
  345. """
  346. row = conn.execute(
  347. "SELECT model, billing_provider, billing_base_url, billing_mode FROM sessions WHERE id = ?", (session_id,),
  348. ).fetchone()
  349. sess = dict(row) if (row is not None and not task) else {}
  350. counts = [v or 0 for v in (input_tokens, output_tokens, cache_read_tokens, cache_write_tokens, reasoning_tokens)]
  351. now = time.time()
  352. conn.execute(_MODEL_USAGE_UPSERT_SQL, (
  353. session_id, model or sess.get("model") or "unknown",
  354. billing_provider or sess.get("billing_provider") or "",
  355. billing_base_url or sess.get("billing_base_url") or "",
  356. billing_mode or sess.get("billing_mode") or "", task or "", api_call_count or 0, *counts,
  357. float(estimated_cost_usd or 0.0), float(actual_cost_usd or 0.0), cost_status, cost_source, now, now))
  358. def record_auxiliary_usage(
  359. self, session_id: str, task: str, *, model: Optional[str]=None, billing_provider: Optional[str]=None,
  360. billing_base_url: Optional[str]=None, input_tokens: int=0, output_tokens: int=0, cache_read_tokens: int=0,
  361. cache_write_tokens: int=0, reasoning_tokens: int=0, estimated_cost_usd: Optional[float]=None,
  362. api_call_count: int=1,
  363. ) -> None:
  364. """Record an auxiliary LLM call's usage (vision, compression, title generation, ...)
  365. as a per-(model, provider, task) delta in ``session_model_usage`` WITHOUT touching
  366. the ``sessions`` summary row (the gateway overwrites those counters with absolute
  367. main-loop totals). ``api_call_count`` may aggregate N calls. Best-effort.
  368. See #23270.
  369. Background-review forks record an aggregate of N fork API calls in one write with
  370. ``task='background_review'`` (issue #87250).
  371. """
  372. usage = {k: v for k, v in locals().items() if k in _MODEL_USAGE_FIELDS}
  373. if not session_id or not task:
  374. return
  375. usage["api_call_count"] = 1 if api_call_count is None else int(api_call_count)
  376. # FK to sessions.id. A missing row is inserted as the ``unknown`` placeholder, which
  377. # the creator upsert later replaces. An existing row keeps source and created_source,
  378. # including a NULL surface update_token_counts can still fill; the UPDATE below only
  379. # fills a NULL profile_name, without writing ``unknown`` anywhere.
  380. from hermes_state_worker_lifecycle import insert_session_row_in_transaction
  381. def _write(conn):
  382. if conn.execute("SELECT 1 FROM sessions WHERE id=?", (session_id,)).fetchone() is None:
  383. insert_session_row_in_transaction(self, conn, session_id, "unknown")
  384. elif own_profile := self._own_profile_name():
  385. conn.execute(
  386. "UPDATE sessions SET profile_name=? WHERE id=? AND profile_name IS NULL",
  387. (own_profile, session_id))
  388. self._record_model_usage(conn, session_id, task=task, **usage)
  389. self._execute_write(_write, patience_s=self._TRANSCRIPT_WRITE_PATIENCE_S)
  390. def auxiliary_usage_by_task(self, session_id: str) -> dict[str, dict[str, float]]:
  391. """Per-task auxiliary usage (``task != ''``: vision, compression, title_generation, ...) summed
  392. over the session's compression lineage. Aux calls bill to the id the turn STARTED with while
  393. compression mints child ids mid-turn, so a single-id read misses rows (#112848)."""
  394. if not session_id:
  395. return {}
  396. chain = self._session_lineage_root_to_tip(session_id)
  397. # An explicit ``/branch`` copy owns its spend: cut the walk at the nearest branch node so a
  398. # resumed branch never absorbs aux rows billed to its source (hermes_state_messages does the same).
  399. for i in range(len(chain) - 1, -1, -1):
  400. if self._is_explicit_branch_session(chain[i]):
  401. chain = chain[i:]
  402. break
  403. rows = self._read_all(
  404. f"""SELECT task,
  405. COALESCE(SUM(api_call_count), 0) AS api_calls,
  406. COALESCE(SUM(input_tokens), 0) AS input_tokens,
  407. COALESCE(SUM(output_tokens), 0) AS output_tokens,
  408. COALESCE(SUM(cache_read_tokens), 0) AS cache_read_tokens,
  409. COALESCE(SUM(cache_write_tokens), 0) AS cache_write_tokens,
  410. COALESCE(SUM(reasoning_tokens), 0) AS reasoning_tokens,
  411. COALESCE(SUM(estimated_cost_usd), 0) AS estimated_cost_usd
  412. FROM session_model_usage
  413. WHERE session_id IN ({','.join('?' * len(chain))}) AND task != ''
  414. GROUP BY task""",
  415. chain,
  416. )
  417. return {row["task"]: {k: row[k] for k in row.keys() if k != "task"} for row in rows}
  418. def usage_totals(self, *, min_message_count: int = 1, include_archived: bool = False) -> dict[str, float]:
  419. """Tokens and spend across the whole store (one scan), so the sidebar total does not
  420. shrink with paging. Spend prefers the billed figure over the estimate."""
  421. where = ["parent_session_id IS NULL", "message_count >= ?"]
  422. params: list[Any] = [min_message_count]
  423. if not include_archived:
  424. where.append("COALESCE(archived, 0) = 0")
  425. row = self._read_one(f"""
  426. SELECT COALESCE(SUM(COALESCE(input_tokens, 0) + COALESCE(output_tokens, 0)), 0),
  427. COALESCE(SUM(COALESCE(actual_cost_usd, estimated_cost_usd, 0)), 0)
  428. FROM sessions
  429. WHERE {' AND '.join(where)}
  430. """, params)
  431. return {"tokens": int(row[0] or 0), "cost_usd": float(row[1] or 0.0)}