← Back to Pull requests

Measure Claude tokens and cost per email for evals (SHY-14) #17

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 22, 2026 20:20 UTC
Author

Closes SHY-14

Decision


Ryan picked option 3: skip the dashboard, measure cost only in evals. PR #14 had already removed the per-email list, and this decision means cost needs no new place on the dashboard. As a result this PR has:
  • no dashboard change

  • no migration

  • no cost stored on ProcessedEmail
  • What's in it


  • cost.py: one price table (US dollars per 1M tokens, Anthropic list prices checked 2026-11-22) for Haiku 5.5 ($0.10 / $0.50), Sonnet 5.5 ($2 / $10) and Opus 5.5 ($4 / $20). These are the three models SHY-12 compares. cost.track() collects the tokens of every Claude call made inside it, per model, so the split setup (Haiku classifies, Sonnet drafts) is priced correctly. A model missing from the table raises an error instead of counting as $0.

  • agent.ask / agent.act: report each call's tokens from LangChain's usage_metadata. act counts every Claude call in its tool loop. A refused call is counted too, because it is billed.

  • Outside cost.track() nothing is kept, so production runs behave as before.
  • How SHY-12's eval uses it:

    with cost.track() as tally:
        agent.run(email, label_names)
    tally.input_tokens, tally.output_tokens, tally.dollars()
    

    Tests


    make test: 86 tests, all pass. The 8 new tests in accounts/test_cost.py cover the prices, a split setup across models, nested tracks, ask (including refusals) and act's multi-call loop.

    No screenshot: nothing in the UI changes.

    🤖 Generated with Claude Code

    <!-- greptile_comment -->

    <!-- greptile_summary -->

    <h2><a href="https://app.greptile.com/api/retrigger?id=115934664&amp;sig=8f31c0c2379041ebf7ba37ddfa9f68730d64837b89e1943fa58ba99de53eba9e"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/RetriggerDark.svg?v=2"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2"><img alt="Retrigger" src="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2" align="right"></picture></a><a href="https://app.greptile.com/-/switch-tenant?tenant=shyyronnie&amp;next=%2FRyancg3%2Femail-agent-hack-2026%2Fpull%2F17&amp;utm_source=greptile_review&amp;utm_medium=github&amp;utm_campaign=view_in_greptile"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptileDark.svg?v=2"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2"><img alt="View in Greptile" src="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2" align="right"></picture></a>Confidence Score: 4/5</h2>

    <!-- greptile-risk -->

    Fix failed-loop accounting before merging so eval costs include calls that were already billed.

    <h2><a href="https://app.greptile.com/api/ide/conductor?prompt=IMPORTANT%3A%20Work%20in%20the%20repository%20%22ryancg3%2Femail-agent-hack-2026%22%20on%20the%20existing%20branch%20%22Ryancg3%2Fryancg3%2Fshy-14-cost-per-email%22.%20Checkout%20that%20branch%20%E2%80%94%20do%20NOT%20create%20a%20new%20branch%20or%20open%20a%20new%20PR.%20Push%20your%20changes%20to%20%22Ryancg3%2Fryancg3%2Fshy-14-cost-per-email%22.%0A%0A%23%23%23%20Issue%201%0Aagent.py%3A269-272%0AFailed%20loops%20lose%20billed%20calls.%20%60act%60%20records%20usage%20only%20after%20%60graph.invoke%60%20returns.%20If%20one%20Claude%20call%20succeeds%20and%20a%20later%20call%20fails%20after%20retries%2C%20execution%20skips%20this%20entire%20recording%20loop.%20The%20earlier%20call%20was%20billed%2C%20but%20the%20tally%20omits%20it%2C%20so%20evals%20understate%20spending%20on%20failed%20emails.%20Record%20each%20response%20as%20it%20arrives%2C%20such%20as%20through%20model%20middleware%2C%20and%20add%20a%20test%20where%20a%20later%20call%20raises.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&expires=1795983704&sig=895ab5d5ea8a227ca7014b5616334421ac9c77523576a596a85d363f9b1d666b&repo=ryancg3%2Femail-agent-hack-2026"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixAllInConductorDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixAllInConductor.svg?v=7"><img alt="Fix All in Conductor" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixAllInConductor.svg?v=7" align="right"></picture></a>Findings</h2>

    1. <img alt="P1" src="https://greptile-static-assets.s3.amazonaws.com/badges/p1.svg?v=9" align="top">&nbsp;Failed loops lose billed calls <a href="https://github.com/Ryancg3/email-agent-hack-2026/pull/17#discussion_r4659845033">▶</a>

    <details open><summary><h3>Summary</h3></summary>

    Adds eval-only tracking for Claude token use and estimated cost, without changing the dashboard or storing costs.

  • Evals can measure Claude tokens and estimated cost for each email run.
  • One issue needs fixing: a failed act loop loses the usage from its earlier successful calls.
    </details>

    <!-- greptile_confidence_score:4 -->

    <sub>Reviews (1) · Last reviewed commit: "Measure Claude tokens and cost per email..." · Reviewed by Greptile</sub>

    <!-- /greptile_comment -->

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: greptile-apps[bot] Review duration: 6m
    All Vouch review confidence checks passed
    Residual risk 0.60 = Change Risk (0.80) × (1 − Review Confidence (0.25)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.80 · 650 lines diff across 8 files
    0.80
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 39% · Reviewer familiarity: 35%
    24%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Sensitive Paths sensitive_paths
    0.080
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #17"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: medium confidence: 24.6% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 80.2% (HIGH) · Higher = larger blast radius
    80.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 24.6% · Lower = rushed or shallow review
    24.6%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 60.5% · Moderate risk
    60.5%