Skip to content

Implement automated content-type detection for token estimation#78

Merged
fajarhide merged 2 commits intomainfrom
fix/est-cost
May 6, 2026
Merged

Implement automated content-type detection for token estimation#78
fajarhide merged 2 commits intomainfrom
fix/est-cost

Conversation

@fajarhide
Copy link
Copy Markdown
Owner

@fajarhide fajarhide commented May 6, 2026

PR Auto Describe

Summary

The PR refines cost reporting, introduces dynamic LLM pricing, enhances token‑estimation precision, and tightens session‑ending validation. It now prefixes all USD amounts, uses an average model price for defaults, detects content hints per tool, and guards against archiving sessions under incorrect IDs.


Key Changes

  • Stats: Uniform “USD” suffix, re‑calculated savings, and formatted output.
  • Pricing: ModelPricing & get_pricing_for_model replace hard‑coded defaults; get_input_cost now pulls the average price.
  • Token Estimation: detect_content_hint added; ContentHint enum reordered/expanded; hint‑based token rates updated.
  • Hooks: build_additional_context now receives tool/command, uses hint detection; session‑end hook validates sessionId.
  • Tests: Updated to reflect dynamic pricing and hint logic; removed obsolete unit tests.

Detailed Breakdown

  • src/cli/stats.rs

    • println! format strings now append “USD” and use ~$ for approximations.
    • Removed the premature cost_saved calculation; recomputed after bytes_saved to avoid double‑counting.
    • In run_detail, added cost_saved after computing bytes_saved.
    • Adjusted savings print to ~${:.3}.
  • src/guard/config.rs

    • Added ModelPricing struct with fields for input, output, cached, reasoning, cache_creation costs.
    • Implemented get_pricing_for_model returning preset prices for Claude‑3‑7‑Sonnet, Claude‑3‑5‑Sonnet, GPT‑4o, or an average of these.
    • get_input_cost now defaults to the average model’s input price via get_pricing_for_model("average").
    • Updated imports and removed unused _output etc. hints in pricing logic.
  • src/hooks/post_tool.rs

    • build_additional_context signature extended to (&tool_name, &command).
    • Token saving now uses detect_content_hint(tool_name, command) to pick appropriate hint.
    • Updated calls accordingly.
  • src/hooks/session_end.rs

    • Removed #[allow(dead_code)] on session_id; added guard:
      if !parsed.session_id.is_empty() && parsed.session_id != state.session_id {
          return None;
      }
    • Prevents accidental archiving under a mismatched session ID.
  • src/util/token_estimate.rs

    • ContentHint enum reordered, added BuildLog, Mixed; removed dead code attribute.
    • Added detect_content_hint(tool_name, command_or_path) determining hint based on file extension or command patterns.
    • Updated estimate_tokens mapping to use new hints.
    • Removed obsolete unit tests; all logic now exercised by integration tests.
  • tests (stats.rs)

    • Calculated expected_price_per_m dynamically from average pricing.
    • Updated assertions to use this value, ensuring consistency with new pricing logic.

Notes

  • All USD amounts now consistently display “USD” and use the ~$ prefix for estimates.
  • The pricing defaults are now derived from a realistic average of popular models, improving cost predictions.
  • Token estimation is more accurate by detecting content types per tool/command, which can affect savings calculations.

Breaking Changes

None. Existing APIs remain functional; new helper detect_content_hint is pure utility and does not alter external behavior. The updated default pricing may change cost outputs but does not break callers.

Last updated: 2026-05-06 03:21:41

@fajarhide fajarhide merged commit c07b770 into main May 6, 2026
4 checks passed
@fajarhide fajarhide deleted the fix/est-cost branch May 6, 2026 03:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant