Skip to content

feat(tracking): per-agent token tracking with rtk gain --agent filter#1408

Open
Mithesh14 wants to merge 1 commit intortk-ai:developfrom
Mithesh14:feat/tracking-per-agent-analytics
Open

feat(tracking): per-agent token tracking with rtk gain --agent filter#1408
Mithesh14 wants to merge 1 commit intortk-ai:developfrom
Mithesh14:feat/tracking-per-agent-analytics

Conversation

@Mithesh14
Copy link
Copy Markdown

@Mithesh14 Mithesh14 commented Apr 20, 2026

Summary

Implements #1314 — per-agent token tracking so you can see savings broken down by AI agent (Claude Code, Cursor, Gemini, Copilot, etc.).

  • Hook injection: Each agent hook (claude, cursor, gemini, copilot/vscode) prepends RTK_AGENT=<agent> to every rewritten command via a new prepend_agent_env() helper in hook_cmd.rs
  • DB schema: tracking.rs auto-migrates existing installs with ALTER TABLE commands ADD COLUMN agent TEXT DEFAULT '' + a compound index on (agent, timestamp) — silent no-op if column already exists
  • Analytics filter: rtk gain --agent <name> filters all stats (daily/weekly/monthly, JSON/CSV export) by agent; title reflects the active scope (Global / Project / Agent / Project+Agent)

Usage

rtk gain --agent claude     # Claude Code only
rtk gain --agent cursor     # Cursor only
rtk gain --agent gemini     # Gemini only
rtk gain --agent copilot    # GitHub Copilot only
rtk gain --project . --agent claude  # project + agent combined

Test plan

  • cargo fmt --all — no formatting issues
  • cargo clippy --all-targets — no warnings
  • cargo test --all — all 1590 existing tests pass (test helper passes empty agent to keep assertions unchanged)
  • Manual smoke test: created schema-v1 DB, launched tracker — migration ran silently, new rows recorded with agent column
  • End-to-end: rtk gain --agent claude and rtk gain --agent cursor both return correct filtered stats

Screenshot

image

Feature working end-to-end: rtk gain now shows per-agent breakdowns with correct savings percentages per agent.


Closes #1314

🤖 Generated with Claude Code

Implements issue rtk-ai#1314. Each AI agent hook (claude, cursor, gemini,
copilot) now injects RTK_AGENT=<agent> into every rewritten command.
The proxy reads this env var and persists the agent name in a new
agent column in the SQLite commands table (auto-migrated on first run).
rtk gain --agent <name> filters all analytics by agent, enabling
per-agent token savings breakdowns.

Changes:
- hooks/hook_cmd.rs: prepend_agent_env() helper; all 4 agent handlers
  (claude, cursor, gemini, copilot/vscode) inject RTK_AGENT=<agent>
- core/tracking.rs: schema migration (ALTER TABLE + index), record()
  reads RTK_AGENT env var, all *_filtered() methods accept agent_filter
- analytics/gain.rs: run() accepts agent param, title reflects scope,
  all print/export functions propagate agent_filter
- main.rs: --agent flag on gain subcommand, dispatched to gain::run()

Closes rtk-ai#1314
@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

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.

3 participants