Autonomous AI sales agent powered by Claude Code or Codex. Finds prospects, writes cold emails, sends campaigns, handles replies, books meetings. No API keys needed — runs on your Claude or Codex subscriptions.
Usage tracking is Harvey-only: remove the transcript reconciler
Ethana46d5bbf3260
diff --git a/CLAUDE.md b/CLAUDE.mdindex 7433ad7..22d8543 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -176,7 +176,7 @@ Users will come back with questions and tasks. Common ones:- **Heartbeat loop** (`main.py`): Every cycle → check quiet hours → check budget → decide → act → log → sleep- **Brain** (`brain.py`): Wraps `claude -p --output-format json --dangerously-skip-permissions` for headless Claude calls; records exact per-call token usage into `usage_events`- **State** (`state.py`): SQLite at `data/harvey.db` with tables for companies, prospects, campaigns, conversations, actions, usage-- **Usage tracking** (`usage.py`, `integrations/quota.py`): per-call attribution (agent/task/model/tokens/cost), transcript reconciliation, and a live subscription-quota gauge read the same way Claude Code's `/usage` does. The budget check throttles Harvey against real quota utilization so it always leaves headroom for your own interactive Claude use (`usage.max_daily_claude_percent`). Dashboard has a Usage tab; CLI has `harvey usage`.+- **Usage tracking** (`usage.py`, `integrations/quota.py`): per-call attribution (agent/task/model/tokens) for Harvey's OWN Claude calls only — it never scans your other Claude Code sessions — plus a live subscription-quota gauge read the same way Claude Code's `/usage` does. The budget check throttles Harvey against real quota utilization so it always leaves headroom for your own interactive Claude use (`usage.max_daily_claude_percent`). Dashboard has a Usage tab; CLI has `harvey usage`.- **Skills** (`skills/`): Markdown knowledge files injected into agent prompts### Sub-Agents@@ -201,7 +201,6 @@ harvey setup # Re-run setup wizardharvey train <url> # Train on a product websiteharvey status # Pipeline summaryharvey usage # Claude quota gauges + per-agent token usage (no dollar costs — subscription plans aren't billed per token)-harvey usage --reconcile # Also backfill usage from Claude Code transcriptsharvey export # Deliverable prospects → sequencer-ready CSV (prospects.csv)harvey export --all # Full raw list, no filtersharvey gmail auth # One-time Gmail OAuth (when provider: gmail)diff --git a/harvey/agents/analyst.py b/harvey/agents/analyst.pyindex 9444f2b..a37d6be 100644--- a/harvey/agents/analyst.py+++ b/harvey/agents/analyst.py@@ -23,15 +23,6 @@ class Analyst:"""Generate analytics report from current pipeline data."""logger.info("Analyst: Running performance analysis...")- # Idle cycles are the cheap moment to reconcile usage accounting- # against Claude Code's transcripts (catches crashed calls and- # non-Harvey usage on this machine). Best-effort only.- try:- from harvey.usage import reconcile_transcripts- await reconcile_transcripts(self.state)- except Exception as e:- logger.debug(f"Analyst: usage reconcile skipped: {e}")-report = {"generated_at": datetime.utcnow().isoformat(),"pipeline": await self._pipeline_summary(),diff --git a/harvey/cli.py b/harvey/cli.pyindex a954822..43ee4ec 100644--- a/harvey/cli.py+++ b/harvey/cli.py@@ -159,11 +159,6 @@ def cmd_usage(args):state = StateManager()await state.init_db()- if args.reconcile:- from harvey.usage import reconcile_transcripts- inserted = await reconcile_transcripts(state, since_days=args.days)- print(f"\n Reconciled transcripts: {inserted} event(s) backfilled.")-# Live quota (best-effort; undocumented endpoint)from harvey.integrations.quota import QuotaClientwindows = None@@ -426,10 +421,6 @@ def main():# harvey usagesub = subparsers.add_parser("usage", help="Show Claude usage and quota")sub.add_argument("--days", type=int, default=30, help="Breakdown window (default: 30)")- sub.add_argument(- "--reconcile", action="store_true",- help="Backfill usage from Claude Code transcripts first",- )sub.set_defaults(func=cmd_usage)args = parser.parse_args()diff --git a/harvey/state.py b/harvey/state.pyindex 3e7002d..d8c0d9a 100644--- a/harvey/state.py+++ b/harvey/state.py@@ -1159,15 +1159,6 @@ class StateManager:await db.commit()return cursor.rowcount > 0- async def get_recorded_session_ids(self) -> set[str]:- """Session IDs already captured live (skip during reconciliation)."""- async with self._connect() as db:- async with db.execute(- "SELECT DISTINCT session_id FROM usage_events "- "WHERE source = 'result_json' AND session_id != ''"- ) as cursor:- return {row[0] for row in await cursor.fetchall()}-_USAGE_SUM = ("COUNT(DISTINCT CASE WHEN session_id != '' THEN session_id ELSE id END) AS calls, ""SUM(input_tokens) AS input_tokens, "diff --git a/harvey/usage.py b/harvey/usage.pyindex 3f8211a..efb671d 100644--- a/harvey/usage.py+++ b/harvey/usage.py@@ -1,83 +1,23 @@-"""Usage accounting — model pricing and Claude Code transcript reconciliation.+"""Usage accounting helpers.-Harvey's primary usage source is the per-call result JSON from `claude -p---output-format json` (recorded live by the Brain). This module supplies:+Harvey's usage ledger is built entirely from its OWN Claude calls: every+`claude -p` invocation goes through the Brain, which parses the call's+result JSON and writes one usage_events row tagged with the agent and task+(see harvey/brain.py). That path is the single source of truth — it never+sees, and never records, your other Claude Code sessions.-1. A pricing table + cost math for when the CLI doesn't report a cost- (transcript rows have ``costUSD: null`` on Max subscriptions).-2. A reconciler that scans Claude Code's JSONL transcripts and backfills- usage_events rows for calls the live path missed (crashes, other tools- using Claude on this machine). Idempotent: rows are keyed by- (message.id, requestId) with a unique index, and sessions already- captured live are skipped entirely.+(An earlier version also scanned every transcript in the Claude config+directory to "backfill" usage. That pulled in unrelated projects' sessions+and polluted Harvey's numbers, so it was removed: Harvey tracks only what+Harvey does.)++This module now holds just the shared location helper the quota client+needs."""-import json-import loggingimport os-from datetime import datetime, timezonefrom pathlib import Path-from harvey.state import StateManager--logger = logging.getLogger("harvey.usage")--# USD per million tokens: (input, output). Matched by longest prefix, so-# order here doesn't matter but specificity does — the dated Opus 4.0 id-# must not fall through to the modern "claude-opus-4" rate.-PRICING: dict[str, tuple[float, float]] = {- "claude-fable-5": (10.0, 50.0),- "claude-mythos": (10.0, 50.0),- "claude-opus-4-2025": (15.0, 75.0), # claude-opus-4-20250514 (Opus 4.0)- "claude-opus-4-0": (15.0, 75.0),- "claude-opus-4-1": (15.0, 75.0),- "claude-opus-4": (5.0, 25.0), # Opus 4.5 / 4.6 / 4.7 / 4.8- "claude-sonnet": (3.0, 15.0),- "claude-haiku-4": (1.0, 5.0),- "claude-3-5-haiku": (0.8, 4.0),- "claude-3-haiku": (0.25, 1.25),-}--# Cache pricing multipliers relative to base input price.-CACHE_READ_MULT = 0.1-CACHE_WRITE_5M_MULT = 1.25-CACHE_WRITE_1H_MULT = 2.0---def match_pricing(model: str) -> tuple[float, float] | None:- """Longest-prefix match a model id to (input, output) $/MTok."""- if not model:- return None- best_key = ""- for key in PRICING:- if model.startswith(key) and len(key) > len(best_key):- best_key = key- return PRICING[best_key] if best_key else None---def compute_cost(- model: str,- input_tokens: int = 0,- output_tokens: int = 0,- cache_read_tokens: int = 0,- cache_write_5m_tokens: int = 0,- cache_write_1h_tokens: int = 0,-) -> float:- """Equivalent API-list-price cost in USD. 0.0 for unknown models."""- prices = match_pricing(model)- if not prices:- return 0.0- p_in, p_out = prices- per_tok_in = p_in / 1_000_000- per_tok_out = p_out / 1_000_000- return (- input_tokens * per_tok_in- + output_tokens * per_tok_out- + cache_read_tokens * per_tok_in * CACHE_READ_MULT- + cache_write_5m_tokens * per_tok_in * CACHE_WRITE_5M_MULT- + cache_write_1h_tokens * per_tok_in * CACHE_WRITE_1H_MULT- )-def claude_config_dir() -> Path:"""Claude Code's config directory (honors CLAUDE_CONFIG_DIR)."""@@ -85,137 +25,3 @@ def claude_config_dir() -> Path:if override:return Path(override).expanduser()return Path.home() / ".claude"---def _iso_to_sqlite_utc(ts: str) -> str | None:- """'2026-08-28T20:15:03.123Z' -> '2026-08-28 20:15:03' (UTC, naive)."""- if not ts:- return None- try:- dt = datetime.fromisoformat(ts.replace("Z", "+00:00"))- if dt.tzinfo is not None:- dt = dt.astimezone(timezone.utc).replace(tzinfo=None)- return dt.strftime("%Y-%m-%d %H:%M:%S")- except ValueError:- return None---def parse_transcript_events(path: Path) -> dict[str, dict]:- """Extract per-request usage from one session transcript.-- Returns {request_key: event_dict}. Claude Code writes multiple- entries per API request (streaming snapshots, resume replays) — the- LAST entry per (message.id, requestId) is authoritative, so later- lines simply overwrite earlier ones here.- """- events: dict[str, dict] = {}- try:- with path.open(encoding="utf-8", errors="replace") as f:- for line in f:- line = line.strip()- if not line:- continue- try:- entry = json.loads(line)- except json.JSONDecodeError:- continue- if not isinstance(entry, dict) or entry.get("type") != "assistant":- continue- message = entry.get("message")- if not isinstance(message, dict):- continue- usage = message.get("usage")- if not isinstance(usage, dict):- continue-- msg_id = str(message.get("id") or "")- req_id = str(entry.get("requestId") or "")- if not msg_id and not req_id:- continue- key = f"{msg_id}:{req_id}"-- model = str(message.get("model") or "")- input_tokens = int(usage.get("input_tokens") or 0)- output_tokens = int(usage.get("output_tokens") or 0)- cache_read = int(usage.get("cache_read_input_tokens") or 0)- cache_creation = int(usage.get("cache_creation_input_tokens") or 0)-- # 5m/1h split (when present) prices cache writes correctly.- split = usage.get("cache_creation")- if isinstance(split, dict):- write_1h = int(split.get("ephemeral_1h_input_tokens") or 0)- write_5m = int(split.get("ephemeral_5m_input_tokens") or 0)- else:- write_1h, write_5m = 0, cache_creation-- cost = entry.get("costUSD")- if not isinstance(cost, (int, float)):- cost = compute_cost(- model,- input_tokens=input_tokens,- output_tokens=output_tokens,- cache_read_tokens=cache_read,- cache_write_5m_tokens=write_5m,- cache_write_1h_tokens=write_1h,- )-- events[key] = {- "request_key": key,- "session_id": str(entry.get("sessionId") or ""),- "model": model,- "input_tokens": input_tokens,- "output_tokens": output_tokens,- "cache_read_tokens": cache_read,- "cache_creation_tokens": cache_creation,- "cost_usd": float(cost),- "created_at": _iso_to_sqlite_utc(str(entry.get("timestamp") or "")),- }- except OSError as e:- logger.debug(f"Could not read transcript {path}: {e}")- return events---async def reconcile_transcripts(- state: StateManager,- since_days: int = 7,- config_dir: Path | None = None,-) -> int:- """Backfill usage_events from Claude Code JSONL transcripts.-- Only touches transcripts modified in the last ``since_days`` days,- skips sessions Harvey already recorded live, and relies on the- request_key unique index for idempotency. Returns rows inserted.- """- base = (config_dir or claude_config_dir()) / "projects"- if not base.is_dir():- logger.debug(f"No Claude transcripts directory at {base}")- return 0-- try:- recorded_sessions = await state.get_recorded_session_ids()- except Exception as e:- logger.warning(f"Could not load recorded sessions: {e}")- recorded_sessions = set()-- cutoff = datetime.now().timestamp() - since_days * 86400- inserted = 0-- for path in base.glob("*/*.jsonl"):- try:- if path.stat().st_mtime < cutoff:- continue- except OSError:- continue-- for event in parse_transcript_events(path).values():- if event["session_id"] and event["session_id"] in recorded_sessions:- continue- try:- if await state.record_usage_event(source="transcript", **event):- inserted += 1- except Exception as e:- logger.debug(f"Failed to record transcript event: {e}")-- if inserted:- logger.info(f"Usage reconcile: backfilled {inserted} transcript event(s).")- return inserteddiff --git a/tests/test_usage.py b/tests/test_usage.pyindex 2b2721b..3cf3f16 100644--- a/tests/test_usage.py+++ b/tests/test_usage.py@@ -10,12 +10,6 @@ import pytest_asynciofrom harvey.brain import Brainfrom harvey.state import StateManager-from harvey.usage import (- compute_cost,- match_pricing,- parse_transcript_events,- reconcile_transcripts,-)from harvey.integrations.quota import _parse_window, _token_from_json_blob@@ -88,34 +82,6 @@ async def test_request_key_dedup(state):assert totals["today"]["output_tokens"] == 50-# ── pricing ──---def test_match_pricing_prefixes():- assert match_pricing("claude-opus-4-8") == (5.0, 25.0)- assert match_pricing("claude-opus-4-5-20251101") == (5.0, 25.0)- # Dated Opus 4.0 must NOT get the modern rate- assert match_pricing("claude-opus-4-20250514") == (15.0, 75.0)- assert match_pricing("claude-haiku-4-5-20251001") == (1.0, 5.0)- assert match_pricing("claude-fable-5") == (10.0, 50.0)- assert match_pricing("gpt-4o") is None- assert match_pricing("") is None---def test_compute_cost_with_cache_tiers():- # 1M of everything on Haiku: 1 + 5 + 0.1 + 1.25 + 2.0- cost = compute_cost(- "claude-haiku-4-5",- input_tokens=1_000_000,- output_tokens=1_000_000,- cache_read_tokens=1_000_000,- cache_write_5m_tokens=1_000_000,- cache_write_1h_tokens=1_000_000,- )- assert cost == pytest.approx(1 + 5 + 0.1 + 1.25 + 2.0)- assert compute_cost("unknown-model", output_tokens=1000) == 0.0--# ── Brain result-JSON parsing ──@@ -171,82 +137,13 @@ async def test_brain_records_usage_from_payload(state):assert by_model[0]["model"] == "claude-haiku-4-5-20251001"-# ── transcript reconciliation ──---def _write_transcript(path: Path, entries: list[dict]):- path.parent.mkdir(parents=True, exist_ok=True)- with path.open("w") as f:- for e in entries:- f.write(json.dumps(e) + "\n")---def _assistant_entry(msg_id, req_id, session, output_tokens, cost=None):- return {- "type": "assistant",- "sessionId": session,- "requestId": req_id,- "timestamp": "2026-08-28T20:15:03.123Z",- "costUSD": cost,- "message": {- "id": msg_id,- "model": "claude-opus-4-8",- "usage": {- "input_tokens": 10,- "output_tokens": output_tokens,- "cache_read_input_tokens": 100,- "cache_creation_input_tokens": 50,- "cache_creation": {- "ephemeral_1h_input_tokens": 50,- "ephemeral_5m_input_tokens": 0,- },- },- },- }---def test_parse_transcript_keeps_last_duplicate(tmp_path):- transcript = tmp_path / "projects" / "proj" / "sess.jsonl"- _write_transcript(transcript, [- _assistant_entry("msg_1", "req_1", "sess-x", 5), # streaming snapshot- _assistant_entry("msg_1", "req_1", "sess-x", 51), # authoritative final- {"type": "user", "message": {"content": "hi"}}, # ignored- {"not": "even a dict of the right shape"}, # ignored- ])- events = parse_transcript_events(transcript)- assert len(events) == 1- event = events["msg_1:req_1"]- assert event["output_tokens"] == 51- # costUSD null on Max → computed from pricing (Opus 4-8: $5/$25)- expected = compute_cost(- "claude-opus-4-8", input_tokens=10, output_tokens=51,- cache_read_tokens=100, cache_write_1h_tokens=50,- )- assert event["cost_usd"] == pytest.approx(expected)- assert event["created_at"] == "2026-08-28 20:15:03"--@pytest.mark.asyncio-async def test_reconcile_skips_live_sessions_and_is_idempotent(state, tmp_path):- # Session recorded live by the Brain → its transcript must be skipped- await state.record_usage_event(- agent="writer", session_id="sess-live", model="claude-opus-4-8",- output_tokens=99, source="result_json",- )- _write_transcript(tmp_path / "projects" / "p1" / "a.jsonl",- [_assistant_entry("m1", "r1", "sess-live", 99)])- _write_transcript(tmp_path / "projects" / "p2" / "b.jsonl",- [_assistant_entry("m2", "r2", "sess-other", 40)])-- inserted = await reconcile_transcripts(state, since_days=7, config_dir=tmp_path)- assert inserted == 1 # only sess-other-- # Second run inserts nothing (request_key unique index)- inserted_again = await reconcile_transcripts(state, since_days=7, config_dir=tmp_path)- assert inserted_again == 0-- totals = await state.usage_totals()- assert totals["month"]["output_tokens"] == 99 + 40+async def test_usage_is_harvey_only(state):+ """Sanity: the ledger only contains rows Harvey's Brain wrote. There is+ no transcript scan to pull in other projects' Claude sessions."""+ import harvey.usage as usage_mod+ assert not hasattr(usage_mod, "reconcile_transcripts")+ assert not hasattr(usage_mod, "parse_transcript_events")# ── quota client helpers ──