# Token Lens agent cost check: comments on every pull request that changes an
# agent's prompts or tool definitions with the token and dollar change per call,
# and what could be trimmed with the same output. Runs inside your CI; nothing is
# sent anywhere.
#
# Copy to .github/workflows/token-lens.yml and set:
#   --model   the model your agent runs on (pricing and token counts follow it)
#   paths     git pathspecs of your agent files (.json specs, or prompt files)
# Optional: --calls-per-month N for a monthly figure, --fail-above <tokens> to
# fail the check when a change adds more than that per call.
name: Token Lens agent cost check

on:
  pull_request:

permissions:
  contents: read
  pull-requests: write

jobs:
  agent-cost:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - uses: actions/setup-node@v4
        with:
          node-version: 22
      - run: >
          npx -y @kunal_dutt/token-lens@latest check-agent
          --model claude-opus-5-5
          --base origin/${{ github.base_ref }}
          --calls-per-month 100000
          --out token-lens.md
          'agents/**' 'prompts/**'
      - name: Comment on the pull request
        if: hashFiles('token-lens.md') != ''
        uses: actions/github-script@v7
        with:
          script: |
            const marker = "<!-- token-lens-agent-check -->";
            const body = marker + "\n" + require("fs").readFileSync("token-lens.md", "utf8");
            const { owner, repo } = context.repo;
            const issue_number = context.issue.number;
            const comments = await github.paginate(github.rest.issues.listComments, { owner, repo, issue_number });
            const mine = comments.find((c) => c.body?.startsWith(marker));
            if (mine) await github.rest.issues.updateComment({ owner, repo, comment_id: mine.id, body });
            else await github.rest.issues.createComment({ owner, repo, issue_number, body });
